2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
26 __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_T.c,v 1.3 2008/08/15 06:12:02 kientzle Exp $");
29 mkfile(const char *fn
, const char *contents
)
31 FILE *f
= fopen(fn
, "w");
32 failure("Couldn't create file '%s', errno=%d (%s)\n",
33 fn
, errno
, strerror(errno
));
34 if (!assert(f
!= NULL
))
35 return (1); /* Failure. */
37 assertEqualInt(strlen(contents
),
38 fwrite(contents
, 1, strlen(contents
), f
));
39 assertEqualInt(0, fclose(f
));
40 return (0); /* Success */
43 DEFINE_TEST(test_option_s
)
47 /* Create a sample file heirarchy. */
48 assertMakeDir("in", 0755);
49 assertMakeDir("in/d1", 0755);
50 assertEqualInt(0, mkfile("in/d1/foo", "foo"));
51 assertEqualInt(0, mkfile("in/d1/bar", "bar"));
53 /* Does bsdtar support -s option ? */
54 systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
56 assertEqualInt(0, stat("check.err", &st
));
57 if (st
.st_size
!= 0) {
58 skipping("%s does not support -s option on this platform",
64 * Test 1: Filename substitution when creating archives.
66 assertMakeDir("test1", 0755);
67 systemf("%s -cf - -s /foo/bar/ in/d1/foo | %s -xf - -C test1",
69 assertFileContents("foo", 3, "test1/in/d1/bar");
70 systemf("%s -cf - -s /d1/d2/ in/d1/foo | %s -xf - -C test1",
72 assertFileContents("foo", 3, "test1/in/d2/foo");
76 * Test 2: Basic substitution when extracting archive.
78 assertMakeDir("test2", 0755);
79 systemf("%s -cf - in/d1/foo | %s -xf - -s /foo/bar/ -C test2",
81 assertFileContents("foo", 3, "test2/in/d1/bar");
84 * Test 3: Files with empty names shouldn't be archived.
86 systemf("%s -cf - -s ,in/d1/foo,, in/d1/foo | %s -tvf - > in.lst",
88 assertEmptyFile("in.lst");
91 * Test 4: Multiple substitutions when extracting archive.
93 assertMakeDir("test4", 0755);
94 systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}baz} -C test4",
96 assertFileContents("foo", 3, "test4/in/d1/bar");
97 assertFileContents("bar", 3, "test4/in/d1/baz");
100 * Test 5: Name-switching substitutions when extracting archive.
102 assertMakeDir("test5", 0755);
103 systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}foo} -C test5",
105 assertFileContents("foo", 3, "test5/in/d1/bar");
106 assertFileContents("bar", 3, "test5/in/d1/foo");