]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/tar/test/test_option_q.c
Test for -q (aka --fast-read).
[FreeBSD/FreeBSD.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         fd = open("foo", O_CREAT | O_WRONLY, 0644);
33         assert(fd >= 0);
34         assertEqualInt(4, write(fd, "foo1", 4));
35         close(fd);
36
37         assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
38
39         fd = open("foo", O_TRUNC | O_WRONLY, 0644);
40         assert(fd >= 0);
41         assertEqualInt(4, write(fd, "foo2", 4));
42         close(fd);
43
44         assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
45
46         fd = open("bar", O_CREAT | O_WRONLY, 0644);
47         assert(fd >= 0);
48         assertEqualInt(4, write(fd, "bar1", 4));
49         close(fd);
50
51         assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
52
53         fd = open("foo", O_TRUNC | O_WRONLY, 0644);
54         assert(fd >= 0);
55         assertEqualInt(4, write(fd, "foo3", 4));
56         close(fd);
57
58         assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
59
60         fd = open("bar", O_TRUNC | O_WRONLY, 0644);
61         assert(fd >= 0);
62         assertEqualInt(4, write(fd, "bar2", 4));
63         close(fd);
64
65         assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
66
67         /* Test 1: -q foo should only extract the first foo. */
68         assertEqualInt(0, mkdir("test1", 0755));
69         assertEqualInt(0, chdir("test1"));
70         assertEqualInt(0,
71             systemf("%s -xf ../archive.tar -q foo >test.out 2>test.err",
72                 testprog));
73         assertFileContents("foo1", 4, "foo");
74         assertEmptyFile("test.out");
75         assertEmptyFile("test.err");
76         assertEqualInt(0, chdir(".."));
77
78         /* Test 2: -q foo bar should extract up to the first bar. */
79         assertEqualInt(0, mkdir("test2", 0755));
80         assertEqualInt(0, chdir("test2"));
81         assertEqualInt(0,
82             systemf("%s -xf ../archive.tar -q foo bar", testprog));
83         assertFileContents("foo2", 4, "foo");
84         assertFileContents("bar1", 4, "bar");
85         assertEqualInt(0, chdir(".."));
86
87         /* Test 3: Same as test 2, but use --fast-read spelling. */
88         assertEqualInt(0, mkdir("test3", 0755));
89         assertEqualInt(0, chdir("test3"));
90         assertEqualInt(0,
91             systemf("%s -xf ../archive.tar --fast-read foo bar", testprog));
92         assertFileContents("foo2", 4, "foo");
93         assertFileContents("bar1", 4, "bar");
94         assertEqualInt(0, chdir(".."));
95
96         /* Test 4: Without -q, should extract everything. */
97         assertEqualInt(0, mkdir("test4", 0755));
98         assertEqualInt(0, chdir("test4"));
99         assertEqualInt(0,
100             systemf("%s -xf ../archive.tar foo bar", testprog));
101         assertFileContents("foo3", 4, "foo");
102         assertFileContents("bar2", 4, "bar");
103         assertEqualInt(0, chdir(".."));
104 }