1/*-
2 * Copyright (c) 2003-2008 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "test.h"
26__FBSDID("$FreeBSD$");
27
28static int
29tryMakeFile(const char *fn)
30{
31	FILE *f = fopen(fn, "w");
32	if (f == NULL)
33		return (0);
34	fclose(f);
35	return (1);
36}
37
38DEFINE_TEST(test_option_T_upper)
39{
40	FILE *f;
41	int r;
42	int gnarlyFilesSupported;
43
44	/* Create a simple dir hierarchy; bail if anything fails. */
45	if (!assertMakeDir("d1", 0755)) return;
46	if (!assertMakeDir("d1/d2", 0755))	return;
47	if (!assertMakeFile("f", 0644, "")) return;
48	if (!assertMakeFile("d1/f1", 0644, "")) return;
49	if (!assertMakeFile("d1/f2", 0644, "")) return;
50	if (!assertMakeFile("d1/d2/f3", 0644, "")) return;
51	if (!assertMakeFile("d1/d2/f4", 0644, "")) return;
52	if (!assertMakeFile("d1/d2/f5", 0644, "")) return;
53	if (!assertMakeFile("d1/d2/f6", 0644, "")) return;
54	/* Some platforms don't permit such things; just skip it. */
55	gnarlyFilesSupported = tryMakeFile("d1/d2/f\x0a");
56
57	/* Populate a file list */
58	f = fopen("filelist", "w+");
59	if (!assert(f != NULL))
60		return;
61	/* Use a variety of text line endings. */
62	fprintf(f, "f\x0d"); /* CR */
63	fprintf(f, "d1/f1\x0d\x0a"); /* CRLF */
64	fprintf(f, "d1/d2/f4\x0a"); /* NL */
65	fprintf(f, "d1/d2/f6"); /* EOF */
66	fclose(f);
67
68	/* Populate a second file list */
69	f = fopen("filelist2", "w+");
70	if (!assert(f != NULL))
71		return;
72	/* Use null-terminated names. */
73	fprintf(f, "d1/d2/f3");
74	assertEqualInt(1, fwrite("\0", 1, 1, f));
75	fprintf(f, "d1/d2/f5");
76	assertEqualInt(1, fwrite("\0", 1, 1, f));
77	if (gnarlyFilesSupported) {
78		fprintf(f, "d1/d2/f\x0a");
79		assertEqualInt(1, fwrite("\0", 1, 1, f));
80	}
81	fclose(f);
82
83	/* Use -c -T to archive up the files. */
84	r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
85	    testprog);
86	assert(r == 0);
87	assertEmptyFile("test1.out");
88	assertEmptyFile("test1.err");
89
90	/* Use -x -T to dearchive the files */
91	if (!assertMakeDir("test1", 0755)) return;
92	systemf("%s -x -f test1.tar -T filelist -C test1"
93	    " > test1b.out 2> test1b.err", testprog);
94	assertEmptyFile("test1b.out");
95	assertEmptyFile("test1b.err");
96
97	/* Verify the files were extracted. */
98	assertFileExists("test1/f");
99	assertFileExists("test1/d1/f1");
100	assertFileNotExists("test1/d1/f2");
101	assertFileNotExists("test1/d1/d2/f3");
102	assertFileExists("test1/d1/d2/f4");
103	assertFileNotExists("test1/d1/d2/f5");
104	assertFileExists("test1/d1/d2/f6");
105	if (gnarlyFilesSupported) {
106		assertFileNotExists("test1/d1/d2/f\x0a");
107	}
108
109	/* Use -r -T to add more files to the archive. */
110	systemf("%s -r -f test1.tar --null -T filelist2 > test2.out 2> test2.err",
111	    testprog);
112	assertEmptyFile("test2.out");
113	assertEmptyFile("test2.err");
114
115	/* Use -x without -T to dearchive the files (ensure -r worked) */
116	if (!assertMakeDir("test3", 0755)) return;
117	systemf("%s -x -f test1.tar -C test3"
118	    " > test3.out 2> test3.err", testprog);
119	assertEmptyFile("test3.out");
120	assertEmptyFile("test3.err");
121	/* Verify the files were extracted.*/
122	assertFileExists("test3/f");
123	assertFileExists("test3/d1/f1");
124	assertFileNotExists("test3/d1/f2");
125	assertFileExists("test3/d1/d2/f3");
126	assertFileExists("test3/d1/d2/f4");
127	assertFileExists("test3/d1/d2/f5");
128	assertFileExists("test3/d1/d2/f6");
129	if (gnarlyFilesSupported) {
130		assertFileExists("test3/d1/d2/f\x0a");
131	}
132
133	/* Use -x -T to dearchive the files (verify -x -T together) */
134	if (!assertMakeDir("test2", 0755)) return;
135	systemf("%s -x -f test1.tar -T filelist -C test2"
136	    " > test2b.out 2> test2b.err", testprog);
137	assertEmptyFile("test2b.out");
138	assertEmptyFile("test2b.err");
139	/* Verify the files were extracted.*/
140	assertFileExists("test2/f");
141	assertFileExists("test2/d1/f1");
142	assertFileNotExists("test2/d1/f2");
143	assertFileNotExists("test2/d1/d2/f3");
144	assertFileExists("test2/d1/d2/f4");
145	assertFileNotExists("test2/d1/d2/f5");
146	assertFileExists("test2/d1/d2/f6");
147	if (gnarlyFilesSupported) {
148		assertFileNotExists("test2/d1/d2/f\x0a");
149	}
150
151	assertMakeDir("test4", 0755);
152	assertMakeDir("test4_out", 0755);
153	assertMakeDir("test4_out2", 0755);
154	assertMakeDir("test4/d1", 0755);
155	assertMakeFile("test4/d1/foo", 0644, "");
156
157
158	/* TODO: Include some use of -C directory-changing within the filelist. */
159	/* I'm pretty sure -C within the filelist is broken on extract. */
160}
161