1 /*-
2 * Copyright (c) 2003-2008 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 #include "test.h"
26
27 /*
28 * Exercise time restores in archive_write_disk(), including
29 * correct handling of omitted time values.
30 * On FreeBSD, we also test birthtime and high-res time restores.
31 */
32
DEFINE_TEST(test_write_disk_times)33 DEFINE_TEST(test_write_disk_times)
34 {
35 struct archive *a;
36 struct archive_entry *ae;
37
38 /* Create an archive_write_disk object. */
39 assert((a = archive_write_disk_new()) != NULL);
40 assertEqualInt(ARCHIVE_OK,
41 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME));
42
43 /*
44 * Easy case: mtime and atime both specified.
45 */
46 assert((ae = archive_entry_new()) != NULL);
47 archive_entry_copy_pathname(ae, "file1");
48 archive_entry_set_mode(ae, S_IFREG | 0777);
49 archive_entry_set_atime(ae, 123456, 0);
50 archive_entry_set_mtime(ae, 234567, 0);
51 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
52 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
53 archive_entry_free(ae);
54 /* Verify */
55 assertFileAtime("file1", 123456, 0);
56 assertFileMtime("file1", 234567, 0);
57
58 /*
59 * mtime specified, but not atime
60 */
61 assert((ae = archive_entry_new()) != NULL);
62 archive_entry_copy_pathname(ae, "file2");
63 archive_entry_set_mode(ae, S_IFREG | 0777);
64 archive_entry_set_mtime(ae, 234567, 0);
65 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
66 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
67 archive_entry_free(ae);
68 assertFileMtime("file2", 234567, 0);
69 assertFileAtimeRecent("file2");
70
71 /*
72 * atime specified, but not mtime
73 */
74 assert((ae = archive_entry_new()) != NULL);
75 archive_entry_copy_pathname(ae, "file3");
76 archive_entry_set_mode(ae, S_IFREG | 0777);
77 archive_entry_set_atime(ae, 345678, 0);
78 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
79 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
80 archive_entry_free(ae);
81 /* Verify: Current mtime and atime as specified. */
82 assertFileAtime("file3", 345678, 0);
83 assertFileMtimeRecent("file3");
84
85 /*
86 * Neither atime nor mtime specified.
87 */
88 assert((ae = archive_entry_new()) != NULL);
89 archive_entry_copy_pathname(ae, "file4");
90 archive_entry_set_mode(ae, S_IFREG | 0777);
91 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
92 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
93 archive_entry_free(ae);
94 /* Verify: Current mtime and atime. */
95 assertFileAtimeRecent("file4");
96 assertFileMtimeRecent("file4");
97
98 #if defined(__FreeBSD__)
99 /*
100 * High-res mtime and atime on FreeBSD.
101 */
102 assert((ae = archive_entry_new()) != NULL);
103 archive_entry_copy_pathname(ae, "file10");
104 archive_entry_set_mode(ae, S_IFREG | 0777);
105 archive_entry_set_atime(ae, 1234567, 23456);
106 archive_entry_set_mtime(ae, 2345678, 4567);
107 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
108 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
109 archive_entry_free(ae);
110 /* Verify */
111 assertFileMtime("file10", 2345678, 4567);
112 assertFileAtime("file10", 1234567, 23456);
113
114 /*
115 * Birthtime, mtime and atime on FreeBSD
116 */
117 assert((ae = archive_entry_new()) != NULL);
118 archive_entry_copy_pathname(ae, "file11");
119 archive_entry_set_mode(ae, S_IFREG | 0777);
120 archive_entry_set_atime(ae, 1234567, 23456);
121 archive_entry_set_birthtime(ae, 3456789, 12345);
122 /* mtime must be later than birthtime! */
123 archive_entry_set_mtime(ae, 12345678, 4567);
124 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
125 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
126 archive_entry_free(ae);
127 /* Verify */
128 assertFileAtime("file11", 1234567, 23456);
129 assertFileBirthtime("file11", 3456789, 12345);
130 assertFileMtime("file11", 12345678, 4567);
131
132 /*
133 * Birthtime only on FreeBSD.
134 */
135 assert((ae = archive_entry_new()) != NULL);
136 archive_entry_copy_pathname(ae, "file12");
137 archive_entry_set_mode(ae, S_IFREG | 0777);
138 archive_entry_set_birthtime(ae, 3456789, 12345);
139 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
140 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
141 archive_entry_free(ae);
142 /* Verify */
143 assertFileAtimeRecent("file12");
144 assertFileBirthtime("file12", 3456789, 12345);
145 assertFileMtimeRecent("file12");
146
147 /*
148 * mtime only on FreeBSD.
149 */
150 assert((ae = archive_entry_new()) != NULL);
151 archive_entry_copy_pathname(ae, "file13");
152 archive_entry_set_mode(ae, S_IFREG | 0777);
153 archive_entry_set_mtime(ae, 4567890, 23456);
154 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
155 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
156 archive_entry_free(ae);
157 /* Verify */
158 assertFileAtimeRecent("file13");
159 assertFileBirthtime("file13", 4567890, 23456);
160 assertFileMtime("file13", 4567890, 23456);
161 #else
162 skipping("Platform-specific time restore tests");
163 #endif
164
165 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
166 }
167