test_option_U_upper.c revision 348608
1141842Sru/*-
2141842Sru * Copyright (c) 2010 Tim Kientzle
3141842Sru * All rights reserved.
4141842Sru *
5141842Sru * Redistribution and use in source and binary forms, with or without
6141842Sru * modification, are permitted provided that the following conditions
77368Sjoerg * are met:
87368Sjoerg * 1. Redistributions of source code must retain the above copyright
97368Sjoerg *    notice, this list of conditions and the following disclaimer.
107368Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1193151Sphk *    notice, this list of conditions and the following disclaimer in the
127368Sjoerg *    documentation and/or other materials provided with the distribution.
1350479Speter *
147368Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
157368Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16206622Suqs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
177368Sjoerg * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
187368Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
197368Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
207368Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
217368Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
227368Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2329526Scharnier * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247395Sphk */
2570403Sru#include "test.h"
26131500Sru__FBSDID("$FreeBSD$");
27131500Sru
287368SjoergDEFINE_TEST(test_option_U_upper)
297368Sjoerg{
307395Sphk	int r;
317395Sphk
3270403Sru	assertMakeFile("file1", 0644, "file1");
3336852Sphk	assertMakeDir("d1", 0755);
347395Sphk	assertMakeFile("d1/file1", 0644, "d1/file1");
357395Sphk	assertEqualInt(0, systemf("%s -cf archive.tar file1 d1/file1", testprog));
367395Sphk
37141846Sru	/*
3870403Sru	 * bsdtar's man page used to claim that -x without -U would
397368Sjoerg	 * not break hard links.  This was and is nonsense.  The first
407368Sjoerg	 * two tests here simply verify that existing hard links get
417368Sjoerg	 * broken regardless.
42131500Sru	 */
43131500Sru
447368Sjoerg	/* Test 1: -x without -U */
457368Sjoerg	assertMakeDir("test1", 0755);
4681251Sru	assertChdir("test1");
477368Sjoerg	assertMakeFile("file1", 0644, "file1new");
487368Sjoerg	assertMakeHardlink("file2", "file1");
497368Sjoerg	assertEqualInt(0,
507368Sjoerg	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
517368Sjoerg	assertFileContents("file1", 5, "file1");
527368Sjoerg	assertFileContents("file1new", 8, "file2");
537368Sjoerg	assertEmptyFile("test.out");
547368Sjoerg	assertEmptyFile("test.err");
557368Sjoerg	assertChdir("..");
5681251Sru
577368Sjoerg
5881251Sru	/* Test 2: -x with -U */
597368Sjoerg	assertMakeDir("test2", 0755);
607368Sjoerg	assertChdir("test2");
6129526Scharnier	assertMakeFile("file1", 0644, "file1new");
627368Sjoerg	assertMakeHardlink("file2", "file1");
637368Sjoerg	assertEqualInt(0,
647368Sjoerg	    systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
6579755Sdd	assertFileContents("file1", 5, "file1");
667395Sphk	assertFileContents("file1new", 8, "file2");
677368Sjoerg	assertEmptyFile("test.out");
687368Sjoerg	assertEmptyFile("test.err");
697368Sjoerg	assertChdir("..");
707368Sjoerg
717368Sjoerg	/*
7281251Sru	 * -U does make a difference in how bsdtar handles unwanted symlinks,
737368Sjoerg	 * though.  It interacts with -P.
747368Sjoerg	 */
7581251Sru	if (!canSymlink())
767368Sjoerg		return;
777368Sjoerg
7881251Sru	/* Test 3: Intermediate dir symlink causes error by default */
797368Sjoerg	assertMakeDir("test3", 0755);
807368Sjoerg	assertChdir("test3");
8170403Sru	assertMakeDir("realDir", 0755);
827368Sjoerg	assertMakeSymlink("d1", "realDir", 1);
837368Sjoerg	r = systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog);
847368Sjoerg	assert(r != 0);
857368Sjoerg	assertIsSymlink("d1", "realDir", 1);
867368Sjoerg	assertFileNotExists("d1/file1");
877368Sjoerg	assertEmptyFile("test.out");
887368Sjoerg	assertNonEmptyFile("test.err");
897368Sjoerg	assertChdir("..");
9081251Sru
917368Sjoerg	/* Test 4: Intermediate dir symlink gets removed with -U */
927368Sjoerg	assertMakeDir("test4", 0755);
9381251Sru	assertChdir("test4");
947368Sjoerg	assertMakeDir("realDir", 0755);
957368Sjoerg	assertMakeSymlink("d1", "realDir", 1);
9681251Sru	assertEqualInt(0,
977368Sjoerg	    systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
987368Sjoerg	assertIsDir("d1", -1);
997368Sjoerg	assertFileContents("d1/file1", 8, "d1/file1");
1007368Sjoerg	assertEmptyFile("test.out");
10170403Sru	assertEmptyFile("test.err");
1027368Sjoerg	assertChdir("..");
1037368Sjoerg
1047368Sjoerg	/* Test 5: Intermediate dir symlink is followed with -P */
10570403Sru	assertMakeDir("test5", 0755);
1067368Sjoerg	assertChdir("test5");
1077368Sjoerg	assertMakeDir("realDir", 0755);
1087368Sjoerg	assertMakeSymlink("d1", "realDir", 1);
1097368Sjoerg	assertEqualInt(0,
1107368Sjoerg	    systemf("%s -xPf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
1117368Sjoerg	assertIsSymlink("d1", "realDir", 1);
1127395Sphk	assertFileContents("d1/file1", 8, "d1/file1");
1137395Sphk	assertEmptyFile("test.out");
1147368Sjoerg	assertEmptyFile("test.err");
1157368Sjoerg	assertChdir("..");
1167368Sjoerg
1177368Sjoerg	/* Test 6: Intermediate dir symlink is followed with -PU */
1187368Sjoerg	assertMakeDir("test6", 0755);
1197368Sjoerg	assertChdir("test6");
1207368Sjoerg	assertMakeDir("realDir", 0755);
1217368Sjoerg	assertMakeSymlink("d1", "realDir", 1);
1227368Sjoerg	assertEqualInt(0,
1237368Sjoerg	    systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
1247368Sjoerg	assertIsSymlink("d1", "realDir", 1);
1257368Sjoerg	assertFileContents("d1/file1", 8, "d1/file1");
1267368Sjoerg	assertEmptyFile("test.out");
1277368Sjoerg	assertEmptyFile("test.err");
1287368Sjoerg	assertChdir("..");
1297368Sjoerg
1307368Sjoerg	/* Test 7: Final file symlink replaced by default */
1317368Sjoerg	assertMakeDir("test7", 0755);
1327368Sjoerg	assertChdir("test7");
1337368Sjoerg	assertMakeDir("d1", 0755);
1347368Sjoerg	assertMakeFile("d1/realfile1", 0644, "realfile1");
1357368Sjoerg	assertMakeSymlink("d1/file1", "d1/realfile1", 0);
1367368Sjoerg	assertEqualInt(0,
13757793Snik	    systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
1387368Sjoerg	assertIsReg("d1/file1", umasked(0644));
1397368Sjoerg	assertFileContents("d1/file1", 8, "d1/file1");
1407368Sjoerg	assertFileContents("realfile1", 9, "d1/realfile1");
1417368Sjoerg	assertEmptyFile("test.out");
1427368Sjoerg	assertEmptyFile("test.err");
1437368Sjoerg	assertChdir("..");
1447368Sjoerg
14581251Sru	/* Test 8: Final file symlink replaced with -PU */
1467368Sjoerg	assertMakeDir("test8", 0755);
1477368Sjoerg	assertChdir("test8");
1487368Sjoerg	assertMakeDir("d1", 0755);
1497368Sjoerg	assertMakeFile("d1/realfile1", 0644, "realfile1");
1507368Sjoerg	assertMakeSymlink("d1/file1", "d1/realfile1", 0);
1517368Sjoerg	assertEqualInt(0,
1527368Sjoerg	    systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
1537368Sjoerg	assertIsReg("d1/file1", umasked(0644));
1547368Sjoerg	assertFileContents("d1/file1", 8, "d1/file1");
1557368Sjoerg	assertFileContents("realfile1", 9, "d1/realfile1");
1567368Sjoerg	assertEmptyFile("test.out");
1577368Sjoerg	assertEmptyFile("test.err");
1587368Sjoerg	assertChdir("..");
1597368Sjoerg}
1607368Sjoerg