1/*-
2 * Copyright (c) 2003-2010 Tim Kientzle
3 * Copyright (c) 2024 Haelwenn (lanodan) Monnier
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#include "test.h"
27
28DEFINE_TEST(test_option_owner)
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	/* Create archive with --owner (numeric) */
46	failure("Error invoking %s c", testprog);
47	assertEqualInt(0,
48	    systemf("%s cf archive2 --owner=65123 --format=ustar file >stdout2.txt 2>stderr2.txt",
49		testprog));
50	assertEmptyFile("stdout2.txt");
51	assertEmptyFile("stderr2.txt");
52	data = slurpfile(&s, "archive2");
53	assertEqualMem(data + 108, "177143 \0", 8);
54	/* Uname field in ustar header should be empty. */
55	assertEqualMem(data + 265, "\0", 1);
56	free(data);
57
58	/* Again with just --owner (name) */
59	failure("Error invoking %s c", testprog);
60	assertEqualInt(0,
61	    systemf("%s cf archive3 --owner=foofoofoo --format=ustar file >stdout3.txt 2>stderr3.txt",
62		testprog));
63	assertEmptyFile("stdout3.txt");
64	assertEmptyFile("stderr3.txt");
65	data = slurpfile(&s, "archive3");
66	/* Uid should be unchanged from original reference. */
67	assertEqualMem(data + 108, reference + 108, 8);
68	assertEqualMem(data + 265, "foofoofoo\0", 10);
69	free(data);
70
71	/* Again with just --owner (name:id) */
72	failure("Error invoking %s c", testprog);
73	assertEqualInt(0,
74	    systemf("%s cf archive4 --owner=foofoofoo:65123 --format=ustar file >stdout4.txt 2>stderr4.txt",
75		testprog));
76	assertEmptyFile("stdout4.txt");
77	assertEmptyFile("stderr4.txt");
78	data = slurpfile(&s, "archive4");
79	assertEqualMem(data + 108, "177143 \0", 8);
80	assertEqualMem(data + 265, "foofoofoo\0", 10);
81	free(data);
82
83	free(reference);
84}
85