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__FBSDID("$FreeBSD$");
27
28/*
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.
32 */
33
34DEFINE_TEST(test_write_disk_times)
35{
36	struct archive *a;
37	struct archive_entry *ae;
38
39	/* Create an archive_write_disk object. */
40	assert((a = archive_write_disk_new()) != NULL);
41	assertEqualInt(ARCHIVE_OK,
42	    archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME));
43
44	/*
45	 * Easy case: mtime and atime both specified.
46	 */
47	assert((ae = archive_entry_new()) != NULL);
48	archive_entry_copy_pathname(ae, "file1");
49	archive_entry_set_mode(ae, S_IFREG | 0777);
50	archive_entry_set_atime(ae, 123456, 0);
51	archive_entry_set_mtime(ae, 234567, 0);
52	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
53	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
54	archive_entry_free(ae);
55	/* Verify */
56	assertFileAtime("file1", 123456, 0);
57	assertFileMtime("file1", 234567, 0);
58
59	/*
60	 * mtime specified, but not atime
61	 */
62	assert((ae = archive_entry_new()) != NULL);
63	archive_entry_copy_pathname(ae, "file2");
64	archive_entry_set_mode(ae, S_IFREG | 0777);
65	archive_entry_set_mtime(ae, 234567, 0);
66	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
67	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
68	archive_entry_free(ae);
69	assertFileMtime("file2", 234567, 0);
70	assertFileAtimeRecent("file2");
71
72	/*
73	 * atime specified, but not mtime
74	 */
75	assert((ae = archive_entry_new()) != NULL);
76	archive_entry_copy_pathname(ae, "file3");
77	archive_entry_set_mode(ae, S_IFREG | 0777);
78	archive_entry_set_atime(ae, 345678, 0);
79	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
80	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
81	archive_entry_free(ae);
82	/* Verify: Current mtime and atime as specified. */
83	assertFileAtime("file3", 345678, 0);
84	assertFileMtimeRecent("file3");
85
86	/*
87	 * Neither atime nor mtime specified.
88	 */
89	assert((ae = archive_entry_new()) != NULL);
90	archive_entry_copy_pathname(ae, "file4");
91	archive_entry_set_mode(ae, S_IFREG | 0777);
92	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
93	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
94	archive_entry_free(ae);
95	/* Verify: Current mtime and atime. */
96	assertFileAtimeRecent("file4");
97	assertFileMtimeRecent("file4");
98
99#if defined(__FreeBSD__)
100	/*
101	 * High-res mtime and atime on FreeBSD.
102	 */
103	assert((ae = archive_entry_new()) != NULL);
104	archive_entry_copy_pathname(ae, "file10");
105	archive_entry_set_mode(ae, S_IFREG | 0777);
106	archive_entry_set_atime(ae, 1234567, 23456);
107	archive_entry_set_mtime(ae, 2345678, 4567);
108	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
109	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
110	archive_entry_free(ae);
111	/* Verify */
112	assertFileMtime("file10", 2345678, 4567);
113	assertFileAtime("file10", 1234567, 23456);
114
115	/*
116	 * Birthtime, mtime and atime on FreeBSD
117	 */
118	assert((ae = archive_entry_new()) != NULL);
119	archive_entry_copy_pathname(ae, "file11");
120	archive_entry_set_mode(ae, S_IFREG | 0777);
121	archive_entry_set_atime(ae, 1234567, 23456);
122	archive_entry_set_birthtime(ae, 3456789, 12345);
123	/* mtime must be later than birthtime! */
124	archive_entry_set_mtime(ae, 12345678, 4567);
125	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
126	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
127	archive_entry_free(ae);
128	/* Verify */
129	assertFileAtime("file11", 1234567, 23456);
130	assertFileBirthtime("file11", 3456789, 12345);
131	assertFileMtime("file11", 12345678, 4567);
132
133	/*
134	 * Birthtime only on FreeBSD.
135	 */
136	assert((ae = archive_entry_new()) != NULL);
137	archive_entry_copy_pathname(ae, "file12");
138	archive_entry_set_mode(ae, S_IFREG | 0777);
139	archive_entry_set_birthtime(ae, 3456789, 12345);
140	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
141	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
142	archive_entry_free(ae);
143	/* Verify */
144	assertFileAtimeRecent("file12");
145	assertFileBirthtime("file12", 3456789, 12345);
146	assertFileMtimeRecent("file12");
147
148	/*
149	 * mtime only on FreeBSD.
150	 */
151	assert((ae = archive_entry_new()) != NULL);
152	archive_entry_copy_pathname(ae, "file13");
153	archive_entry_set_mode(ae, S_IFREG | 0777);
154	archive_entry_set_mtime(ae, 4567890, 23456);
155	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
156	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
157	archive_entry_free(ae);
158	/* Verify */
159	assertFileAtimeRecent("file13");
160	assertFileBirthtime("file13", 4567890, 23456);
161	assertFileMtime("file13", 4567890, 23456);
162#else
163	skipping("Platform-specific time restore tests");
164#endif
165
166	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
167}
168