]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_option_s.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_s.c
1 /*-
2  * Copyright (c) 2003-2008 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 static int
29 mkfile(const char *fn, const char *contents)
30 {
31         int fd = open(fn, O_RDWR | O_CREAT, 0644);
32         failure("Couldn't create file '%s', fd=%d, errno=%d (%s)\n",
33             fn, fd, errno, strerror(errno));
34         if (!assert(fd > 0))
35                 return (1); /* Failure. */
36         if (contents != NULL)
37                 assertEqualInt(strlen(contents),
38                     write(fd, contents, strlen(contents)));
39         assertEqualInt(0, close(fd));
40         return (0); /* Success */
41 }
42
43 DEFINE_TEST(test_option_s)
44 {
45         struct stat st;
46
47         /* Create a sample file heirarchy. */
48         assertEqualInt(0, mkdir("in", 0755));
49         assertEqualInt(0, mkdir("in/d1", 0755));
50         assertEqualInt(0, mkfile("in/d1/foo", "foo"));
51         assertEqualInt(0, mkfile("in/d1/bar", "bar"));
52
53         /* Does bsdtar support -s option ? */
54         systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
55             testprog);
56         assertEqualInt(0, stat("check.err", &st));
57         if (st.st_size != 0) {
58                 skipping("bsdtar does not support -s option on this platform");
59                 return;
60         }
61
62         /*
63          * Test 1: Filename substitution when creating archives.
64          */
65         assertEqualInt(0, mkdir("test1", 0755));
66         systemf("%s -cf - -s /foo/bar/ in/d1/foo | %s -xf - -C test1",
67             testprog, testprog);
68         assertFileContents("foo", 3, "test1/in/d1/bar");
69         systemf("%s -cf - -s /d1/d2/ in/d1/foo | %s -xf - -C test1",
70             testprog, testprog);
71         assertFileContents("foo", 3, "test1/in/d2/foo");
72
73
74         /*
75          * Test 2: Basic substitution when extracting archive.
76          */
77         assertEqualInt(0, mkdir("test2", 0755));
78         systemf("%s -cf - in/d1/foo | %s -xf - -s /foo/bar/ -C test2",
79             testprog, testprog);
80         assertFileContents("foo", 3, "test2/in/d1/bar");
81
82         /*
83          * Test 3: Files with empty names shouldn't be archived.
84          */
85         systemf("%s -cf - -s ,in/d1/foo,, in/d1/foo | %s -tvf - > in.lst",
86             testprog, testprog);
87         assertEmptyFile("in.lst");
88
89         /*
90          * Test 4: Multiple substitutions when extracting archive.
91          */
92         assertEqualInt(0, mkdir("test4", 0755));
93         systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}baz} -C test4",
94             testprog, testprog);
95         assertFileContents("foo", 3, "test4/in/d1/bar");
96         assertFileContents("bar", 3, "test4/in/d1/baz");
97
98         /*
99          * Test 5: Name-switching substitutions when extracting archive.
100          */
101         assertEqualInt(0, mkdir("test5", 0755));
102         systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}foo} -C test5",
103             testprog, testprog);
104         assertFileContents("foo", 3, "test5/in/d1/bar");
105         assertFileContents("bar", 3, "test5/in/d1/foo");
106 }