xref: /freebsd/contrib/libarchive/tar/test/test_option_T_upper.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
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 
27 static int
28 tryMakeFile(const char *fn)
29 {
30 	FILE *f = fopen(fn, "w");
31 	if (f == NULL)
32 		return (0);
33 	fclose(f);
34 	return (1);
35 }
36 
37 DEFINE_TEST(test_option_T_upper)
38 {
39 	FILE *f;
40 	int r;
41 	int gnarlyFilesSupported;
42 
43 	/* Create a simple dir hierarchy; bail if anything fails. */
44 	if (!assertMakeDir("d1", 0755)) return;
45 	if (!assertMakeDir("d1/d2", 0755))	return;
46 	if (!assertMakeFile("f", 0644, "")) return;
47 	if (!assertMakeFile("d1/f1", 0644, "")) return;
48 	if (!assertMakeFile("d1/f2", 0644, "")) return;
49 	if (!assertMakeFile("d1/d2/f3", 0644, "")) return;
50 	if (!assertMakeFile("d1/d2/f4", 0644, "")) return;
51 	if (!assertMakeFile("d1/d2/f5", 0644, "")) return;
52 	if (!assertMakeFile("d1/d2/f6", 0644, "")) return;
53 	/* Some platforms don't permit such things; just skip it. */
54 	gnarlyFilesSupported = tryMakeFile("d1/d2/f\x0a");
55 
56 	/* Populate a file list */
57 	f = fopen("filelist", "w+");
58 	if (!assert(f != NULL))
59 		return;
60 	/* Use a variety of text line endings. */
61 	fprintf(f, "f\x0d"); /* CR */
62 	fprintf(f, "d1/f1\x0d\x0a"); /* CRLF */
63 	fprintf(f, "d1/d2/f4\x0a"); /* NL */
64 	fprintf(f, "d1/d2/f6"); /* EOF */
65 	fclose(f);
66 
67 	/* Populate a second file list */
68 	f = fopen("filelist2", "w+");
69 	if (!assert(f != NULL))
70 		return;
71 	/* Use null-terminated names. */
72 	fprintf(f, "d1/d2/f3");
73 	assertEqualInt(1, fwrite("\0", 1, 1, f));
74 	fprintf(f, "d1/d2/f5");
75 	assertEqualInt(1, fwrite("\0", 1, 1, f));
76 	if (gnarlyFilesSupported) {
77 		fprintf(f, "d1/d2/f\x0a");
78 		assertEqualInt(1, fwrite("\0", 1, 1, f));
79 	}
80 	fclose(f);
81 
82 	/* Use -c -T to archive up the files. */
83 	r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
84 	    testprog);
85 	assert(r == 0);
86 	assertEmptyFile("test1.out");
87 	assertEmptyFile("test1.err");
88 
89 	/* Use -x -T to dearchive the files */
90 	if (!assertMakeDir("test1", 0755)) return;
91 	systemf("%s -x -f test1.tar -T filelist -C test1"
92 	    " > test1b.out 2> test1b.err", testprog);
93 	assertEmptyFile("test1b.out");
94 	assertEmptyFile("test1b.err");
95 
96 	/* Verify the files were extracted. */
97 	assertFileExists("test1/f");
98 	assertFileExists("test1/d1/f1");
99 	assertFileNotExists("test1/d1/f2");
100 	assertFileNotExists("test1/d1/d2/f3");
101 	assertFileExists("test1/d1/d2/f4");
102 	assertFileNotExists("test1/d1/d2/f5");
103 	assertFileExists("test1/d1/d2/f6");
104 	if (gnarlyFilesSupported) {
105 		assertFileNotExists("test1/d1/d2/f\x0a");
106 	}
107 
108 	/* Use -r -T to add more files to the archive. */
109 	systemf("%s -r -f test1.tar --null -T filelist2 > test2.out 2> test2.err",
110 	    testprog);
111 	assertEmptyFile("test2.out");
112 	assertEmptyFile("test2.err");
113 
114 	/* Use -x without -T to dearchive the files (ensure -r worked) */
115 	if (!assertMakeDir("test3", 0755)) return;
116 	systemf("%s -x -f test1.tar -C test3"
117 	    " > test3.out 2> test3.err", testprog);
118 	assertEmptyFile("test3.out");
119 	assertEmptyFile("test3.err");
120 	/* Verify the files were extracted.*/
121 	assertFileExists("test3/f");
122 	assertFileExists("test3/d1/f1");
123 	assertFileNotExists("test3/d1/f2");
124 	assertFileExists("test3/d1/d2/f3");
125 	assertFileExists("test3/d1/d2/f4");
126 	assertFileExists("test3/d1/d2/f5");
127 	assertFileExists("test3/d1/d2/f6");
128 	if (gnarlyFilesSupported) {
129 		assertFileExists("test3/d1/d2/f\x0a");
130 	}
131 
132 	/* Use -x -T to dearchive the files (verify -x -T together) */
133 	if (!assertMakeDir("test2", 0755)) return;
134 	systemf("%s -x -f test1.tar -T filelist -C test2"
135 	    " > test2b.out 2> test2b.err", testprog);
136 	assertEmptyFile("test2b.out");
137 	assertEmptyFile("test2b.err");
138 	/* Verify the files were extracted.*/
139 	assertFileExists("test2/f");
140 	assertFileExists("test2/d1/f1");
141 	assertFileNotExists("test2/d1/f2");
142 	assertFileNotExists("test2/d1/d2/f3");
143 	assertFileExists("test2/d1/d2/f4");
144 	assertFileNotExists("test2/d1/d2/f5");
145 	assertFileExists("test2/d1/d2/f6");
146 	if (gnarlyFilesSupported) {
147 		assertFileNotExists("test2/d1/d2/f\x0a");
148 	}
149 
150 	assertMakeDir("test4", 0755);
151 	assertMakeDir("test4_out", 0755);
152 	assertMakeDir("test4_out2", 0755);
153 	assertMakeDir("test4/d1", 0755);
154 	assertMakeFile("test4/d1/foo", 0644, "");
155 
156 
157 	/* TODO: Include some use of -C directory-changing within the filelist. */
158 	/* I'm pretty sure -C within the filelist is broken on extract. */
159 }
160