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
27DEFINE_TEST(test_option_X_upper)
28{
29	int r;
30
31	/*
32	 * Create a sample archive.
33	 */
34	assertMakeFile("file1", 0644, "file1");
35	assertMakeFile("file2", 0644, "file2");
36	assertMakeFile("file3a", 0644, "file3a");
37	assertMakeFile("file4a", 0644, "file4a");
38	assertEqualInt(0,
39	    systemf("%s -cf archive.tar file1 file2 file3a file4a", testprog));
40
41	/*
42	 * Now, try extracting from the test archive with various -X usage.
43	 */
44
45	/* Test 1: Without -X */
46	assertMakeDir("test1", 0755);
47	assertChdir("test1");
48	r = systemf("%s -xf ../archive.tar >test.out 2>test.err",
49	    testprog);
50	if (!assertEqualInt(0, r))
51		return;
52
53	assertFileContents("file1", 5, "file1");
54	assertFileContents("file2", 5, "file2");
55	assertFileContents("file3a", 6, "file3a");
56	assertFileContents("file4a", 6, "file4a");
57	assertEmptyFile("test.out");
58	assertEmptyFile("test.err");
59	assertChdir("..");
60
61	/* Test 2: Use -X to skip one file */
62	assertMakeDir("test2", 0755);
63	assertChdir("test2");
64	assertMakeFile("exclusions", 0644, "file1\n");
65	assertEqualInt(0,
66	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
67	assertFileNotExists("file1");
68	assertFileContents("file2", 5, "file2");
69	assertFileContents("file3a", 6, "file3a");
70	assertFileContents("file4a", 6, "file4a");
71	assertEmptyFile("test.out");
72	assertEmptyFile("test.err");
73	assertChdir("..");
74
75	/* Test 3: Use -X to skip multiple files */
76	assertMakeDir("test3", 0755);
77	assertChdir("test3");
78	assertMakeFile("exclusions", 0644, "file1\nfile2\n");
79	assertEqualInt(0,
80	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
81	assertFileNotExists("file1");
82	assertFileNotExists("file2");
83	assertFileContents("file3a", 6, "file3a");
84	assertFileContents("file4a", 6, "file4a");
85	assertEmptyFile("test.out");
86	assertEmptyFile("test.err");
87	assertChdir("..");
88
89	/* Test 4: Omit trailing \n */
90	assertMakeDir("test4", 0755);
91	assertChdir("test4");
92	assertMakeFile("exclusions", 0644, "file1\nfile2");
93	assertEqualInt(0,
94	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
95	assertFileNotExists("file1");
96	assertFileNotExists("file2");
97	assertFileContents("file3a", 6, "file3a");
98	assertFileContents("file4a", 6, "file4a");
99	assertEmptyFile("test.out");
100	assertEmptyFile("test.err");
101	assertChdir("..");
102
103	/* Test 5: include/exclude without overlap */
104	assertMakeDir("test5", 0755);
105	assertChdir("test5");
106	assertMakeFile("exclusions", 0644, "file1\nfile2");
107	assertEqualInt(0,
108	    systemf("%s -xf ../archive.tar -X exclusions file3a >test.out 2>test.err", testprog));
109	assertFileNotExists("file1");
110	assertFileNotExists("file2");
111	assertFileContents("file3a", 6, "file3a");
112	assertFileNotExists("file4a");
113	assertEmptyFile("test.out");
114	assertEmptyFile("test.err");
115	assertChdir("..");
116
117	/* Test 6: Overlapping include/exclude */
118	assertMakeDir("test6", 0755);
119	assertChdir("test6");
120	assertMakeFile("exclusions", 0644, "file1\nfile2");
121	assertEqualInt(0,
122	    systemf("%s -xf ../archive.tar -X exclusions file1 file3a >test.out 2>test.err", testprog));
123	assertFileNotExists("file1");
124	assertFileNotExists("file2");
125	assertFileContents("file3a", 6, "file3a");
126	assertFileNotExists("file4a");
127	assertEmptyFile("test.out");
128	assertEmptyFile("test.err");
129	assertChdir("..");
130
131	/* Test 7: with pattern */
132	assertMakeDir("test7", 0755);
133	assertChdir("test7");
134	assertMakeFile("exclusions", 0644, "file*a\nfile1");
135	assertEqualInt(0,
136	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
137	assertFileNotExists("file1");
138	assertFileContents("file2", 5, "file2");
139	assertFileNotExists("file3a");
140	assertFileNotExists("file4a");
141	assertEmptyFile("test.out");
142	assertEmptyFile("test.err");
143	assertChdir("..");
144
145	/* Test 8: with empty exclusions file */
146	assertMakeDir("test8", 0755);
147	assertChdir("test8");
148	assertMakeFile("exclusions", 0644, "");
149	assertEqualInt(0,
150	    systemf("%s -xf ../archive.tar -X exclusions >test.out 2>test.err", testprog));
151	assertFileContents("file1", 5, "file1");
152	assertFileContents("file2", 5, "file2");
153	assertFileContents("file3a", 6, "file3a");
154	assertFileContents("file4a", 6, "file4a");
155	assertEmptyFile("test.out");
156	assertEmptyFile("test.err");
157	assertChdir("..");
158}
159