1228753Smm/*-
2228753Smm * Copyright (c) 2003-2008 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753Smmstatic int
29232153SmmtryMakeFile(const char *fn)
30228753Smm{
31228753Smm	FILE *f = fopen(fn, "w");
32232153Smm	if (f == NULL)
33232153Smm		return (0);
34228753Smm	fclose(f);
35232153Smm	return (1);
36228753Smm}
37228753Smm
38228753SmmDEFINE_TEST(test_option_T_upper)
39228753Smm{
40228753Smm	FILE *f;
41228753Smm	int r;
42228753Smm	int gnarlyFilesSupported;
43228753Smm
44232153Smm	/* Create a simple dir hierarchy; bail if anything fails. */
45228753Smm	if (!assertMakeDir("d1", 0755)) return;
46228753Smm	if (!assertMakeDir("d1/d2", 0755))	return;
47232153Smm	if (!assertMakeFile("f", 0644, "")) return;
48232153Smm	if (!assertMakeFile("d1/f1", 0644, "")) return;
49232153Smm	if (!assertMakeFile("d1/f2", 0644, "")) return;
50232153Smm	if (!assertMakeFile("d1/d2/f3", 0644, "")) return;
51232153Smm	if (!assertMakeFile("d1/d2/f4", 0644, "")) return;
52232153Smm	if (!assertMakeFile("d1/d2/f5", 0644, "")) return;
53232153Smm	if (!assertMakeFile("d1/d2/f6", 0644, "")) return;
54228753Smm	/* Some platforms don't permit such things; just skip it. */
55232153Smm	gnarlyFilesSupported = tryMakeFile("d1/d2/f\x0a");
56228753Smm
57228753Smm	/* Populate a file list */
58228753Smm	f = fopen("filelist", "w+");
59228753Smm	if (!assert(f != NULL))
60228753Smm		return;
61228753Smm	/* Use a variety of text line endings. */
62228753Smm	fprintf(f, "f\x0d"); /* CR */
63228753Smm	fprintf(f, "d1/f1\x0d\x0a"); /* CRLF */
64228753Smm	fprintf(f, "d1/d2/f4\x0a"); /* NL */
65228753Smm	fprintf(f, "d1/d2/f6"); /* EOF */
66228753Smm	fclose(f);
67228753Smm
68228753Smm	/* Populate a second file list */
69228753Smm	f = fopen("filelist2", "w+");
70228753Smm	if (!assert(f != NULL))
71228753Smm		return;
72228753Smm	/* Use null-terminated names. */
73228753Smm	fprintf(f, "d1/d2/f3");
74232153Smm	assertEqualInt(1, fwrite("\0", 1, 1, f));
75228753Smm	fprintf(f, "d1/d2/f5");
76232153Smm	assertEqualInt(1, fwrite("\0", 1, 1, f));
77228753Smm	if (gnarlyFilesSupported) {
78228753Smm		fprintf(f, "d1/d2/f\x0a");
79232153Smm		assertEqualInt(1, fwrite("\0", 1, 1, f));
80228753Smm	}
81228753Smm	fclose(f);
82228753Smm
83228753Smm	/* Use -c -T to archive up the files. */
84228753Smm	r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
85228753Smm	    testprog);
86228753Smm	assert(r == 0);
87228753Smm	assertEmptyFile("test1.out");
88228753Smm	assertEmptyFile("test1.err");
89228753Smm
90228753Smm	/* Use -x -T to dearchive the files */
91228753Smm	if (!assertMakeDir("test1", 0755)) return;
92228753Smm	systemf("%s -x -f test1.tar -T filelist -C test1"
93228753Smm	    " > test1b.out 2> test1b.err", testprog);
94228753Smm	assertEmptyFile("test1b.out");
95228753Smm	assertEmptyFile("test1b.err");
96228753Smm
97228753Smm	/* Verify the files were extracted. */
98228753Smm	assertFileExists("test1/f");
99228753Smm	assertFileExists("test1/d1/f1");
100228753Smm	assertFileNotExists("test1/d1/f2");
101228753Smm	assertFileNotExists("test1/d1/d2/f3");
102228753Smm	assertFileExists("test1/d1/d2/f4");
103228753Smm	assertFileNotExists("test1/d1/d2/f5");
104228753Smm	assertFileExists("test1/d1/d2/f6");
105228753Smm	if (gnarlyFilesSupported) {
106228753Smm		assertFileNotExists("test1/d1/d2/f\x0a");
107228753Smm	}
108228753Smm
109228753Smm	/* Use -r -T to add more files to the archive. */
110228753Smm	systemf("%s -r -f test1.tar --null -T filelist2 > test2.out 2> test2.err",
111228753Smm	    testprog);
112228753Smm	assertEmptyFile("test2.out");
113228753Smm	assertEmptyFile("test2.err");
114228753Smm
115228753Smm	/* Use -x without -T to dearchive the files (ensure -r worked) */
116228753Smm	if (!assertMakeDir("test3", 0755)) return;
117228753Smm	systemf("%s -x -f test1.tar -C test3"
118228753Smm	    " > test3.out 2> test3.err", testprog);
119228753Smm	assertEmptyFile("test3.out");
120228753Smm	assertEmptyFile("test3.err");
121228753Smm	/* Verify the files were extracted.*/
122228753Smm	assertFileExists("test3/f");
123228753Smm	assertFileExists("test3/d1/f1");
124228753Smm	assertFileNotExists("test3/d1/f2");
125228753Smm	assertFileExists("test3/d1/d2/f3");
126228753Smm	assertFileExists("test3/d1/d2/f4");
127228753Smm	assertFileExists("test3/d1/d2/f5");
128228753Smm	assertFileExists("test3/d1/d2/f6");
129228753Smm	if (gnarlyFilesSupported) {
130228753Smm		assertFileExists("test3/d1/d2/f\x0a");
131228753Smm	}
132228753Smm
133228753Smm	/* Use -x -T to dearchive the files (verify -x -T together) */
134228753Smm	if (!assertMakeDir("test2", 0755)) return;
135228753Smm	systemf("%s -x -f test1.tar -T filelist -C test2"
136228753Smm	    " > test2b.out 2> test2b.err", testprog);
137228753Smm	assertEmptyFile("test2b.out");
138228753Smm	assertEmptyFile("test2b.err");
139228753Smm	/* Verify the files were extracted.*/
140228753Smm	assertFileExists("test2/f");
141228753Smm	assertFileExists("test2/d1/f1");
142228753Smm	assertFileNotExists("test2/d1/f2");
143228753Smm	assertFileNotExists("test2/d1/d2/f3");
144228753Smm	assertFileExists("test2/d1/d2/f4");
145228753Smm	assertFileNotExists("test2/d1/d2/f5");
146228753Smm	assertFileExists("test2/d1/d2/f6");
147228753Smm	if (gnarlyFilesSupported) {
148228753Smm		assertFileNotExists("test2/d1/d2/f\x0a");
149228753Smm	}
150228753Smm
151228753Smm	assertMakeDir("test4", 0755);
152228753Smm	assertMakeDir("test4_out", 0755);
153228753Smm	assertMakeDir("test4_out2", 0755);
154228753Smm	assertMakeDir("test4/d1", 0755);
155232153Smm	assertMakeFile("test4/d1/foo", 0644, "");
156228753Smm
157228753Smm
158228753Smm	/* TODO: Include some use of -C directory-changing within the filelist. */
159228753Smm	/* I'm pretty sure -C within the filelist is broken on extract. */
160228753Smm}
161