test_option_U_upper.c revision 307138
1157841Sflz/*-
298186Sgordon * Copyright (c) 2010 Tim Kientzle
378344Sobrien * All rights reserved.
4157473Sflz *
578344Sobrien * Redistribution and use in source and binary forms, with or without
678344Sobrien * modification, are permitted provided that the following conditions
778344Sobrien * are met:
878344Sobrien * 1. Redistributions of source code must retain the above copyright
978344Sobrien *    notice, this list of conditions and the following disclaimer.
1078344Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1178344Sobrien *    notice, this list of conditions and the following disclaimer in the
1278344Sobrien *    documentation and/or other materials provided with the distribution.
1378344Sobrien *
1478344Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1578344Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1678344Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1778344Sobrien * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1878344Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1978344Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2078344Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2178344Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2278344Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2378344Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2478344Sobrien */
2578344Sobrien#include "test.h"
2678344Sobrien__FBSDID("$FreeBSD$");
2778344Sobrien
2878344SobrienDEFINE_TEST(test_option_U_upper)
2978344Sobrien{
3078344Sobrien	int r;
3178344Sobrien
3278344Sobrien	assertMakeFile("file1", 0644, "file1");
3378344Sobrien	assertMakeDir("d1", 0755);
3478344Sobrien	assertMakeFile("d1/file1", 0644, "d1/file1");
3578344Sobrien	assertEqualInt(0, systemf("%s -cf archive.tar file1 d1/file1", testprog));
3678344Sobrien
3778344Sobrien	/*
3878344Sobrien	 * bsdtar's man page used to claim that -x without -U would
3978344Sobrien	 * not break hard links.  This was and is nonsense.  The first
4078344Sobrien	 * two tests here simply verify that existing hard links get
4178344Sobrien	 * broken regardless.
42157473Sflz	 */
43157473Sflz
4478344Sobrien	/* Test 1: -x without -U */
4598186Sgordon	assertMakeDir("test1", 0755);
4698186Sgordon	assertChdir("test1");
4798186Sgordon	assertMakeFile("file1", 0644, "file1new");
48131550Scperciva	assertMakeHardlink("file2", "file1");
49131550Scperciva	assertEqualInt(0,
50131550Scperciva	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
51131550Scperciva	assertFileContents("file1", 5, "file1");
5298186Sgordon	assertFileContents("file1new", 8, "file2");
5398186Sgordon	assertEmptyFile("test.out");
5498186Sgordon	assertEmptyFile("test.err");
55103018Sgordon	assertChdir("..");
56124832Smtm
57157710Sflz
58124832Smtm	/* Test 2: -x with -U */
5998186Sgordon	assertMakeDir("test2", 0755);
60103018Sgordon	assertChdir("test2");
6198186Sgordon	assertMakeFile("file1", 0644, "file1new");
6298186Sgordon	assertMakeHardlink("file2", "file1");
6398186Sgordon	assertEqualInt(0,
6498186Sgordon	    systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
6598186Sgordon	assertFileContents("file1", 5, "file1");
6698186Sgordon	assertFileContents("file1new", 8, "file2");
6798186Sgordon	assertEmptyFile("test.out");
6898186Sgordon	assertEmptyFile("test.err");
6998186Sgordon	assertChdir("..");
7078344Sobrien
7178344Sobrien	/*
7278344Sobrien	 * -U does make a difference in how bsdtar handles unwanted symlinks,
7378344Sobrien	 * though.  It interacts with -P.
7498186Sgordon	 */
7598186Sgordon	if (!canSymlink())
7698186Sgordon		return;
7798186Sgordon
7898186Sgordon	/* Test 3: Intermediate dir symlink causes error by default */
7998186Sgordon	assertMakeDir("test3", 0755);
8098186Sgordon	assertChdir("test3");
8198186Sgordon	assertMakeDir("realDir", 0755);
8298186Sgordon	assertMakeSymlink("d1", "realDir");
8398186Sgordon	r = systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog);
8498186Sgordon	assert(r != 0);
8598186Sgordon	assertIsSymlink("d1", "realDir");
8698186Sgordon	assertFileNotExists("d1/file1");
8798186Sgordon	assertEmptyFile("test.out");
8898186Sgordon	assertNonEmptyFile("test.err");
8998186Sgordon	assertChdir("..");
9098186Sgordon
91103018Sgordon	/* Test 4: Intermediate dir symlink gets removed with -U */
9298186Sgordon	assertMakeDir("test4", 0755);
9398186Sgordon	assertChdir("test4");
9498186Sgordon	assertMakeDir("realDir", 0755);
9598186Sgordon	assertMakeSymlink("d1", "realDir");
9698186Sgordon	assertEqualInt(0,
9798186Sgordon	    systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
9898186Sgordon	assertIsDir("d1", -1);
9998186Sgordon	assertFileContents("d1/file1", 8, "d1/file1");
10098186Sgordon	assertEmptyFile("test.out");
10198186Sgordon	assertEmptyFile("test.err");
10298186Sgordon	assertChdir("..");
10398186Sgordon
10498186Sgordon	/* Test 5: Intermediate dir symlink is followed with -P */
10598186Sgordon	assertMakeDir("test5", 0755);
10698186Sgordon	assertChdir("test5");
10798186Sgordon	assertMakeDir("realDir", 0755);
10898186Sgordon	assertMakeSymlink("d1", "realDir");
10998186Sgordon	assertEqualInt(0,
11098186Sgordon	    systemf("%s -xPf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
11198186Sgordon	assertIsSymlink("d1", "realDir");
11298186Sgordon	assertFileContents("d1/file1", 8, "d1/file1");
11398186Sgordon	assertEmptyFile("test.out");
11498186Sgordon	assertEmptyFile("test.err");
11598186Sgordon	assertChdir("..");
116146490Sschweikh
11798186Sgordon	/* Test 6: Intermediate dir symlink is followed with -PU */
11898186Sgordon	assertMakeDir("test6", 0755);
11998186Sgordon	assertChdir("test6");
12098186Sgordon	assertMakeDir("realDir", 0755);
12198186Sgordon	assertMakeSymlink("d1", "realDir");
12298186Sgordon	assertEqualInt(0,
12398186Sgordon	    systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
12478344Sobrien	assertIsSymlink("d1", "realDir");
12578344Sobrien	assertFileContents("d1/file1", 8, "d1/file1");
12678344Sobrien	assertEmptyFile("test.out");
12778344Sobrien	assertEmptyFile("test.err");
12878344Sobrien	assertChdir("..");
12978344Sobrien
13078344Sobrien	/* Test 7: Final file symlink replaced by default */
13198186Sgordon	assertMakeDir("test7", 0755);
13278344Sobrien	assertChdir("test7");
13378344Sobrien	assertMakeDir("d1", 0755);
13478344Sobrien	assertMakeFile("d1/realfile1", 0644, "realfile1");
13578344Sobrien	assertMakeSymlink("d1/file1", "d1/realfile1");
13678344Sobrien	assertEqualInt(0,
13778344Sobrien	    systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
13878344Sobrien	assertIsReg("d1/file1", umasked(0644));
13978344Sobrien	assertFileContents("d1/file1", 8, "d1/file1");
14078344Sobrien	assertFileContents("realfile1", 9, "d1/realfile1");
14178344Sobrien	assertEmptyFile("test.out");
14278344Sobrien	assertEmptyFile("test.err");
14378344Sobrien	assertChdir("..");
144157473Sflz
14578344Sobrien	/* Test 8: Final file symlink replaced with -PU */
14678344Sobrien	assertMakeDir("test8", 0755);
14778344Sobrien	assertChdir("test8");
14878344Sobrien	assertMakeDir("d1", 0755);
14978344Sobrien	assertMakeFile("d1/realfile1", 0644, "realfile1");
150157473Sflz	assertMakeSymlink("d1/file1", "d1/realfile1");
15198186Sgordon	assertEqualInt(0,
15298186Sgordon	    systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
15378344Sobrien	assertIsReg("d1/file1", umasked(0644));
15498186Sgordon	assertFileContents("d1/file1", 8, "d1/file1");
15598186Sgordon	assertFileContents("realfile1", 9, "d1/realfile1");
15698186Sgordon	assertEmptyFile("test.out");
157126286Smtm	assertEmptyFile("test.err");
15898186Sgordon	assertChdir("..");
15998186Sgordon}
16098186Sgordon