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 #define BUFF_SIZE 1000000
29 #define FILE_BUFF_SIZE 100000
30 
31 DEFINE_TEST(test_read_extract)
32 {
33 	struct archive_entry *ae;
34 	struct archive *a;
35 	size_t used;
36 	int i, numEntries = 0;
37 	char *buff, *file_buff;
38 
39 	buff = malloc(BUFF_SIZE);
40 	file_buff = malloc(FILE_BUFF_SIZE);
41 
42 	/* Force the umask to something predictable. */
43 	assertUmask(022);
44 
45 	/* Create a new archive in memory containing various types of entries. */
46 	assert((a = archive_write_new()) != NULL);
47 	assertA(0 == archive_write_set_format_ustar(a));
48 	assertA(0 == archive_write_add_filter_none(a));
49 	assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
50 	/* A directory to be restored with EXTRACT_PERM. */
51 	++numEntries;
52 	assert((ae = archive_entry_new()) != NULL);
53 	archive_entry_copy_pathname(ae, "dir_0775");
54 	archive_entry_set_mode(ae, S_IFDIR | 0775);
55 	assertA(0 == archive_write_header(a, ae));
56 	archive_entry_free(ae);
57 	/* A regular file. */
58 	++numEntries;
59 	assert((ae = archive_entry_new()) != NULL);
60 	archive_entry_copy_pathname(ae, "file");
61 	archive_entry_set_mode(ae, S_IFREG | 0755);
62 	fill_with_pseudorandom_data(file_buff, FILE_BUFF_SIZE);
63 	archive_entry_set_size(ae, FILE_BUFF_SIZE);
64 	assertA(0 == archive_write_header(a, ae));
65 	assertA(FILE_BUFF_SIZE == archive_write_data(a, file_buff, FILE_BUFF_SIZE));
66 	archive_entry_free(ae);
67 	/* A directory that should obey umask when restored. */
68 	++numEntries;
69 	assert((ae = archive_entry_new()) != NULL);
70 	archive_entry_copy_pathname(ae, "dir");
71 	archive_entry_set_mode(ae, S_IFDIR | 0777);
72 	assertA(0 == archive_write_header(a, ae));
73 	archive_entry_free(ae);
74 	/* A file in the directory. */
75 	++numEntries;
76 	assert((ae = archive_entry_new()) != NULL);
77 	archive_entry_copy_pathname(ae, "dir/file");
78 	archive_entry_set_mode(ae, S_IFREG | 0700);
79 	assertA(0 == archive_write_header(a, ae));
80 	archive_entry_free(ae);
81 	/* A file in a dir that is not already in the archive. */
82 	++numEntries;
83 	assert((ae = archive_entry_new()) != NULL);
84 	archive_entry_copy_pathname(ae, "dir2/file");
85 	archive_entry_set_mode(ae, S_IFREG | 0000);
86 	assertA(0 == archive_write_header(a, ae));
87 	archive_entry_free(ae);
88 	/* A dir with a trailing /. */
89 	++numEntries;
90 	assert((ae = archive_entry_new()) != NULL);
91 	archive_entry_copy_pathname(ae, "dir3/.");
92 	archive_entry_set_mode(ae, S_IFDIR | 0710);
93 	assertA(0 == archive_write_header(a, ae));
94 	archive_entry_free(ae);
95 	/* Multiple dirs with a single entry. */
96 	++numEntries;
97 	assert((ae = archive_entry_new()) != NULL);
98 	archive_entry_copy_pathname(ae, "dir4/a/../b/../c/");
99 	archive_entry_set_mode(ae, S_IFDIR | 0711);
100 	assertA(0 == archive_write_header(a, ae));
101 	archive_entry_free(ae);
102 	/* A symlink. */
103 	if (canSymlink()) {
104 		++numEntries;
105 		assert((ae = archive_entry_new()) != NULL);
106 		archive_entry_copy_pathname(ae, "symlink");
107 		archive_entry_set_mode(ae, AE_IFLNK | 0755);
108 		archive_entry_set_symlink(ae, "file");
109 		assertA(0 == archive_write_header(a, ae));
110 		archive_entry_free(ae);
111 	}
112 	/* Close out the archive. */
113 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
114 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
115 
116 	/* Extract the entries to disk. */
117 	assert((a = archive_read_new()) != NULL);
118 	assertA(0 == archive_read_support_format_all(a));
119 	assertA(0 == archive_read_support_filter_all(a));
120 	assertA(0 == archive_read_open_memory(a, buff, BUFF_SIZE));
121 	/* Restore first entry with _EXTRACT_PERM. */
122 	failure("Error reading first entry");
123 	assertA(0 == archive_read_next_header(a, &ae));
124 	assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM));
125 	/* Rest of entries get restored with no flags. */
126 	for (i = 1; i < numEntries; i++) {
127 		failure("Error reading entry %d", i);
128 		assertA(0 == archive_read_next_header(a, &ae));
129 		failure("Failed to extract entry %d: %s", i,
130 			archive_entry_pathname(ae));
131 		assertA(0 == archive_read_extract(a, ae, 0));
132 	}
133 	assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
134 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
135 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
136 
137 	/* Test the entries on disk. */
138 	/* This first entry was extracted with ARCHIVE_EXTRACT_PERM,
139 	 * so the permissions should have been restored exactly,
140 	 * including resetting the gid bit on those platforms
141 	 * where gid is inherited by subdirs. */
142 	failure("This was 0775 in archive, and should be 0775 on disk");
143 	assertIsDir("dir_0775", 0775);
144 	/* Everything else was extracted without ARCHIVE_EXTRACT_PERM,
145 	 * so there may be some sloppiness about gid bits on directories. */
146 	assertIsReg("file", 0755);
147 	assertFileSize("file", FILE_BUFF_SIZE);
148 	assertFileContents(file_buff, FILE_BUFF_SIZE, "file");
149 	/* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit
150 	 * when checking dir modes, as some systems inherit sgid bit
151 	 * from the parent dir. */
152 	failure("This was 0777 in archive, but umask should make it 0755");
153 	assertIsDir("dir", 0755);
154 	assertIsReg("dir/file", 0700);
155 	assertIsDir("dir2", 0755);
156 	assertIsReg("dir2/file", 0000);
157 	assertIsDir("dir3", 0710);
158 	assertIsDir("dir4", 0755);
159 	assertIsDir("dir4/a", 0755);
160 	assertIsDir("dir4/b", 0755);
161 	assertIsDir("dir4/c", 0711);
162 	if (canSymlink())
163 		assertIsSymlink("symlink", "file", 0);
164 
165 	free(buff);
166 	free(file_buff);
167 }
168