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