Home | History | Annotate | Line # | Download | only in test
      1      1.1     joerg /*-
      2  1.1.1.5  christos  * SPDX-License-Identifier: BSD-2-Clause
      3  1.1.1.5  christos  *
      4      1.1     joerg  * Copyright (c) 2003-2007 Tim Kientzle
      5      1.1     joerg  * All rights reserved.
      6      1.1     joerg  */
      7      1.1     joerg #include "test.h"
      8      1.1     joerg 
      9      1.1     joerg DEFINE_TEST(test_strip_components)
     10      1.1     joerg {
     11      1.1     joerg 	assertMakeDir("d0", 0755);
     12      1.1     joerg 	assertChdir("d0");
     13      1.1     joerg 	assertMakeDir("d1", 0755);
     14      1.1     joerg 	assertMakeDir("d1/d2", 0755);
     15      1.1     joerg 	assertMakeDir("d1/d2/d3", 0755);
     16  1.1.1.2     joerg 	assertMakeFile("d1/d2/f1", 0644, "");
     17      1.1     joerg 	assertMakeHardlink("l1", "d1/d2/f1");
     18      1.1     joerg 	assertMakeHardlink("d1/l2", "d1/d2/f1");
     19      1.1     joerg 	if (canSymlink()) {
     20  1.1.1.3     joerg 		assertMakeSymlink("s1", "d1/d2/f1", 0);
     21  1.1.1.3     joerg 		assertMakeSymlink("d1/s2", "d2/f1", 0);
     22      1.1     joerg 	}
     23      1.1     joerg 	assertChdir("..");
     24      1.1     joerg 
     25  1.1.1.2     joerg 	/*
     26  1.1.1.2     joerg 	 * Test 1: Strip components when extracting archives.
     27  1.1.1.2     joerg 	 */
     28  1.1.1.2     joerg 	if (canSymlink())
     29  1.1.1.2     joerg 		assertEqualInt(0, systemf("%s -cf test.tar d0/l1 d0/s1 d0/d1",
     30  1.1.1.2     joerg 		    testprog));
     31  1.1.1.2     joerg 	else
     32  1.1.1.2     joerg 		assertEqualInt(0, systemf("%s -cf test.tar d0/l1 d0/d1",
     33  1.1.1.2     joerg 		    testprog));
     34      1.1     joerg 
     35      1.1     joerg 	assertMakeDir("target", 0755);
     36      1.1     joerg 	assertEqualInt(0, systemf("%s -x -C target --strip-components 2 "
     37      1.1     joerg 	    "-f test.tar", testprog));
     38      1.1     joerg 
     39      1.1     joerg 	failure("d0/ is too short and should not get restored");
     40      1.1     joerg 	assertFileNotExists("target/d0");
     41      1.1     joerg 	failure("d0/d1/ is too short and should not get restored");
     42      1.1     joerg 	assertFileNotExists("target/d1");
     43  1.1.1.2     joerg 	failure("d0/s1 is too short and should not get restored");
     44  1.1.1.2     joerg 	assertFileNotExists("target/s1");
     45      1.1     joerg 	failure("d0/d1/s2 is a symlink to something that won't be extracted");
     46      1.1     joerg 	/* If platform supports symlinks, target/s2 is a broken symlink. */
     47      1.1     joerg 	/* If platform does not support symlink, target/s2 doesn't exist. */
     48      1.1     joerg 	if (canSymlink())
     49  1.1.1.3     joerg 		assertIsSymlink("target/s2", "d2/f1", 0);
     50  1.1.1.3     joerg 	else
     51  1.1.1.3     joerg 		assertFileNotExists("target/s2");
     52      1.1     joerg 	failure("d0/d1/d2 should be extracted");
     53      1.1     joerg 	assertIsDir("target/d2", -1);
     54      1.1     joerg 
     55      1.1     joerg 	/*
     56  1.1.1.2     joerg 	 * Test 1b: Strip components extracting archives involving hardlinks.
     57  1.1.1.2     joerg 	 *
     58      1.1     joerg 	 * This next is a complicated case.  d0/l1, d0/d1/l2, and
     59      1.1     joerg 	 * d0/d1/d2/f1 are all hardlinks to the same file; d0/l1 can't
     60      1.1     joerg 	 * be extracted with --strip-components=2 and the other two
     61      1.1     joerg 	 * can.  Remember that tar normally stores the first file with
     62      1.1     joerg 	 * a body and the other as hardlink entries to the first
     63      1.1     joerg 	 * appearance.  So the final result depends on the order in
     64      1.1     joerg 	 * which these three names get archived.  If d0/l1 is first,
     65      1.1     joerg 	 * none of the three can be restored.  If either of the longer
     66      1.1     joerg 	 * names are first, then the two longer ones can both be
     67  1.1.1.2     joerg 	 * restored.  Note that the "tar -cf" command above explicitly
     68  1.1.1.2     joerg 	 * lists d0/l1 before d0/d1.
     69      1.1     joerg 	 *
     70  1.1.1.2     joerg 	 * It may be worth extending this test to exercise other
     71  1.1.1.2     joerg 	 * archiving orders.
     72      1.1     joerg 	 *
     73      1.1     joerg 	 * Of course, this is all totally different for cpio and newc
     74      1.1     joerg 	 * formats because the hardlink management is different.
     75      1.1     joerg 	 * TODO: Rename this to test_strip_components_tar and create
     76      1.1     joerg 	 * parallel tests for cpio and newc formats.
     77      1.1     joerg 	 */
     78      1.1     joerg 	failure("d0/l1 is too short and should not get restored");
     79      1.1     joerg 	assertFileNotExists("target/l1");
     80      1.1     joerg 	failure("d0/d1/l2 is a hardlink to file whose name was too short");
     81      1.1     joerg 	assertFileNotExists("target/l2");
     82      1.1     joerg 	failure("d0/d1/d2/f1 is a hardlink to file whose name was too short");
     83      1.1     joerg 	assertFileNotExists("target/d2/f1");
     84  1.1.1.2     joerg 
     85  1.1.1.2     joerg 	/*
     86  1.1.1.2     joerg 	 * Test 2: Strip components when creating archives.
     87  1.1.1.2     joerg 	 */
     88  1.1.1.2     joerg 	if (canSymlink())
     89  1.1.1.2     joerg 		assertEqualInt(0, systemf("%s --strip-components 2 -cf test2.tar "
     90  1.1.1.2     joerg 					"d0/l1 d0/s1 d0/d1", testprog));
     91  1.1.1.2     joerg 	else
     92  1.1.1.2     joerg 		assertEqualInt(0, systemf("%s --strip-components 2 -cf test2.tar "
     93  1.1.1.2     joerg 					"d0/l1 d0/d1", testprog));
     94  1.1.1.2     joerg 
     95  1.1.1.2     joerg 	assertMakeDir("target2", 0755);
     96  1.1.1.2     joerg 	assertEqualInt(0, systemf("%s -x -C target2 -f test2.tar", testprog));
     97  1.1.1.2     joerg 
     98  1.1.1.2     joerg 	failure("d0/ is too short and should not have been archived");
     99  1.1.1.2     joerg 	assertFileNotExists("target2/d0");
    100  1.1.1.2     joerg 	failure("d0/d1/ is too short and should not have been archived");
    101  1.1.1.2     joerg 	assertFileNotExists("target2/d1");
    102  1.1.1.2     joerg 	failure("d0/s1 is too short and should not get restored");
    103  1.1.1.2     joerg 	assertFileNotExists("target/s1");
    104  1.1.1.2     joerg 	/* If platform supports symlinks, target/s2 is included. */
    105  1.1.1.2     joerg 	if (canSymlink()) {
    106  1.1.1.2     joerg 		failure("d0/d1/s2 is a symlink to something included in archive");
    107  1.1.1.3     joerg 		assertIsSymlink("target2/s2", "d2/f1", 0);
    108  1.1.1.2     joerg 	}
    109  1.1.1.2     joerg 	failure("d0/d1/d2 should be archived");
    110  1.1.1.2     joerg 	assertIsDir("target2/d2", -1);
    111  1.1.1.2     joerg 
    112  1.1.1.2     joerg 	/*
    113  1.1.1.2     joerg 	 * Test 2b: Strip components creating archives involving hardlinks.
    114  1.1.1.2     joerg 	 */
    115  1.1.1.2     joerg 	failure("d0/l1 is too short and should not have been archived");
    116  1.1.1.2     joerg 	assertFileNotExists("target/l1");
    117  1.1.1.2     joerg 	failure("d0/d1/l2 is a hardlink to file whose name was too short");
    118  1.1.1.2     joerg 	assertFileNotExists("target/l2");
    119  1.1.1.2     joerg 	failure("d0/d1/d2/f1 is a hardlink to file whose name was too short");
    120  1.1.1.2     joerg 	assertFileNotExists("target/d2/f1");
    121      1.1     joerg }
    122