test_symlink_dir.c revision 228763
1204739Sjoel/*-
2192551Sthompsa * Copyright (c) 2003-2007 Tim Kientzle
3192551Sthompsa * All rights reserved.
4192551Sthompsa *
5192551Sthompsa * Redistribution and use in source and binary forms, with or without
6192551Sthompsa * modification, are permitted provided that the following conditions
7192551Sthompsa * are met:
8192551Sthompsa * 1. Redistributions of source code must retain the above copyright
9192551Sthompsa *    notice, this list of conditions and the following disclaimer.
10192551Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
11192551Sthompsa *    notice, this list of conditions and the following disclaimer in the
12192551Sthompsa *    documentation and/or other materials provided with the distribution.
13192551Sthompsa *
14192551Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15192551Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16192551Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17192551Sthompsa * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18192551Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19192551Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20192551Sthompsa * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21192551Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22192551Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23192551Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24192551Sthompsa */
2550476Speter#include "test.h"
2644192Sn_hibma__FBSDID("$FreeBSD: head/contrib/libarchive/tar/test/test_symlink_dir.c 228763 2011-12-21 11:13:29Z mm $");
27365552Slwhsu
2853200Sphantom/*
2979538Sru * tar -x -P should follow existing symlinks for dirs, but not other
3044192Sn_hibma * content.  Plain tar -x should remove symlinks when they're in the
3144192Sn_hibma * way of a dir extraction.
3244192Sn_hibma */
3344192Sn_hibma
34164524Sbruefferstatic int
35164524Sbrueffermkfile(const char *name, int mode, const char *contents, size_t size)
36164524Sbrueffer{
37164524Sbrueffer	FILE *f = fopen(name, "wb");
3856467Sasmodai	size_t written;
39164524Sbrueffer
4089891Sjoe	(void)mode; /* UNUSED */
41164524Sbrueffer	if (f == NULL)
42164524Sbrueffer		return (-1);
43164524Sbrueffer	written = fwrite(contents, 1, size, f);
44164524Sbrueffer	fclose(f);
45164524Sbrueffer	if (size != written)
46164524Sbrueffer		return (-1);
47192551Sthompsa	return (0);
48192551Sthompsa}
49192551Sthompsa
50192551SthompsaDEFINE_TEST(test_symlink_dir)
51192551Sthompsa{
5244192Sn_hibma	assertUmask(0);
5350845Schris
5444192Sn_hibma	assertMakeDir("source", 0755);
55365552Slwhsu	assertEqualInt(0, mkfile("source/file", 0755, "a", 1));
5644192Sn_hibma	assertEqualInt(0, mkfile("source/file2", 0755, "ab", 2));
5744192Sn_hibma	assertMakeDir("source/dir", 0755);
5844192Sn_hibma	assertMakeDir("source/dir/d", 0755);
59192551Sthompsa	assertEqualInt(0, mkfile("source/dir/f", 0755, "abc", 3));
60236443Sjoel	assertMakeDir("source/dir2", 0755);
61192551Sthompsa	assertMakeDir("source/dir2/d2", 0755);
62192551Sthompsa	assertEqualInt(0, mkfile("source/dir2/f2", 0755, "abcd", 4));
63192551Sthompsa	assertMakeDir("source/dir3", 0755);
64192551Sthompsa	assertMakeDir("source/dir3/d3", 0755);
65192551Sthompsa	assertEqualInt(0, mkfile("source/dir3/f3", 0755, "abcde", 5));
6657676Ssheldonh
67192551Sthompsa	assertEqualInt(0,
68192551Sthompsa	    systemf("%s -cf test.tar -C source dir dir2 dir3 file file2",
69365552Slwhsu		testprog));
7044192Sn_hibma
7144192Sn_hibma	/*
72365552Slwhsu	 * Extract with -x and without -P.
7344192Sn_hibma	 */
7444192Sn_hibma	assertMakeDir("dest1", 0755);
7544192Sn_hibma	/* "dir" is a symlink to an existing "dest1/real_dir" */
76318624Sngie	assertMakeDir("dest1/real_dir", 0755);
7744192Sn_hibma	if (canSymlink()) {
78365552Slwhsu		assertMakeSymlink("dest1/dir", "real_dir");
79192551Sthompsa		/* "dir2" is a symlink to a non-existing "real_dir2" */
80236443Sjoel		assertMakeSymlink("dest1/dir2", "real_dir2");
81365552Slwhsu	} else {
82365552Slwhsu		skipping("some symlink checks");
83365552Slwhsu	}
84365552Slwhsu	/* "dir3" is a symlink to an existing "non_dir3" */
85192551Sthompsa	assertEqualInt(0, mkfile("dest1/non_dir3", 0755, "abcdef", 6));
86192551Sthompsa	if (canSymlink())
87365552Slwhsu		assertMakeSymlink("dest1/dir3", "non_dir3");
88318624Sngie	/* "file" is a symlink to existing "real_file" */
89318624Sngie	assertEqualInt(0, mkfile("dest1/real_file", 0755, "abcdefg", 7));
9044192Sn_hibma	if (canSymlink()) {
91192551Sthompsa		assertMakeSymlink("dest1/file", "real_file");
92318624Sngie		/* "file2" is a symlink to non-existing "real_file2" */
93318624Sngie		assertMakeSymlink("dest1/file2", "real_file2");
9444192Sn_hibma	}
9544192Sn_hibma	assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog));
96318624Sngie
9744192Sn_hibma	/* dest1/dir symlink should be replaced */
9892585Sru	failure("symlink to dir was followed when it shouldn't be");
9992585Sru	assertIsDir("dest1/dir", -1);
10092585Sru	/* dest1/dir2 symlink should be replaced */
10144192Sn_hibma	failure("Broken symlink wasn't replaced with dir");
102318624Sngie	assertIsDir("dest1/dir2", -1);
103318624Sngie	/* dest1/dir3 symlink should be replaced */
10444192Sn_hibma	failure("Symlink to non-dir wasn't replaced with dir");
10544192Sn_hibma	assertIsDir("dest1/dir3", -1);
10644192Sn_hibma	/* dest1/file symlink should be replaced */
107318624Sngie	failure("Symlink to existing file should be replaced");
108318624Sngie	assertIsReg("dest1/file", -1);
109318624Sngie	/* dest1/file2 symlink should be replaced */
110318624Sngie	failure("Symlink to non-existing file should be replaced");
111318624Sngie	assertIsReg("dest1/file2", -1);
11244192Sn_hibma
11344192Sn_hibma	/*
114318624Sngie	 * Extract with both -x and -P
115318624Sngie	 */
11644192Sn_hibma	assertMakeDir("dest2", 0755);
117365552Slwhsu	/* "dir" is a symlink to existing "real_dir" */
11844192Sn_hibma	assertMakeDir("dest2/real_dir", 0755);
11944192Sn_hibma	if (canSymlink())
120192551Sthompsa		assertMakeSymlink("dest2/dir", "real_dir");
12144192Sn_hibma	/* "dir2" is a symlink to a non-existing "real_dir2" */
122192551Sthompsa	if (canSymlink())
12344192Sn_hibma		assertMakeSymlink("dest2/dir2", "real_dir2");
12444192Sn_hibma	/* "dir3" is a symlink to an existing "non_dir3" */
125365552Slwhsu	assertEqualInt(0, mkfile("dest2/non_dir3", 0755, "abcdefgh", 8));
12644192Sn_hibma	if (canSymlink())
127365552Slwhsu		assertMakeSymlink("dest2/dir3", "non_dir3");
12892585Sru	/* "file" is a symlink to existing "real_file" */
129192551Sthompsa	assertEqualInt(0, mkfile("dest2/real_file", 0755, "abcdefghi", 9));
13089891Sjoe	if (canSymlink())
131171566Simp		assertMakeSymlink("dest2/file", "real_file");
132258618Slwhsu	/* "file2" is a symlink to non-existing "real_file2" */
13389891Sjoe	if (canSymlink())
134145317Ssimon		assertMakeSymlink("dest2/file2", "real_file2");
13589891Sjoe	assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog));
136232258Skevlo
13779366Sru	/* dest2/dir symlink should be followed */
13844192Sn_hibma	if (canSymlink()) {
139171566Simp		assertIsSymlink("dest2/dir", "real_dir");
140107688Sjoe		assertIsDir("dest2/real_dir", -1);
141171566Simp	}
14244192Sn_hibma
14344192Sn_hibma	/* Contents of 'dir' should be restored */
14444192Sn_hibma	assertIsDir("dest2/dir/d", -1);
145108099Strhodes	assertIsReg("dest2/dir/f", -1);
14689891Sjoe	assertFileSize("dest2/dir/f", 3);
14744192Sn_hibma	/* dest2/dir2 symlink should be removed */
148110954Strhodes	failure("Broken symlink wasn't replaced with dir");
14989891Sjoe	assertIsDir("dest2/dir2", -1);
150110954Strhodes	/* dest2/dir3 symlink should be removed */
151318624Sngie	failure("Symlink to non-dir wasn't replaced with dir");
152219004Shselasky	assertIsDir("dest2/dir3", -1);
153318624Sngie	/* dest2/file symlink should be removed;
154192551Sthompsa	 * even -P shouldn't follow symlinks for files */
15544192Sn_hibma	failure("Symlink to existing file should be removed");
15644192Sn_hibma	assertIsReg("dest2/file", -1);
157365552Slwhsu	/* dest2/file2 symlink should be removed */
158192551Sthompsa	failure("Symlink to non-existing file should be removed");
15944192Sn_hibma	assertIsReg("dest2/file2", -1);
16044192Sn_hibma}
161267938Sbapt