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 
29 DEFINE_TEST(test_option_t)
30 {
31 	char *p;
32 	int r;
33 
34 	/* List reference archive, make sure the TOC is correct. */
35 	extract_reference_file("test_option_t.cpio");
36 	r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog);
37 	assertEqualInt(r, 0);
38 	assertTextFileContents("1 block\n", "it.err");
39 	extract_reference_file("test_option_t.stdout");
40 	p = slurpfile(NULL, "test_option_t.stdout");
41 	assertTextFileContents(p, "it.out");
42 	free(p);
43 
44 	/* We accept plain "-t" as a synonym for "-it" */
45 	r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog);
46 	assertEqualInt(r, 0);
47 	assertTextFileContents("1 block\n", "t.err");
48 	extract_reference_file("test_option_t.stdout");
49 	p = slurpfile(NULL, "test_option_t.stdout");
50 	assertTextFileContents(p, "t.out");
51 	free(p);
52 
53 	/* But "-ot" is an error. */
54 	assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err",
55 			    testprog));
56 	assertEmptyFile("ot.out");
57 
58 	/* List reference archive verbosely, make sure the TOC is correct. */
59 	r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog);
60 	assertEqualInt(r, 0);
61 	assertTextFileContents("1 block\n", "tv.err");
62 	extract_reference_file("test_option_tv.stdout");
63 
64 	/* This doesn't work because the usernames on different systems
65 	 * are different and cpio now looks up numeric UIDs on
66 	 * the local system. */
67 	/* assertEqualFile("tv.out", "test_option_tv.stdout"); */
68 
69 	/* List reference archive with numeric IDs, verify TOC is correct. */
70 	r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err",
71 		    testprog);
72 	assertEqualInt(r, 0);
73 	assertTextFileContents("1 block\n", "itnv.err");
74 	p = slurpfile(NULL, "itnv.out");
75 	/* Since -n uses numeric UID/GID, this part should be the
76 	 * same on every system. */
77 	assertEqualMem(p, "-rw-r--r--   1 1000     1000            0 ",42);
78 	/* Date varies depending on local timezone. */
79 	if (memcmp(p + 42, "Dec 31  1969", 12) == 0) {
80 		/* East of Greenwich we get Dec 31, 1969. */
81 	} else {
82 		/* West of Greenwich get Jan 1, 1970 */
83 		assertEqualMem(p + 42, "Jan ", 4);
84 		/* Some systems format "Jan 01", some "Jan  1" */
85 		assert(p[46] == ' ' || p[46] == '0');
86 		assertEqualMem(p + 47, "1  1970 ", 8);
87 	}
88 	assertEqualMem(p + 54, " file", 5);
89 	free(p);
90 
91 	/* But "-n" without "-t" is an error. */
92 	assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err",
93 			    testprog));
94 	assertEmptyFile("in.out");
95 }
96