xref: /freebsd/contrib/libarchive/cpio/test/test_option_t.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2007 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 #ifdef HAVE_LOCALE_H
10 #include <locale.h>
11 #endif
12 
DEFINE_TEST(test_option_t)13 DEFINE_TEST(test_option_t)
14 {
15 	char *p;
16 	int r;
17 	time_t mtime;
18 	char date[48];
19 	char date2[32];
20 	struct tm *tmptr;
21 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
22 	struct tm tmbuf;
23 #endif
24 
25 	/* List reference archive, make sure the TOC is correct. */
26 	extract_reference_file("test_option_t.cpio");
27 	r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog);
28 	assertEqualInt(r, 0);
29 	assertTextFileContents("1 block\n", "it.err");
30 	extract_reference_file("test_option_t.stdout");
31 	p = slurpfile(NULL, "test_option_t.stdout");
32 	assertTextFileContents(p, "it.out");
33 	free(p);
34 
35 	/* We accept plain "-t" as a synonym for "-it" */
36 	r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog);
37 	assertEqualInt(r, 0);
38 	assertTextFileContents("1 block\n", "t.err");
39 	extract_reference_file("test_option_t.stdout");
40 	p = slurpfile(NULL, "test_option_t.stdout");
41 	assertTextFileContents(p, "t.out");
42 	free(p);
43 
44 	/* But "-ot" is an error. */
45 	assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err",
46 			    testprog));
47 	assertEmptyFile("ot.out");
48 
49 	/* List reference archive verbosely, make sure the TOC is correct. */
50 	r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog);
51 	assertEqualInt(r, 0);
52 	assertTextFileContents("1 block\n", "tv.err");
53 	extract_reference_file("test_option_tv.stdout");
54 
55 	/* This doesn't work because the usernames on different systems
56 	 * are different and cpio now looks up numeric UIDs on
57 	 * the local system. */
58 	/* assertEqualFile("tv.out", "test_option_tv.stdout"); */
59 
60 	/* List reference archive with numeric IDs, verify TOC is correct. */
61 	r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err",
62 		    testprog);
63 	assertEqualInt(r, 0);
64 	assertTextFileContents("1 block\n", "itnv.err");
65 	p = slurpfile(NULL, "itnv.out");
66 	/* Since -n uses numeric UID/GID, this part should be the
67 	 * same on every system. */
68 	assertEqualMem(p, "-rw-r--r--   1 1000     1000            0 ",42);
69 
70 	/* Date varies depending on local timezone and locale. */
71 	mtime = 1;
72 #ifdef HAVE_LOCALE_H
73 	setlocale(LC_ALL, "");
74 	setlocale(LC_TIME, "");
75 #endif
76 #if defined(HAVE_LOCALTIME_S)
77         tmptr = localtime_s(&tmbuf, &mtime) ? NULL : &tmbuf;
78 #elif defined(HAVE_LOCALTIME_R)
79         tmptr = localtime_r(&mtime, &tmbuf);
80 #else
81         tmptr = localtime(&mtime);
82 #endif
83 #if defined(_WIN32) && !defined(__CYGWIN__)
84 	strftime(date2, sizeof(date2)-1, "%b %d  %Y", tmptr);
85 	_snprintf(date, sizeof(date)-1, "%12s file", date2);
86 #else
87 	strftime(date2, sizeof(date2)-1, "%b %e  %Y", tmptr);
88 	snprintf(date, sizeof(date)-1, "%12s file", date2);
89 #endif
90 	assertEqualMem(p + 42, date, strlen(date));
91 	free(p);
92 
93 	/* But "-n" without "-t" is an error. */
94 	assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err",
95 			    testprog));
96 	assertEmptyFile("in.out");
97 }
98