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_API_VERSION > 1
109 assertA(0 == archive_write_finish(a));
111 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_API_VERSION > 1
132 assert(0 == archive_read_finish(a));
134 archive_read_finish(a);
137 /* Test the entries on disk. */
138 assert(0 == stat("dir_0775", &st));
139 failure("This was 0775 in archive, and should be 0775 on disk");
140 assert(st.st_mode == (S_IFDIR | 0775));
141 assert(0 == stat("file", &st));
142 failure("st.st_mode=%o should be %o", st.st_mode, S_IFREG | 0755);
143 assert(st.st_mode == (S_IFREG | 0755));
144 failure("The file extracted to disk is the wrong size.");
145 assert(st.st_size == FILE_BUFF_SIZE);
146 fd = open("file", O_RDONLY);
147 failure("The file on disk could not be opened.");
149 bytes_read = read(fd, buff, FILE_BUFF_SIZE);
150 failure("The file contents read from disk are the wrong size");
151 assert(bytes_read == FILE_BUFF_SIZE);
152 failure("The file contents on disk do not match the file contents that were put into the archive.");
153 assert(memcmp(buff, file_buff, FILE_BUFF_SIZE) == 0);
154 assert(0 == stat("dir", &st));
155 failure("This was 0777 in archive, but umask should make it 0755");
156 assert(st.st_mode == (S_IFDIR | 0755));
157 assert(0 == stat("dir/file", &st));
158 assert(st.st_mode == (S_IFREG | 0700));
159 assert(0 == stat("dir2", &st));
160 assert(st.st_mode == (S_IFDIR | 0755));
161 assert(0 == stat("dir2/file", &st));
162 assert(st.st_mode == (S_IFREG | 0000));
163 assert(0 == stat("dir3", &st));
164 assert(st.st_mode == (S_IFDIR | 0710));
165 assert(0 == stat("dir4", &st));
166 assert(st.st_mode == (S_IFDIR | 0755));
167 assert(0 == stat("dir4/a", &st));
168 assert(st.st_mode == (S_IFDIR | 0755));
169 assert(0 == stat("dir4/b", &st));
170 assert(st.st_mode == (S_IFDIR | 0755));
171 assert(0 == stat("dir4/c", &st));
172 assert(st.st_mode == (S_IFDIR | 0711));
173 assert(0 == lstat("symlink", &st));
174 assert(S_ISLNK(st.st_mode));
176 /* Systems that lack lchmod() can't set symlink perms, so skip this. */
177 assert((st.st_mode & 07777) == 0755);
179 assert(0 == stat("symlink", &st));
180 assert(st.st_mode == (S_IFREG | 0755));