xref: /freebsd/contrib/libarchive/cpio/test/test_option_t.c (revision f374ba41f55c1a127303d92d830dd58eef2f5243)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27 
28 #ifdef HAVE_LOCALE_H
29 #include <locale.h>
30 #endif
31 
32 DEFINE_TEST(test_option_t)
33 {
34 	char *p;
35 	int r;
36 	time_t mtime;
37 	char date[32];
38 	char date2[32];
39 	struct tm *tmptr;
40 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
41 	struct tm tmbuf;
42 #endif
43 #if defined(HAVE__LOCALTIME64_S)
44 	errno_t terr;
45 	__time64_t tmptime;
46 #endif
47 
48 	/* List reference archive, make sure the TOC is correct. */
49 	extract_reference_file("test_option_t.cpio");
50 	r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog);
51 	assertEqualInt(r, 0);
52 	assertTextFileContents("1 block\n", "it.err");
53 	extract_reference_file("test_option_t.stdout");
54 	p = slurpfile(NULL, "test_option_t.stdout");
55 	assertTextFileContents(p, "it.out");
56 	free(p);
57 
58 	/* We accept plain "-t" as a synonym for "-it" */
59 	r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog);
60 	assertEqualInt(r, 0);
61 	assertTextFileContents("1 block\n", "t.err");
62 	extract_reference_file("test_option_t.stdout");
63 	p = slurpfile(NULL, "test_option_t.stdout");
64 	assertTextFileContents(p, "t.out");
65 	free(p);
66 
67 	/* But "-ot" is an error. */
68 	assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err",
69 			    testprog));
70 	assertEmptyFile("ot.out");
71 
72 	/* List reference archive verbosely, make sure the TOC is correct. */
73 	r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog);
74 	assertEqualInt(r, 0);
75 	assertTextFileContents("1 block\n", "tv.err");
76 	extract_reference_file("test_option_tv.stdout");
77 
78 	/* This doesn't work because the usernames on different systems
79 	 * are different and cpio now looks up numeric UIDs on
80 	 * the local system. */
81 	/* assertEqualFile("tv.out", "test_option_tv.stdout"); */
82 
83 	/* List reference archive with numeric IDs, verify TOC is correct. */
84 	r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err",
85 		    testprog);
86 	assertEqualInt(r, 0);
87 	assertTextFileContents("1 block\n", "itnv.err");
88 	p = slurpfile(NULL, "itnv.out");
89 	/* Since -n uses numeric UID/GID, this part should be the
90 	 * same on every system. */
91 	assertEqualMem(p, "-rw-r--r--   1 1000     1000            0 ",42);
92 
93 	/* Date varies depending on local timezone and locale. */
94 	mtime = 1;
95 #ifdef HAVE_LOCALE_H
96 	setlocale(LC_ALL, "");
97 #endif
98 #if defined(HAVE_LOCALTIME_R)
99         tmptr = localtime_r(&mtime, &tmbuf);
100 #elif defined(HAVE__LOCALTIME64_S)
101         tmptime = mtime;
102         terr = _localtime64_s(&tmbuf, &tmptime);
103         if (terr)
104                 tmptr = NULL;
105         else
106                 tmptr = &tmbuf;
107 #else
108         tmptr = localtime(&mtime);
109 #endif
110 #if defined(_WIN32) && !defined(__CYGWIN__)
111 	strftime(date2, sizeof(date2)-1, "%b %d  %Y", tmptr);
112 	_snprintf(date, sizeof(date)-1, "%12.12s file", date2);
113 #else
114 	strftime(date2, sizeof(date2)-1, "%b %e  %Y", tmptr);
115 	snprintf(date, sizeof(date)-1, "%12.12s file", date2);
116 #endif
117 	assertEqualMem(p + 42, date, strlen(date));
118 	free(p);
119 
120 	/* But "-n" without "-t" is an error. */
121 	assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err",
122 			    testprog));
123 	assertEmptyFile("in.out");
124 }
125