test_symlink_dir.c revision 228759
1174421Sdougb/*- 2174421Sdougb * Copyright (c) 2003-2007 Tim Kientzle 3174421Sdougb * All rights reserved. 4174421Sdougb * 5174421Sdougb * Redistribution and use in source and binary forms, with or without 6174421Sdougb * modification, are permitted provided that the following conditions 7174421Sdougb * are met: 8174421Sdougb * 1. Redistributions of source code must retain the above copyright 9174421Sdougb * notice, this list of conditions and the following disclaimer. 10174421Sdougb * 2. Redistributions in binary form must reproduce the above copyright 11174421Sdougb * notice, this list of conditions and the following disclaimer in the 12174421Sdougb * documentation and/or other materials provided with the distribution. 13174421Sdougb * 14174421Sdougb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15174421Sdougb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16174421Sdougb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17174421Sdougb * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18174421Sdougb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19174421Sdougb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20174421Sdougb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21174421Sdougb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22174421Sdougb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23174421Sdougb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24174421Sdougb */ 25174421Sdougb#include "test.h" 26174421Sdougb__FBSDID("$FreeBSD: src/usr.bin/tar/test/test_symlink_dir.c,v 1.1 2008/09/14 02:16:04 kientzle Exp $"); 27174421Sdougb 28174421Sdougb/* 29174421Sdougb * tar -x -P should follow existing symlinks for dirs, but not other 30174421Sdougb * content. Plain tar -x should remove symlinks when they're in the 31174421Sdougb * way of a dir extraction. 32174421Sdougb */ 33174421Sdougb 34174421Sdougbstatic int 35174421Sdougbmkfile(const char *name, int mode, const char *contents, size_t size) 36174421Sdougb{ 37174421Sdougb FILE *f = fopen(name, "wb"); 38174421Sdougb size_t written; 39174421Sdougb 40174421Sdougb (void)mode; /* UNUSED */ 41174421Sdougb if (f == NULL) 42174421Sdougb return (-1); 43174421Sdougb written = fwrite(contents, 1, size, f); 44174421Sdougb fclose(f); 45174421Sdougb if (size != written) 46174421Sdougb return (-1); 47174421Sdougb return (0); 48174421Sdougb} 49174421Sdougb 50174421SdougbDEFINE_TEST(test_symlink_dir) 51174421Sdougb{ 52174421Sdougb assertUmask(0); 53174421Sdougb 54174421Sdougb assertMakeDir("source", 0755); 55174421Sdougb assertEqualInt(0, mkfile("source/file", 0755, "a", 1)); 56174421Sdougb assertEqualInt(0, mkfile("source/file2", 0755, "ab", 2)); 57174421Sdougb assertMakeDir("source/dir", 0755); 58174421Sdougb assertMakeDir("source/dir/d", 0755); 59174421Sdougb assertEqualInt(0, mkfile("source/dir/f", 0755, "abc", 3)); 60174421Sdougb assertMakeDir("source/dir2", 0755); 61174421Sdougb assertMakeDir("source/dir2/d2", 0755); 62174421Sdougb assertEqualInt(0, mkfile("source/dir2/f2", 0755, "abcd", 4)); 63174421Sdougb assertMakeDir("source/dir3", 0755); 64174421Sdougb assertMakeDir("source/dir3/d3", 0755); 65174421Sdougb assertEqualInt(0, mkfile("source/dir3/f3", 0755, "abcde", 5)); 66174421Sdougb 67174421Sdougb assertEqualInt(0, 68174421Sdougb systemf("%s -cf test.tar -C source dir dir2 dir3 file file2", 69174421Sdougb testprog)); 70174421Sdougb 71174421Sdougb /* 72174421Sdougb * Extract with -x and without -P. 73174421Sdougb */ 74174421Sdougb assertMakeDir("dest1", 0755); 75174421Sdougb /* "dir" is a symlink to an existing "dest1/real_dir" */ 76174421Sdougb assertMakeDir("dest1/real_dir", 0755); 77174421Sdougb if (canSymlink()) { 78174421Sdougb assertMakeSymlink("dest1/dir", "real_dir"); 79174421Sdougb /* "dir2" is a symlink to a non-existing "real_dir2" */ 80174421Sdougb assertMakeSymlink("dest1/dir2", "real_dir2"); 81174421Sdougb } else { 82174421Sdougb skipping("some symlink checks"); 83174421Sdougb } 84174421Sdougb /* "dir3" is a symlink to an existing "non_dir3" */ 85174421Sdougb assertEqualInt(0, mkfile("dest1/non_dir3", 0755, "abcdef", 6)); 86174421Sdougb if (canSymlink()) 87174421Sdougb assertMakeSymlink("dest1/dir3", "non_dir3"); 88 /* "file" is a symlink to existing "real_file" */ 89 assertEqualInt(0, mkfile("dest1/real_file", 0755, "abcdefg", 7)); 90 if (canSymlink()) { 91 assertMakeSymlink("dest1/file", "real_file"); 92 /* "file2" is a symlink to non-existing "real_file2" */ 93 assertMakeSymlink("dest1/file2", "real_file2"); 94 } 95 assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog)); 96 97 /* dest1/dir symlink should be replaced */ 98 failure("symlink to dir was followed when it shouldn't be"); 99 assertIsDir("dest1/dir", -1); 100 /* dest1/dir2 symlink should be replaced */ 101 failure("Broken symlink wasn't replaced with dir"); 102 assertIsDir("dest1/dir2", -1); 103 /* dest1/dir3 symlink should be replaced */ 104 failure("Symlink to non-dir wasn't replaced with dir"); 105 assertIsDir("dest1/dir3", -1); 106 /* dest1/file symlink should be replaced */ 107 failure("Symlink to existing file should be replaced"); 108 assertIsReg("dest1/file", -1); 109 /* dest1/file2 symlink should be replaced */ 110 failure("Symlink to non-existing file should be replaced"); 111 assertIsReg("dest1/file2", -1); 112 113 /* 114 * Extract with both -x and -P 115 */ 116 assertMakeDir("dest2", 0755); 117 /* "dir" is a symlink to existing "real_dir" */ 118 assertMakeDir("dest2/real_dir", 0755); 119 if (canSymlink()) 120 assertMakeSymlink("dest2/dir", "real_dir"); 121 /* "dir2" is a symlink to a non-existing "real_dir2" */ 122 if (canSymlink()) 123 assertMakeSymlink("dest2/dir2", "real_dir2"); 124 /* "dir3" is a symlink to an existing "non_dir3" */ 125 assertEqualInt(0, mkfile("dest2/non_dir3", 0755, "abcdefgh", 8)); 126 if (canSymlink()) 127 assertMakeSymlink("dest2/dir3", "non_dir3"); 128 /* "file" is a symlink to existing "real_file" */ 129 assertEqualInt(0, mkfile("dest2/real_file", 0755, "abcdefghi", 9)); 130 if (canSymlink()) 131 assertMakeSymlink("dest2/file", "real_file"); 132 /* "file2" is a symlink to non-existing "real_file2" */ 133 if (canSymlink()) 134 assertMakeSymlink("dest2/file2", "real_file2"); 135 assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog)); 136 137 /* dest2/dir symlink should be followed */ 138 if (canSymlink()) { 139 assertIsSymlink("dest2/dir", "real_dir"); 140 assertIsDir("dest2/real_dir", -1); 141 } 142 143 /* Contents of 'dir' should be restored */ 144 assertIsDir("dest2/dir/d", -1); 145 assertIsReg("dest2/dir/f", -1); 146 assertFileSize("dest2/dir/f", 3); 147 /* dest2/dir2 symlink should be removed */ 148 failure("Broken symlink wasn't replaced with dir"); 149 assertIsDir("dest2/dir2", -1); 150 /* dest2/dir3 symlink should be removed */ 151 failure("Symlink to non-dir wasn't replaced with dir"); 152 assertIsDir("dest2/dir3", -1); 153 /* dest2/file symlink should be removed; 154 * even -P shouldn't follow symlinks for files */ 155 failure("Symlink to existing file should be removed"); 156 assertIsReg("dest2/file", -1); 157 /* dest2/file2 symlink should be removed */ 158 failure("Symlink to non-existing file should be removed"); 159 assertIsReg("dest2/file2", -1); 160} 161