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: src/usr.bin/tar/test/test_basic.c,v 1.2 2008/05/26 17:10:10 kientzle Exp $"); 27 28 29 static void 30 basic_tar(const char *target, const char *pack_options, const char *unpack_options) 31 { 32 struct stat st, st2; 33 char buff[128]; 34 int r; 35 36 assertEqualInt(0, mkdir(target, 0775)); 37 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); 41 assertEqualInt(r, 0); 42 43 chdir(target); 44 45 /* Verify that nothing went to stderr. */ 46 assertEmptyFile("pack.err"); 47 48 /* 49 * Use tar to unpack the archive into another directory. 50 */ 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); 53 assertEqualInt(r, 0); 54 55 /* Verify that nothing went to stderr. */ 56 assertEmptyFile("unpack.err"); 57 58 /* 59 * Verify unpacked files. 60 */ 61 62 /* Regular file with 2 links. */ 63 r = lstat("file", &st); 64 failure("Failed to stat file %s/file, errno=%d", target, errno); 65 assertEqualInt(r, 0); 66 if (r == 0) { 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); 72 } 73 74 /* Another name for the same file. */ 75 r = lstat("linkfile", &st2); 76 failure("Failed to stat file %s/linkfile, errno=%d", target, errno); 77 assertEqualInt(r, 0); 78 if (r == 0) { 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); 88 } 89 90 /* Symlink */ 91 r = lstat("symlink", &st); 92 failure("Failed to stat file %s/symlink, errno=%d", target, errno); 93 assertEqualInt(r, 0); 94 if (r == 0) { 95 failure("symlink should be a symlink; actual mode is %o", 96 st.st_mode); 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); 101 buff[r] = '\0'; 102 assertEqualString(buff, "file"); 103 } 104 } 105 106 /* dir */ 107 r = lstat("dir", &st); 108 if (r == 0) { 109 assertEqualInt(r, 0); 110 assert(S_ISDIR(st.st_mode)); 111 assertEqualInt(0775, st.st_mode & 0777); 112 } 113 114 chdir(".."); 115 } 116 117 DEFINE_TEST(test_basic) 118 { 119 int fd; 120 int filelist; 121 int oldumask; 122 123 oldumask = umask(0); 124 125 /* 126 * Create an assortment of files on disk. 127 */ 128 filelist = open("filelist", O_CREAT | O_WRONLY, 0644); 129 130 /* File with 10 bytes content. */ 131 fd = open("file", O_CREAT | O_WRONLY, 0644); 132 assert(fd >= 0); 133 assertEqualInt(10, write(fd, "123456789", 10)); 134 close(fd); 135 write(filelist, "file\n", 5); 136 137 /* hardlink to above file. */ 138 assertEqualInt(0, link("file", "linkfile")); 139 write(filelist, "linkfile\n", 9); 140 141 /* Symlink to above file. */ 142 assertEqualInt(0, symlink("file", "symlink")); 143 write(filelist, "symlink\n", 8); 144 145 /* Directory. */ 146 assertEqualInt(0, mkdir("dir", 0775)); 147 write(filelist, "dir\n", 4); 148 /* All done. */ 149 close(filelist); 150 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", ""); 156 157 umask(oldumask); 158 } 159