1228753Smm/*-
2228753Smm * Copyright (c) 2003-2008 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD: releng/10.2/contrib/libarchive/libarchive/test/test_write_disk_times.c 232153 2012-02-25 10:58:02Z mm $");
27228753Smm
28228753Smm/*
29228753Smm * Exercise time restores in archive_write_disk(), including
30228753Smm * correct handling of omitted time values.
31228753Smm * On FreeBSD, we also test birthtime and high-res time restores.
32228753Smm */
33228753Smm
34228753SmmDEFINE_TEST(test_write_disk_times)
35228753Smm{
36228753Smm	struct archive *a;
37228753Smm	struct archive_entry *ae;
38228753Smm
39228753Smm	/* Create an archive_write_disk object. */
40228753Smm	assert((a = archive_write_disk_new()) != NULL);
41228753Smm	assertEqualInt(ARCHIVE_OK,
42228753Smm	    archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME));
43228753Smm
44228753Smm	/*
45228753Smm	 * Easy case: mtime and atime both specified.
46228753Smm	 */
47228753Smm	assert((ae = archive_entry_new()) != NULL);
48228753Smm	archive_entry_copy_pathname(ae, "file1");
49228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
50228753Smm	archive_entry_set_atime(ae, 123456, 0);
51228753Smm	archive_entry_set_mtime(ae, 234567, 0);
52228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
53228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
54228753Smm	archive_entry_free(ae);
55228753Smm	/* Verify */
56228753Smm	assertFileAtime("file1", 123456, 0);
57228753Smm	assertFileMtime("file1", 234567, 0);
58228753Smm
59228753Smm	/*
60228753Smm	 * mtime specified, but not atime
61228753Smm	 */
62228753Smm	assert((ae = archive_entry_new()) != NULL);
63228753Smm	archive_entry_copy_pathname(ae, "file2");
64228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
65228753Smm	archive_entry_set_mtime(ae, 234567, 0);
66228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
67228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
68228753Smm	archive_entry_free(ae);
69228753Smm	assertFileMtime("file2", 234567, 0);
70228753Smm	assertFileAtimeRecent("file2");
71228753Smm
72228753Smm	/*
73228753Smm	 * atime specified, but not mtime
74228753Smm	 */
75228753Smm	assert((ae = archive_entry_new()) != NULL);
76228753Smm	archive_entry_copy_pathname(ae, "file3");
77228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
78228753Smm	archive_entry_set_atime(ae, 345678, 0);
79228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
80228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
81228753Smm	archive_entry_free(ae);
82228753Smm	/* Verify: Current mtime and atime as specified. */
83228753Smm	assertFileAtime("file3", 345678, 0);
84228753Smm	assertFileMtimeRecent("file3");
85228753Smm
86228753Smm	/*
87228753Smm	 * Neither atime nor mtime specified.
88228753Smm	 */
89228753Smm	assert((ae = archive_entry_new()) != NULL);
90228753Smm	archive_entry_copy_pathname(ae, "file4");
91228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
92228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
93228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
94228753Smm	archive_entry_free(ae);
95228753Smm	/* Verify: Current mtime and atime. */
96228753Smm	assertFileAtimeRecent("file4");
97228753Smm	assertFileMtimeRecent("file4");
98228753Smm
99228753Smm#if defined(__FreeBSD__)
100228753Smm	/*
101228753Smm	 * High-res mtime and atime on FreeBSD.
102228753Smm	 */
103228753Smm	assert((ae = archive_entry_new()) != NULL);
104228753Smm	archive_entry_copy_pathname(ae, "file10");
105228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
106228753Smm	archive_entry_set_atime(ae, 1234567, 23456);
107228753Smm	archive_entry_set_mtime(ae, 2345678, 4567);
108228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
109228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
110228753Smm	archive_entry_free(ae);
111228753Smm	/* Verify */
112228753Smm	assertFileMtime("file10", 2345678, 4567);
113228753Smm	assertFileAtime("file10", 1234567, 23456);
114228753Smm
115228753Smm	/*
116228753Smm	 * Birthtime, mtime and atime on FreeBSD
117228753Smm	 */
118228753Smm	assert((ae = archive_entry_new()) != NULL);
119228753Smm	archive_entry_copy_pathname(ae, "file11");
120228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
121228753Smm	archive_entry_set_atime(ae, 1234567, 23456);
122228753Smm	archive_entry_set_birthtime(ae, 3456789, 12345);
123228753Smm	/* mtime must be later than birthtime! */
124228753Smm	archive_entry_set_mtime(ae, 12345678, 4567);
125228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
126228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
127228753Smm	archive_entry_free(ae);
128228753Smm	/* Verify */
129228753Smm	assertFileAtime("file11", 1234567, 23456);
130228753Smm	assertFileBirthtime("file11", 3456789, 12345);
131228753Smm	assertFileMtime("file11", 12345678, 4567);
132228753Smm
133228753Smm	/*
134228753Smm	 * Birthtime only on FreeBSD.
135228753Smm	 */
136228753Smm	assert((ae = archive_entry_new()) != NULL);
137228753Smm	archive_entry_copy_pathname(ae, "file12");
138228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
139228753Smm	archive_entry_set_birthtime(ae, 3456789, 12345);
140228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
141228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
142228753Smm	archive_entry_free(ae);
143228753Smm	/* Verify */
144228753Smm	assertFileAtimeRecent("file12");
145228753Smm	assertFileBirthtime("file12", 3456789, 12345);
146228753Smm	assertFileMtimeRecent("file12");
147228753Smm
148228753Smm	/*
149228753Smm	 * mtime only on FreeBSD.
150228753Smm	 */
151228753Smm	assert((ae = archive_entry_new()) != NULL);
152228753Smm	archive_entry_copy_pathname(ae, "file13");
153228753Smm	archive_entry_set_mode(ae, S_IFREG | 0777);
154228753Smm	archive_entry_set_mtime(ae, 4567890, 23456);
155228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
156228753Smm	assertEqualInt(ARCHIVE_OK, archive_write_finish_entry(a));
157228753Smm	archive_entry_free(ae);
158228753Smm	/* Verify */
159228753Smm	assertFileAtimeRecent("file13");
160228753Smm	assertFileBirthtime("file13", 4567890, 23456);
161228753Smm	assertFileMtime("file13", 4567890, 23456);
162228753Smm#else
163228753Smm	skipping("Platform-specific time restore tests");
164228753Smm#endif
165228753Smm
166232153Smm	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
167228753Smm}
168