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$");
29 mkfile(const char *fn, const char *contents)
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));
35 return (1); /* Failure. */
37 assertEqualInt(strlen(contents),
38 write(fd, contents, strlen(contents)));
39 assertEqualInt(0, close(fd));
40 return (0); /* Success */
43 DEFINE_TEST(test_option_s)
47 /* Create a sample file hierarchy. */
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"));
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("bsdtar does not support -s option on this platform");
63 * Test 1: Filename substitution when creating archives.
65 assertEqualInt(0, mkdir("test1", 0755));
66 systemf("%s -cf - -s /foo/bar/ in/d1/foo | %s -xf - -C test1",
68 assertFileContents("foo", 3, "test1/in/d1/bar");
69 systemf("%s -cf - -s /d1/d2/ in/d1/foo | %s -xf - -C test1",
71 assertFileContents("foo", 3, "test1/in/d2/foo");
75 * Test 2: Basic substitution when extracting archive.
77 assertEqualInt(0, mkdir("test2", 0755));
78 systemf("%s -cf - in/d1/foo | %s -xf - -s /foo/bar/ -C test2",
80 assertFileContents("foo", 3, "test2/in/d1/bar");
83 * Test 3: Files with empty names shouldn't be archived.
85 systemf("%s -cf - -s ,in/d1/foo,, in/d1/foo | %s -tvf - > in.lst",
87 assertEmptyFile("in.lst");
90 * Test 4: Multiple substitutions when extracting archive.
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",
95 assertFileContents("foo", 3, "test4/in/d1/bar");
96 assertFileContents("bar", 3, "test4/in/d1/baz");
99 * Test 5: Name-switching substitutions when extracting archive.
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",
104 assertFileContents("foo", 3, "test5/in/d1/bar");
105 assertFileContents("bar", 3, "test5/in/d1/foo");