xref: /freebsd/contrib/libarchive/tar/test/test_option_mtime.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
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 
DEFINE_TEST(test_option_mtime)9 DEFINE_TEST(test_option_mtime)
10 {
11 	/* Create three files with different mtimes. */
12 	assertMakeDir("in", 0755);
13 	assertChdir("in");
14 	assertMakeFile("new_mtime", 0666, "new");
15 	assertUtimes("new_mtime", 100000, 0, 100000, 0);
16 	assertMakeFile("mid_mtime", 0666, "mid");
17 	assertUtimes("mid_mtime", 10000, 0, 10000, 0);
18 	assertMakeFile("old_mtime", 0666, "old");
19 	// assertion_utimes silently discards 0 :(
20 	assertUtimes("old_mtime", 1, 0, 1, 0);
21 
22 	/* Archive with --mtime 86400 */
23 	assertEqualInt(0,
24 		systemf("%s --format pax -cf ../noclamp.tar "
25 			"--mtime \"1970/1/2 0:0:0 UTC\" .",
26 			testprog));
27 	assertChdir("..");
28 
29 	assertMakeDir("out.noclamp", 0755);
30 	assertChdir("out.noclamp");
31 	assertEqualInt(0, systemf("%s xf ../noclamp.tar", testprog));
32 	assertFileMtime("new_mtime", 86400, 0);
33 	assertFileMtime("mid_mtime", 86400, 0);
34 	assertFileMtime("old_mtime", 86400, 0);
35 	assertChdir("..");
36 
37 	/* Archive with --mtime 86400 --clamp-mtime */
38 	assertChdir("in");
39 	assertEqualInt(0,
40 		systemf("%s --format pax -cf ../clamp.tar "
41 			"--mtime \"1970/1/2 0:0:0 UTC\" --clamp-mtime .",
42 			testprog));
43 	assertChdir("..");
44 
45 	assertMakeDir("out.clamp", 0755);
46 	assertChdir("out.clamp");
47 	assertEqualInt(0, systemf("%s xf ../clamp.tar", testprog));
48 	assertFileMtime("new_mtime", 86400, 0);
49 	assertFileMtime("mid_mtime", 10000, 0);
50 	assertFileMtime("old_mtime", 1, 0);
51 	assertChdir("..");
52 
53 	/* Archive-to-archive copy with --mtime 0 */
54 	assertEqualInt(0,
55 		systemf("%s --format pax -cf ./archive2archive.tar "
56 			"--mtime \"1970/1/1 0:0:0 UTC\" @noclamp.tar",
57 			testprog));
58 	assertMakeDir("out.archive2archive", 0755);
59 	assertChdir("out.archive2archive");
60 	assertEqualInt(0, systemf("%s xf ../archive2archive.tar", testprog));
61 	assertFileMtime("new_mtime", 0, 0);
62 	assertFileMtime("mid_mtime", 0, 0);
63 	assertFileMtime("old_mtime", 0, 0);
64 	assertChdir("..");
65 }
66