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;
38 char *buff, *file_buff;
42 buff = malloc(BUFF_SIZE);
43 file_buff = malloc(FILE_BUFF_SIZE);
45 /* Force the umask to something predictable. */
48 /* Create a new archive in memory containing various types of entries. */
49 assert((a = archive_write_new()) != NULL);
50 assertA(0 == archive_write_set_format_ustar(a));
51 assertA(0 == archive_write_set_compression_none(a));
52 assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
53 /* A directory to be restored with EXTRACT_PERM. */
54 assert((ae = archive_entry_new()) != NULL);
55 archive_entry_copy_pathname(ae, "dir_0775");
56 archive_entry_set_mode(ae, S_IFDIR | 0775);
57 assertA(0 == archive_write_header(a, ae));
58 archive_entry_free(ae);
60 assert((ae = archive_entry_new()) != NULL);
61 archive_entry_copy_pathname(ae, "file");
62 archive_entry_set_mode(ae, S_IFREG | 0755);
63 for (i = 0; i < FILE_BUFF_SIZE; i++)
64 file_buff[i] = (unsigned char)rand();
65 archive_entry_set_size(ae, FILE_BUFF_SIZE);
66 assertA(0 == archive_write_header(a, ae));
67 assertA(FILE_BUFF_SIZE == archive_write_data(a, file_buff, FILE_BUFF_SIZE));
68 archive_entry_free(ae);
69 /* A directory that should obey umask when restored. */
70 assert((ae = archive_entry_new()) != NULL);
71 archive_entry_copy_pathname(ae, "dir");
72 archive_entry_set_mode(ae, S_IFDIR | 0777);
73 assertA(0 == archive_write_header(a, ae));
74 archive_entry_free(ae);
75 /* A file in the directory. */
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 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 assert((ae = archive_entry_new()) != NULL);
89 archive_entry_copy_pathname(ae, "dir3/.");
90 archive_entry_set_mode(ae, S_IFDIR | 0710);
91 assertA(0 == archive_write_header(a, ae));
92 archive_entry_free(ae);
93 /* Multiple dirs with a single entry. */
94 assert((ae = archive_entry_new()) != NULL);
95 archive_entry_copy_pathname(ae, "dir4/a/../b/../c/");
96 archive_entry_set_mode(ae, S_IFDIR | 0711);
97 assertA(0 == archive_write_header(a, ae));
98 archive_entry_free(ae);
100 assert((ae = archive_entry_new()) != NULL);
101 archive_entry_copy_pathname(ae, "symlink");
102 archive_entry_set_mode(ae, S_IFLNK | 0755);
103 archive_entry_set_symlink(ae, "file");
104 assertA(0 == archive_write_header(a, ae));
105 archive_entry_free(ae);
106 /* Close out the archive. */
107 assertA(0 == archive_write_close(a));
108 #if ARCHIVE_VERSION_NUMBER < 2000000
109 archive_write_finish(a);
111 assertA(0 == archive_write_finish(a));
114 /* Extract the entries to disk. */
115 assert((a = archive_read_new()) != NULL);
116 assertA(0 == archive_read_support_format_all(a));
117 assertA(0 == archive_read_support_compression_all(a));
118 assertA(0 == archive_read_open_memory(a, buff, BUFF_SIZE));
119 /* Restore first entry with _EXTRACT_PERM. */
120 failure("Error reading first entry", i);
121 assertA(0 == archive_read_next_header(a, &ae));
122 assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM));
123 /* Rest of entries get restored with no flags. */
124 for (i = 0; i < 7; i++) {
125 failure("Error reading entry %d", i+1);
126 assertA(0 == archive_read_next_header(a, &ae));
127 assertA(0 == archive_read_extract(a, ae, 0));
129 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
130 assert(0 == archive_read_close(a));
131 #if ARCHIVE_VERSION_NUMBER < 2000000
132 archive_read_finish(a);
134 assert(0 == archive_read_finish(a));
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 assert(0 == stat("dir_0775", &st));
143 failure("This was 0775 in archive, and should be 0775 on disk");
144 assertEqualInt(st.st_mode, S_IFDIR | 0775);
145 /* Everything else was extracted without ARCHIVE_EXTRACT_PERM,
146 * so there may be some sloppiness about gid bits on directories. */
147 assert(0 == stat("file", &st));
148 failure("st.st_mode=%o should be %o", st.st_mode, S_IFREG | 0755);
149 assertEqualInt(st.st_mode, S_IFREG | 0755);
150 failure("The file extracted to disk is the wrong size.");
151 assert(st.st_size == FILE_BUFF_SIZE);
152 fd = open("file", O_RDONLY);
153 failure("The file on disk could not be opened.");
155 bytes_read = read(fd, buff, FILE_BUFF_SIZE);
156 failure("The file contents read from disk are the wrong size");
157 assert(bytes_read == FILE_BUFF_SIZE);
158 failure("The file contents on disk do not match the file contents that were put into the archive.");
159 assert(memcmp(buff, file_buff, FILE_BUFF_SIZE) == 0);
160 assert(0 == stat("dir", &st));
161 failure("This was 0777 in archive, but umask should make it 0755");
162 /* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit
163 * when checking dir modes, as some systems inherit sgid bit
164 * from the parent dir. */
165 assertEqualInt(0755, st.st_mode & 0777);
166 assert(0 == stat("dir/file", &st));
167 assert(st.st_mode == (S_IFREG | 0700));
168 assert(0 == stat("dir2", &st));
169 assertEqualInt(0755, st.st_mode & 0777);
170 assert(0 == stat("dir2/file", &st));
171 assert(st.st_mode == (S_IFREG | 0000));
172 assert(0 == stat("dir3", &st));
173 assertEqualInt(0710, st.st_mode & 0777);
174 assert(0 == stat("dir4", &st));
175 assertEqualInt(0755, st.st_mode & 0777);
176 assert(0 == stat("dir4/a", &st));
177 assertEqualInt(0755, st.st_mode & 0777);
178 assert(0 == stat("dir4/b", &st));
179 assertEqualInt(0755, st.st_mode & 0777);
180 assert(0 == stat("dir4/c", &st));
181 assertEqualInt(0711, st.st_mode & 0777);
182 assert(0 == lstat("symlink", &st));
183 assert(S_ISLNK(st.st_mode));
185 /* Systems that lack lchmod() can't set symlink perms, so skip this. */
186 assert((st.st_mode & 07777) == 0755);
188 assert(0 == stat("symlink", &st));
189 assert(st.st_mode == (S_IFREG | 0755));