xref: /freebsd/contrib/libarchive/tar/test/test_option_uid_uname.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2003-2010 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 DEFINE_TEST(test_option_uid_uname)
29 {
30 	char *reference, *data;
31 	size_t s;
32 
33 	assertUmask(0);
34 	assertMakeFile("file", 0644, "1234567890");
35 
36 	/* Create archive with no special options. */
37 	failure("Error invoking %s c", testprog);
38 	assertEqualInt(0,
39 	    systemf("%s cf archive1 --format=ustar file >stdout1.txt 2>stderr1.txt",
40 		testprog));
41 	assertEmptyFile("stdout1.txt");
42 	assertEmptyFile("stderr1.txt");
43 	reference = slurpfile(&s, "archive1");
44 
45 	/* Again with both --uid and --uname */
46 	failure("Error invoking %s c", testprog);
47 	assertEqualInt(0,
48 	    systemf("%s cf archive2 --uid=17 --uname=foofoofoo --format=ustar file >stdout2.txt 2>stderr2.txt",
49 		testprog));
50 	assertEmptyFile("stdout2.txt");
51 	assertEmptyFile("stderr2.txt");
52 	data = slurpfile(&s, "archive2");
53 	/* Should force uid and uname fields in ustar header. */
54 	assertEqualMem(data + 108, "000021 \0", 8);
55 	assertEqualMem(data + 265, "foofoofoo\0", 10);
56 
57 	/* Again with just --uid */
58 	failure("Error invoking %s c", testprog);
59 	assertEqualInt(0,
60 	    systemf("%s cf archive3 --uid=17 --format=ustar file >stdout3.txt 2>stderr3.txt",
61 		testprog));
62 	assertEmptyFile("stdout3.txt");
63 	assertEmptyFile("stderr3.txt");
64 	data = slurpfile(&s, "archive3");
65 	assertEqualMem(data + 108, "000021 \0", 8);
66 	/* Uname field in ustar header should be empty. */
67 	assertEqualMem(data + 265, "\0", 1);
68 
69 	/* Again with just --uname */
70 	failure("Error invoking %s c", testprog);
71 	assertEqualInt(0,
72 	    systemf("%s cf archive4 --uname=foofoofoo --format=ustar file >stdout4.txt 2>stderr4.txt",
73 		testprog));
74 	assertEmptyFile("stdout4.txt");
75 	assertEmptyFile("stderr4.txt");
76 	data = slurpfile(&s, "archive4");
77 	/* Uid should be unchanged from original reference. */
78 	assertEqualMem(data + 108, reference + 108, 8);
79 	assertEqualMem(data + 265, "foofoofoo\0", 10);
80 }
81