xref: /freebsd/contrib/libarchive/tar/test/test_option_mtime.c (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2025 Zhaofeng Li
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 #if defined(_WIN32) && !defined(__CYGWIN__)
10 /* system() on Windows runs its arguments through CMD.EXE, which has
11  * notoriously unfriendly quoting rules. The current best documented way around
12  * them is to wrap your *entire commandline* in sacrificial quotes.
13  *
14  * See CMD.EXE /? for more information. Excerpted here:
15  * | Otherwise, old behavior is to see if the first character is
16  * | a quote character and if so, strip the leading character and
17  * | remove the last quote character on the command line, preserving
18  * | any text after the last quote character.
19  *
20  * Since this test makes heavy use of systemf() with quoted arguments inside
21  * the commandline, this macro is unfortunately an easier workaround.
22  */
23 #define systemf(command, ...) systemf("\"" command "\"", __VA_ARGS__)
24 #endif
25 
26 DEFINE_TEST(test_option_mtime)
27 {
28 	/* Create three files with different mtimes. */
29 	assertMakeDir("in", 0755);
30 	assertChdir("in");
31 	assertMakeFile("new_mtime", 0666, "new");
32 	assertUtimes("new_mtime", 100000, 0, 100000, 0);
33 	assertMakeFile("mid_mtime", 0666, "mid");
34 	assertUtimes("mid_mtime", 10000, 0, 10000, 0);
35 	assertMakeFile("old_mtime", 0666, "old");
36 	// assertion_utimes silently discards 0 :(
37 	assertUtimes("old_mtime", 1, 0, 1, 0);
38 
39 	/* Archive with --mtime 86400 */
40 	assertEqualInt(0,
41 		systemf("%s --format pax -cf ../noclamp.tar "
42 			"--mtime \"1970/1/2 0:0:0 UTC\" .",
43 			testprog));
44 	assertChdir("..");
45 
46 	assertMakeDir("out.noclamp", 0755);
47 	assertChdir("out.noclamp");
48 	assertEqualInt(0, systemf("%s xf ../noclamp.tar", testprog));
49 	assertFileMtime("new_mtime", 86400, 0);
50 	assertFileMtime("mid_mtime", 86400, 0);
51 	assertFileMtime("old_mtime", 86400, 0);
52 	assertChdir("..");
53 
54 	/* Archive with --mtime 86400 --clamp-mtime */
55 	assertChdir("in");
56 	assertEqualInt(0,
57 		systemf("%s --format pax -cf ../clamp.tar "
58 			"--mtime \"1970/1/2 0:0:0 UTC\" --clamp-mtime .",
59 			testprog));
60 	assertChdir("..");
61 
62 	assertMakeDir("out.clamp", 0755);
63 	assertChdir("out.clamp");
64 	assertEqualInt(0, systemf("%s xf ../clamp.tar", testprog));
65 	assertFileMtime("new_mtime", 86400, 0);
66 	assertFileMtime("mid_mtime", 10000, 0);
67 	assertFileMtime("old_mtime", 1, 0);
68 	assertChdir("..");
69 
70 	/* Archive-to-archive copy with --mtime 0 */
71 	assertEqualInt(0,
72 		systemf("%s --format pax -cf ./archive2archive.tar "
73 			"--mtime \"1970/1/1 0:0:0 UTC\" @noclamp.tar",
74 			testprog));
75 	assertMakeDir("out.archive2archive", 0755);
76 	assertChdir("out.archive2archive");
77 	assertEqualInt(0, systemf("%s xf ../archive2archive.tar", testprog));
78 	assertFileMtime("new_mtime", 0, 0);
79 	assertFileMtime("mid_mtime", 0, 0);
80 	assertFileMtime("old_mtime", 0, 0);
81 	assertChdir("..");
82 }
83