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
28char buff2[64];
29
30DEFINE_TEST(test_write_format_pax)
31{
32	size_t buffsize = 1000000;
33	char *buff;
34	struct archive_entry *ae;
35	struct archive *a;
36	size_t used;
37
38	buff = malloc(buffsize); /* million bytes of work area */
39	assert(buff != NULL);
40
41	/* Create a new archive in memory. */
42	assert((a = archive_write_new()) != NULL);
43	assertA(0 == archive_write_set_format_pax(a));
44	assertA(0 == archive_write_set_compression_none(a));
45	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
46
47	/*
48	 * "file" has a bunch of attributes and 8 bytes of data.
49	 */
50	assert((ae = archive_entry_new()) != NULL);
51	archive_entry_set_atime(ae, 2, 20);
52	archive_entry_set_birthtime(ae, 3, 30);
53	archive_entry_set_ctime(ae, 4, 40);
54	archive_entry_set_mtime(ae, 5, 50);
55	archive_entry_copy_pathname(ae, "file");
56	archive_entry_set_mode(ae, S_IFREG | 0755);
57	archive_entry_set_size(ae, 8);
58	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
59	archive_entry_free(ae);
60	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
61
62	/*
63	 * "file2" is similar but has birthtime later than mtime.
64	 */
65	assert((ae = archive_entry_new()) != NULL);
66	archive_entry_set_atime(ae, 2, 20);
67	archive_entry_set_birthtime(ae, 8, 80);
68	archive_entry_set_ctime(ae, 4, 40);
69	archive_entry_set_mtime(ae, 5, 50);
70	archive_entry_copy_pathname(ae, "file2");
71	archive_entry_set_mode(ae, S_IFREG | 0755);
72	archive_entry_set_size(ae, 8);
73	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
74	archive_entry_free(ae);
75	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
76
77	/*
78	 * XXX TODO XXX Archive directory, other file types.
79	 * Archive extended attributes, ACLs, other metadata.
80	 * Verify they get read back correctly.
81	 */
82
83	/* Close out the archive. */
84	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
85	assertEqualIntA(a, ARCHIVE_OK, archive_write_finish(a));
86
87	/*
88	 *
89	 * Now, read the data back.
90	 *
91	 */
92	assert((a = archive_read_new()) != NULL);
93	assertEqualIntA(a, 0, archive_read_support_format_all(a));
94	assertEqualIntA(a, 0, archive_read_support_compression_all(a));
95	assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
96
97	/*
98	 * Read "file"
99	 */
100	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
101	assertEqualInt(2, archive_entry_atime(ae));
102	assertEqualInt(20, archive_entry_atime_nsec(ae));
103	assertEqualInt(3, archive_entry_birthtime(ae));
104	assertEqualInt(30, archive_entry_birthtime_nsec(ae));
105	assertEqualInt(4, archive_entry_ctime(ae));
106	assertEqualInt(40, archive_entry_ctime_nsec(ae));
107	assertEqualInt(5, archive_entry_mtime(ae));
108	assertEqualInt(50, archive_entry_mtime_nsec(ae));
109	assertEqualString("file", archive_entry_pathname(ae));
110	assert((S_IFREG | 0755) == archive_entry_mode(ae));
111	assertEqualInt(8, archive_entry_size(ae));
112	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
113	assertEqualMem(buff2, "12345678", 8);
114
115	/*
116	 * Read "file2"
117	 */
118	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
119	assert(archive_entry_atime_is_set(ae));
120	assertEqualInt(2, archive_entry_atime(ae));
121	assertEqualInt(20, archive_entry_atime_nsec(ae));
122	/* Birthtime > mtime above, so it doesn't get stored at all. */
123	assert(!archive_entry_birthtime_is_set(ae));
124	assertEqualInt(0, archive_entry_birthtime(ae));
125	assertEqualInt(0, archive_entry_birthtime_nsec(ae));
126	assert(archive_entry_ctime_is_set(ae));
127	assertEqualInt(4, archive_entry_ctime(ae));
128	assertEqualInt(40, archive_entry_ctime_nsec(ae));
129	assert(archive_entry_mtime_is_set(ae));
130	assertEqualInt(5, archive_entry_mtime(ae));
131	assertEqualInt(50, archive_entry_mtime_nsec(ae));
132	assertEqualString("file2", archive_entry_pathname(ae));
133	assert((S_IFREG | 0755) == archive_entry_mode(ae));
134	assertEqualInt(8, archive_entry_size(ae));
135	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
136	assertEqualMem(buff2, "12345678", 8);
137
138	/*
139	 * Verify the end of the archive.
140	 */
141	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
142	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
143	assertEqualIntA(a, ARCHIVE_OK, archive_read_finish(a));
144
145	free(buff);
146}
147