]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_basic.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tar / test / test_basic.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
29 static void
30 basic_tar(const char *target, const char *pack_options,
31     const char *unpack_options, const char *flist)
32 {
33         struct stat st, st2;
34 #if !defined(_WIN32) || defined(__CYGWIN__)
35         char buff[128];
36 #endif
37         int r;
38
39         assertEqualInt(0, mkdir(target, 0775));
40
41         /* Use the tar program to create an archive. */
42 #if !defined(_WIN32) || defined(__CYGWIN__)
43         r = systemf("%s cf - %s `cat %s` >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
44 #else
45         r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
46 #endif
47         failure("Error invoking %s cf -", testprog, pack_options);
48         assertEqualInt(r, 0);
49
50         chdir(target);
51
52         /* Verify that nothing went to stderr. */
53         assertEmptyFile("pack.err");
54
55         /*
56          * Use tar to unpack the archive into another directory.
57          */
58         r = systemf("%s xf archive %s >unpack.out 2>unpack.err", testprog, unpack_options);
59         failure("Error invoking %s xf archive %s", testprog, unpack_options);
60         assertEqualInt(r, 0);
61
62         /* Verify that nothing went to stderr. */
63         assertEmptyFile("unpack.err");
64
65         /*
66          * Verify unpacked files.
67          */
68
69         /* Regular file with 2 links. */
70         r = lstat("file", &st);
71         failure("Failed to stat file %s/file, errno=%d", target, errno);
72         assertEqualInt(r, 0);
73         if (r == 0) {
74                 assert(S_ISREG(st.st_mode));
75 #if !defined(_WIN32) || defined(__CYGWIN__)
76                 assertEqualInt(0644, st.st_mode & 0777);
77 #else
78                 assertEqualInt(0600, st.st_mode & 0700);
79 #endif
80                 assertEqualInt(10, st.st_size);
81                 failure("file %s/file", target);
82                 assertEqualInt(2, st.st_nlink);
83         }
84
85         /* Another name for the same file. */
86         r = lstat("linkfile", &st2);
87         failure("Failed to stat file %s/linkfile, errno=%d", target, errno);
88         assertEqualInt(r, 0);
89         if (r == 0) {
90                 assert(S_ISREG(st2.st_mode));
91 #if !defined(_WIN32) || defined(__CYGWIN__)
92                 assertEqualInt(0644, st2.st_mode & 0777);
93 #else
94                 assertEqualInt(0600, st2.st_mode & 0700);
95 #endif
96                 assertEqualInt(10, st2.st_size);
97                 failure("file %s/linkfile", target);
98                 assertEqualInt(2, st2.st_nlink);
99                 /* Verify that the two are really hardlinked. */
100                 assertEqualInt(st.st_dev, st2.st_dev);
101                 failure("%s/linkfile and %s/file aren't really hardlinks", target, target);
102                 assertEqualInt(st.st_ino, st2.st_ino);
103         }
104
105 #if !defined(_WIN32) || defined(__CYGWIN__)
106         /* Symlink */
107         r = lstat("symlink", &st);
108         failure("Failed to stat file %s/symlink, errno=%d", target, errno);
109         assertEqualInt(r, 0);
110         if (r == 0) {
111                 failure("symlink should be a symlink; actual mode is %o",
112                     st.st_mode);
113                 assert(S_ISLNK(st.st_mode));
114                 if (S_ISLNK(st.st_mode)) {
115                         r = readlink("symlink", buff, sizeof(buff));
116                         assertEqualInt(r, 4);
117                         buff[r] = '\0';
118                         assertEqualString(buff, "file");
119                 }
120         }
121 #endif
122
123         /* dir */
124         r = lstat("dir", &st);
125         if (r == 0) {
126                 assertEqualInt(r, 0);
127                 assert(S_ISDIR(st.st_mode));
128 #if !defined(_WIN32) || defined(__CYGWIN__)
129                 assertEqualInt(0775, st.st_mode & 0777);
130 #else
131                 assertEqualInt(0700, st.st_mode & 0700);
132 #endif
133         }
134
135         chdir("..");
136 }
137
138 DEFINE_TEST(test_basic)
139 {
140         int fd;
141         int filelist;
142         int oldumask;
143         const char *flist;
144
145         oldumask = umask(0);
146
147         /*
148          * Create an assortment of files on disk.
149          */
150         filelist = open("filelist", O_CREAT | O_WRONLY, 0644);
151
152         /* File with 10 bytes content. */
153         fd = open("file", O_CREAT | O_WRONLY, 0644);
154         assert(fd >= 0);
155         assertEqualInt(10, write(fd, "123456789", 10));
156         close(fd);
157         write(filelist, "file\n", 5);
158
159         /* hardlink to above file. */
160         assertEqualInt(0, link("file", "linkfile"));
161         write(filelist, "linkfile\n", 9);
162
163         /* Symlink to above file. */
164         assertEqualInt(0, symlink("file", "symlink"));
165         write(filelist, "symlink\n", 8);
166
167         /* Directory. */
168         assertEqualInt(0, mkdir("dir", 0775));
169         write(filelist, "dir\n", 4);
170         /* All done. */
171         close(filelist);
172
173 #if !defined(_WIN32) || defined(__CYGWIN__)
174         flist = "filelist";
175 #else
176         flist = "file linkfile symlink dir";
177 #endif
178         /* Archive/dearchive with a variety of options. */
179         basic_tar("copy", "", "", flist);
180         /* tar doesn't handle cpio symlinks correctly */
181         /* basic_tar("copy_odc", "--format=odc", ""); */
182         basic_tar("copy_ustar", "--format=ustar", "", flist);
183
184         umask(oldumask);
185 }