1231200Smm/*-
2231200Smm * Copyright (c) 2003-2010 Tim Kientzle
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231200Smm */
25231200Smm#include "test.h"
26231200Smm__FBSDID("$FreeBSD$");
27231200Smm
28231200SmmDEFINE_TEST(test_option_uid_uname)
29231200Smm{
30231200Smm	char *reference, *data;
31231200Smm	size_t s;
32231200Smm
33231200Smm	assertUmask(0);
34231200Smm	assertMakeFile("file", 0644, "1234567890");
35231200Smm
36231200Smm	/* Create archive with no special options. */
37231200Smm	failure("Error invoking %s c", testprog);
38231200Smm	assertEqualInt(0,
39231200Smm	    systemf("%s cf archive1 --format=ustar file >stdout1.txt 2>stderr1.txt",
40231200Smm		testprog));
41231200Smm	assertEmptyFile("stdout1.txt");
42231200Smm	assertEmptyFile("stderr1.txt");
43231200Smm	reference = slurpfile(&s, "archive1");
44231200Smm
45231200Smm	/* Again with both --uid and --uname */
46231200Smm	failure("Error invoking %s c", testprog);
47231200Smm	assertEqualInt(0,
48313570Smm	    systemf("%s cf archive2 --uid=65123 --uname=foofoofoo --format=ustar file >stdout2.txt 2>stderr2.txt",
49231200Smm		testprog));
50231200Smm	assertEmptyFile("stdout2.txt");
51231200Smm	assertEmptyFile("stderr2.txt");
52231200Smm	data = slurpfile(&s, "archive2");
53231200Smm	/* Should force uid and uname fields in ustar header. */
54313570Smm	assertEqualMem(data + 108, "177143 \0", 8);
55231200Smm	assertEqualMem(data + 265, "foofoofoo\0", 10);
56310569Sngie	free(data);
57231200Smm
58231200Smm	/* Again with just --uid */
59231200Smm	failure("Error invoking %s c", testprog);
60231200Smm	assertEqualInt(0,
61313570Smm	    systemf("%s cf archive3 --uid=65123 --format=ustar file >stdout3.txt 2>stderr3.txt",
62231200Smm		testprog));
63231200Smm	assertEmptyFile("stdout3.txt");
64231200Smm	assertEmptyFile("stderr3.txt");
65231200Smm	data = slurpfile(&s, "archive3");
66313570Smm	assertEqualMem(data + 108, "177143 \0", 8);
67231200Smm	/* Uname field in ustar header should be empty. */
68231200Smm	assertEqualMem(data + 265, "\0", 1);
69310569Sngie	free(data);
70231200Smm
71231200Smm	/* Again with just --uname */
72231200Smm	failure("Error invoking %s c", testprog);
73231200Smm	assertEqualInt(0,
74231200Smm	    systemf("%s cf archive4 --uname=foofoofoo --format=ustar file >stdout4.txt 2>stderr4.txt",
75231200Smm		testprog));
76231200Smm	assertEmptyFile("stdout4.txt");
77231200Smm	assertEmptyFile("stderr4.txt");
78231200Smm	data = slurpfile(&s, "archive4");
79231200Smm	/* Uid should be unchanged from original reference. */
80231200Smm	assertEqualMem(data + 108, reference + 108, 8);
81231200Smm	assertEqualMem(data + 265, "foofoofoo\0", 10);
82310569Sngie	free(data);
83310569Sngie
84310569Sngie	free(reference);
85231200Smm}
86