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