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
27#ifdef HAVE_SYS_WAIT_H
28#include <sys/wait.h>
29#endif
30
31DEFINE_TEST(test_option_n)
32{
33	int status;
34
35	assertMakeDir("d1", 0755);
36	assertMakeFile("d1/file1", 0644, "d1/file1");
37
38	/* Test 1: -c without -n */
39	assertMakeDir("test1", 0755);
40	assertChdir("test1");
41	assertEqualInt(0,
42	    systemf("%s -cf archive.tar -C .. d1 >c.out 2>c.err", testprog));
43	assertEmptyFile("c.out");
44	assertEmptyFile("c.err");
45	assertEqualInt(0,
46	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
47	assertEmptyFile("x.out");
48	assertEmptyFile("x.err");
49	assertFileContents("d1/file1", 8, "d1/file1");
50	assertChdir("..");
51
52	/* Test 2: -c with -n */
53	assertMakeDir("test2", 0755);
54	assertChdir("test2");
55	assertEqualInt(0,
56	    systemf("%s -cnf archive.tar -C .. d1 >c.out 2>c.err", testprog));
57	assertEmptyFile("c.out");
58	assertEmptyFile("c.err");
59	assertEqualInt(0,
60	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
61	assertEmptyFile("x.out");
62	assertEmptyFile("x.err");
63	assertIsDir("d1", umasked(0755));
64	assertFileNotExists("d1/file1");
65	assertChdir("..");
66
67	/*
68	 * Create a test archive with the following content:
69	 * d1/
70	 * d1/file1
71	 * d1/file2
72	 * file3
73	 * d2/file4
74	 *
75	 * Extracting uses the same code as listing and thus does not
76	 * get tested separately. This also covers the
77	 * archive_match_set_inclusion_recursion()
78	 * API.
79	 */
80	assertMakeFile("d1/file2", 0644, "d1/file2");
81	assertMakeFile("file3", 0644, "file3");
82	assertMakeDir("d2", 0755);
83	assertMakeFile("d2/file4", 0644, "d2/file4");
84	assertEqualInt(0,
85	    systemf("%s -cnf partial-archive.tar d1 d1/file1 d1/file2 file3 "
86	    "d2/file4 >c.out 2>c.err", testprog));
87
88	/* Test 3: -t without other options */
89	assertEqualInt(0,
90	    systemf("%s -tf partial-archive.tar >test3.out 2>test3.err",
91	    testprog));
92	assertEmptyFile("test3.err");
93	assertTextFileContents("d1/\n"
94			      "d1/file1\n"
95			      "d1/file2\n"
96			      "file3\n"
97			      "d2/file4\n",
98			      "test3.out");
99
100	/* Test 4: -t without -n and some entries selected */
101	assertEqualInt(0,
102	    systemf("%s -tf partial-archive.tar d1 file3 d2/file4 "
103	    ">test4.out 2>test4.err", testprog));
104	assertEmptyFile("test4.err");
105	assertTextFileContents("d1/\n"
106			      "d1/file1\n"
107			      "d1/file2\n"
108			      "file3\n"
109			      "d2/file4\n",
110			      "test4.out");
111
112	/* Test 5: -t with -n and some entries selected */
113	assertEqualInt(0,
114	    systemf("%s -tnf partial-archive.tar d1 file3 d2/file4 "
115	    ">test5.out 2>test5.err", testprog));
116	assertEmptyFile("test5.err");
117	assertTextFileContents("d1/\n"
118			      "file3\n"
119			      "d2/file4\n",
120			      "test5.out");
121
122	/* Test 6: -t without -n and non-existent directory selected */
123	assertEqualInt(0,
124	    systemf("%s -tf partial-archive.tar d2 >test6.out 2>test6.err",
125	    testprog));
126	assertEmptyFile("test6.err");
127	assertTextFileContents("d2/file4\n",
128			      "test6.out");
129
130	/* Test 7: -t with -n and non-existent directory selected */
131	status = systemf("%s -tnf partial-archive.tar d2 "
132	">test7.out 2>test7.err", testprog);
133	assert(status);
134	assert(status != -1);
135#if !defined(_WIN32) || defined(__CYGWIN__)
136	assert(WIFEXITED(status));
137	assertEqualInt(1, WEXITSTATUS(status));
138#endif
139	assertNonEmptyFile("test7.err");
140	assertEmptyFile("test7.out");
141}
142