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