test_option_n.c revision 348608
138494Sobrien/*-
238494Sobrien * Copyright (c) 2010 Tim Kientzle
338494Sobrien * All rights reserved.
438494Sobrien *
538494Sobrien * Redistribution and use in source and binary forms, with or without
638494Sobrien * modification, are permitted provided that the following conditions
738494Sobrien * are met:
838494Sobrien * 1. Redistributions of source code must retain the above copyright
9310490Scy *    notice, this list of conditions and the following disclaimer.
1038494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1138494Sobrien *    notice, this list of conditions and the following disclaimer in the
1238494Sobrien *    documentation and/or other materials provided with the distribution.
1338494Sobrien *
1438494Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15310490Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16174294Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17174294Sobrien * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18174294Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19174294Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20310490Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2138494Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2238494Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2338494Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24310490Scy */
2538494Sobrien#include "test.h"
2638494Sobrien__FBSDID("$FreeBSD$");
2738494Sobrien
2838494Sobrien#ifdef HAVE_SYS_WAIT_H
2938494Sobrien#include <sys/wait.h>
3038494Sobrien#endif
3138494Sobrien
3241142SobrienDEFINE_TEST(test_option_n)
3341142Sobrien{
3451292Sobrien	int status;
3551292Sobrien
3651292Sobrien	assertMakeDir("d1", 0755);
3751292Sobrien	assertMakeFile("d1/file1", 0644, "d1/file1");
38310490Scy
3938494Sobrien	/* Test 1: -c without -n */
4038494Sobrien	assertMakeDir("test1", 0755);
4138494Sobrien	assertChdir("test1");
4238494Sobrien	assertEqualInt(0,
43310490Scy	    systemf("%s -cf archive.tar -C .. d1 >c.out 2>c.err", testprog));
4438494Sobrien	assertEmptyFile("c.out");
4538494Sobrien	assertEmptyFile("c.err");
4638494Sobrien	assertEqualInt(0,
4738494Sobrien	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
4838494Sobrien	assertEmptyFile("x.out");
4938494Sobrien	assertEmptyFile("x.err");
5038494Sobrien	assertFileContents("d1/file1", 8, "d1/file1");
5138494Sobrien	assertChdir("..");
5238494Sobrien
5338494Sobrien	/* Test 2: -c with -n */
5438494Sobrien	assertMakeDir("test2", 0755);
55310490Scy	assertChdir("test2");
5638494Sobrien	assertEqualInt(0,
5738494Sobrien	    systemf("%s -cnf archive.tar -C .. d1 >c.out 2>c.err", testprog));
5838494Sobrien	assertEmptyFile("c.out");
5938494Sobrien	assertEmptyFile("c.err");
6038494Sobrien	assertEqualInt(0,
6138494Sobrien	    systemf("%s -xf archive.tar >x.out 2>x.err", testprog));
6238494Sobrien	assertEmptyFile("x.out");
6338494Sobrien	assertEmptyFile("x.err");
6438494Sobrien	assertIsDir("d1", umasked(0755));
6538494Sobrien	assertFileNotExists("d1/file1");
6638494Sobrien	assertChdir("..");
6738494Sobrien
6838494Sobrien	/*
6938494Sobrien	 * Create a test archive with the following content:
7038494Sobrien	 * d1/
71310490Scy	 * d1/file1
7238494Sobrien	 * d1/file2
7338494Sobrien	 * file3
7438494Sobrien	 * d2/file4
75310490Scy	 *
7638494Sobrien	 * Extracting uses the same code as listing and thus does not
7738494Sobrien	 * get tested separately. This also covers the
7838494Sobrien	 * archive_match_set_inclusion_recursion()
7938494Sobrien	 * API.
8038494Sobrien	 */
8138494Sobrien	assertMakeFile("d1/file2", 0644, "d1/file2");
8238494Sobrien	assertMakeFile("file3", 0644, "file3");
8338494Sobrien	assertMakeDir("d2", 0755);
8438494Sobrien	assertMakeFile("d2/file4", 0644, "d2/file4");
8538494Sobrien	assertEqualInt(0,
8638494Sobrien	    systemf("%s -cnf partial-archive.tar d1 d1/file1 d1/file2 file3 "
8782794Sobrien	    "d2/file4 >c.out 2>c.err", testprog));
8882794Sobrien
89310490Scy	/* Test 3: -t without other options */
9038494Sobrien	assertEqualInt(0,
9138494Sobrien	    systemf("%s -tf partial-archive.tar >test3.out 2>test3.err",
9238494Sobrien	    testprog));
9338494Sobrien	assertEmptyFile("test3.err");
9438494Sobrien	assertTextFileContents("d1/\n"
9538494Sobrien			      "d1/file1\n"
9638494Sobrien			      "d1/file2\n"
9738494Sobrien			      "file3\n"
9838494Sobrien			      "d2/file4\n",
9941142Sobrien			      "test3.out");
10041142Sobrien
10141142Sobrien	/* Test 4: -t without -n and some entries selected */
102310490Scy	assertEqualInt(0,
10338494Sobrien	    systemf("%s -tf partial-archive.tar d1 file3 d2/file4 "
10438494Sobrien	    ">test4.out 2>test4.err", testprog));
10538494Sobrien	assertEmptyFile("test4.err");
10638494Sobrien	assertTextFileContents("d1/\n"
107310490Scy			      "d1/file1\n"
108310490Scy			      "d1/file2\n"
10938494Sobrien			      "file3\n"
110310490Scy			      "d2/file4\n",
11138494Sobrien			      "test4.out");
11238494Sobrien
11338494Sobrien	/* Test 5: -t with -n and some entries selected */
11438494Sobrien	assertEqualInt(0,
11538494Sobrien	    systemf("%s -tnf partial-archive.tar d1 file3 d2/file4 "
11638494Sobrien	    ">test5.out 2>test5.err", testprog));
11738494Sobrien	assertEmptyFile("test5.err");
11838494Sobrien	assertTextFileContents("d1/\n"
11938494Sobrien			      "file3\n"
12051292Sobrien			      "d2/file4\n",
12151292Sobrien			      "test5.out");
12251292Sobrien
12351292Sobrien	/* Test 6: -t without -n and non-existant directory selected */
12451292Sobrien	assertEqualInt(0,
12551292Sobrien	    systemf("%s -tf partial-archive.tar d2 >test6.out 2>test6.err",
12651292Sobrien	    testprog));
12751292Sobrien	assertEmptyFile("test6.err");
12851292Sobrien	assertTextFileContents("d2/file4\n",
12982794Sobrien			      "test6.out");
13082794Sobrien
13182794Sobrien	/* Test 7: -t with -n and non-existant directory selected */
13282794Sobrien	status = systemf("%s -tnf partial-archive.tar d2 "
13382794Sobrien	">test7.out 2>test7.err", testprog);
13482794Sobrien	assert(status);
135310490Scy	assert(status != -1);
13638494Sobrien#if !defined(_WIN32) || defined(__CYGWIN__)
13738494Sobrien	assert(WIFEXITED(status));
13838494Sobrien	assertEqualInt(1, WEXITSTATUS(status));
139310490Scy#endif
14038494Sobrien	assertNonEmptyFile("test7.err");
141310490Scy	assertEmptyFile("test7.out");
14238494Sobrien}
14338494Sobrien