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 #endif /* _WIN32 && !__CYGWIN__ */
237 
DEFINE_TEST(test_write_disk)238 DEFINE_TEST(test_write_disk)
239 {
240 	struct archive_entry *ae;
241 #if defined(_WIN32) && !defined(__CYGWIN__)
242 	wchar_t *fullpath;
243 	DWORD l;
244 #endif
245 
246 	/* Force the umask to something predictable. */
247 	assertUmask(UMASK);
248 
249 	/* A regular file. */
250 	assert((ae = archive_entry_new()) != NULL);
251 	archive_entry_copy_pathname(ae, "file");
252 	archive_entry_set_mode(ae, S_IFREG | 0755);
253 	create_reg_file(ae, "Test creating a regular file");
254 	archive_entry_free(ae);
255 
256 	/* Another regular file. */
257 	assert((ae = archive_entry_new()) != NULL);
258 	archive_entry_copy_pathname(ae, "file2");
259 	archive_entry_set_mode(ae, S_IFREG | 0755);
260 	create_reg_file2(ae, "Test creating another regular file");
261 	archive_entry_free(ae);
262 
263 	/* A regular file with a size restriction */
264 	assert((ae = archive_entry_new()) != NULL);
265 	archive_entry_copy_pathname(ae, "file3");
266 	archive_entry_set_mode(ae, S_IFREG | 0755);
267 	create_reg_file3(ae, "Regular file with size restriction");
268 	archive_entry_free(ae);
269 
270 	/* A regular file with an unspecified size */
271 	assert((ae = archive_entry_new()) != NULL);
272 	archive_entry_copy_pathname(ae, "file3");
273 	archive_entry_set_mode(ae, S_IFREG | 0755);
274 	create_reg_file4(ae, "Regular file with unspecified size");
275 	archive_entry_free(ae);
276 
277 	/* A regular file over an existing file */
278 	assert((ae = archive_entry_new()) != NULL);
279 	archive_entry_copy_pathname(ae, "file");
280 	archive_entry_set_mode(ae, S_IFREG | 0724);
281 	create(ae, "Test creating a file over an existing file.");
282 	archive_entry_free(ae);
283 
284 	/* A directory. */
285 	assert((ae = archive_entry_new()) != NULL);
286 	archive_entry_copy_pathname(ae, "dir");
287 	archive_entry_set_mode(ae, S_IFDIR | 0555);
288 	create(ae, "Test creating a regular dir.");
289 	archive_entry_free(ae);
290 
291 	/* A directory over an existing file. */
292 	assert((ae = archive_entry_new()) != NULL);
293 	archive_entry_copy_pathname(ae, "file");
294 	archive_entry_set_mode(ae, S_IFDIR | 0742);
295 	create(ae, "Test creating a dir over an existing file.");
296 	archive_entry_free(ae);
297 
298 	/* A file over an existing dir. */
299 	assert((ae = archive_entry_new()) != NULL);
300 	archive_entry_copy_pathname(ae, "file");
301 	archive_entry_set_mode(ae, S_IFREG | 0744);
302 	create(ae, "Test creating a file over an existing dir.");
303 	archive_entry_free(ae);
304 
305 #if defined(_WIN32) && !defined(__CYGWIN__)
306 	/* A file with unusable characters in its file name. */
307 	assert((ae = archive_entry_new()) != NULL);
308 	archive_entry_copy_pathname_w(ae, L"f:i*l?e\"f<i>l|e");
309 	archive_entry_set_mode(ae, S_IFREG | 0755);
310 	create_reg_file_win(ae, "Test creating a regular file"
311 	    " with unusable characters in its file name");
312 	archive_entry_free(ae);
313 
314 	/* A file with unusable characters in its directory name. */
315 	assert((ae = archive_entry_new()) != NULL);
316 	archive_entry_copy_pathname_w(ae, L"d:i*r?e\"c<t>o|ry/file1");
317 	archive_entry_set_mode(ae, S_IFREG | 0755);
318 	create_reg_file_win(ae, "Test creating a regular file"
319 	    " with unusable characters in its file name");
320 	archive_entry_free(ae);
321 
322 	/* A full-path file with unusable characters in its file name. */
323 	assert((l = GetCurrentDirectoryW(0, NULL)) != 0);
324 	assert((fullpath = malloc((l + 20) * sizeof(wchar_t))) != NULL);
325 	assert((l = GetCurrentDirectoryW(l, fullpath)) != 0);
326 	wcscat(fullpath, L"\\f:i*l?e\"f<i>l|e");
327 	assert((ae = archive_entry_new()) != NULL);
328 	archive_entry_copy_pathname_w(ae, fullpath);
329 	archive_entry_set_mode(ae, S_IFREG | 0755);
330 	create_reg_file_win(ae, "Test creating a regular file"
331 	    " with unusable characters in its file name");
332 	archive_entry_free(ae);
333 	free(fullpath);
334 
335 	/* A full-path file with unusable characters in its directory name. */
336 	assert((l = GetCurrentDirectoryW(0, NULL)) != 0);
337 	assert((fullpath = malloc((l + 30) * sizeof(wchar_t))) != NULL);
338 	assert((l = GetCurrentDirectoryW(l, fullpath)) != 0);
339 	wcscat(fullpath, L"\\d:i*r?e\"c<t>o|ry/file1");
340 	assert((ae = archive_entry_new()) != NULL);
341 	archive_entry_copy_pathname_w(ae, fullpath);
342 	archive_entry_set_mode(ae, S_IFREG | 0755);
343 	create_reg_file_win(ae, "Test creating a regular file"
344 	    " with unusable characters in its file name");
345 	archive_entry_free(ae);
346 	free(fullpath);
347 #endif /* _WIN32 && !__CYGWIN__ */
348 }
349