xref: /freebsd/contrib/libarchive/tar/test/test_option_safe_writes.c (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Martin Matuska
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 DEFINE_TEST(test_option_safe_writes)
10 {
11 	/* Create files */
12 	assertMakeDir("in", 0755);
13 	assertEqualInt(0, chdir("in"));
14 	assertMakeFile("f", 0644, "a");
15 	assertMakeFile("fh", 0644, "b");
16 	assertMakeFile("d", 0644, "c");
17 	assertMakeFile("fs", 0644, "d");
18 	assertMakeFile("ds", 0644, "e");
19 	assertMakeDir("fd", 0755);
20 	assertEqualInt(0, chdir(".."));
21 
22 	/* Tar files up */
23 	assertEqualInt(0,
24 	    systemf("%s -c -C in -f t.tar f fh d fs ds fd "
25 	    ">pack.out 2>pack.err", testprog));
26 
27         /* Verify that nothing went to stdout or stderr. */
28         assertEmptyFile("pack.err");
29         assertEmptyFile("pack.out");
30 
31 	/* Create various objects */
32 	assertMakeDir("out", 0755);
33 	assertEqualInt(0, chdir("out"));
34 	assertMakeFile("f", 0644, "a");
35 	assertMakeHardlink("fh", "f");
36 	assertMakeFile("fd", 0644, "b");
37 	assertMakeDir("d", 0755);
38 	if (canSymlink()) {
39 		assertMakeSymlink("fs", "f", 0);
40 		assertMakeSymlink("ds", "d", 1);
41 	}
42 	assertEqualInt(0, chdir(".."));
43 
44 	/* Extract created archive with safe writes */
45 	assertEqualInt(0,
46 	    systemf("%s -x -C out --safe-writes -f t.tar "
47 	    ">unpack.out 2>unpack.err", testprog));
48 
49         /* Verify that nothing went to stdout or stderr. */
50         assertEmptyFile("unpack.err");
51         assertEmptyFile("unpack.out");
52 
53 	/* Verify that files were overwritten properly */
54 	assertEqualInt(0, chdir("out"));
55 	assertTextFileContents("a","f");
56 	assertTextFileContents("b","fh");
57 	assertTextFileContents("c","d");
58 	assertTextFileContents("d","fs");
59 	assertTextFileContents("e","ds");
60 	assertIsDir("fd", 0755);
61 }
62