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$");
36 assertEqualInt(0, mkdir("original", 0775));
38 assertEqualInt(0, mkdir("f", 0775));
39 assertEqualInt(0, mkdir("l", 0775));
40 assertEqualInt(0, mkdir("m", 0775));
41 assertEqualInt(0, mkdir("s", 0775));
42 assertEqualInt(0, mkdir("d", 0775));
44 for (i = 0; i < 200; i++) {
47 /* Create a file named "f/abcdef..." */
48 buff[i + 2] = 'a' + (i % 26);
50 fd = open(buff, O_CREAT | O_WRONLY, 0644);
52 assertEqualInt(i + 3, write(fd, buff, strlen(buff)));
55 /* Create a link named "l/abcdef..." to the above. */
58 assertEqualInt(0, link(buff, buff2));
60 /* Create a link named "m/abcdef..." to the above. */
63 assertEqualInt(0, link(buff, buff2));
65 /* Create a symlink named "s/abcdef..." to the above. */
66 strcpy(buff2 + 3, buff);
71 assertEqualInt(0, symlink(buff2, buff));
73 /* Create a dir named "d/abcdef...". */
75 assertEqualInt(0, mkdir(buff, 0775));
85 verify_tree(int limit)
99 /* Generate the names we know should be there and verify them. */
100 for (i = 1; i < 200; i++) {
101 /* Generate a base name of the correct length. */
102 for (j = 0; j < i; ++j)
103 filename[j] = 'a' + (j % 26);
105 for (n = i; n > 0; n /= 10)
106 filename[--j] = '0' + (n % 10);
110 /* Verify a file named "f/abcdef..." */
112 strcat(name1, filename);
113 if (limit != LIMIT_USTAR || strlen(filename) <= 100) {
114 fd = open(name1, O_RDONLY);
115 failure("Couldn't open \"%s\": %s",
116 name1, strerror(errno));
117 if (assert(fd >= 0)) {
118 len = read(fd, contents, i + 10);
120 assertEqualInt(len, i + 2);
121 /* Verify contents of 'contents' */
122 contents[len] = '\0';
123 failure("Each test file contains its own name");
124 assertEqualString(name1, contents);
125 /* stat() for dev/ino for next check */
126 assertEqualInt(0, lstat(name1, &st));
131 * ustar allows 100 chars for links, and we have
132 * "original/" as part of the name, so the link
133 * names here can't exceed 91 chars.
136 strcat(name2, filename);
137 if (limit != LIMIT_USTAR || strlen(name2) <= 100) {
138 /* Verify hardlink "l/abcdef..." */
139 assertEqualInt(0, (r = lstat(name2, &st2)));
141 assertEqualInt(st2.st_dev, st.st_dev);
142 assertEqualInt(st2.st_ino, st.st_ino);
145 /* Verify hardlink "m_abcdef..." */
147 assertEqualInt(0, (r = lstat(name2, &st2)));
149 assertEqualInt(st2.st_dev, st.st_dev);
150 assertEqualInt(st2.st_ino, st.st_ino);
155 * Symlink text doesn't include the 'original/' prefix,
156 * so the limit here is 100 characters.
158 /* Verify symlink "s/abcdef..." */
159 strcpy(name2, "../s/");
160 strcat(name2, filename);
161 if (limit != LIMIT_USTAR || strlen(name2) <= 100) {
162 /* This is a symlink. */
163 failure("Couldn't stat %s (length %d)",
164 filename, strlen(filename));
165 if (assertEqualInt(0, lstat(name2 + 3, &st2))) {
166 assert(S_ISLNK(st2.st_mode));
167 /* This is a symlink to the file above. */
168 failure("Couldn't stat %s", name2 + 3);
169 if (assertEqualInt(0, stat(name2 + 3, &st2))) {
170 assertEqualInt(st2.st_dev, st.st_dev);
171 assertEqualInt(st2.st_ino, st.st_ino);
176 /* Verify dir "d/abcdef...". */
178 strcat(name1, filename);
179 if (limit != LIMIT_USTAR || strlen(filename) < 100) {
181 failure("Couldn't stat %s (length %d)",
182 name1, strlen(filename));
183 if (assertEqualInt(0, lstat(name1, &st2))) {
184 if (assert(S_ISDIR(st2.st_mode))) {
185 /* TODO: opendir/readdir this
186 * directory and make sure
194 /* Now make sure nothing is there that shouldn't be. */
195 for (dp = "dflms"; *dp != '\0'; ++dp) {
197 dir[0] = *dp; dir[1] = '\0';
199 failure("Unable to open dir '%s'", dir);
200 if (!assert(d != NULL))
202 while ((de = readdir(d)) != NULL) {
206 if (limit == LIMIT_USTAR) {
207 failure("strlen(p) = %d", strlen(p));
208 assert(strlen(p) <= 100);
211 if (limit == LIMIT_USTAR) {
212 failure("strlen(p)=%d", strlen(p));
213 assert(strlen(p) < 100);
216 if (limit == LIMIT_USTAR) {
217 failure("strlen(p)=%d", strlen(p));
218 assert(strlen(p) < 101);
220 /* Our files have very particular filename patterns. */
221 if (p[0] != '.' || (p[1] != '.' && p[1] != '\0')) {
222 for (i = 0; p[i] != '\0' && i < 200; i++) {
223 failure("i=%d, p[i]='%c' 'a'+(i%%26)='%c'", i, p[i], 'a' + (i % 26));
224 assertEqualInt(p[i], 'a' + (i % 26));
226 assert(p[i] == '\0');
230 assert(p[1] == '\0' || (p[1] == '.' && p[2] == '\0'));
233 failure("File %s shouldn't be here", p);
246 assertEqualInt(0, mkdir("plain", 0775));
247 assertEqualInt(0, chdir("plain"));
250 * Use the tar program to create an archive.
252 r = systemf("%s cf archive -C ../original f d l m s >pack.out 2>pack.err",
254 failure("Error invoking \"%s cf\"", testprog);
255 assertEqualInt(r, 0);
257 /* Verify that nothing went to stdout or stderr. */
258 assertEmptyFile("pack.err");
259 assertEmptyFile("pack.out");
262 * Use tar to unpack the archive into another directory.
264 r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
265 failure("Error invoking %s xf archive", testprog);
266 assertEqualInt(r, 0);
268 /* Verify that nothing went to stdout or stderr. */
269 assertEmptyFile("unpack.err");
270 assertEmptyFile("unpack.out");
272 verify_tree(LIMIT_NONE);
273 assertEqualInt(0, chdir(".."));
279 const char *target = "ustar";
282 assertEqualInt(0, mkdir(target, 0775));
283 assertEqualInt(0, chdir(target));
286 * Use the tar program to create an archive.
288 r = systemf("%s cf archive --format=ustar -C ../original f d l m s >pack.out 2>pack.err",
290 failure("Error invoking \"%s cf archive --format=ustar\"", testprog);
291 assertEqualInt(r, 0);
293 /* Verify that nothing went to stdout. */
294 assertEmptyFile("pack.out");
295 /* Stderr is non-empty, since there are a bunch of files
296 * with filenames too long to archive. */
299 * Use tar to unpack the archive into another directory.
301 r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
302 failure("Error invoking %s xf archive", testprog);
303 assertEqualInt(r, 0);
305 /* Verify that nothing went to stdout or stderr. */
306 assertEmptyFile("unpack.err");
307 assertEmptyFile("unpack.out");
310 verify_tree(LIMIT_USTAR);
314 DEFINE_TEST(test_copy)
320 create_tree(); /* Create sample files in "original" dir. */
322 /* Test simple "tar -c | tar -x" pipeline copy. */
325 /* Same, but constrain to ustar format. */