]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_copy.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tar / test / test_copy.c
1 /*-
2  * Copyright (c) 2003-2007 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
28 #define LOOP_MAX        170
29
30 static void
31 create_tree(void)
32 {
33         char buff[260];
34         char buff2[260];
35         int i;
36         int fd;
37
38         assertEqualInt(0, mkdir("original", 0775));
39         chdir("original");
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));
45
46         for (i = 0; i < LOOP_MAX; i++) {
47                 buff[0] = 'f';
48                 buff[1] = '/';
49                 /* Create a file named "f/abcdef..." */
50                 buff[i + 2] = 'a' + (i % 26);
51                 buff[i + 3] = '\0';
52                 fd = open(buff, O_CREAT | O_WRONLY, 0644);
53                 assert(fd >= 0);
54                 assertEqualInt(i + 3, write(fd, buff, strlen(buff)));
55                 close(fd);
56
57                 /* Create a link named "l/abcdef..." to the above. */
58                 strcpy(buff2, buff);
59                 buff2[0] = 'l';
60                 assertEqualInt(0, link(buff, buff2));
61
62                 /* Create a link named "m/abcdef..." to the above. */
63                 strcpy(buff2, buff);
64                 buff2[0] = 'm';
65                 assertEqualInt(0, link(buff, buff2));
66
67 #if !defined(_WIN32) || defined(__CYGWIN__)
68                 /* Create a symlink named "s/abcdef..." to the above. */
69                 strcpy(buff2 + 3, buff);
70                 buff[0] = 's';
71                 buff2[0] = '.';
72                 buff2[1] = '.';
73                 buff2[2] = '/';
74                 assertEqualInt(0, symlink(buff2, buff));
75 #else
76                 skipping("create a symlink to the above");
77 #endif
78                 /* Create a dir named "d/abcdef...". */
79                 buff[0] = 'd';
80                 assertEqualInt(0, mkdir(buff, 0775));
81         }
82
83         chdir("..");
84 }
85
86 #define LIMIT_NONE 0
87 #define LIMIT_USTAR 1
88
89 static void
90 verify_tree(int limit)
91 {
92         struct stat st, st2;
93         char filename[260];
94         char name1[260];
95         char name2[260];
96         char contents[260];
97         int i, j, r;
98         int fd;
99         int len;
100         const char *p, *dp;
101         DIR *d;
102         struct dirent *de;
103
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);
109 #if 0
110                 for (n = i; n > 0; n /= 10)
111                         filename[--j] = '0' + (n % 10);
112 #endif
113                 filename[i] = '\0';
114
115                 /* Verify a file named "f/abcdef..." */
116                 strcpy(name1, "f/");
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);
124                                 close(fd);
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));
132                         }
133                 }
134
135                 /*
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.
139                  */
140                 strcpy(name2, "l/");
141                 strcat(name2, filename);
142                 if (limit != LIMIT_USTAR || strlen(name2) <= 100) {
143                         /* Verify hardlink "l/abcdef..." */
144                         assertEqualInt(0, (r = lstat(name2, &st2)));
145                         if (r == 0) {
146                                 assertEqualInt(st2.st_dev, st.st_dev);
147                                 assertEqualInt(st2.st_ino, st.st_ino);
148                         }
149
150                         /* Verify hardlink "m_abcdef..." */
151                         name2[0] = 'm';
152                         assertEqualInt(0, (r = lstat(name2, &st2)));
153                         if (r == 0) {
154                                 assertEqualInt(st2.st_dev, st.st_dev);
155                                 assertEqualInt(st2.st_ino, st.st_ino);
156                         }
157                 }
158
159 #if !defined(_WIN32) || defined(__CYGWIN__)
160                 /*
161                  * Symlink text doesn't include the 'original/' prefix,
162                  * so the limit here is 100 characters.
163                  */
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);
178                                 }
179                         }
180                 }
181 #else
182                 skipping("verify symlink");
183 #endif
184                 /* Verify dir "d/abcdef...". */
185                 strcpy(name1, "d/");
186                 strcat(name1, filename);
187                 if (limit != LIMIT_USTAR || strlen(filename) < 100) {
188                         /* This is a dir. */
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
195                                          * it's empty.
196                                          */
197                                 }
198                         }
199                 }
200         }
201
202         /* Now make sure nothing is there that shouldn't be. */
203         for (dp = "dflms"; *dp != '\0'; ++dp) {
204                 char dir[2];
205                 dir[0] = *dp; dir[1] = '\0';
206                 d = opendir(dir);
207                 failure("Unable to open dir '%s'", dir);
208                 if (!assert(d != NULL))
209                         continue;
210                 while ((de = readdir(d)) != NULL) {
211                         p = de->d_name;
212                         switch(dp[0]) {
213                         case 'l': case 'm':
214                                 if (limit == LIMIT_USTAR) {
215                                         failure("strlen(p) = %d", strlen(p));
216                                         assert(strlen(p) <= 100);
217                                 }
218                         case 'd':
219                                 if (limit == LIMIT_USTAR) {
220                                         failure("strlen(p)=%d", strlen(p));
221                                         assert(strlen(p) < 100);
222                                 }
223                         case 'f': case 's':
224                                 if (limit == LIMIT_USTAR) {
225                                         failure("strlen(p)=%d", strlen(p));
226                                         assert(strlen(p) < 101);
227                                 }
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));
233                                         }
234                                         assert(p[i] == '\0');
235                                 }
236                                 break;
237                         case '.':
238                                 assert(p[1] == '\0' || (p[1] == '.' && p[2] == '\0'));
239                                 break;
240                         default:
241                                 failure("File %s shouldn't be here", p);
242                                 assert(0);
243                         }
244                 }
245                 closedir(d);
246         }
247 }
248
249 static void
250 copy_basic(void)
251 {
252         int r;
253
254         assertEqualInt(0, mkdir("plain", 0775));
255         assertEqualInt(0, chdir("plain"));
256
257         /*
258          * Use the tar program to create an archive.
259          */
260         r = systemf("%s cf archive -C ../original f d l m s >pack.out 2>pack.err",
261             testprog);
262         failure("Error invoking \"%s cf\"", testprog);
263         assertEqualInt(r, 0);
264
265         /* Verify that nothing went to stdout or stderr. */
266         assertEmptyFile("pack.err");
267         assertEmptyFile("pack.out");
268
269         /*
270          * Use tar to unpack the archive into another directory.
271          */
272         r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
273         failure("Error invoking %s xf archive", testprog);
274         assertEqualInt(r, 0);
275
276         /* Verify that nothing went to stdout or stderr. */
277         assertEmptyFile("unpack.err");
278         assertEmptyFile("unpack.out");
279
280         verify_tree(LIMIT_NONE);
281         assertEqualInt(0, chdir(".."));
282 }
283
284 static void
285 copy_ustar(void)
286 {
287         const char *target = "ustar";
288         int r;
289
290         assertEqualInt(0, mkdir(target, 0775));
291         assertEqualInt(0, chdir(target));
292
293         /*
294          * Use the tar program to create an archive.
295          */
296         r = systemf("%s cf archive --format=ustar -C ../original f d l m s >pack.out 2>pack.err",
297             testprog);
298         failure("Error invoking \"%s cf archive --format=ustar\"", testprog);
299         assertEqualInt(r, 0);
300
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. */
305
306         /*
307          * Use tar to unpack the archive into another directory.
308          */
309         r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
310         failure("Error invoking %s xf archive", testprog);
311         assertEqualInt(r, 0);
312
313         /* Verify that nothing went to stdout or stderr. */
314         assertEmptyFile("unpack.err");
315         assertEmptyFile("unpack.out");
316
317         chdir("original");
318         verify_tree(LIMIT_USTAR);
319         chdir("../..");
320 }
321
322 DEFINE_TEST(test_copy)
323 {
324         int oldumask;
325
326         oldumask = umask(0);
327
328         create_tree(); /* Create sample files in "original" dir. */
329
330         /* Test simple "tar -c | tar -x" pipeline copy. */
331         copy_basic();
332
333         /* Same, but constrain to ustar format. */
334         copy_ustar();
335
336         umask(oldumask);
337 }