]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_option_q.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tar / test / test_option_q.c
1 /*-
2  * Copyright (c) 2003-2007 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 DEFINE_TEST(test_option_q)
29 {
30         int fd;
31
32         /*
33          * Create an archive with several different versions of the
34          * same files.  By default, the last version will overwrite
35          * any earlier versions.  The -q/--fast-read option will
36          * stop early, so we can verify -q/--fast-read by seeing
37          * which version of each file actually ended up being
38          * extracted.  This also exercises -r mode, since that's
39          * what we use to build up the test archive.
40          */
41
42         fd = open("foo", O_CREAT | O_WRONLY, 0644);
43         assert(fd >= 0);
44         assertEqualInt(4, write(fd, "foo1", 4));
45         close(fd);
46
47         assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
48
49         fd = open("foo", O_TRUNC | O_WRONLY, 0644);
50         assert(fd >= 0);
51         assertEqualInt(4, write(fd, "foo2", 4));
52         close(fd);
53
54         assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
55
56         fd = open("bar", O_CREAT | O_WRONLY, 0644);
57         assert(fd >= 0);
58         assertEqualInt(4, write(fd, "bar1", 4));
59         close(fd);
60
61         assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
62
63         fd = open("foo", O_TRUNC | O_WRONLY, 0644);
64         assert(fd >= 0);
65         assertEqualInt(4, write(fd, "foo3", 4));
66         close(fd);
67
68         assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
69
70         fd = open("bar", O_TRUNC | O_WRONLY, 0644);
71         assert(fd >= 0);
72         assertEqualInt(4, write(fd, "bar2", 4));
73         close(fd);
74
75         assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
76
77         /*
78          * Now, try extracting from the test archive with various
79          * combinations of -q.
80          */
81
82         /* Test 1: -q foo should only extract the first foo. */
83         assertEqualInt(0, mkdir("test1", 0755));
84         assertEqualInt(0, chdir("test1"));
85         assertEqualInt(0,
86             systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
87                 testprog));
88         assertFileContents("foo1", 4, "foo");
89         assertEmptyFile("test.out");
90         assertEmptyFile("test.err");
91         assertEqualInt(0, chdir(".."));
92
93         /* Test 2: -q foo bar should extract up to the first bar. */
94         assertEqualInt(0, mkdir("test2", 0755));
95         assertEqualInt(0, chdir("test2"));
96         assertEqualInt(0,
97             systemf("%s -xf ../archive.tar -q foo bar >test.out 2>test.err", testprog));
98         assertFileContents("foo2", 4, "foo");
99         assertFileContents("bar1", 4, "bar");
100         assertEmptyFile("test.out");
101         assertEmptyFile("test.err");
102         assertEqualInt(0, chdir(".."));
103
104         /* Test 3: Same as test 2, but use --fast-read spelling. */
105         assertEqualInt(0, mkdir("test3", 0755));
106         assertEqualInt(0, chdir("test3"));
107         assertEqualInt(0,
108             systemf("%s -xf ../archive.tar --fast-read foo bar >test.out 2>test.err", testprog));
109         assertFileContents("foo2", 4, "foo");
110         assertFileContents("bar1", 4, "bar");
111         assertEmptyFile("test.out");
112         assertEmptyFile("test.err");
113         assertEqualInt(0, chdir(".."));
114
115         /* Test 4: Without -q, should extract everything. */
116         assertEqualInt(0, mkdir("test4", 0755));
117         assertEqualInt(0, chdir("test4"));
118         assertEqualInt(0,
119             systemf("%s -xf ../archive.tar foo bar >test.out 2>test.err", testprog));
120         assertFileContents("foo3", 4, "foo");
121         assertFileContents("bar2", 4, "bar");
122         assertEmptyFile("test.out");
123         assertEmptyFile("test.err");
124         assertEqualInt(0, chdir(".."));
125 }