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