xref: /freebsd/contrib/libarchive/libarchive/test/test_archive_match_time.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2012 Michihiro NAKAJIMA
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 
26 #include "test.h"
27 
28 #define parse_date archive_parse_date
29 
30 static void
test_newer_time(void)31 test_newer_time(void)
32 {
33 	struct archive_entry *ae;
34 	struct archive *m;
35 
36 	if (!assert((m = archive_match_new()) != NULL))
37 		return;
38 	if (!assert((ae = archive_entry_new()) != NULL)) {
39 		archive_match_free(m);
40 		return;
41 	}
42 
43 	assertEqualIntA(m, 0, archive_match_include_time(m,
44 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
45 	    ARCHIVE_MATCH_NEWER, 7880, 0));
46 
47 	archive_entry_copy_pathname(ae, "file1");
48 	archive_entry_set_mtime(ae, 7880, 0);
49 	archive_entry_set_ctime(ae, 7880, 0);
50 	failure("Both Its mtime and ctime should be excluded");
51 	assertEqualInt(1, archive_match_time_excluded(m, ae));
52 	assertEqualInt(1, archive_match_excluded(m, ae));
53 	archive_entry_set_mtime(ae, 7879, 999);
54 	archive_entry_set_ctime(ae, 7879, 999);
55 	failure("Both Its mtime and ctime should be excluded");
56 	assertEqualInt(1, archive_match_time_excluded(m, ae));
57 	assertEqualInt(1, archive_match_excluded(m, ae));
58 
59 	archive_entry_set_mtime(ae, 7881, 0);
60 	archive_entry_set_ctime(ae, 7881, 0);
61 	failure("Both Its mtime and ctime should not be excluded");
62 	assertEqualInt(0, archive_match_time_excluded(m, ae));
63 	assertEqualInt(0, archive_match_excluded(m, ae));
64 
65 	archive_entry_set_mtime(ae, 7880, 1);
66 	archive_entry_set_ctime(ae, 7880, 0);
67 	failure("Its mtime should be excluded");
68 	assertEqualInt(1, archive_match_time_excluded(m, ae));
69 	assertEqualInt(1, archive_match_excluded(m, ae));
70 
71 	archive_entry_set_mtime(ae, 7880, 0);
72 	archive_entry_set_ctime(ae, 7880, 1);
73 	failure("Its ctime should be excluded");
74 	assertEqualInt(1, archive_match_time_excluded(m, ae));
75 	assertEqualInt(1, archive_match_excluded(m, ae));
76 
77 	/* Clean up. */
78 	archive_entry_free(ae);
79 	archive_match_free(m);
80 }
81 
82 static void
test_newer_time_str(void)83 test_newer_time_str(void)
84 {
85 	struct archive_entry *ae;
86 	struct archive *m;
87 	time_t now, t;
88 
89 	if (!assert((m = archive_match_new()) != NULL))
90 		return;
91 	if (!assert((ae = archive_entry_new()) != NULL)) {
92 		archive_match_free(m);
93 		return;
94 	}
95 
96 	time(&now);
97 
98 	assertEqualIntA(m, 0, archive_match_include_date(m,
99 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
100 	    ARCHIVE_MATCH_NEWER, "1980/2/1 0:0:0 UTC"));
101 
102 	/* Test1: Allow newer time. */
103 	archive_entry_copy_pathname(ae, "file1");
104 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
105 	archive_entry_set_mtime(ae, t, 0);
106 	archive_entry_set_ctime(ae, t, 0);
107 	failure("Both Its mtime and ctime should be excluded");
108 	assertEqualInt(1, archive_match_time_excluded(m, ae));
109 	assertEqualInt(1, archive_match_excluded(m, ae));
110 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
111 	archive_entry_set_mtime(ae, t, 0);
112 	archive_entry_set_ctime(ae, t, 0);
113 	failure("Both Its mtime and ctime should be excluded");
114 	assertEqualInt(1, archive_match_time_excluded(m, ae));
115 	assertEqualInt(1, archive_match_excluded(m, ae));
116 
117 	t = parse_date(now, "1980/2/1 0:0:1 UTC");
118 	archive_entry_set_mtime(ae, t, 0);
119 	archive_entry_set_ctime(ae, t, 0);
120 	failure("Both Its mtime and ctime should not be excluded");
121 	assertEqualInt(0, archive_match_time_excluded(m, ae));
122 	assertEqualInt(0, archive_match_excluded(m, ae));
123 
124 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
125 	archive_entry_set_mtime(ae, t, 1);
126 	archive_entry_set_ctime(ae, t, 0);
127 	failure("Its mtime should be excluded");
128 	assertEqualInt(1, archive_match_time_excluded(m, ae));
129 	assertEqualInt(1, archive_match_excluded(m, ae));
130 
131 	archive_entry_set_mtime(ae, t, 0);
132 	archive_entry_set_ctime(ae, t, 1);
133 	failure("Its ctime should be excluded");
134 	assertEqualInt(1, archive_match_time_excluded(m, ae));
135 	assertEqualInt(1, archive_match_excluded(m, ae));
136 
137 
138 	/* Test2: Allow equal or newer time. */
139 	assertEqualIntA(m, 0, archive_match_include_date(m,
140 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
141 	    ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_EQUAL,
142 	    "1980/2/1 0:0:0 UTC"));
143 
144 	archive_entry_copy_pathname(ae, "file1");
145 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
146 	archive_entry_set_mtime(ae, t, 0);
147 	archive_entry_set_ctime(ae, t, 0);
148 	failure("Both Its mtime and ctime should not be excluded");
149 	assertEqualInt(0, archive_match_time_excluded(m, ae));
150 	assertEqualInt(0, archive_match_excluded(m, ae));
151 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
152 	archive_entry_set_mtime(ae, t, 0);
153 	archive_entry_set_ctime(ae, t, 0);
154 	failure("Both Its mtime and ctime should be excluded");
155 	assertEqualInt(1, archive_match_time_excluded(m, ae));
156 	assertEqualInt(1, archive_match_excluded(m, ae));
157 
158 	t = parse_date(now, "1980/2/1 0:0:1 UTC");
159 	archive_entry_set_mtime(ae, t, 0);
160 	archive_entry_set_ctime(ae, t, 0);
161 	failure("Both Its mtime and ctime should not be excluded");
162 	assertEqualInt(0, archive_match_time_excluded(m, ae));
163 	assertEqualInt(0, archive_match_excluded(m, ae));
164 
165 	/* Clean up. */
166 	archive_entry_free(ae);
167 	archive_match_free(m);
168 }
169 
170 static void
test_newer_time_str_w(void)171 test_newer_time_str_w(void)
172 {
173 	struct archive_entry *ae;
174 	struct archive *m;
175 	time_t now, t;
176 
177 	if (!assert((m = archive_match_new()) != NULL))
178 		return;
179 	if (!assert((ae = archive_entry_new()) != NULL)) {
180 		archive_match_free(m);
181 		return;
182 	}
183 
184 	time(&now);
185 
186 	assertEqualIntA(m, 0, archive_match_include_date_w(m,
187 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
188 	    ARCHIVE_MATCH_NEWER, L"1980/2/1 0:0:0 UTC"));
189 
190 	/* Test1: Allow newer time. */
191 	archive_entry_copy_pathname(ae, "file1");
192 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
193 	archive_entry_set_mtime(ae, t, 0);
194 	archive_entry_set_ctime(ae, t, 0);
195 	failure("Both Its mtime and ctime should be excluded");
196 	assertEqualInt(1, archive_match_time_excluded(m, ae));
197 	assertEqualInt(1, archive_match_excluded(m, ae));
198 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
199 	archive_entry_set_mtime(ae, t, 0);
200 	archive_entry_set_ctime(ae, t, 0);
201 	failure("Both Its mtime and ctime should be excluded");
202 	assertEqualInt(1, archive_match_time_excluded(m, ae));
203 	assertEqualInt(1, archive_match_excluded(m, ae));
204 
205 	t = parse_date(now, "1980/2/1 0:0:1 UTC");
206 	archive_entry_set_mtime(ae, t, 0);
207 	archive_entry_set_ctime(ae, t, 0);
208 	failure("Both Its mtime and ctime should not be excluded");
209 	assertEqualInt(0, archive_match_time_excluded(m, ae));
210 	assertEqualInt(0, archive_match_excluded(m, ae));
211 
212 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
213 	archive_entry_set_mtime(ae, t, 1);
214 	archive_entry_set_ctime(ae, t, 0);
215 	failure("Its mtime should be excluded");
216 	assertEqualInt(1, archive_match_time_excluded(m, ae));
217 	assertEqualInt(1, archive_match_excluded(m, ae));
218 
219 	archive_entry_set_mtime(ae, t, 0);
220 	archive_entry_set_ctime(ae, t, 1);
221 	failure("Its ctime should be excluded");
222 	assertEqualInt(1, archive_match_time_excluded(m, ae));
223 	assertEqualInt(1, archive_match_excluded(m, ae));
224 
225 
226 	/* Test2: Allow equal or newer time. */
227 	assertEqualIntA(m, 0, archive_match_include_date_w(m,
228 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
229 	    ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_EQUAL,
230 	    L"1980/2/1 0:0:0 UTC"));
231 
232 	archive_entry_copy_pathname(ae, "file1");
233 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
234 	archive_entry_set_mtime(ae, t, 0);
235 	archive_entry_set_ctime(ae, t, 0);
236 	failure("Both Its mtime and ctime should not be excluded");
237 	assertEqualInt(0, archive_match_time_excluded(m, ae));
238 	assertEqualInt(0, archive_match_excluded(m, ae));
239 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
240 	archive_entry_set_mtime(ae, t, 0);
241 	archive_entry_set_ctime(ae, t, 0);
242 	failure("Both Its mtime and ctime should be excluded");
243 	assertEqualInt(1, archive_match_time_excluded(m, ae));
244 	assertEqualInt(1, archive_match_excluded(m, ae));
245 
246 	t = parse_date(now, "1980/2/1 0:0:1 UTC");
247 	archive_entry_set_mtime(ae, t, 0);
248 	archive_entry_set_ctime(ae, t, 0);
249 	failure("Both Its mtime and ctime should not be excluded");
250 	assertEqualInt(0, archive_match_time_excluded(m, ae));
251 	assertEqualInt(0, archive_match_excluded(m, ae));
252 
253 	/* Clean up. */
254 	archive_entry_free(ae);
255 	archive_match_free(m);
256 }
257 
258 static void
test_newer_mtime_than_file_mbs(void)259 test_newer_mtime_than_file_mbs(void)
260 {
261 	struct archive *a;
262 	struct archive_entry *ae;
263 	struct archive *m;
264 
265 	if (!assert((m = archive_match_new()) != NULL))
266 		return;
267 	if (!assert((ae = archive_entry_new()) != NULL)) {
268 		archive_match_free(m);
269 		return;
270 	}
271 	if (!assert((a = archive_read_disk_new()) != NULL)) {
272 		archive_match_free(m);
273 		archive_entry_free(ae);
274 		return;
275 	}
276 
277 	/*
278 	 * Test: newer mtime than a file specified in MBS file name.
279 	 */
280 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
281 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, "mid_mtime"));
282 
283 	/* Verify 'old_mtime' file. */
284 	archive_entry_copy_pathname(ae, "old_mtime");
285 	assertEqualIntA(a, ARCHIVE_OK,
286 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
287 	failure("old_mtime should be excluded");
288 	assertEqualInt(1, archive_match_time_excluded(m, ae));
289 	assertEqualInt(1, archive_match_excluded(m, ae));
290 
291 	/* Verify 'mid_mtime' file. */
292 	archive_entry_clear(ae);
293 	archive_entry_copy_pathname(ae, "mid_mtime");
294 	assertEqualIntA(a, ARCHIVE_OK,
295 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
296 	failure("mid_mtime should be excluded");
297 	assertEqualInt(1, archive_match_time_excluded(m, ae));
298 	assertEqualInt(1, archive_match_excluded(m, ae));
299 
300 	/* Verify 'new_mtime' file. */
301 	archive_entry_clear(ae);
302 	archive_entry_copy_pathname(ae, "new_mtime");
303 	assertEqualIntA(a, ARCHIVE_OK,
304 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
305 	failure("new_mtime should not be excluded");
306 	assertEqualInt(0, archive_match_time_excluded(m, ae));
307 	assertEqualInt(0, archive_match_excluded(m, ae));
308 
309 	/* Clean up. */
310 	archive_read_free(a);
311 	archive_entry_free(ae);
312 	archive_match_free(m);
313 }
314 
315 static void
test_newer_ctime_than_file_mbs(void)316 test_newer_ctime_than_file_mbs(void)
317 {
318 #if defined(_WIN32) && !defined(__CYGWIN__)
319 	skipping("Can't set ctime on Windows");
320 	return;
321 #else
322 	struct archive *a;
323 	struct archive_entry *ae;
324 	struct archive *m;
325 
326 	if (!assert((m = archive_match_new()) != NULL))
327 		return;
328 	if (!assert((ae = archive_entry_new()) != NULL)) {
329 		archive_match_free(m);
330 		return;
331 	}
332 	if (!assert((a = archive_read_disk_new()) != NULL)) {
333 		archive_match_free(m);
334 		archive_entry_free(ae);
335 		return;
336 	}
337 
338 	/*
339 	 * Test: newer ctime than a file specified in MBS file name.
340 	 */
341 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
342 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, "mid_ctime"));
343 
344 	/* Verify 'old_ctime' file. */
345 	archive_entry_copy_pathname(ae, "old_ctime");
346 	assertEqualIntA(a, ARCHIVE_OK,
347 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
348 	failure("old_ctime should be excluded");
349 	assertEqualInt(1, archive_match_time_excluded(m, ae));
350 	assertEqualInt(1, archive_match_excluded(m, ae));
351 
352 	/* Verify 'mid_ctime' file. */
353 	archive_entry_clear(ae);
354 	archive_entry_copy_pathname(ae, "mid_ctime");
355 	assertEqualIntA(a, ARCHIVE_OK,
356 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
357 	failure("mid_ctime should be excluded");
358 	assertEqualInt(1, archive_match_time_excluded(m, ae));
359 	assertEqualInt(1, archive_match_excluded(m, ae));
360 
361 	/* Verify 'new_ctime' file. */
362 	archive_entry_clear(ae);
363 	archive_entry_copy_pathname(ae, "new_ctime");
364 	assertEqualIntA(a, ARCHIVE_OK,
365 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
366 	failure("new_ctime should not be excluded");
367 	assertEqualInt(0, archive_match_time_excluded(m, ae));
368 	assertEqualInt(0, archive_match_excluded(m, ae));
369 
370 	/* Clean up. */
371 	archive_read_free(a);
372 	archive_entry_free(ae);
373 	archive_match_free(m);
374 #endif
375 }
376 
377 static void
test_newer_mtime_than_file_wcs(void)378 test_newer_mtime_than_file_wcs(void)
379 {
380 	struct archive *a;
381 	struct archive_entry *ae;
382 	struct archive *m;
383 
384 	if (!assert((m = archive_match_new()) != NULL))
385 		return;
386 	if (!assert((ae = archive_entry_new()) != NULL)) {
387 		archive_match_free(m);
388 		return;
389 	}
390 	if (!assert((a = archive_read_disk_new()) != NULL)) {
391 		archive_match_free(m);
392 		archive_entry_free(ae);
393 		return;
394 	}
395 
396 	/*
397 	 * Test: newer mtime than a file specified in WCS file name.
398 	 */
399 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
400 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, L"mid_mtime"));
401 
402 	/* Verify 'old_mtime' file. */
403 	archive_entry_copy_pathname(ae, "old_mtime");
404 	assertEqualIntA(a, ARCHIVE_OK,
405 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
406 	failure("old_mtime should be excluded");
407 	assertEqualInt(1, archive_match_time_excluded(m, ae));
408 	assertEqualInt(1, archive_match_excluded(m, ae));
409 
410 	/* Verify 'mid_mtime' file. */
411 	archive_entry_clear(ae);
412 	archive_entry_copy_pathname(ae, "mid_mtime");
413 	assertEqualIntA(a, ARCHIVE_OK,
414 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
415 	failure("mid_mtime should be excluded");
416 	assertEqualInt(1, archive_match_time_excluded(m, ae));
417 	assertEqualInt(1, archive_match_excluded(m, ae));
418 
419 	/* Verify 'new_mtime' file. */
420 	archive_entry_clear(ae);
421 	archive_entry_copy_pathname(ae, "new_mtime");
422 	assertEqualIntA(a, ARCHIVE_OK,
423 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
424 	failure("new_mtime should not be excluded");
425 	assertEqualInt(0, archive_match_time_excluded(m, ae));
426 	assertEqualInt(0, archive_match_excluded(m, ae));
427 
428 	/* Clean up. */
429 	archive_read_free(a);
430 	archive_entry_free(ae);
431 	archive_match_free(m);
432 }
433 
434 static void
test_newer_ctime_than_file_wcs(void)435 test_newer_ctime_than_file_wcs(void)
436 {
437 #if defined(_WIN32) && !defined(__CYGWIN__)
438 	skipping("Can't set ctime on Windows");
439 	return;
440 #else
441 	struct archive *a;
442 	struct archive_entry *ae;
443 	struct archive *m;
444 
445 	if (!assert((m = archive_match_new()) != NULL))
446 		return;
447 	if (!assert((ae = archive_entry_new()) != NULL)) {
448 		archive_match_free(m);
449 		return;
450 	}
451 	if (!assert((a = archive_read_disk_new()) != NULL)) {
452 		archive_match_free(m);
453 		archive_entry_free(ae);
454 		return;
455 	}
456 
457 	/*
458 	 * Test: newer ctime than a file specified in WCS file name.
459 	 */
460 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
461 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, L"mid_ctime"));
462 
463 	/* Verify 'old_ctime' file. */
464 	archive_entry_clear(ae);
465 	archive_entry_copy_pathname(ae, "old_ctime");
466 	assertEqualIntA(a, ARCHIVE_OK,
467 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
468 	failure("old_ctime should be excluded");
469 	assertEqualInt(1, archive_match_time_excluded(m, ae));
470 	assertEqualInt(1, archive_match_excluded(m, ae));
471 
472 	/* Verify 'mid_ctime' file. */
473 	archive_entry_clear(ae);
474 	archive_entry_copy_pathname(ae, "mid_ctime");
475 	assertEqualIntA(a, ARCHIVE_OK,
476 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
477 	failure("mid_ctime should be excluded");
478 	assertEqualInt(1, archive_match_time_excluded(m, ae));
479 	assertEqualInt(1, archive_match_excluded(m, ae));
480 
481 	/* Verify 'new_ctime' file. */
482 	archive_entry_clear(ae);
483 	archive_entry_copy_pathname(ae, "new_ctime");
484 	assertEqualIntA(a, ARCHIVE_OK,
485 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
486 	failure("new_ctime should not be excluded");
487 	assertEqualInt(0, archive_match_time_excluded(m, ae));
488 	assertEqualInt(0, archive_match_excluded(m, ae));
489 
490 	/* Clean up. */
491 	archive_read_free(a);
492 	archive_entry_free(ae);
493 	archive_match_free(m);
494 #endif
495 }
496 
497 static void
test_older_time(void)498 test_older_time(void)
499 {
500 	struct archive_entry *ae;
501 	struct archive *m;
502 
503 	if (!assert((m = archive_match_new()) != NULL))
504 		return;
505 	if (!assert((ae = archive_entry_new()) != NULL)) {
506 		archive_match_free(m);
507 		return;
508 	}
509 
510 	assertEqualIntA(m, 0, archive_match_include_time(m,
511 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
512 	    ARCHIVE_MATCH_OLDER, 7880, 0));
513 
514 	archive_entry_copy_pathname(ae, "file1");
515 	archive_entry_set_mtime(ae, 7880, 0);
516 	archive_entry_set_ctime(ae, 7880, 0);
517 	failure("Both Its mtime and ctime should be excluded");
518 	assertEqualInt(1, archive_match_time_excluded(m, ae));
519 	assertEqualInt(1, archive_match_excluded(m, ae));
520 	archive_entry_set_mtime(ae, 7879, 999);
521 	archive_entry_set_ctime(ae, 7879, 999);
522 	failure("Both Its mtime and ctime should not be excluded");
523 	assertEqualInt(0, archive_match_time_excluded(m, ae));
524 	assertEqualInt(0, archive_match_excluded(m, ae));
525 
526 	archive_entry_set_mtime(ae, 7881, 0);
527 	archive_entry_set_ctime(ae, 7881, 0);
528 	failure("Both Its mtime and ctime should be excluded");
529 	assertEqualInt(1, archive_match_time_excluded(m, ae));
530 	assertEqualInt(1, archive_match_excluded(m, ae));
531 
532 	archive_entry_set_mtime(ae, 7880, 1);
533 	archive_entry_set_ctime(ae, 7879, 0);
534 	failure("Its mtime should be excluded");
535 	assertEqualInt(1, archive_match_time_excluded(m, ae));
536 	assertEqualInt(1, archive_match_excluded(m, ae));
537 
538 	archive_entry_set_mtime(ae, 7879, 0);
539 	archive_entry_set_ctime(ae, 7880, 1);
540 	failure("Its ctime should be excluded");
541 	assertEqualInt(1, archive_match_time_excluded(m, ae));
542 	assertEqualInt(1, archive_match_excluded(m, ae));
543 
544 	/* Clean up. */
545 	archive_entry_free(ae);
546 	archive_match_free(m);
547 }
548 
549 static void
test_older_time_str(void)550 test_older_time_str(void)
551 {
552 	struct archive_entry *ae;
553 	struct archive *m;
554 	time_t now, t;
555 
556 	if (!assert((m = archive_match_new()) != NULL))
557 		return;
558 	if (!assert((ae = archive_entry_new()) != NULL)) {
559 		archive_match_free(m);
560 		return;
561 	}
562 
563 	time(&now);
564 
565 	/* Test1: Allow newer time. */
566 	assertEqualIntA(m, 0, archive_match_include_date(m,
567 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
568 	    ARCHIVE_MATCH_OLDER, "1980/2/1 0:0:0 UTC"));
569 
570 	archive_entry_copy_pathname(ae, "file1");
571 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
572 	archive_entry_set_mtime(ae, t, 0);
573 	archive_entry_set_ctime(ae, t, 0);
574 	failure("Both Its mtime and ctime should be excluded");
575 	assertEqualInt(1, archive_match_time_excluded(m, ae));
576 	assertEqualInt(1, archive_match_excluded(m, ae));
577 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
578 	archive_entry_set_mtime(ae, t, 0);
579 	archive_entry_set_ctime(ae, t, 0);
580 	failure("Both Its mtime and ctime should not be excluded");
581 	assertEqualInt(0, archive_match_time_excluded(m, ae));
582 	assertEqualInt(0, archive_match_excluded(m, ae));
583 
584 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
585 	archive_entry_set_mtime(ae, t, 0);
586 	archive_entry_set_ctime(ae, t, 0);
587 	failure("Both Its mtime and ctime should be excluded");
588 	assertEqualInt(1, archive_match_time_excluded(m, ae));
589 	assertEqualInt(1, archive_match_excluded(m, ae));
590 
591 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
592 	archive_entry_set_mtime(ae, t, 0);
593 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
594 	archive_entry_set_ctime(ae, t, 0);
595 	failure("Its mtime should be excluded");
596 	assertEqualInt(1, archive_match_time_excluded(m, ae));
597 	assertEqualInt(1, archive_match_excluded(m, ae));
598 
599 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
600 	archive_entry_set_mtime(ae, t, 0);
601 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
602 	archive_entry_set_ctime(ae, t, 0);
603 	failure("Its ctime should be excluded");
604 	assertEqualInt(1, archive_match_time_excluded(m, ae));
605 	assertEqualInt(1, archive_match_excluded(m, ae));
606 
607 	/* Test2: Allow equal or newer time. */
608 	assertEqualIntA(m, 0, archive_match_include_date(m,
609 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
610 	    ARCHIVE_MATCH_OLDER | ARCHIVE_MATCH_EQUAL,
611 	    "1980/2/1 0:0:0 UTC"));
612 
613 	archive_entry_copy_pathname(ae, "file1");
614 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
615 	archive_entry_set_mtime(ae, t, 0);
616 	archive_entry_set_ctime(ae, t, 0);
617 	failure("Both Its mtime and ctime should not be excluded");
618 	assertEqualInt(0, archive_match_time_excluded(m, ae));
619 	assertEqualInt(0, archive_match_excluded(m, ae));
620 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
621 	archive_entry_set_mtime(ae, t, 0);
622 	archive_entry_set_ctime(ae, t, 0);
623 	failure("Both Its mtime and ctime should not be excluded");
624 	assertEqualInt(0, archive_match_time_excluded(m, ae));
625 	assertEqualInt(0, archive_match_excluded(m, ae));
626 
627 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
628 	archive_entry_set_mtime(ae, t, 0);
629 	archive_entry_set_ctime(ae, t, 0);
630 	failure("Both Its mtime and ctime should be excluded");
631 	assertEqualInt(1, archive_match_time_excluded(m, ae));
632 	assertEqualInt(1, archive_match_excluded(m, ae));
633 
634 	/* Clean up. */
635 	archive_entry_free(ae);
636 	archive_match_free(m);
637 }
638 
639 static void
test_older_time_str_w(void)640 test_older_time_str_w(void)
641 {
642 	struct archive_entry *ae;
643 	struct archive *m;
644 	time_t now, t;
645 
646 	if (!assert((m = archive_match_new()) != NULL))
647 		return;
648 	if (!assert((ae = archive_entry_new()) != NULL)) {
649 		archive_match_free(m);
650 		return;
651 	}
652 
653 	time(&now);
654 
655 	/* Test1: Allow newer time. */
656 	assertEqualIntA(m, 0, archive_match_include_date_w(m,
657 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
658 	    ARCHIVE_MATCH_OLDER, L"1980/2/1 0:0:0 UTC"));
659 
660 	archive_entry_copy_pathname(ae, "file1");
661 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
662 	archive_entry_set_mtime(ae, t, 0);
663 	archive_entry_set_ctime(ae, t, 0);
664 	failure("Both Its mtime and ctime should be excluded");
665 	assertEqualInt(1, archive_match_time_excluded(m, ae));
666 	assertEqualInt(1, archive_match_excluded(m, ae));
667 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
668 	archive_entry_set_mtime(ae, t, 0);
669 	archive_entry_set_ctime(ae, t, 0);
670 	failure("Both Its mtime and ctime should not be excluded");
671 	assertEqualInt(0, archive_match_time_excluded(m, ae));
672 	assertEqualInt(0, archive_match_excluded(m, ae));
673 
674 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
675 	archive_entry_set_mtime(ae, t, 0);
676 	archive_entry_set_ctime(ae, t, 0);
677 	failure("Both Its mtime and ctime should be excluded");
678 	assertEqualInt(1, archive_match_time_excluded(m, ae));
679 	assertEqualInt(1, archive_match_excluded(m, ae));
680 
681 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
682 	archive_entry_set_mtime(ae, t, 0);
683 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
684 	archive_entry_set_ctime(ae, t, 0);
685 	failure("Its mtime should be excluded");
686 	assertEqualInt(1, archive_match_time_excluded(m, ae));
687 	assertEqualInt(1, archive_match_excluded(m, ae));
688 
689 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
690 	archive_entry_set_mtime(ae, t, 0);
691 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
692 	archive_entry_set_ctime(ae, t, 0);
693 	failure("Its ctime should be excluded");
694 	assertEqualInt(1, archive_match_time_excluded(m, ae));
695 	assertEqualInt(1, archive_match_excluded(m, ae));
696 
697 	/* Test2: Allow equal or newer time. */
698 	assertEqualIntA(m, 0, archive_match_include_date_w(m,
699 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME |
700 	    ARCHIVE_MATCH_OLDER | ARCHIVE_MATCH_EQUAL,
701 	    L"1980/2/1 0:0:0 UTC"));
702 
703 	archive_entry_copy_pathname(ae, "file1");
704 	t = parse_date(now, "1980/2/1 0:0:0 UTC");
705 	archive_entry_set_mtime(ae, t, 0);
706 	archive_entry_set_ctime(ae, t, 0);
707 	failure("Both Its mtime and ctime should not be excluded");
708 	assertEqualInt(0, archive_match_time_excluded(m, ae));
709 	assertEqualInt(0, archive_match_excluded(m, ae));
710 	t = parse_date(now, "1980/1/1 0:0:0 UTC");
711 	archive_entry_set_mtime(ae, t, 0);
712 	archive_entry_set_ctime(ae, t, 0);
713 	failure("Both Its mtime and ctime should not be excluded");
714 	assertEqualInt(0, archive_match_time_excluded(m, ae));
715 	assertEqualInt(0, archive_match_excluded(m, ae));
716 
717 	t = parse_date(now, "1980/3/1 0:0:0 UTC");
718 	archive_entry_set_mtime(ae, t, 0);
719 	archive_entry_set_ctime(ae, t, 0);
720 	failure("Both Its mtime and ctime should be excluded");
721 	assertEqualInt(1, archive_match_time_excluded(m, ae));
722 	assertEqualInt(1, archive_match_excluded(m, ae));
723 
724 	/* Clean up. */
725 	archive_entry_free(ae);
726 	archive_match_free(m);
727 }
728 
729 static void
test_older_mtime_than_file_mbs(void)730 test_older_mtime_than_file_mbs(void)
731 {
732 	struct archive *a;
733 	struct archive_entry *ae;
734 	struct archive *m;
735 
736 	if (!assert((m = archive_match_new()) != NULL))
737 		return;
738 	if (!assert((ae = archive_entry_new()) != NULL)) {
739 		archive_match_free(m);
740 		return;
741 	}
742 	if (!assert((a = archive_read_disk_new()) != NULL)) {
743 		archive_match_free(m);
744 		archive_entry_free(ae);
745 		return;
746 	}
747 
748 	/*
749 	 * Test: older mtime than a file specified in MBS file name.
750 	 */
751 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
752 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, "mid_mtime"));
753 
754 	/* Verify 'old_mtime' file. */
755 	archive_entry_copy_pathname(ae, "old_mtime");
756 	assertEqualIntA(a, ARCHIVE_OK,
757 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
758 	failure("old_mtime should not be excluded");
759 	assertEqualInt(0, archive_match_time_excluded(m, ae));
760 	assertEqualInt(0, archive_match_excluded(m, ae));
761 
762 	/* Verify 'mid_mtime' file. */
763 	archive_entry_clear(ae);
764 	archive_entry_copy_pathname(ae, "mid_mtime");
765 	assertEqualIntA(a, ARCHIVE_OK,
766 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
767 	failure("mid_mtime should be excluded");
768 	assertEqualInt(1, archive_match_time_excluded(m, ae));
769 	assertEqualInt(1, archive_match_excluded(m, ae));
770 
771 	/* Verify 'new_mtime' file. */
772 	archive_entry_clear(ae);
773 	archive_entry_copy_pathname(ae, "new_mtime");
774 	assertEqualIntA(a, ARCHIVE_OK,
775 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
776 	failure("new_mtime should be excluded");
777 	assertEqualInt(1, archive_match_time_excluded(m, ae));
778 	assertEqualInt(1, archive_match_excluded(m, ae));
779 
780 	/* Clean up. */
781 	archive_read_free(a);
782 	archive_entry_free(ae);
783 	archive_match_free(m);
784 }
785 
786 static void
test_older_ctime_than_file_mbs(void)787 test_older_ctime_than_file_mbs(void)
788 {
789 #if defined(_WIN32) && !defined(__CYGWIN__)
790 	skipping("Can't set ctime on Windows");
791 	return;
792 #else
793 	struct archive *a;
794 	struct archive_entry *ae;
795 	struct archive *m;
796 
797 	if (!assert((m = archive_match_new()) != NULL))
798 		return;
799 	if (!assert((ae = archive_entry_new()) != NULL)) {
800 		archive_match_free(m);
801 		return;
802 	}
803 	if (!assert((a = archive_read_disk_new()) != NULL)) {
804 		archive_match_free(m);
805 		archive_entry_free(ae);
806 		return;
807 	}
808 
809 	/*
810 	 * Test: older ctime than a file specified in MBS file name.
811 	 */
812 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
813 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, "mid_ctime"));
814 
815 	/* Verify 'old_ctime' file. */
816 	archive_entry_clear(ae);
817 	archive_entry_copy_pathname(ae, "old_ctime");
818 	assertEqualIntA(a, ARCHIVE_OK,
819 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
820 	failure("old_ctime should not be excluded");
821 	assertEqualInt(0, archive_match_time_excluded(m, ae));
822 	assertEqualInt(0, archive_match_excluded(m, ae));
823 
824 	/* Verify 'mid_ctime' file. */
825 	archive_entry_clear(ae);
826 	archive_entry_copy_pathname(ae, "mid_ctime");
827 	assertEqualIntA(a, ARCHIVE_OK,
828 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
829 	failure("mid_ctime should be excluded");
830 	assertEqualInt(1, archive_match_time_excluded(m, ae));
831 	assertEqualInt(1, archive_match_excluded(m, ae));
832 
833 	/* Verify 'new_ctime' file. */
834 	archive_entry_clear(ae);
835 	archive_entry_copy_pathname(ae, "new_ctime");
836 	assertEqualIntA(a, ARCHIVE_OK,
837 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
838 	failure("new_ctime should be excluded");
839 	assertEqualInt(1, archive_match_time_excluded(m, ae));
840 	assertEqualInt(1, archive_match_excluded(m, ae));
841 
842 	/* Clean up. */
843 	archive_read_free(a);
844 	archive_entry_free(ae);
845 	archive_match_free(m);
846 #endif
847 }
848 
849 static void
test_older_mtime_than_file_wcs(void)850 test_older_mtime_than_file_wcs(void)
851 {
852 	struct archive *a;
853 	struct archive_entry *ae;
854 	struct archive *m;
855 
856 	if (!assert((m = archive_match_new()) != NULL))
857 		return;
858 	if (!assert((ae = archive_entry_new()) != NULL)) {
859 		archive_match_free(m);
860 		return;
861 	}
862 	if (!assert((a = archive_read_disk_new()) != NULL)) {
863 		archive_match_free(m);
864 		archive_entry_free(ae);
865 		return;
866 	}
867 
868 	/*
869 	 * Test: older mtime than a file specified in WCS file name.
870 	 */
871 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
872 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, L"mid_mtime"));
873 
874 	/* Verify 'old_mtime' file. */
875 	archive_entry_copy_pathname(ae, "old_mtime");
876 	assertEqualIntA(a, ARCHIVE_OK,
877 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
878 	failure("old_mtime should not be excluded");
879 	assertEqualInt(0, archive_match_time_excluded(m, ae));
880 	assertEqualInt(0, archive_match_excluded(m, ae));
881 
882 	/* Verify 'mid_mtime' file. */
883 	archive_entry_clear(ae);
884 	archive_entry_copy_pathname(ae, "mid_mtime");
885 	assertEqualIntA(a, ARCHIVE_OK,
886 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
887 	failure("mid_mtime should be excluded");
888 	assertEqualInt(1, archive_match_time_excluded(m, ae));
889 	assertEqualInt(1, archive_match_excluded(m, ae));
890 
891 	/* Verify 'new_mtime' file. */
892 	archive_entry_clear(ae);
893 	archive_entry_copy_pathname(ae, "new_mtime");
894 	assertEqualIntA(a, ARCHIVE_OK,
895 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
896 	failure("new_mtime should be excluded");
897 	assertEqualInt(1, archive_match_time_excluded(m, ae));
898 	assertEqualInt(1, archive_match_excluded(m, ae));
899 
900 	/* Clean up. */
901 	archive_read_free(a);
902 	archive_entry_free(ae);
903 	archive_match_free(m);
904 }
905 
906 static void
test_older_ctime_than_file_wcs(void)907 test_older_ctime_than_file_wcs(void)
908 {
909 #if defined(_WIN32) && !defined(__CYGWIN__)
910 	skipping("Can't set ctime on Windows");
911 	return;
912 #else
913 	struct archive *a;
914 	struct archive_entry *ae;
915 	struct archive *m;
916 
917 	if (!assert((m = archive_match_new()) != NULL))
918 		return;
919 	if (!assert((ae = archive_entry_new()) != NULL)) {
920 		archive_match_free(m);
921 		return;
922 	}
923 	if (!assert((a = archive_read_disk_new()) != NULL)) {
924 		archive_match_free(m);
925 		archive_entry_free(ae);
926 		return;
927 	}
928 
929 	/*
930 	 * Test: older ctime than a file specified in WCS file name.
931 	 */
932 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
933 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, L"mid_ctime"));
934 
935 	/* Verify 'old_ctime' file. */
936 	archive_entry_clear(ae);
937 	archive_entry_copy_pathname(ae, "old_ctime");
938 	assertEqualIntA(a, ARCHIVE_OK,
939 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
940 	failure("old_ctime should not be excluded");
941 	assertEqualInt(0, archive_match_time_excluded(m, ae));
942 	assertEqualInt(0, archive_match_excluded(m, ae));
943 
944 	/* Verify 'mid_ctime' file. */
945 	archive_entry_clear(ae);
946 	archive_entry_copy_pathname(ae, "mid_ctime");
947 	assertEqualIntA(a, ARCHIVE_OK,
948 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
949 	failure("mid_ctime should be excluded");
950 	assertEqualInt(1, archive_match_time_excluded(m, ae));
951 	assertEqualInt(1, archive_match_excluded(m, ae));
952 
953 	/* Verify 'new_ctime' file. */
954 	archive_entry_clear(ae);
955 	archive_entry_copy_pathname(ae, "new_ctime");
956 	assertEqualIntA(a, ARCHIVE_OK,
957 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
958 	failure("new_ctime should be excluded");
959 	assertEqualInt(1, archive_match_time_excluded(m, ae));
960 	assertEqualInt(1, archive_match_excluded(m, ae));
961 
962 	/* Clean up. */
963 	archive_read_free(a);
964 	archive_entry_free(ae);
965 	archive_match_free(m);
966 #endif
967 }
968 
969 static void
test_mtime_between_files_mbs(void)970 test_mtime_between_files_mbs(void)
971 {
972 	struct archive *a;
973 	struct archive_entry *ae;
974 	struct archive *m;
975 
976 	if (!assert((m = archive_match_new()) != NULL))
977 		return;
978 	if (!assert((ae = archive_entry_new()) != NULL)) {
979 		archive_match_free(m);
980 		return;
981 	}
982 	if (!assert((a = archive_read_disk_new()) != NULL)) {
983 		archive_match_free(m);
984 		archive_entry_free(ae);
985 		return;
986 	}
987 
988 	/*
989 	 * Test: mtime between  file specified in MBS file name.
990 	 */
991 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
992 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, "old_mtime"));
993 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
994 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, "new_mtime"));
995 
996 	/* Verify 'old_mtime' file. */
997 	archive_entry_copy_pathname(ae, "old_mtime");
998 	assertEqualIntA(a, ARCHIVE_OK,
999 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1000 	failure("old_mtime should be excluded");
1001 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1002 	assertEqualInt(1, archive_match_excluded(m, ae));
1003 
1004 	/* Verify 'mid_mtime' file. */
1005 	archive_entry_clear(ae);
1006 	archive_entry_copy_pathname(ae, "mid_mtime");
1007 	assertEqualIntA(a, ARCHIVE_OK,
1008 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1009 	failure("mid_mtime should not be excluded");
1010 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1011 	assertEqualInt(0, archive_match_excluded(m, ae));
1012 
1013 	/* Verify 'new_mtime' file. */
1014 	archive_entry_clear(ae);
1015 	archive_entry_copy_pathname(ae, "new_mtime");
1016 	assertEqualIntA(a, ARCHIVE_OK,
1017 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1018 	failure("new_mtime should be excluded");
1019 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1020 	assertEqualInt(1, archive_match_excluded(m, ae));
1021 
1022 	/* Clean up. */
1023 	archive_read_free(a);
1024 	archive_entry_free(ae);
1025 	archive_match_free(m);
1026 }
1027 
1028 static void
test_mtime_between_files_wcs(void)1029 test_mtime_between_files_wcs(void)
1030 {
1031 	struct archive *a;
1032 	struct archive_entry *ae;
1033 	struct archive *m;
1034 
1035 	if (!assert((m = archive_match_new()) != NULL))
1036 		return;
1037 	if (!assert((ae = archive_entry_new()) != NULL)) {
1038 		archive_match_free(m);
1039 		return;
1040 	}
1041 	if (!assert((a = archive_read_disk_new()) != NULL)) {
1042 		archive_match_free(m);
1043 		archive_entry_free(ae);
1044 		return;
1045 	}
1046 
1047 	/*
1048 	 * Test: mtime between  file specified in WCS file name.
1049 	 */
1050 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
1051 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER, L"old_mtime"));
1052 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
1053 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER, L"new_mtime"));
1054 
1055 	/* Verify 'old_mtime' file. */
1056 	archive_entry_copy_pathname(ae, "old_mtime");
1057 	assertEqualIntA(a, ARCHIVE_OK,
1058 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1059 	failure("old_mtime should be excluded");
1060 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1061 	assertEqualInt(1, archive_match_excluded(m, ae));
1062 
1063 	/* Verify 'mid_mtime' file. */
1064 	archive_entry_clear(ae);
1065 	archive_entry_copy_pathname(ae, "mid_mtime");
1066 	assertEqualIntA(a, ARCHIVE_OK,
1067 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1068 	failure("mid_mtime should not be excluded");
1069 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1070 	assertEqualInt(0, archive_match_excluded(m, ae));
1071 
1072 	/* Verify 'new_mtime' file. */
1073 	archive_entry_clear(ae);
1074 	archive_entry_copy_pathname(ae, "new_mtime");
1075 	assertEqualIntA(a, ARCHIVE_OK,
1076 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1077 	failure("new_mtime should be excluded");
1078 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1079 	assertEqualInt(1, archive_match_excluded(m, ae));
1080 
1081 	/* Clean up. */
1082 	archive_read_free(a);
1083 	archive_entry_free(ae);
1084 	archive_match_free(m);
1085 }
1086 
1087 static void
test_ctime_between_files_mbs(void)1088 test_ctime_between_files_mbs(void)
1089 {
1090 #if defined(_WIN32) && !defined(__CYGWIN__)
1091 	skipping("Can't set ctime on Windows");
1092 	return;
1093 #else
1094 	struct archive *a;
1095 	struct archive_entry *ae;
1096 	struct archive *m;
1097 
1098 	if (!assert((m = archive_match_new()) != NULL))
1099 		return;
1100 	if (!assert((ae = archive_entry_new()) != NULL)) {
1101 		archive_match_free(m);
1102 		return;
1103 	}
1104 	if (!assert((a = archive_read_disk_new()) != NULL)) {
1105 		archive_match_free(m);
1106 		archive_entry_free(ae);
1107 		return;
1108 	}
1109 
1110 	/*
1111 	 * Test: ctime between files specified in MBS file name.
1112 	 */
1113 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
1114 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, "old_ctime"));
1115 	assertEqualIntA(m, 0, archive_match_include_file_time(m,
1116 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, "new_ctime"));
1117 
1118 	/* Verify 'old_ctime' file. */
1119 	archive_entry_copy_pathname(ae, "old_ctime");
1120 	assertEqualIntA(a, ARCHIVE_OK,
1121 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1122 	failure("old_ctime should be excluded");
1123 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1124 	assertEqualInt(1, archive_match_excluded(m, ae));
1125 
1126 	/* Verify 'mid_ctime' file. */
1127 	archive_entry_clear(ae);
1128 	archive_entry_copy_pathname(ae, "mid_ctime");
1129 	assertEqualIntA(a, ARCHIVE_OK,
1130 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1131 	failure("mid_ctime should not be excluded");
1132 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1133 	assertEqualInt(0, archive_match_excluded(m, ae));
1134 
1135 	/* Verify 'new_ctime' file. */
1136 	archive_entry_clear(ae);
1137 	archive_entry_copy_pathname(ae, "new_ctime");
1138 	assertEqualIntA(a, ARCHIVE_OK,
1139 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1140 	failure("new_ctime should be excluded");
1141 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1142 	assertEqualInt(1, archive_match_excluded(m, ae));
1143 
1144 	/* Clean up. */
1145 	archive_read_free(a);
1146 	archive_entry_free(ae);
1147 	archive_match_free(m);
1148 #endif
1149 }
1150 
1151 static void
test_ctime_between_files_wcs(void)1152 test_ctime_between_files_wcs(void)
1153 {
1154 #if defined(_WIN32) && !defined(__CYGWIN__)
1155 	skipping("Can't set ctime on Windows");
1156 	return;
1157 #else
1158 	struct archive *a;
1159 	struct archive_entry *ae;
1160 	struct archive *m;
1161 
1162 	if (!assert((m = archive_match_new()) != NULL))
1163 		return;
1164 	if (!assert((ae = archive_entry_new()) != NULL)) {
1165 		archive_match_free(m);
1166 		return;
1167 	}
1168 	if (!assert((a = archive_read_disk_new()) != NULL)) {
1169 		archive_match_free(m);
1170 		archive_entry_free(ae);
1171 		return;
1172 	}
1173 
1174 	/*
1175 	 * Test: ctime between files specified in WCS file name.
1176 	 */
1177 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
1178 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER, L"old_ctime"));
1179 	assertEqualIntA(m, 0, archive_match_include_file_time_w(m,
1180 	    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER, L"new_ctime"));
1181 
1182 	/* Verify 'old_ctime' file. */
1183 	archive_entry_copy_pathname(ae, "old_ctime");
1184 	assertEqualIntA(a, ARCHIVE_OK,
1185 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1186 	failure("old_ctime should be excluded");
1187 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1188 	assertEqualInt(1, archive_match_excluded(m, ae));
1189 
1190 	/* Verify 'mid_ctime' file. */
1191 	archive_entry_clear(ae);
1192 	archive_entry_copy_pathname(ae, "mid_ctime");
1193 	assertEqualIntA(a, ARCHIVE_OK,
1194 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1195 	failure("mid_ctime should not be excluded");
1196 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1197 	assertEqualInt(0, archive_match_excluded(m, ae));
1198 
1199 	/* Verify 'new_ctime' file. */
1200 	archive_entry_clear(ae);
1201 	archive_entry_copy_pathname(ae, "new_ctime");
1202 	assertEqualIntA(a, ARCHIVE_OK,
1203 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
1204 	failure("new_ctime should be excluded");
1205 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1206 	assertEqualInt(1, archive_match_excluded(m, ae));
1207 
1208 	/* Clean up. */
1209 	archive_read_free(a);
1210 	archive_entry_free(ae);
1211 	archive_match_free(m);
1212 #endif
1213 }
1214 
1215 static void
excluded(struct archive * m)1216 excluded(struct archive *m)
1217 {
1218 	struct archive_entry *ae;
1219 
1220 	if (!assert((ae = archive_entry_new()) != NULL))
1221 		return;
1222 
1223 	archive_entry_copy_pathname(ae, "file1");
1224 	archive_entry_set_mtime(ae, 7879, 999);
1225 	failure("It should be excluded");
1226 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1227 	assertEqualInt(1, archive_match_excluded(m, ae));
1228 	archive_entry_set_mtime(ae, 7880, 0);
1229 	failure("It should be excluded");
1230 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1231 	assertEqualInt(1, archive_match_excluded(m, ae));
1232 	archive_entry_set_mtime(ae, 7880, 1);
1233 	failure("It should not be excluded");
1234 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1235 	assertEqualInt(0, archive_match_excluded(m, ae));
1236 
1237 	archive_entry_copy_pathname(ae, "file2");
1238 	archive_entry_set_mtime(ae, 7879, 999);
1239 	failure("It should not be excluded");
1240 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1241 	assertEqualInt(0, archive_match_excluded(m, ae));
1242 	archive_entry_set_mtime(ae, 7880, 0);
1243 	failure("It should not be excluded");
1244 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1245 	assertEqualInt(0, archive_match_excluded(m, ae));
1246 	archive_entry_set_mtime(ae, 7880, 1);
1247 	failure("It should not be excluded");
1248 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1249 	assertEqualInt(0, archive_match_excluded(m, ae));
1250 
1251 	archive_entry_copy_pathname(ae, "file3");
1252 	archive_entry_set_mtime(ae, 7879, 999);
1253 	failure("It should be excluded");
1254 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1255 	assertEqualInt(1, archive_match_excluded(m, ae));
1256 	archive_entry_set_mtime(ae, 7880, 0);
1257 	failure("It should be excluded");
1258 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1259 	assertEqualInt(1, archive_match_excluded(m, ae));
1260 	archive_entry_set_mtime(ae, 7880, 1);
1261 	failure("It should be excluded");
1262 	assertEqualInt(1, archive_match_time_excluded(m, ae));
1263 	assertEqualInt(1, archive_match_excluded(m, ae));
1264 
1265 	/*
1266 	 * "file4" is not registered, that sort of a file should not be
1267 	 * excluded with any mtime.
1268 	 */
1269 	archive_entry_copy_pathname(ae, "file4");
1270 	archive_entry_set_mtime(ae, 7879, 999);
1271 	failure("It should not be excluded");
1272 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1273 	assertEqualInt(0, archive_match_excluded(m, ae));
1274 	archive_entry_set_mtime(ae, 7880, 0);
1275 	failure("It should not be excluded");
1276 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1277 	assertEqualInt(0, archive_match_excluded(m, ae));
1278 	archive_entry_set_mtime(ae, 7880, 1);
1279 	failure("It should not be excluded");
1280 	assertEqualInt(0, archive_match_time_excluded(m, ae));
1281 	assertEqualInt(0, archive_match_excluded(m, ae));
1282 
1283 
1284 	/* Clean up. */
1285 	archive_entry_free(ae);
1286 }
1287 
1288 static void
test_pathname_newer_mtime(void)1289 test_pathname_newer_mtime(void)
1290 {
1291 	struct archive_entry *ae;
1292 	struct archive *m;
1293 
1294 	if (!assert((m = archive_match_new()) != NULL))
1295 		return;
1296 	if (!assert((ae = archive_entry_new()) != NULL)) {
1297 		archive_match_free(m);
1298 		return;
1299 	}
1300 
1301 	archive_entry_copy_pathname(ae, "file1");
1302 	archive_entry_set_mtime(ae, 7880, 0);
1303 	assertEqualIntA(m, 0, archive_match_exclude_entry(m,
1304 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
1305 	    ARCHIVE_MATCH_EQUAL, ae));
1306 	archive_entry_copy_pathname(ae, "file2");
1307 	archive_entry_set_mtime(ae, 1, 0);
1308 	assertEqualIntA(m, 0, archive_match_exclude_entry(m,
1309 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
1310 	    ARCHIVE_MATCH_EQUAL, ae));
1311 	archive_entry_copy_pathname(ae, "file3");
1312 	archive_entry_set_mtime(ae, 99999, 0);
1313 	assertEqualIntA(m, 0, archive_match_exclude_entry(m,
1314 	    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER |
1315 	    ARCHIVE_MATCH_EQUAL, ae));
1316 
1317 	excluded(m);
1318 
1319 	/* Clean up. */
1320 	archive_entry_free(ae);
1321 	archive_match_free(m);
1322 }
1323 
DEFINE_TEST(test_archive_match_time)1324 DEFINE_TEST(test_archive_match_time)
1325 {
1326 	struct stat st;
1327 
1328 	/* Test: matching newer times. */
1329 	test_newer_time();
1330 	test_newer_time_str();
1331 	test_newer_time_str_w();
1332 	/* Test: matching older times. */
1333 	test_older_time();
1334 	test_older_time_str();
1335 	test_older_time_str_w();
1336 
1337 	/*
1338 	 * Create sample files for tests matching mtime.
1339 	 * ctimes of those files may be all the same or the ctime of
1340 	 * new_mtime may be older than old_mtime.
1341 	 */
1342 	assertMakeFile("new_mtime", 0666, "new");
1343 	assertUtimes("new_mtime", 10002, 0, 10002, 0);
1344 	assertMakeFile("mid_mtime", 0666, "mid");
1345 	assertUtimes("mid_mtime", 10001, 0, 10001, 0);
1346 	assertMakeFile("old_mtime", 0666, "old");
1347 	assertUtimes("old_mtime", 10000, 0, 10000, 0);
1348 
1349 	/*
1350 	 * Create sample files for tests matching ctime.
1351 	 * the mtime of mid_ctime is older than old_ctime and also the mtime
1352 	 * of new_ctime is older than both mid_ctime and old_ctime.
1353 	 */
1354 	assertMakeFile("old_ctime", 0666, "old");
1355 	assertUtimes("old_ctime", 10002, 0, 10002, 0);
1356 	assertEqualInt(0, stat("old_ctime", &st));
1357 	sleepUntilAfter(st.st_ctime);
1358 	assertMakeFile("mid_ctime", 0666, "mid");
1359 	assertUtimes("mid_ctime", 10001, 0, 10001, 0);
1360 	assertEqualInt(0, stat("mid_ctime", &st));
1361 	sleepUntilAfter(st.st_ctime);
1362 	assertMakeFile("new_ctime", 0666, "new");
1363 	assertUtimes("new_ctime", 10000, 0, 10000, 0);
1364 
1365 	/*
1366 	 * Test: matching mtime which indicated by files on the disk.
1367 	 */
1368 	test_newer_mtime_than_file_mbs();
1369 	test_newer_mtime_than_file_wcs();
1370 	test_older_mtime_than_file_mbs();
1371 	test_older_mtime_than_file_wcs();
1372 	test_mtime_between_files_mbs();
1373 	test_mtime_between_files_wcs();
1374 
1375 	/*
1376 	 * Test: matching ctime which indicated by files on the disk.
1377 	 */
1378 	test_newer_ctime_than_file_mbs();
1379 	test_newer_ctime_than_file_wcs();
1380 	test_older_ctime_than_file_mbs();
1381 	test_older_ctime_than_file_wcs();
1382 	test_ctime_between_files_mbs();
1383 	test_ctime_between_files_wcs();
1384 
1385 	/* Test: matching both pathname and mtime. */
1386 	test_pathname_newer_mtime();
1387 }
1388