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