xref: /freebsd/contrib/libarchive/tar/test/test_basic.c (revision 580744621f33383027108364dcadad718df46ffe)
1 /*-
2  * Copyright (c) 2003-2007 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 
28 static const char *
29 make_files(void)
30 {
31 	FILE *f;
32 
33 	/* File with 10 bytes content. */
34 	f = fopen("file", "wb");
35 	assert(f != NULL);
36 	assertEqualInt(10, fwrite("123456789", 1, 10, f));
37 	fclose(f);
38 
39 	/* hardlink to above file. */
40 	assertMakeHardlink("linkfile", "file");
41 	assertIsHardlink("file", "linkfile");
42 
43 	/* Symlink to above file. */
44 	if (canSymlink())
45 		assertMakeSymlink("symlink", "file", 0);
46 
47 	/* Directory. */
48 	assertMakeDir("dir", 0775);
49 
50 	return canSymlink()
51 	    ? "file linkfile symlink dir"
52 	    : "file linkfile dir";
53 }
54 
55 static void
56 verify_files(const char *target)
57 {
58 	assertChdir(target);
59 
60 	/* Regular file with 2 links. */
61 	failure("%s", target);
62 	assertIsReg("file", -1);
63 	failure("%s", target);
64 	assertFileSize("file", 10);
65 	failure("%s", target);
66 	assertFileContents("123456789", 10, "file");
67 	failure("%s", target);
68 	assertFileNLinks("file", 2);
69 
70 	/* Another name for the same file. */
71 	failure("%s", target);
72 	assertIsReg("linkfile", -1);
73 	failure("%s", target);
74 	assertFileSize("linkfile", 10);
75 	assertFileContents("123456789", 10, "linkfile");
76 	assertFileNLinks("linkfile", 2);
77 	assertIsHardlink("file", "linkfile");
78 
79 	/* Symlink */
80 	if (canSymlink())
81 		assertIsSymlink("symlink", "file", 0);
82 
83 	/* dir */
84 	failure("%s", target);
85 	assertIsDir("dir", 0775);
86 	assertChdir("..");
87 }
88 
89 static void
90 run_tar(const char *target, const char *pack_options,
91     const char *unpack_options, const char *flist)
92 {
93 	int r;
94 
95 	assertMakeDir(target, 0775);
96 
97 	/* Use the tar program to create an archive. */
98 	r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
99 	failure("Error invoking %s cf -%s", testprog, pack_options);
100 	assertEqualInt(r, 0);
101 
102 	assertChdir(target);
103 
104 	/* Verify that nothing went to stderr. */
105 	assertEmptyFile("pack.err");
106 
107 	/*
108 	 * Use tar to unpack the archive into another directory.
109 	 */
110 	r = systemf("%s xf archive %s >unpack.out 2>unpack.err",
111 	    testprog, unpack_options);
112 	failure("Error invoking %s xf archive %s", testprog, unpack_options);
113 	assertEqualInt(r, 0);
114 
115 	/* Verify that nothing went to stderr. */
116 	assertEmptyFile("unpack.err");
117 	assertChdir("..");
118 }
119 
120 DEFINE_TEST(test_basic)
121 {
122 	const char *flist;
123 
124 	assertUmask(0);
125 	flist = make_files();
126 	/* Archive/dearchive with a variety of options. */
127 	run_tar("copy", "", "", flist);
128 	verify_files("copy");
129 
130 	run_tar("copy_ustar", "--format=ustar", "", flist);
131 	verify_files("copy_ustar");
132 
133 	/* tar doesn't handle cpio symlinks correctly */
134 	/* run_tar("copy_odc", "--format=odc", ""); */
135 }
136