test_option_s.c revision 228776
1228753Smm/*-
2228753Smm * Copyright (c) 2003-2008 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD: head/contrib/libarchive/tar/test/test_option_s.c 228776 2011-12-21 15:30:49Z mm $");
27228753Smm
28228753Smmstatic int
29228753Smmmkfile(const char *fn, const char *contents)
30228753Smm{
31228753Smm	FILE *f = fopen(fn, "w");
32228753Smm	failure("Couldn't create file '%s', errno=%d (%s)\n",
33228753Smm	    fn, errno, strerror(errno));
34228753Smm	if (!assert(f != NULL))
35228753Smm		return (1); /* Failure. */
36228753Smm	if (contents != NULL)
37228753Smm		assertEqualInt(strlen(contents),
38228753Smm		    fwrite(contents, 1, strlen(contents), f));
39228753Smm	assertEqualInt(0, fclose(f));
40228753Smm	return (0); /* Success */
41228753Smm}
42228753Smm
43228753SmmDEFINE_TEST(test_option_s)
44228753Smm{
45228753Smm	struct stat st;
46228753Smm
47228776Smm	/* Create a sample file hierarchy. */
48228753Smm	assertMakeDir("in", 0755);
49228753Smm	assertMakeDir("in/d1", 0755);
50228753Smm	assertEqualInt(0, mkfile("in/d1/foo", "foo"));
51228753Smm	assertEqualInt(0, mkfile("in/d1/bar", "bar"));
52228753Smm
53228753Smm	/* Does bsdtar support -s option ? */
54228753Smm	systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
55228753Smm	    testprog);
56228753Smm	assertEqualInt(0, stat("check.err", &st));
57228753Smm	if (st.st_size != 0) {
58228753Smm		skipping("%s does not support -s option on this platform",
59228753Smm			testprog);
60228753Smm		return;
61228753Smm	}
62228753Smm
63228753Smm	/*
64228753Smm	 * Test 1: Filename substitution when creating archives.
65228753Smm	 */
66228753Smm	assertMakeDir("test1", 0755);
67228753Smm	systemf("%s -cf - -s /foo/bar/ in/d1/foo | %s -xf - -C test1",
68228753Smm	    testprog, testprog);
69228753Smm	assertFileContents("foo", 3, "test1/in/d1/bar");
70228753Smm	systemf("%s -cf - -s /d1/d2/ in/d1/foo | %s -xf - -C test1",
71228753Smm	    testprog, testprog);
72228753Smm	assertFileContents("foo", 3, "test1/in/d2/foo");
73228753Smm
74228753Smm
75228753Smm	/*
76228753Smm	 * Test 2: Basic substitution when extracting archive.
77228753Smm	 */
78228753Smm	assertMakeDir("test2", 0755);
79228753Smm	systemf("%s -cf - in/d1/foo | %s -xf - -s /foo/bar/ -C test2",
80228753Smm	    testprog, testprog);
81228753Smm	assertFileContents("foo", 3, "test2/in/d1/bar");
82228753Smm
83228753Smm	/*
84228753Smm	 * Test 3: Files with empty names shouldn't be archived.
85228753Smm	 */
86228753Smm	systemf("%s -cf - -s ,in/d1/foo,, in/d1/foo | %s -tvf - > in.lst",
87228753Smm	    testprog, testprog);
88228753Smm	assertEmptyFile("in.lst");
89228753Smm
90228753Smm	/*
91228753Smm	 * Test 4: Multiple substitutions when extracting archive.
92228753Smm	 */
93228753Smm	assertMakeDir("test4", 0755);
94228753Smm	systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}baz} -C test4",
95228753Smm	    testprog, testprog);
96228753Smm	assertFileContents("foo", 3, "test4/in/d1/bar");
97228753Smm	assertFileContents("bar", 3, "test4/in/d1/baz");
98228753Smm
99228753Smm	/*
100228753Smm	 * Test 5: Name-switching substitutions when extracting archive.
101228753Smm	 */
102228753Smm	assertMakeDir("test5", 0755);
103228753Smm	systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}foo} -C test5",
104228753Smm	    testprog, testprog);
105228753Smm	assertFileContents("foo", 3, "test5/in/d1/bar");
106228753Smm	assertFileContents("bar", 3, "test5/in/d1/foo");
107228753Smm}
108