2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
28 #define BUFF_SIZE 1000000
29 #define FILE_BUFF_SIZE 100000
31 DEFINE_TEST(test_read_extract)
33 struct archive_entry *ae;
35 #if !defined(_WIN32) || defined(__CYGWIN__)
40 char *buff, *file_buff;
41 #if !defined(_WIN32) || defined(__CYGWIN__)
46 buff = malloc(BUFF_SIZE);
47 file_buff = malloc(FILE_BUFF_SIZE);
49 /* Force the umask to something predictable. */
52 /* Create a new archive in memory containing various types of entries. */
53 assert((a = archive_write_new()) != NULL);
54 assertA(0 == archive_write_set_format_ustar(a));
55 assertA(0 == archive_write_set_compression_none(a));
56 assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
57 /* A directory to be restored with EXTRACT_PERM. */
58 assert((ae = archive_entry_new()) != NULL);
59 archive_entry_copy_pathname(ae, "dir_0775");
60 archive_entry_set_mode(ae, S_IFDIR | 0775);
61 assertA(0 == archive_write_header(a, ae));
62 archive_entry_free(ae);
64 assert((ae = archive_entry_new()) != NULL);
65 archive_entry_copy_pathname(ae, "file");
66 archive_entry_set_mode(ae, S_IFREG | 0755);
67 for (i = 0; i < FILE_BUFF_SIZE; i++)
68 file_buff[i] = (unsigned char)rand();
69 archive_entry_set_size(ae, FILE_BUFF_SIZE);
70 assertA(0 == archive_write_header(a, ae));
71 assertA(FILE_BUFF_SIZE == archive_write_data(a, file_buff, FILE_BUFF_SIZE));
72 archive_entry_free(ae);
73 /* A directory that should obey umask when restored. */
74 assert((ae = archive_entry_new()) != NULL);
75 archive_entry_copy_pathname(ae, "dir");
76 archive_entry_set_mode(ae, S_IFDIR | 0777);
77 assertA(0 == archive_write_header(a, ae));
78 archive_entry_free(ae);
79 /* A file in the directory. */
80 assert((ae = archive_entry_new()) != NULL);
81 archive_entry_copy_pathname(ae, "dir/file");
82 archive_entry_set_mode(ae, S_IFREG | 0700);
83 assertA(0 == archive_write_header(a, ae));
84 archive_entry_free(ae);
85 /* A file in a dir that is not already in the archive. */
86 assert((ae = archive_entry_new()) != NULL);
87 archive_entry_copy_pathname(ae, "dir2/file");
88 archive_entry_set_mode(ae, S_IFREG | 0000);
89 assertA(0 == archive_write_header(a, ae));
90 archive_entry_free(ae);
91 /* A dir with a trailing /. */
92 assert((ae = archive_entry_new()) != NULL);
93 archive_entry_copy_pathname(ae, "dir3/.");
94 archive_entry_set_mode(ae, S_IFDIR | 0710);
95 assertA(0 == archive_write_header(a, ae));
96 archive_entry_free(ae);
97 /* Multiple dirs with a single entry. */
98 assert((ae = archive_entry_new()) != NULL);
99 archive_entry_copy_pathname(ae, "dir4/a/../b/../c/");
100 archive_entry_set_mode(ae, S_IFDIR | 0711);
101 assertA(0 == archive_write_header(a, ae));
102 archive_entry_free(ae);
104 assert((ae = archive_entry_new()) != NULL);
105 archive_entry_copy_pathname(ae, "symlink");
106 archive_entry_set_mode(ae, S_IFLNK | 0755);
107 archive_entry_set_symlink(ae, "file");
108 assertA(0 == archive_write_header(a, ae));
109 archive_entry_free(ae);
110 /* Close out the archive. */
111 assertA(0 == archive_write_close(a));
112 #if ARCHIVE_VERSION_NUMBER < 2000000
113 archive_write_finish(a);
115 assertA(0 == archive_write_finish(a));
118 /* Extract the entries to disk. */
119 assert((a = archive_read_new()) != NULL);
120 assertA(0 == archive_read_support_format_all(a));
121 assertA(0 == archive_read_support_compression_all(a));
122 assertA(0 == archive_read_open_memory(a, buff, BUFF_SIZE));
123 /* Restore first entry with _EXTRACT_PERM. */
124 failure("Error reading first entry", i);
125 assertA(0 == archive_read_next_header(a, &ae));
126 assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM));
127 /* Rest of entries get restored with no flags. */
128 for (i = 0; i < 7; i++) {
129 failure("Error reading entry %d", i+1);
130 assertA(0 == archive_read_next_header(a, &ae));
131 assertA(0 == archive_read_extract(a, ae, 0));
133 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
134 assert(0 == archive_read_close(a));
135 #if ARCHIVE_VERSION_NUMBER < 2000000
136 archive_read_finish(a);
138 assert(0 == archive_read_finish(a));
141 #if !defined(_WIN32) || defined(__CYGWIN__)
142 /* Test the entries on disk. */
143 /* This first entry was extracted with ARCHIVE_EXTRACT_PERM,
144 * so the permissions should have been restored exactly,
145 * including resetting the gid bit on those platforms
146 * where gid is inherited by subdirs. */
147 assert(0 == stat("dir_0775", &st));
148 failure("This was 0775 in archive, and should be 0775 on disk");
149 assertEqualInt(st.st_mode, S_IFDIR | 0775);
150 /* Everything else was extracted without ARCHIVE_EXTRACT_PERM,
151 * so there may be some sloppiness about gid bits on directories. */
152 assert(0 == stat("file", &st));
153 failure("st.st_mode=%o should be %o", st.st_mode, S_IFREG | 0755);
154 assertEqualInt(st.st_mode, S_IFREG | 0755);
155 failure("The file extracted to disk is the wrong size.");
156 assert(st.st_size == FILE_BUFF_SIZE);
157 fd = open("file", O_RDONLY);
158 failure("The file on disk could not be opened.");
160 bytes_read = read(fd, buff, FILE_BUFF_SIZE);
162 failure("The file contents read from disk are the wrong size");
163 assert(bytes_read == FILE_BUFF_SIZE);
164 failure("The file contents on disk do not match the file contents that were put into the archive.");
165 assert(memcmp(buff, file_buff, FILE_BUFF_SIZE) == 0);
166 assert(0 == stat("dir", &st));
167 failure("This was 0777 in archive, but umask should make it 0755");
168 /* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit
169 * when checking dir modes, as some systems inherit sgid bit
170 * from the parent dir. */
171 assertEqualInt(0755, st.st_mode & 0777);
172 assert(0 == stat("dir/file", &st));
173 assert(st.st_mode == (S_IFREG | 0700));
174 assert(0 == stat("dir2", &st));
175 assertEqualInt(0755, st.st_mode & 0777);
176 assert(0 == stat("dir2/file", &st));
177 assert(st.st_mode == (S_IFREG | 0000));
178 assert(0 == stat("dir3", &st));
179 assertEqualInt(0710, st.st_mode & 0777);
180 assert(0 == stat("dir4", &st));
181 assertEqualInt(0755, st.st_mode & 0777);
182 assert(0 == stat("dir4/a", &st));
183 assertEqualInt(0755, st.st_mode & 0777);
184 assert(0 == stat("dir4/b", &st));
185 assertEqualInt(0755, st.st_mode & 0777);
186 assert(0 == stat("dir4/c", &st));
187 assertEqualInt(0711, st.st_mode & 0777);
188 assert(0 == lstat("symlink", &st));
189 assert(S_ISLNK(st.st_mode));
191 /* Systems that lack lchmod() can't set symlink perms, so skip this. */
192 assert((st.st_mode & 07777) == 0755);
194 assert(0 == stat("symlink", &st));
195 assert(st.st_mode == (S_IFREG | 0755));