unstack, sort: cleanup and improvement
[minix.git] / commands / tar / test / test_option_q.c
blob68867b52a82ae105d18fe75fab9cf6c7779043c6
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
25 #include "test.h"
26 __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_q.c,v 1.3 2008/08/22 01:35:08 kientzle Exp $");
28 DEFINE_TEST(test_option_q)
30 FILE *f;
31 int r;
34 * Create an archive with several different versions of the
35 * same files. By default, the last version will overwrite
36 * any earlier versions. The -q/--fast-read option will
37 * stop early, so we can verify -q/--fast-read by seeing
38 * which version of each file actually ended up being
39 * extracted. This also exercises -r mode, since that's
40 * what we use to build up the test archive.
43 f = fopen("foo", "w");
44 assert(f != NULL);
45 fprintf(f, "foo1");
46 fclose(f);
48 assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
50 f = fopen("foo", "w");
51 assert(f != NULL);
52 fprintf(f, "foo2");
53 fclose(f);
55 assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
57 f = fopen("bar", "w");
58 assert(f != NULL);
59 fprintf(f, "bar1");
60 fclose(f);
62 assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
64 f = fopen("foo", "w");
65 assert(f != NULL);
66 fprintf(f, "foo3");
67 fclose(f);
69 assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
71 f = fopen("bar", "w");
72 assert(f != NULL);
73 fprintf(f, "bar2");
74 fclose(f);
76 assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
79 * Now, try extracting from the test archive with various
80 * combinations of -q.
83 /* Test 1: -q foo should only extract the first foo. */
84 assertMakeDir("test1", 0755);
85 assertChdir("test1");
86 r = systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
87 testprog);
88 failure("Fatal error trying to use -q option");
89 if (!assertEqualInt(0, r))
90 return;
92 assertFileContents("foo1", 4, "foo");
93 assertEmptyFile("test.out");
94 assertEmptyFile("test.err");
95 assertChdir("..");
97 /* Test 2: -q foo bar should extract up to the first bar. */
98 assertMakeDir("test2", 0755);
99 assertChdir("test2");
100 assertEqualInt(0,
101 systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
102 assertFileContents("foo2", 4, "foo");
103 assertFileContents("bar1", 4, "bar");
104 assertEmptyFile("test.out");
105 assertEmptyFile("test.err");
106 assertChdir("..");
108 /* Test 3: Same as test 2, but use --fast-read spelling. */
109 assertMakeDir("test3", 0755);
110 assertChdir("test3");
111 assertEqualInt(0,
112 systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
113 assertFileContents("foo2", 4, "foo");
114 assertFileContents("bar1", 4, "bar");
115 assertEmptyFile("test.out");
116 assertEmptyFile("test.err");
117 assertChdir("..");
119 /* Test 4: Without -q, should extract everything. */
120 assertMakeDir("test4", 0755);
121 assertChdir("test4");
122 assertEqualInt(0,
123 systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
124 assertFileContents("foo3", 4, "foo");
125 assertFileContents("bar2", 4, "bar");
126 assertEmptyFile("test.out");
127 assertEmptyFile("test.err");
128 assertChdir("..");