2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
38 assertEqualInt(0, mkdir("original", 0775));
40 assertEqualInt(0, mkdir("f", 0775));
41 assertEqualInt(0, mkdir("l", 0775));
42 assertEqualInt(0, mkdir("m", 0775));
43 assertEqualInt(0, mkdir("s", 0775));
44 assertEqualInt(0, mkdir("d", 0775));
46 for (i = 0; i < LOOP_MAX; i++) {
49 /* Create a file named "f/abcdef..." */
50 buff[i + 2] = 'a' + (i % 26);
52 fd = open(buff, O_CREAT | O_WRONLY, 0644);
54 assertEqualInt(i + 3, write(fd, buff, strlen(buff)));
57 /* Create a link named "l/abcdef..." to the above. */
60 assertEqualInt(0, link(buff, buff2));
62 /* Create a link named "m/abcdef..." to the above. */
65 assertEqualInt(0, link(buff, buff2));
67 #if !defined(_WIN32) || defined(__CYGWIN__)
68 /* Create a symlink named "s/abcdef..." to the above. */
69 strcpy(buff2 + 3, buff);
74 assertEqualInt(0, symlink(buff2, buff));
76 skipping("create a symlink to the above");
78 /* Create a dir named "d/abcdef...". */
80 assertEqualInt(0, mkdir(buff, 0775));
90 verify_tree(int limit)
104 /* Generate the names we know should be there and verify them. */
105 for (i = 1; i < LOOP_MAX; i++) {
106 /* Generate a base name of the correct length. */
107 for (j = 0; j < i; ++j)
108 filename[j] = 'a' + (j % 26);
110 for (n = i; n > 0; n /= 10)
111 filename[--j] = '0' + (n % 10);
115 /* Verify a file named "f/abcdef..." */
117 strcat(name1, filename);
118 if (limit != LIMIT_USTAR || strlen(filename) <= 100) {
119 fd = open(name1, O_RDONLY);
120 failure("Couldn't open \"%s\": %s",
121 name1, strerror(errno));
122 if (assert(fd >= 0)) {
123 len = read(fd, contents, i + 10);
125 assertEqualInt(len, i + 2);
126 /* Verify contents of 'contents' */
127 contents[len] = '\0';
128 failure("Each test file contains its own name");
129 assertEqualString(name1, contents);
130 /* stat() for dev/ino for next check */
131 assertEqualInt(0, lstat(name1, &st));
136 * ustar allows 100 chars for links, and we have
137 * "original/" as part of the name, so the link
138 * names here can't exceed 91 chars.
141 strcat(name2, filename);
142 if (limit != LIMIT_USTAR || strlen(name2) <= 100) {
143 /* Verify hardlink "l/abcdef..." */
144 assertEqualInt(0, (r = lstat(name2, &st2)));
146 assertEqualInt(st2.st_dev, st.st_dev);
147 assertEqualInt(st2.st_ino, st.st_ino);
150 /* Verify hardlink "m_abcdef..." */
152 assertEqualInt(0, (r = lstat(name2, &st2)));
154 assertEqualInt(st2.st_dev, st.st_dev);
155 assertEqualInt(st2.st_ino, st.st_ino);
159 #if !defined(_WIN32) || defined(__CYGWIN__)
161 * Symlink text doesn't include the 'original/' prefix,
162 * so the limit here is 100 characters.
164 /* Verify symlink "s/abcdef..." */
165 strcpy(name2, "../s/");
166 strcat(name2, filename);
167 if (limit != LIMIT_USTAR || strlen(name2) <= 100) {
168 /* This is a symlink. */
169 failure("Couldn't stat %s (length %d)",
170 filename, strlen(filename));
171 if (assertEqualInt(0, lstat(name2 + 3, &st2))) {
172 assert(S_ISLNK(st2.st_mode));
173 /* This is a symlink to the file above. */
174 failure("Couldn't stat %s", name2 + 3);
175 if (assertEqualInt(0, stat(name2 + 3, &st2))) {
176 assertEqualInt(st2.st_dev, st.st_dev);
177 assertEqualInt(st2.st_ino, st.st_ino);
182 skipping("verify symlink");
184 /* Verify dir "d/abcdef...". */
186 strcat(name1, filename);
187 if (limit != LIMIT_USTAR || strlen(filename) < 100) {
189 failure("Couldn't stat %s (length %d)",
190 name1, strlen(filename));
191 if (assertEqualInt(0, lstat(name1, &st2))) {
192 if (assert(S_ISDIR(st2.st_mode))) {
193 /* TODO: opendir/readdir this
194 * directory and make sure
202 /* Now make sure nothing is there that shouldn't be. */
203 for (dp = "dflms"; *dp != '\0'; ++dp) {
205 dir[0] = *dp; dir[1] = '\0';
207 failure("Unable to open dir '%s'", dir);
208 if (!assert(d != NULL))
210 while ((de = readdir(d)) != NULL) {
214 if (limit == LIMIT_USTAR) {
215 failure("strlen(p) = %d", strlen(p));
216 assert(strlen(p) <= 100);
219 if (limit == LIMIT_USTAR) {
220 failure("strlen(p)=%d", strlen(p));
221 assert(strlen(p) < 100);
224 if (limit == LIMIT_USTAR) {
225 failure("strlen(p)=%d", strlen(p));
226 assert(strlen(p) < 101);
228 /* Our files have very particular filename patterns. */
229 if (p[0] != '.' || (p[1] != '.' && p[1] != '\0')) {
230 for (i = 0; p[i] != '\0' && i < LOOP_MAX; i++) {
231 failure("i=%d, p[i]='%c' 'a'+(i%%26)='%c'", i, p[i], 'a' + (i % 26));
232 assertEqualInt(p[i], 'a' + (i % 26));
234 assert(p[i] == '\0');
238 assert(p[1] == '\0' || (p[1] == '.' && p[2] == '\0'));
241 failure("File %s shouldn't be here", p);
254 assertEqualInt(0, mkdir("plain", 0775));
255 assertEqualInt(0, chdir("plain"));
258 * Use the tar program to create an archive.
260 r = systemf("%s cf archive -C ../original f d l m s >pack.out 2>pack.err",
262 failure("Error invoking \"%s cf\"", testprog);
263 assertEqualInt(r, 0);
265 /* Verify that nothing went to stdout or stderr. */
266 assertEmptyFile("pack.err");
267 assertEmptyFile("pack.out");
270 * Use tar to unpack the archive into another directory.
272 r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
273 failure("Error invoking %s xf archive", testprog);
274 assertEqualInt(r, 0);
276 /* Verify that nothing went to stdout or stderr. */
277 assertEmptyFile("unpack.err");
278 assertEmptyFile("unpack.out");
280 verify_tree(LIMIT_NONE);
281 assertEqualInt(0, chdir(".."));
287 const char *target = "ustar";
290 assertEqualInt(0, mkdir(target, 0775));
291 assertEqualInt(0, chdir(target));
294 * Use the tar program to create an archive.
296 r = systemf("%s cf archive --format=ustar -C ../original f d l m s >pack.out 2>pack.err",
298 failure("Error invoking \"%s cf archive --format=ustar\"", testprog);
299 assertEqualInt(r, 0);
301 /* Verify that nothing went to stdout. */
302 assertEmptyFile("pack.out");
303 /* Stderr is non-empty, since there are a bunch of files
304 * with filenames too long to archive. */
307 * Use tar to unpack the archive into another directory.
309 r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
310 failure("Error invoking %s xf archive", testprog);
311 assertEqualInt(r, 0);
313 /* Verify that nothing went to stdout or stderr. */
314 assertEmptyFile("unpack.err");
315 assertEmptyFile("unpack.out");
318 verify_tree(LIMIT_USTAR);
322 DEFINE_TEST(test_copy)
328 create_tree(); /* Create sample files in "original" dir. */
330 /* Test simple "tar -c | tar -x" pipeline copy. */
333 /* Same, but constrain to ustar format. */