xref: /freebsd/contrib/libarchive/tar/test/test_option_n.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * Copyright (c) 2010 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27 
28 #ifdef HAVE_SYS_WAIT_H
29 #include <sys/wait.h>
30 #endif
31 
32 DEFINE_TEST(test_option_n)
33 {
34 	int status;
35 
36 	assertMakeDir("d1", 0755);
37 	assertMakeFile("d1/file1", 0644, "d1/file1");
38 
39 	/* Test 1: -c without -n */
40 	assertMakeDir("test1", 0755);
41 	assertChdir("test1");
42 	assertEqualInt(0,
43 	    systemf("%s -cf archive.tar -C .. d1 >c.out 2>c.err", testprog));
44 	assertEmptyFile("c.out");
45 	assertEmptyFile("c.err");
46 	assertEqualInt(0,
47 	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
48 	assertEmptyFile("x.out");
49 	assertEmptyFile("x.err");
50 	assertFileContents("d1/file1", 8, "d1/file1");
51 	assertChdir("..");
52 
53 	/* Test 2: -c with -n */
54 	assertMakeDir("test2", 0755);
55 	assertChdir("test2");
56 	assertEqualInt(0,
57 	    systemf("%s -cnf archive.tar -C .. d1 >c.out 2>c.err", testprog));
58 	assertEmptyFile("c.out");
59 	assertEmptyFile("c.err");
60 	assertEqualInt(0,
61 	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
62 	assertEmptyFile("x.out");
63 	assertEmptyFile("x.err");
64 	assertIsDir("d1", umasked(0755));
65 	assertFileNotExists("d1/file1");
66 	assertChdir("..");
67 
68 	/*
69 	 * Create a test archive with the following content:
70 	 * d1/
71 	 * d1/file1
72 	 * d1/file2
73 	 * file3
74 	 * d2/file4
75 	 *
76 	 * Extracting uses the same code as listing and thus does not
77 	 * get tested separately. This also covers the
78 	 * archive_match_set_inclusion_recursion()
79 	 * API.
80 	 */
81 	assertMakeFile("d1/file2", 0644, "d1/file2");
82 	assertMakeFile("file3", 0644, "file3");
83 	assertMakeDir("d2", 0755);
84 	assertMakeFile("d2/file4", 0644, "d2/file4");
85 	assertEqualInt(0,
86 	    systemf("%s -cnf partial-archive.tar d1 d1/file1 d1/file2 file3 "
87 	    "d2/file4 >c.out 2>c.err", testprog));
88 
89 	/* Test 3: -t without other options */
90 	assertEqualInt(0,
91 	    systemf("%s -tf partial-archive.tar >test3.out 2>test3.err",
92 	    testprog));
93 	assertEmptyFile("test3.err");
94 	assertTextFileContents("d1/\n"
95 			      "d1/file1\n"
96 			      "d1/file2\n"
97 			      "file3\n"
98 			      "d2/file4\n",
99 			      "test3.out");
100 
101 	/* Test 4: -t without -n and some entries selected */
102 	assertEqualInt(0,
103 	    systemf("%s -tf partial-archive.tar d1 file3 d2/file4 "
104 	    ">test4.out 2>test4.err", testprog));
105 	assertEmptyFile("test4.err");
106 	assertTextFileContents("d1/\n"
107 			      "d1/file1\n"
108 			      "d1/file2\n"
109 			      "file3\n"
110 			      "d2/file4\n",
111 			      "test4.out");
112 
113 	/* Test 5: -t with -n and some entries selected */
114 	assertEqualInt(0,
115 	    systemf("%s -tnf partial-archive.tar d1 file3 d2/file4 "
116 	    ">test5.out 2>test5.err", testprog));
117 	assertEmptyFile("test5.err");
118 	assertTextFileContents("d1/\n"
119 			      "file3\n"
120 			      "d2/file4\n",
121 			      "test5.out");
122 
123 	/* Test 6: -t without -n and non-existent directory selected */
124 	assertEqualInt(0,
125 	    systemf("%s -tf partial-archive.tar d2 >test6.out 2>test6.err",
126 	    testprog));
127 	assertEmptyFile("test6.err");
128 	assertTextFileContents("d2/file4\n",
129 			      "test6.out");
130 
131 	/* Test 7: -t with -n and non-existent directory selected */
132 	status = systemf("%s -tnf partial-archive.tar d2 "
133 	">test7.out 2>test7.err", testprog);
134 	assert(status);
135 	assert(status != -1);
136 #if !defined(_WIN32) || defined(__CYGWIN__)
137 	assert(WIFEXITED(status));
138 	assertEqualInt(1, WEXITSTATUS(status));
139 #endif
140 	assertNonEmptyFile("test7.err");
141 	assertEmptyFile("test7.out");
142 }
143