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: src/usr.bin/tar/test/test_basic.c,v 1.2 2008/05/26 17:10:10 kientzle Exp $");
30 basic_tar(const char *target
, const char *pack_options
, const char *unpack_options
)
36 assertEqualInt(0, mkdir(target
, 0775));
38 /* Use the tar program to create an archive. */
39 r
= systemf("%s cf - %s `cat filelist` >%s/archive 2>%s/pack.err", testprog
, pack_options
, target
, target
);
40 failure("Error invoking %s cf -", testprog
, pack_options
);
45 /* Verify that nothing went to stderr. */
46 assertEmptyFile("pack.err");
49 * Use tar to unpack the archive into another directory.
51 r
= systemf("%s xf archive %s >unpack.out 2>unpack.err", testprog
, unpack_options
);
52 failure("Error invoking %s xf archive %s", testprog
, unpack_options
);
55 /* Verify that nothing went to stderr. */
56 assertEmptyFile("unpack.err");
59 * Verify unpacked files.
62 /* Regular file with 2 links. */
63 r
= lstat("file", &st
);
64 failure("Failed to stat file %s/file, errno=%d", target
, errno
);
67 assert(S_ISREG(st
.st_mode
));
68 assertEqualInt(0644, st
.st_mode
& 0777);
69 assertEqualInt(10, st
.st_size
);
70 failure("file %s/file", target
);
71 assertEqualInt(2, st
.st_nlink
);
74 /* Another name for the same file. */
75 r
= lstat("linkfile", &st2
);
76 failure("Failed to stat file %s/linkfile, errno=%d", target
, errno
);
79 assert(S_ISREG(st2
.st_mode
));
80 assertEqualInt(0644, st2
.st_mode
& 0777);
81 assertEqualInt(10, st2
.st_size
);
82 failure("file %s/linkfile", target
);
83 assertEqualInt(2, st2
.st_nlink
);
84 /* Verify that the two are really hardlinked. */
85 assertEqualInt(st
.st_dev
, st2
.st_dev
);
86 failure("%s/linkfile and %s/file aren't really hardlinks", target
, target
);
87 assertEqualInt(st
.st_ino
, st2
.st_ino
);
91 r
= lstat("symlink", &st
);
92 failure("Failed to stat file %s/symlink, errno=%d", target
, errno
);
95 failure("symlink should be a symlink; actual mode is %o",
97 assert(S_ISLNK(st
.st_mode
));
98 if (S_ISLNK(st
.st_mode
)) {
99 r
= readlink("symlink", buff
, sizeof(buff
));
100 assertEqualInt(r
, 4);
102 assertEqualString(buff
, "file");
107 r
= lstat("dir", &st
);
109 assertEqualInt(r
, 0);
110 assert(S_ISDIR(st
.st_mode
));
111 assertEqualInt(0775, st
.st_mode
& 0777);
117 DEFINE_TEST(test_basic
)
126 * Create an assortment of files on disk.
128 filelist
= open("filelist", O_CREAT
| O_WRONLY
, 0644);
130 /* File with 10 bytes content. */
131 fd
= open("file", O_CREAT
| O_WRONLY
, 0644);
133 assertEqualInt(10, write(fd
, "123456789", 10));
135 write(filelist
, "file\n", 5);
137 /* hardlink to above file. */
138 assertEqualInt(0, link("file", "linkfile"));
139 write(filelist
, "linkfile\n", 9);
141 /* Symlink to above file. */
142 assertEqualInt(0, symlink("file", "symlink"));
143 write(filelist
, "symlink\n", 8);
146 assertEqualInt(0, mkdir("dir", 0775));
147 write(filelist
, "dir\n", 4);
151 /* Archive/dearchive with a variety of options. */
152 basic_tar("copy", "", "");
153 /* tar doesn't handle cpio symlinks correctly */
154 /* basic_tar("copy_odc", "--format=odc", ""); */
155 basic_tar("copy_ustar", "--format=ustar", "");