xref: /minix3/external/bsd/libarchive/dist/tar/test/test_option_s.c (revision 543adbed3a3a783ed36434adafbc258b6bde442d)
1*543adbedSBen Gras /*-
2*543adbedSBen Gras  * Copyright (c) 2003-2008 Tim Kientzle
3*543adbedSBen Gras  * All rights reserved.
4*543adbedSBen Gras  *
5*543adbedSBen Gras  * Redistribution and use in source and binary forms, with or without
6*543adbedSBen Gras  * modification, are permitted provided that the following conditions
7*543adbedSBen Gras  * are met:
8*543adbedSBen Gras  * 1. Redistributions of source code must retain the above copyright
9*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer.
10*543adbedSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
11*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer in the
12*543adbedSBen Gras  *    documentation and/or other materials provided with the distribution.
13*543adbedSBen Gras  *
14*543adbedSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*543adbedSBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*543adbedSBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*543adbedSBen Gras  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*543adbedSBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*543adbedSBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*543adbedSBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*543adbedSBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*543adbedSBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*543adbedSBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*543adbedSBen Gras  */
25*543adbedSBen Gras #include "test.h"
26*543adbedSBen Gras __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_option_T.c,v 1.3 2008/08/15 06:12:02 kientzle Exp $");
27*543adbedSBen Gras 
28*543adbedSBen Gras static int
mkfile(const char * fn,const char * contents)29*543adbedSBen Gras mkfile(const char *fn, const char *contents)
30*543adbedSBen Gras {
31*543adbedSBen Gras 	FILE *f = fopen(fn, "w");
32*543adbedSBen Gras 	failure("Couldn't create file '%s', errno=%d (%s)\n",
33*543adbedSBen Gras 	    fn, errno, strerror(errno));
34*543adbedSBen Gras 	if (!assert(f != NULL))
35*543adbedSBen Gras 		return (1); /* Failure. */
36*543adbedSBen Gras 	if (contents != NULL)
37*543adbedSBen Gras 		assertEqualInt(strlen(contents),
38*543adbedSBen Gras 		    fwrite(contents, 1, strlen(contents), f));
39*543adbedSBen Gras 	assertEqualInt(0, fclose(f));
40*543adbedSBen Gras 	return (0); /* Success */
41*543adbedSBen Gras }
42*543adbedSBen Gras 
DEFINE_TEST(test_option_s)43*543adbedSBen Gras DEFINE_TEST(test_option_s)
44*543adbedSBen Gras {
45*543adbedSBen Gras 	struct stat st;
46*543adbedSBen Gras 
47*543adbedSBen Gras 	/* Create a sample file heirarchy. */
48*543adbedSBen Gras 	assertMakeDir("in", 0755);
49*543adbedSBen Gras 	assertMakeDir("in/d1", 0755);
50*543adbedSBen Gras 	assertEqualInt(0, mkfile("in/d1/foo", "foo"));
51*543adbedSBen Gras 	assertEqualInt(0, mkfile("in/d1/bar", "bar"));
52*543adbedSBen Gras 
53*543adbedSBen Gras 	/* Does bsdtar support -s option ? */
54*543adbedSBen Gras 	systemf("%s -cf - -s /foo/bar/ in/d1/foo > NUL 2> check.err",
55*543adbedSBen Gras 	    testprog);
56*543adbedSBen Gras 	assertEqualInt(0, stat("check.err", &st));
57*543adbedSBen Gras 	if (st.st_size != 0) {
58*543adbedSBen Gras 		skipping("%s does not support -s option on this platform",
59*543adbedSBen Gras 			testprog);
60*543adbedSBen Gras 		return;
61*543adbedSBen Gras 	}
62*543adbedSBen Gras 
63*543adbedSBen Gras 	/*
64*543adbedSBen Gras 	 * Test 1: Filename substitution when creating archives.
65*543adbedSBen Gras 	 */
66*543adbedSBen Gras 	assertMakeDir("test1", 0755);
67*543adbedSBen Gras 	systemf("%s -cf - -s /foo/bar/ in/d1/foo | %s -xf - -C test1",
68*543adbedSBen Gras 	    testprog, testprog);
69*543adbedSBen Gras 	assertFileContents("foo", 3, "test1/in/d1/bar");
70*543adbedSBen Gras 	systemf("%s -cf - -s /d1/d2/ in/d1/foo | %s -xf - -C test1",
71*543adbedSBen Gras 	    testprog, testprog);
72*543adbedSBen Gras 	assertFileContents("foo", 3, "test1/in/d2/foo");
73*543adbedSBen Gras 
74*543adbedSBen Gras 
75*543adbedSBen Gras 	/*
76*543adbedSBen Gras 	 * Test 2: Basic substitution when extracting archive.
77*543adbedSBen Gras 	 */
78*543adbedSBen Gras 	assertMakeDir("test2", 0755);
79*543adbedSBen Gras 	systemf("%s -cf - in/d1/foo | %s -xf - -s /foo/bar/ -C test2",
80*543adbedSBen Gras 	    testprog, testprog);
81*543adbedSBen Gras 	assertFileContents("foo", 3, "test2/in/d1/bar");
82*543adbedSBen Gras 
83*543adbedSBen Gras 	/*
84*543adbedSBen Gras 	 * Test 3: Files with empty names shouldn't be archived.
85*543adbedSBen Gras 	 */
86*543adbedSBen Gras 	systemf("%s -cf - -s ,in/d1/foo,, in/d1/foo | %s -tvf - > in.lst",
87*543adbedSBen Gras 	    testprog, testprog);
88*543adbedSBen Gras 	assertEmptyFile("in.lst");
89*543adbedSBen Gras 
90*543adbedSBen Gras 	/*
91*543adbedSBen Gras 	 * Test 4: Multiple substitutions when extracting archive.
92*543adbedSBen Gras 	 */
93*543adbedSBen Gras 	assertMakeDir("test4", 0755);
94*543adbedSBen Gras 	systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}baz} -C test4",
95*543adbedSBen Gras 	    testprog, testprog);
96*543adbedSBen Gras 	assertFileContents("foo", 3, "test4/in/d1/bar");
97*543adbedSBen Gras 	assertFileContents("bar", 3, "test4/in/d1/baz");
98*543adbedSBen Gras 
99*543adbedSBen Gras 	/*
100*543adbedSBen Gras 	 * Test 5: Name-switching substitutions when extracting archive.
101*543adbedSBen Gras 	 */
102*543adbedSBen Gras 	assertMakeDir("test5", 0755);
103*543adbedSBen Gras 	systemf("%s -cf - in/d1/foo in/d1/bar | %s -xf - -s /foo/bar/ -s }bar}foo} -C test5",
104*543adbedSBen Gras 	    testprog, testprog);
105*543adbedSBen Gras 	assertFileContents("foo", 3, "test5/in/d1/bar");
106*543adbedSBen Gras 	assertFileContents("bar", 3, "test5/in/d1/foo");
107*543adbedSBen Gras }
108