2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
29 * Exercise time restores in archive_write_disk(), including
30 * correct handling of omitted time values.
31 * On FreeBSD, we also test birthtime and high-res time restores.
34 DEFINE_TEST(test_write_disk_times)
37 struct archive_entry *ae;
39 time_t now = time(NULL);
41 /* Create an archive_write_disk object. */
42 assert((a = archive_write_disk_new()) != NULL);
43 assertEqualInt(ARCHIVE_OK,
44 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME));
47 * Easy case: mtime and atime both specified.
49 assert((ae = archive_entry_new()) != NULL);
50 archive_entry_copy_pathname(ae, "file1");
51 archive_entry_set_mode(ae, S_IFREG | 0777);
52 archive_entry_set_atime(ae, 123456, 0);
53 archive_entry_set_mtime(ae, 234567, 0);
54 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
55 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
56 archive_entry_free(ae);
58 assertEqualInt(0, stat("file1", &st));
59 assertEqualInt(123456, st.st_atime);
60 assertEqualInt(234567, st.st_mtime);
63 * mtime specified, but not atime
65 assert((ae = archive_entry_new()) != NULL);
66 archive_entry_copy_pathname(ae, "file2");
67 archive_entry_set_mode(ae, S_IFREG | 0777);
68 archive_entry_set_mtime(ae, 234567, 0);
69 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
70 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
71 archive_entry_free(ae);
72 /* Verify: Current atime and mtime as specified. */
73 assertEqualInt(0, stat("file2", &st));
74 assertEqualInt(234567, st.st_mtime);
75 failure("now: %ld st.st_atime: %ld", (long)now, (long)st.st_atime);
76 assert(st.st_atime >= now && st.st_atime < now + 3);
79 * atime specified, but not mtime
81 assert((ae = archive_entry_new()) != NULL);
82 archive_entry_copy_pathname(ae, "file3");
83 archive_entry_set_mode(ae, S_IFREG | 0777);
84 archive_entry_set_atime(ae, 345678, 0);
85 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
86 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
87 archive_entry_free(ae);
88 /* Verify: Current mtime and atime as specified. */
89 assertEqualInt(0, stat("file3", &st));
90 assertEqualInt(345678, st.st_atime);
91 failure("now: %ld st.st_mtime: %ld", (long)now, (long)st.st_mtime);
92 assert(st.st_mtime >= now && st.st_mtime < now + 3);
95 * Neither atime nor mtime specified.
97 assert((ae = archive_entry_new()) != NULL);
98 archive_entry_copy_pathname(ae, "file4");
99 archive_entry_set_mode(ae, S_IFREG | 0777);
100 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
101 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
102 archive_entry_free(ae);
103 /* Verify: Current mtime and atime. */
104 assertEqualInt(0, stat("file4", &st));
105 failure("now: %ld st.st_atime: %ld", (long)now, (long)st.st_atime);
106 assert(st.st_atime >= now && st.st_atime < now + 3);
107 failure("now: %ld st.st_mtime: %ld", (long)now, (long)st.st_mtime);
108 assert(st.st_mtime >= now && st.st_mtime < now + 3);
110 #if defined(__FreeBSD__)
112 * High-res mtime and atime on FreeBSD.
114 assert((ae = archive_entry_new()) != NULL);
115 archive_entry_copy_pathname(ae, "file10");
116 archive_entry_set_mode(ae, S_IFREG | 0777);
117 archive_entry_set_atime(ae, 1234567, 23456);
118 archive_entry_set_mtime(ae, 2345678, 4567);
119 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
120 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
121 archive_entry_free(ae);
123 /* FreeBSD can only store usec resolution, hence rounding here. */
124 assertEqualInt(0, stat("file10", &st));
125 assertEqualInt(1234567, st.st_atime);
126 assertEqualInt(23000, st.st_atimespec.tv_nsec);
127 assertEqualInt(2345678, st.st_mtime);
128 assertEqualInt(4000, st.st_mtimespec.tv_nsec);
131 * Birthtime, mtime and atime on FreeBSD
133 assert((ae = archive_entry_new()) != NULL);
134 archive_entry_copy_pathname(ae, "file11");
135 archive_entry_set_mode(ae, S_IFREG | 0777);
136 archive_entry_set_atime(ae, 1234567, 23456);
137 archive_entry_set_birthtime(ae, 3456789, 12345);
138 /* mtime must be later than birthtime! */
139 archive_entry_set_mtime(ae, 12345678, 4567);
140 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
141 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
142 archive_entry_free(ae);
144 /* FreeBSD can only store usec resolution, hence rounding here. */
145 assertEqualInt(0, stat("file11", &st));
146 assertEqualInt(1234567, st.st_atime);
147 assertEqualInt(23000, st.st_atimespec.tv_nsec);
148 assertEqualInt(3456789, st.st_birthtime);
149 assertEqualInt(12000, st.st_birthtimespec.tv_nsec);
150 assertEqualInt(12345678, st.st_mtime);
151 assertEqualInt(4000, st.st_mtimespec.tv_nsec);
154 * Birthtime only on FreeBSD.
156 assert((ae = archive_entry_new()) != NULL);
157 archive_entry_copy_pathname(ae, "file12");
158 archive_entry_set_mode(ae, S_IFREG | 0777);
159 archive_entry_set_birthtime(ae, 3456789, 12345);
160 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
161 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
162 archive_entry_free(ae);
164 /* FreeBSD can only store usec resolution, hence rounding here. */
165 assertEqualInt(0, stat("file12", &st));
166 assertEqualInt(3456789, st.st_birthtime);
167 assertEqualInt(12000, st.st_birthtimespec.tv_nsec);
168 failure("now: %ld st.st_atime: %ld", (long)now, (long)st.st_atime);
169 assert(st.st_atime >= now && st.st_atime < now + 3);
170 failure("now: %ld st.st_mtime: %ld", (long)now, (long)st.st_mtime);
171 assert(st.st_mtime >= now && st.st_mtime < now + 3);
174 * mtime only on FreeBSD.
176 assert((ae = archive_entry_new()) != NULL);
177 archive_entry_copy_pathname(ae, "file13");
178 archive_entry_set_mode(ae, S_IFREG | 0777);
179 archive_entry_set_mtime(ae, 4567890, 23456);
180 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
181 assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
182 archive_entry_free(ae);
184 /* FreeBSD can only store usec resolution, hence rounding here. */
185 assertEqualInt(0, stat("file13", &st));
186 assertEqualInt(4567890, st.st_birthtime);
187 assertEqualInt(23000, st.st_birthtimespec.tv_nsec);
188 assertEqualInt(4567890, st.st_mtime);
189 assertEqualInt(23000, st.st_mtimespec.tv_nsec);
190 failure("now: %ld st.st_atime: %ld", (long)now, (long)st.st_atime);
191 assert(st.st_atime >= now && st.st_atime < now + 3);
193 skipping("Platform-specific time restore tests");
196 archive_write_finish(a);