xref: /freebsd/contrib/libarchive/tar/test/test_option_owner.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1b9128a37SMartin Matuska /*-
2*bd66c1b4SMartin Matuska  * SPDX-License-Identifier: BSD-2-Clause
3*bd66c1b4SMartin Matuska  *
4b9128a37SMartin Matuska  * Copyright (c) 2003-2010 Tim Kientzle
5b9128a37SMartin Matuska  * Copyright (c) 2024 Haelwenn (lanodan) Monnier
6b9128a37SMartin Matuska  * All rights reserved.
7b9128a37SMartin Matuska  */
8b9128a37SMartin Matuska #include "test.h"
9b9128a37SMartin Matuska 
DEFINE_TEST(test_option_owner)10b9128a37SMartin Matuska DEFINE_TEST(test_option_owner)
11b9128a37SMartin Matuska {
12b9128a37SMartin Matuska 	char *reference, *data;
13b9128a37SMartin Matuska 	size_t s;
14b9128a37SMartin Matuska 
15b9128a37SMartin Matuska 	assertUmask(0);
16b9128a37SMartin Matuska 	assertMakeFile("file", 0644, "1234567890");
17b9128a37SMartin Matuska 
18b9128a37SMartin Matuska 	/* Create archive with no special options. */
19b9128a37SMartin Matuska 	failure("Error invoking %s c", testprog);
20b9128a37SMartin Matuska 	assertEqualInt(0,
21b9128a37SMartin Matuska 	    systemf("%s cf archive1 --format=ustar file >stdout1.txt 2>stderr1.txt",
22b9128a37SMartin Matuska 		testprog));
23b9128a37SMartin Matuska 	assertEmptyFile("stdout1.txt");
24b9128a37SMartin Matuska 	assertEmptyFile("stderr1.txt");
25b9128a37SMartin Matuska 	reference = slurpfile(&s, "archive1");
26b9128a37SMartin Matuska 
27b9128a37SMartin Matuska 	/* Create archive with --owner (numeric) */
28b9128a37SMartin Matuska 	failure("Error invoking %s c", testprog);
29b9128a37SMartin Matuska 	assertEqualInt(0,
30b9128a37SMartin Matuska 	    systemf("%s cf archive2 --owner=65123 --format=ustar file >stdout2.txt 2>stderr2.txt",
31b9128a37SMartin Matuska 		testprog));
32b9128a37SMartin Matuska 	assertEmptyFile("stdout2.txt");
33b9128a37SMartin Matuska 	assertEmptyFile("stderr2.txt");
34b9128a37SMartin Matuska 	data = slurpfile(&s, "archive2");
35b9128a37SMartin Matuska 	assertEqualMem(data + 108, "177143 \0", 8);
36b9128a37SMartin Matuska 	/* Uname field in ustar header should be empty. */
37b9128a37SMartin Matuska 	assertEqualMem(data + 265, "\0", 1);
38b9128a37SMartin Matuska 	free(data);
39b9128a37SMartin Matuska 
40b9128a37SMartin Matuska 	/* Again with just --owner (name) */
41b9128a37SMartin Matuska 	failure("Error invoking %s c", testprog);
42b9128a37SMartin Matuska 	assertEqualInt(0,
43b9128a37SMartin Matuska 	    systemf("%s cf archive3 --owner=foofoofoo --format=ustar file >stdout3.txt 2>stderr3.txt",
44b9128a37SMartin Matuska 		testprog));
45b9128a37SMartin Matuska 	assertEmptyFile("stdout3.txt");
46b9128a37SMartin Matuska 	assertEmptyFile("stderr3.txt");
47b9128a37SMartin Matuska 	data = slurpfile(&s, "archive3");
48b9128a37SMartin Matuska 	/* Uid should be unchanged from original reference. */
49b9128a37SMartin Matuska 	assertEqualMem(data + 108, reference + 108, 8);
50b9128a37SMartin Matuska 	assertEqualMem(data + 265, "foofoofoo\0", 10);
51b9128a37SMartin Matuska 	free(data);
52b9128a37SMartin Matuska 
53b9128a37SMartin Matuska 	/* Again with just --owner (name:id) */
54b9128a37SMartin Matuska 	failure("Error invoking %s c", testprog);
55b9128a37SMartin Matuska 	assertEqualInt(0,
56b9128a37SMartin Matuska 	    systemf("%s cf archive4 --owner=foofoofoo:65123 --format=ustar file >stdout4.txt 2>stderr4.txt",
57b9128a37SMartin Matuska 		testprog));
58b9128a37SMartin Matuska 	assertEmptyFile("stdout4.txt");
59b9128a37SMartin Matuska 	assertEmptyFile("stderr4.txt");
60b9128a37SMartin Matuska 	data = slurpfile(&s, "archive4");
61b9128a37SMartin Matuska 	assertEqualMem(data + 108, "177143 \0", 8);
62b9128a37SMartin Matuska 	assertEqualMem(data + 265, "foofoofoo\0", 10);
63b9128a37SMartin Matuska 	free(data);
64b9128a37SMartin Matuska 
65b9128a37SMartin Matuska 	free(reference);
66b9128a37SMartin Matuska }
67