xref: /freebsd/contrib/libarchive/tar/test/test_option_n.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 #ifdef HAVE_SYS_WAIT_H
10 #include <sys/wait.h>
11 #endif
12 
DEFINE_TEST(test_option_n)13 DEFINE_TEST(test_option_n)
14 {
15 	int status;
16 
17 	assertMakeDir("d1", 0755);
18 	assertMakeFile("d1/file1", 0644, "d1/file1");
19 
20 	/* Test 1: -c without -n */
21 	assertMakeDir("test1", 0755);
22 	assertChdir("test1");
23 	assertEqualInt(0,
24 	    systemf("%s -cf archive.tar -C .. d1 >c.out 2>c.err", testprog));
25 	assertEmptyFile("c.out");
26 	assertEmptyFile("c.err");
27 	assertEqualInt(0,
28 	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
29 	assertEmptyFile("x.out");
30 	assertEmptyFile("x.err");
31 	assertFileContents("d1/file1", 8, "d1/file1");
32 	assertChdir("..");
33 
34 	/* Test 2: -c with -n */
35 	assertMakeDir("test2", 0755);
36 	assertChdir("test2");
37 	assertEqualInt(0,
38 	    systemf("%s -cnf archive.tar -C .. d1 >c.out 2>c.err", testprog));
39 	assertEmptyFile("c.out");
40 	assertEmptyFile("c.err");
41 	assertEqualInt(0,
42 	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
43 	assertEmptyFile("x.out");
44 	assertEmptyFile("x.err");
45 	assertIsDir("d1", umasked(0755));
46 	assertFileNotExists("d1/file1");
47 	assertChdir("..");
48 
49 	/*
50 	 * Create a test archive with the following content:
51 	 * d1/
52 	 * d1/file1
53 	 * d1/file2
54 	 * file3
55 	 * d2/file4
56 	 *
57 	 * Extracting uses the same code as listing and thus does not
58 	 * get tested separately. This also covers the
59 	 * archive_match_set_inclusion_recursion()
60 	 * API.
61 	 */
62 	assertMakeFile("d1/file2", 0644, "d1/file2");
63 	assertMakeFile("file3", 0644, "file3");
64 	assertMakeDir("d2", 0755);
65 	assertMakeFile("d2/file4", 0644, "d2/file4");
66 	assertEqualInt(0,
67 	    systemf("%s -cnf partial-archive.tar d1 d1/file1 d1/file2 file3 "
68 	    "d2/file4 >c.out 2>c.err", testprog));
69 
70 	/* Test 3: -t without other options */
71 	assertEqualInt(0,
72 	    systemf("%s -tf partial-archive.tar >test3.out 2>test3.err",
73 	    testprog));
74 	assertEmptyFile("test3.err");
75 	assertTextFileContents("d1/\n"
76 			      "d1/file1\n"
77 			      "d1/file2\n"
78 			      "file3\n"
79 			      "d2/file4\n",
80 			      "test3.out");
81 
82 	/* Test 4: -t without -n and some entries selected */
83 	assertEqualInt(0,
84 	    systemf("%s -tf partial-archive.tar d1 file3 d2/file4 "
85 	    ">test4.out 2>test4.err", testprog));
86 	assertEmptyFile("test4.err");
87 	assertTextFileContents("d1/\n"
88 			      "d1/file1\n"
89 			      "d1/file2\n"
90 			      "file3\n"
91 			      "d2/file4\n",
92 			      "test4.out");
93 
94 	/* Test 5: -t with -n and some entries selected */
95 	assertEqualInt(0,
96 	    systemf("%s -tnf partial-archive.tar d1 file3 d2/file4 "
97 	    ">test5.out 2>test5.err", testprog));
98 	assertEmptyFile("test5.err");
99 	assertTextFileContents("d1/\n"
100 			      "file3\n"
101 			      "d2/file4\n",
102 			      "test5.out");
103 
104 	/* Test 6: -t without -n and non-existent directory selected */
105 	assertEqualInt(0,
106 	    systemf("%s -tf partial-archive.tar d2 >test6.out 2>test6.err",
107 	    testprog));
108 	assertEmptyFile("test6.err");
109 	assertTextFileContents("d2/file4\n",
110 			      "test6.out");
111 
112 	/* Test 7: -t with -n and non-existent directory selected */
113 	status = systemf("%s -tnf partial-archive.tar d2 "
114 	">test7.out 2>test7.err", testprog);
115 	assert(status);
116 	assert(status != -1);
117 #if !defined(_WIN32) || defined(__CYGWIN__)
118 	assert(WIFEXITED(status));
119 	assertEqualInt(1, WEXITSTATUS(status));
120 #endif
121 	assertNonEmptyFile("test7.err");
122 	assertEmptyFile("test7.out");
123 }
124