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 UMASK 022
28 /*
29 * When comparing mode values, ignore high-order bits
30 * that are set on some OSes. This should cover the bits
31 * we're interested in (standard mode bits + file type bits)
32 * while ignoring extra markers such as Haiku/BeOS index
33 * flags.
34 */
35 #define MODE_MASK 0777777
36
create(struct archive_entry * ae,const char * msg)37 static void create(struct archive_entry *ae, const char *msg)
38 {
39 struct archive *ad;
40 struct stat st;
41
42 /* Write the entry to disk. */
43 assert((ad = archive_write_disk_new()) != NULL);
44 failure("%s", msg);
45 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
46 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
47 assertEqualInt(0, archive_write_free(ad));
48
49 /* Test the entries on disk. */
50 assert(0 == stat(archive_entry_pathname(ae), &st));
51 failure("%s", msg);
52
53 #if !defined(_WIN32) || defined(__CYGWIN__)
54 /* When verifying a dir, ignore the S_ISGID bit, as some systems set
55 * that automatically. */
56 if (archive_entry_filetype(ae) == AE_IFDIR)
57 st.st_mode &= ~S_ISGID;
58 assertEqualInt(st.st_mode & MODE_MASK,
59 archive_entry_mode(ae) & ~UMASK & MODE_MASK);
60 #endif
61 }
62
create_reg_file(struct archive_entry * ae,const char * msg)63 static void create_reg_file(struct archive_entry *ae, const char *msg)
64 {
65 static const char data[]="abcdefghijklmnopqrstuvwxyz";
66 struct archive *ad;
67
68 /* Write the entry to disk. */
69 assert((ad = archive_write_disk_new()) != NULL);
70 archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_TIME);
71 failure("%s", msg);
72 /*
73 * A touchy API design issue: archive_write_data() does (as of
74 * 2.4.12) enforce the entry size as a limit on the data
75 * written to the file. This was not enforced prior to
76 * 2.4.12. The change was prompted by the refined
77 * hardlink-restore semantics introduced at that time. In
78 * short, libarchive needs to know whether a "hardlink entry"
79 * is going to overwrite the contents so that it can know
80 * whether or not to open the file for writing. This implies
81 * that there is a fundamental semantic difference between an
82 * entry with a zero size and one with a non-zero size in the
83 * case of hardlinks and treating the hardlink case
84 * differently from the regular file case is just asking for
85 * trouble. So, a zero size must always mean that no data
86 * will be accepted, which is consistent with the file size in
87 * the entry being a maximum size.
88 */
89 archive_entry_set_size(ae, sizeof(data));
90 archive_entry_set_mtime(ae, 123456789, 0);
91 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
92 assertEqualInt(sizeof(data), archive_write_data(ad, data, sizeof(data)));
93 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
94 assertEqualInt(0, archive_write_free(ad));
95
96 /* Test the entries on disk. */
97 assertIsReg(archive_entry_pathname(ae), archive_entry_mode(ae) & 0777);
98 assertFileSize(archive_entry_pathname(ae), sizeof(data));
99 /* test_write_disk_times has more detailed tests of this area. */
100 assertFileMtime(archive_entry_pathname(ae), 123456789, 0);
101 failure("No atime given, so atime should get set to current time");
102 assertFileAtimeRecent(archive_entry_pathname(ae));
103 }
104
create_reg_file2(struct archive_entry * ae,const char * msg)105 static void create_reg_file2(struct archive_entry *ae, const char *msg)
106 {
107 const int datasize = 100000;
108 char *data;
109 struct archive *ad;
110 int i;
111
112 data = malloc(datasize);
113 for (i = 0; i < datasize; i++)
114 data[i] = (char)(i % 256);
115
116 /* Write the entry to disk. */
117 assert((ad = archive_write_disk_new()) != NULL);
118 failure("%s", msg);
119 /*
120 * See above for an explanation why this next call
121 * is necessary.
122 */
123 archive_entry_set_size(ae, datasize);
124 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
125 for (i = 0; i < datasize - 999; i += 1000) {
126 assertEqualIntA(ad, ARCHIVE_OK,
127 archive_write_data_block(ad, data + i, 1000, i));
128 }
129 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
130 assertEqualInt(0, archive_write_free(ad));
131
132 /* Test the entries on disk. */
133 assertIsReg(archive_entry_pathname(ae), archive_entry_mode(ae) & 0777);
134 assertFileSize(archive_entry_pathname(ae), i);
135 assertFileContents(data, datasize, archive_entry_pathname(ae));
136 free(data);
137 }
138
create_reg_file3(struct archive_entry * ae,const char * msg)139 static void create_reg_file3(struct archive_entry *ae, const char *msg)
140 {
141 static const char data[]="abcdefghijklmnopqrstuvwxyz";
142 struct archive *ad;
143 struct stat st;
144
145 /* Write the entry to disk. */
146 assert((ad = archive_write_disk_new()) != NULL);
147 failure("%s", msg);
148 /* Set the size smaller than the data and verify the truncation. */
149 archive_entry_set_size(ae, 5);
150 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
151 assertEqualInt(5, archive_write_data(ad, data, sizeof(data)));
152 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
153 assertEqualInt(0, archive_write_free(ad));
154
155 /* Test the entry on disk. */
156 assert(0 == stat(archive_entry_pathname(ae), &st));
157 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
158 st.st_mode, archive_entry_mode(ae));
159 #if !defined(_WIN32) || defined(__CYGWIN__)
160 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
161 #endif
162 assertEqualInt(st.st_size, 5);
163 }
164
165
create_reg_file4(struct archive_entry * ae,const char * msg)166 static void create_reg_file4(struct archive_entry *ae, const char *msg)
167 {
168 static const char data[]="abcdefghijklmnopqrstuvwxyz";
169 struct archive *ad;
170 struct stat st;
171
172 /* Write the entry to disk. */
173 assert((ad = archive_write_disk_new()) != NULL);
174 /* Leave the size unset. The data should not be truncated. */
175 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
176 assertEqualInt(ARCHIVE_OK,
177 archive_write_data_block(ad, data, sizeof(data), 0));
178 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
179 assertEqualInt(0, archive_write_free(ad));
180
181 /* Test the entry on disk. */
182 assert(0 == stat(archive_entry_pathname(ae), &st));
183 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
184 st.st_mode, archive_entry_mode(ae));
185 #if !defined(_WIN32) || defined(__CYGWIN__)
186 assertEqualInt(st.st_mode, (archive_entry_mode(ae) & ~UMASK));
187 #endif
188 failure("%s", msg);
189 assertEqualInt(st.st_size, sizeof(data));
190 }
191
192 #if defined(_WIN32) && !defined(__CYGWIN__)
create_reg_file_win(struct archive_entry * ae,const char * msg)193 static void create_reg_file_win(struct archive_entry *ae, const char *msg)
194 {
195 static const char data[]="abcdefghijklmnopqrstuvwxyz";
196 struct archive *ad;
197 struct _stat st;
198 wchar_t *p, *fname;
199 size_t l;
200
201 /* Write the entry to disk. */
202 assert((ad = archive_write_disk_new()) != NULL);
203 archive_write_disk_set_options(ad, ARCHIVE_EXTRACT_TIME);
204 failure("%s", msg);
205 archive_entry_set_size(ae, sizeof(data));
206 archive_entry_set_mtime(ae, 123456789, 0);
207 assertEqualIntA(ad, 0, archive_write_header(ad, ae));
208 assertEqualInt(sizeof(data), archive_write_data(ad, data, sizeof(data)));
209 assertEqualIntA(ad, 0, archive_write_finish_entry(ad));
210 assertEqualInt(0, archive_write_free(ad));
211
212 /* Test the entries on disk. */
213 l = wcslen(archive_entry_pathname_w(ae));
214 fname = malloc((l + 1) * sizeof(wchar_t));
215 assert(NULL != fname);
216 wcscpy(fname, archive_entry_pathname_w(ae));
217 p = fname;
218 /* Skip leading drive letter from archives created
219 * on Windows. */
220 if (((p[0] >= L'a' && p[0] <= L'z') ||
221 (p[0] >= L'A' && p[0] <= L'Z')) &&
222 p[1] == L':' && p[2] == L'\\') {
223 p += 3;
224 }
225 /* Replace unusable characters in Windows to '_' */
226 for (; *p != L'\0'; p++)
227 if (*p == L':' || *p == L'*' || *p == L'?' ||
228 *p == L'"' || *p == L'<' || *p == L'>' || *p == L'|')
229 *p = '_';
230 assert(0 == _wstat(fname, &st));
231 failure("st.st_mode=%o archive_entry_mode(ae)=%o",
232 st.st_mode, archive_entry_mode(ae));
233 assertEqualInt(st.st_size, sizeof(data));
234 free(fname);
235 }
236 #else
create_fail(struct archive_entry * ae,int experr,const char * msg)237 static void create_fail(struct archive_entry *ae, int experr,
238 const char *msg)
239 {
240 struct archive *ad;
241 struct stat st;
242
243 /* Write the entry to disk. */
244 assert((ad = archive_write_disk_new()) != NULL);
245 failure("%s", msg);
246 assertEqualIntA(ad, ARCHIVE_FAILED, archive_write_header(ad, ae));
247 assertEqualIntA(ad, experr, archive_errno(ad));
248 assertEqualInt(0, archive_write_free(ad));
249 assertEqualInt(-1, stat(archive_entry_pathname(ae), &st));
250 assertEqualInt(ENOENT, errno);
251 }
252 #endif /* _WIN32 && !__CYGWIN__ */
253
DEFINE_TEST(test_write_disk)254 DEFINE_TEST(test_write_disk)
255 {
256 struct archive_entry *ae;
257 #if defined(_WIN32) && !defined(__CYGWIN__)
258 wchar_t *fullpath;
259 DWORD l;
260 #endif
261
262 /* Force the umask to something predictable. */
263 assertUmask(UMASK);
264
265 /* A regular file. */
266 assert((ae = archive_entry_new()) != NULL);
267 archive_entry_copy_pathname(ae, "file");
268 archive_entry_set_mode(ae, S_IFREG | 0755);
269 create_reg_file(ae, "Test creating a regular file");
270 archive_entry_free(ae);
271
272 /* Another regular file. */
273 assert((ae = archive_entry_new()) != NULL);
274 archive_entry_copy_pathname(ae, "file2");
275 archive_entry_set_mode(ae, S_IFREG | 0755);
276 create_reg_file2(ae, "Test creating another regular file");
277 archive_entry_free(ae);
278
279 /* A regular file with a size restriction */
280 assert((ae = archive_entry_new()) != NULL);
281 archive_entry_copy_pathname(ae, "file3");
282 archive_entry_set_mode(ae, S_IFREG | 0755);
283 create_reg_file3(ae, "Regular file with size restriction");
284 archive_entry_free(ae);
285
286 /* A regular file with an unspecified size */
287 assert((ae = archive_entry_new()) != NULL);
288 archive_entry_copy_pathname(ae, "file3");
289 archive_entry_set_mode(ae, S_IFREG | 0755);
290 create_reg_file4(ae, "Regular file with unspecified size");
291 archive_entry_free(ae);
292
293 /* A regular file over an existing file */
294 assert((ae = archive_entry_new()) != NULL);
295 archive_entry_copy_pathname(ae, "file");
296 archive_entry_set_mode(ae, S_IFREG | 0724);
297 create(ae, "Test creating a file over an existing file.");
298 archive_entry_free(ae);
299
300 /* A directory. */
301 assert((ae = archive_entry_new()) != NULL);
302 archive_entry_copy_pathname(ae, "dir");
303 archive_entry_set_mode(ae, S_IFDIR | 0555);
304 create(ae, "Test creating a regular dir.");
305 archive_entry_free(ae);
306
307 /* A directory over an existing file. */
308 assert((ae = archive_entry_new()) != NULL);
309 archive_entry_copy_pathname(ae, "file");
310 archive_entry_set_mode(ae, S_IFDIR | 0742);
311 create(ae, "Test creating a dir over an existing file.");
312 archive_entry_free(ae);
313
314 /* A file over an existing dir. */
315 assert((ae = archive_entry_new()) != NULL);
316 archive_entry_copy_pathname(ae, "file");
317 archive_entry_set_mode(ae, S_IFREG | 0744);
318 create(ae, "Test creating a file over an existing dir.");
319 archive_entry_free(ae);
320
321 #if defined(_WIN32) && !defined(__CYGWIN__)
322 /* A file with unusable characters in its file name. */
323 assert((ae = archive_entry_new()) != NULL);
324 archive_entry_copy_pathname_w(ae, L"f:i*l?e\"f<i>l|e");
325 archive_entry_set_mode(ae, S_IFREG | 0755);
326 create_reg_file_win(ae, "Test creating a regular file"
327 " with unusable characters in its file name");
328 archive_entry_free(ae);
329
330 /* A file with unusable characters in its directory name. */
331 assert((ae = archive_entry_new()) != NULL);
332 archive_entry_copy_pathname_w(ae, L"d:i*r?e\"c<t>o|ry/file1");
333 archive_entry_set_mode(ae, S_IFREG | 0755);
334 create_reg_file_win(ae, "Test creating a regular file"
335 " with unusable characters in its file name");
336 archive_entry_free(ae);
337
338 /* A full-path file with unusable characters in its file name. */
339 assert((l = GetCurrentDirectoryW(0, NULL)) != 0);
340 assert((fullpath = malloc((l + 20) * sizeof(wchar_t))) != NULL);
341 assert((l = GetCurrentDirectoryW(l, fullpath)) != 0);
342 wcscat(fullpath, L"\\f:i*l?e\"f<i>l|e");
343 assert((ae = archive_entry_new()) != NULL);
344 archive_entry_copy_pathname_w(ae, fullpath);
345 archive_entry_set_mode(ae, S_IFREG | 0755);
346 create_reg_file_win(ae, "Test creating a regular file"
347 " with unusable characters in its file name");
348 archive_entry_free(ae);
349 free(fullpath);
350
351 /* A full-path file with unusable characters in its directory name. */
352 assert((l = GetCurrentDirectoryW(0, NULL)) != 0);
353 assert((fullpath = malloc((l + 30) * sizeof(wchar_t))) != NULL);
354 assert((l = GetCurrentDirectoryW(l, fullpath)) != 0);
355 wcscat(fullpath, L"\\d:i*r?e\"c<t>o|ry/file1");
356 assert((ae = archive_entry_new()) != NULL);
357 archive_entry_copy_pathname_w(ae, fullpath);
358 archive_entry_set_mode(ae, S_IFREG | 0755);
359 create_reg_file_win(ae, "Test creating a regular file"
360 " with unusable characters in its file name");
361 archive_entry_free(ae);
362 free(fullpath);
363 #else /* !_WIN32 || __CYGWIN__ */
364 /* A directory with a /../ in the middle */
365 assert((ae = archive_entry_new()) != NULL);
366 archive_entry_copy_pathname(ae, "a/b/../b/file");
367 archive_entry_set_mode(ae, S_IFREG | 0644);
368 /* First attempt should fail with EACCES */
369 assertEqualInt(0, mkdir("a", 0111));
370 create_fail(ae, EACCES,
371 "Test failing to create parent directory with /../");
372 /* Now let it succeed */
373 assertEqualInt(0, chmod("a", 0755));
374 create(ae, "Test creating parent directory with /../");
375 archive_entry_free(ae);
376 #endif /* _WIN32 && !__CYGWIN__ */
377 }
378