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