1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2008 Tim Kientzle
5 * All rights reserved.
6 */
7 #include "test.h"
8
9 static int
tryMakeFile(const char * fn)10 tryMakeFile(const char *fn)
11 {
12 FILE *f = fopen(fn, "w");
13 if (f == NULL)
14 return (0);
15 fclose(f);
16 return (1);
17 }
18
DEFINE_TEST(test_option_T_upper)19 DEFINE_TEST(test_option_T_upper)
20 {
21 FILE *f;
22 int r;
23 int gnarlyFilesSupported;
24
25 /* Create a simple dir hierarchy; bail if anything fails. */
26 if (!assertMakeDir("d1", 0755)) return;
27 if (!assertMakeDir("d1/d2", 0755)) return;
28 if (!assertMakeFile("f", 0644, "")) return;
29 if (!assertMakeFile("d1/f1", 0644, "")) return;
30 if (!assertMakeFile("d1/f2", 0644, "")) return;
31 if (!assertMakeFile("d1/d2/f3", 0644, "")) return;
32 if (!assertMakeFile("d1/d2/f4", 0644, "")) return;
33 if (!assertMakeFile("d1/d2/f5", 0644, "")) return;
34 if (!assertMakeFile("d1/d2/f6", 0644, "")) return;
35 /* Some platforms don't permit such things; just skip it. */
36 gnarlyFilesSupported = tryMakeFile("d1/d2/f\x0a");
37
38 /* Populate a file list */
39 f = fopen("filelist", "w+");
40 if (!assert(f != NULL))
41 return;
42 /* Use a variety of text line endings. */
43 fprintf(f, "f\x0d"); /* CR */
44 fprintf(f, "d1/f1\x0d\x0a"); /* CRLF */
45 fprintf(f, "d1/d2/f4\x0a"); /* NL */
46 fprintf(f, "d1/d2/f6"); /* EOF */
47 fclose(f);
48
49 /* Populate a second file list */
50 f = fopen("filelist2", "w+");
51 if (!assert(f != NULL))
52 return;
53 /* Use null-terminated names. */
54 fprintf(f, "d1/d2/f3");
55 assertEqualInt(1, fwrite("\0", 1, 1, f));
56 fprintf(f, "d1/d2/f5");
57 assertEqualInt(1, fwrite("\0", 1, 1, f));
58 if (gnarlyFilesSupported) {
59 fprintf(f, "d1/d2/f\x0a");
60 assertEqualInt(1, fwrite("\0", 1, 1, f));
61 }
62 fclose(f);
63
64 /* Use -c -T to archive up the files. */
65 r = systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
66 testprog);
67 assert(r == 0);
68 assertEmptyFile("test1.out");
69 assertEmptyFile("test1.err");
70
71 /* Use -x -T to dearchive the files */
72 if (!assertMakeDir("test1", 0755)) return;
73 systemf("%s -x -f test1.tar -T filelist -C test1"
74 " > test1b.out 2> test1b.err", testprog);
75 assertEmptyFile("test1b.out");
76 assertEmptyFile("test1b.err");
77
78 /* Verify the files were extracted. */
79 assertFileExists("test1/f");
80 assertFileExists("test1/d1/f1");
81 assertFileNotExists("test1/d1/f2");
82 assertFileNotExists("test1/d1/d2/f3");
83 assertFileExists("test1/d1/d2/f4");
84 assertFileNotExists("test1/d1/d2/f5");
85 assertFileExists("test1/d1/d2/f6");
86 if (gnarlyFilesSupported) {
87 assertFileNotExists("test1/d1/d2/f\x0a");
88 }
89
90 /* Use -r -T to add more files to the archive. */
91 systemf("%s -r -f test1.tar --null -T filelist2 > test2.out 2> test2.err",
92 testprog);
93 assertEmptyFile("test2.out");
94 assertEmptyFile("test2.err");
95
96 /* Use -x without -T to dearchive the files (ensure -r worked) */
97 if (!assertMakeDir("test3", 0755)) return;
98 systemf("%s -x -f test1.tar -C test3"
99 " > test3.out 2> test3.err", testprog);
100 assertEmptyFile("test3.out");
101 assertEmptyFile("test3.err");
102 /* Verify the files were extracted.*/
103 assertFileExists("test3/f");
104 assertFileExists("test3/d1/f1");
105 assertFileNotExists("test3/d1/f2");
106 assertFileExists("test3/d1/d2/f3");
107 assertFileExists("test3/d1/d2/f4");
108 assertFileExists("test3/d1/d2/f5");
109 assertFileExists("test3/d1/d2/f6");
110 if (gnarlyFilesSupported) {
111 assertFileExists("test3/d1/d2/f\x0a");
112 }
113
114 /* Use -x -T to dearchive the files (verify -x -T together) */
115 if (!assertMakeDir("test2", 0755)) return;
116 systemf("%s -x -f test1.tar -T filelist -C test2"
117 " > test2b.out 2> test2b.err", testprog);
118 assertEmptyFile("test2b.out");
119 assertEmptyFile("test2b.err");
120 /* Verify the files were extracted.*/
121 assertFileExists("test2/f");
122 assertFileExists("test2/d1/f1");
123 assertFileNotExists("test2/d1/f2");
124 assertFileNotExists("test2/d1/d2/f3");
125 assertFileExists("test2/d1/d2/f4");
126 assertFileNotExists("test2/d1/d2/f5");
127 assertFileExists("test2/d1/d2/f6");
128 if (gnarlyFilesSupported) {
129 assertFileNotExists("test2/d1/d2/f\x0a");
130 }
131
132 assertMakeDir("test4", 0755);
133 assertMakeDir("test4_out", 0755);
134 assertMakeDir("test4_out2", 0755);
135 assertMakeDir("test4/d1", 0755);
136 assertMakeFile("test4/d1/foo", 0644, "");
137
138
139 /* TODO: Include some use of -C directory-changing within the filelist. */
140 /* I'm pretty sure -C within the filelist is broken on extract. */
141 }
142