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
28static int
29mkfile(const char *fn, const char *contents)
30{
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. */
36	if (contents != NULL)
37		assertEqualInt(strlen(contents),
38		    fwrite(contents, 1, strlen(contents), f));
39	assertEqualInt(0, fclose(f));
40	return (0); /* Success */
41}
42
43DEFINE_TEST(test_option_s)
44{
45	struct stat st;
46
47	/* Create a sample file hierarchy. */
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"));
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("%s does not support -s option on this platform",
59			testprog);
60		return;
61	}
62
63	/*
64	 * Test 1: Filename substitution when creating archives.
65	 */
66	assertMakeDir("test1", 0755);
67	systemf("%s -cf - -s /foo/bar/ in/d1/foo | %s -xf - -C test1",
68	    testprog, testprog);
69	assertFileContents("foo", 3, "test1/in/d1/bar");
70	systemf("%s -cf - -s /d1/d2/ in/d1/foo | %s -xf - -C test1",
71	    testprog, testprog);
72	assertFileContents("foo", 3, "test1/in/d2/foo");
73
74
75	/*
76	 * Test 2: Basic substitution when extracting archive.
77	 */
78	assertMakeDir("test2", 0755);
79	systemf("%s -cf - in/d1/foo | %s -xf - -s /foo/bar/ -C test2",
80	    testprog, testprog);
81	assertFileContents("foo", 3, "test2/in/d1/bar");
82
83	/*
84	 * Test 3: Files with empty names shouldn't be archived.
85	 */
86	systemf("%s -cf - -s ,in/d1/foo,, in/d1/foo | %s -tvf - > in.lst",
87	    testprog, testprog);
88	assertEmptyFile("in.lst");
89
90	/*
91	 * Test 4: Multiple substitutions when extracting archive.
92	 */
93	assertMakeDir("test4", 0755);
94	systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}baz} -C test4",
95	    testprog, testprog);
96	assertFileContents("foo", 3, "test4/in/d1/bar");
97	assertFileContents("bar", 3, "test4/in/d1/baz");
98
99	/*
100	 * Test 5: Name-switching substitutions when extracting archive.
101	 */
102	assertMakeDir("test5", 0755);
103	systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}foo} -C test5",
104	    testprog, testprog);
105	assertFileContents("foo", 3, "test5/in/d1/bar");
106	assertFileContents("bar", 3, "test5/in/d1/foo");
107}
108