test_write_format_pax.c revision 302001
16751Swpaul/*-
250479Speter * Copyright (c) 2003-2008 Tim Kientzle
36751Swpaul * All rights reserved.
4156813Sru *
5156813Sru * Redistribution and use in source and binary forms, with or without
66751Swpaul * modification, are permitted provided that the following conditions
780029Sobrien * are met:
835891Sbde * 1. Redistributions of source code must retain the above copyright
935891Sbde *    notice, this list of conditions and the following disclaimer.
1080029Sobrien * 2. Redistributions in binary form must reproduce the above copyright
116798Sphk *    notice, this list of conditions and the following disclaimer in the
12156813Sru *    documentation and/or other materials provided with the distribution.
13137675Sbz *
14137675Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1580029Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1635891Sbde * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
176751Swpaul * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
189394Sbde * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
196751Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2035891Sbde * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231118Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
226751Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2335891Sbde * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231118Sdim */
256751Swpaul#include "test.h"
269394Sbde__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/test/test_write_format_pax.c 302001 2016-06-17 22:40:10Z mm $");
27231118Sdim
289394Sbdestatic char buff2[64];
296751Swpaul
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	int i;
38	char nulls[1024];
39	int64_t offset, length;
40
41	buff = malloc(buffsize); /* million bytes of work area */
42	assert(buff != NULL);
43
44	/* Create a new archive in memory. */
45	assert((a = archive_write_new()) != NULL);
46	assertA(0 == archive_write_set_format_pax(a));
47	assertA(0 == archive_write_add_filter_none(a));
48	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
49
50	/*
51	 * "file" has a bunch of attributes and 8 bytes of data.
52	 */
53	assert((ae = archive_entry_new()) != NULL);
54	archive_entry_set_atime(ae, 2, 20);
55	archive_entry_set_birthtime(ae, 3, 30);
56	archive_entry_set_ctime(ae, 4, 40);
57	archive_entry_set_mtime(ae, 5, 50);
58	archive_entry_copy_pathname(ae, "file");
59	archive_entry_set_mode(ae, S_IFREG | 0755);
60	archive_entry_set_size(ae, 8);
61	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
62	archive_entry_free(ae);
63	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
64
65	/*
66	 * "file2" is similar but has birthtime later than mtime.
67	 */
68	assert((ae = archive_entry_new()) != NULL);
69	archive_entry_set_atime(ae, 2, 20);
70	archive_entry_set_birthtime(ae, 8, 80);
71	archive_entry_set_ctime(ae, 4, 40);
72	archive_entry_set_mtime(ae, 5, 50);
73	archive_entry_copy_pathname(ae, "file2");
74	archive_entry_set_mode(ae, S_IFREG | 0755);
75	archive_entry_set_size(ae, 8);
76	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
77	archive_entry_free(ae);
78	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
79
80	/*
81	 * "file3" is sparse file and has hole size of which is
82	 * 1024000 bytes, and has 8 bytes data after the hole.
83	 */
84	assert((ae = archive_entry_new()) != NULL);
85	archive_entry_set_atime(ae, 2, 20);
86	archive_entry_set_birthtime(ae, 3, 30);
87	archive_entry_set_ctime(ae, 4, 40);
88	archive_entry_set_mtime(ae, 5, 50);
89	archive_entry_copy_pathname(ae, "file3");
90	archive_entry_set_mode(ae, S_IFREG | 0755);
91	archive_entry_set_size(ae, 1024008);
92	archive_entry_sparse_add_entry(ae, 1024000, 8);
93	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
94	archive_entry_free(ae);
95	memset(nulls, 0, sizeof(nulls));
96	for (i = 0; i < 1024000; i += 1024)
97		/* write hole data, which won't be stored into an archive file. */
98		assertEqualIntA(a, 1024, archive_write_data(a, nulls, 1024));
99	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
100
101	/*
102	 * XXX TODO XXX Archive directory, other file types.
103	 * Archive extended attributes, ACLs, other metadata.
104	 * Verify they get read back correctly.
105	 */
106
107	/* Close out the archive. */
108	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
109	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
110
111	/*
112	 *
113	 * Now, read the data back.
114	 *
115	 */
116	assert((a = archive_read_new()) != NULL);
117	assertEqualIntA(a, 0, archive_read_support_format_all(a));
118	assertEqualIntA(a, 0, archive_read_support_filter_all(a));
119	assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
120
121	/*
122	 * Read "file"
123	 */
124	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
125	assertEqualInt(2, archive_entry_atime(ae));
126	assertEqualInt(20, archive_entry_atime_nsec(ae));
127	assertEqualInt(3, archive_entry_birthtime(ae));
128	assertEqualInt(30, archive_entry_birthtime_nsec(ae));
129	assertEqualInt(4, archive_entry_ctime(ae));
130	assertEqualInt(40, archive_entry_ctime_nsec(ae));
131	assertEqualInt(5, archive_entry_mtime(ae));
132	assertEqualInt(50, archive_entry_mtime_nsec(ae));
133	assertEqualString("file", archive_entry_pathname(ae));
134	assert((S_IFREG | 0755) == archive_entry_mode(ae));
135	assertEqualInt(8, archive_entry_size(ae));
136	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
137	assertEqualMem(buff2, "12345678", 8);
138
139	/*
140	 * Read "file2"
141	 */
142	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
143	assert(archive_entry_atime_is_set(ae));
144	assertEqualInt(2, archive_entry_atime(ae));
145	assertEqualInt(20, archive_entry_atime_nsec(ae));
146	/* Birthtime > mtime above, so it doesn't get stored at all. */
147	assert(!archive_entry_birthtime_is_set(ae));
148	assertEqualInt(0, archive_entry_birthtime(ae));
149	assertEqualInt(0, archive_entry_birthtime_nsec(ae));
150	assert(archive_entry_ctime_is_set(ae));
151	assertEqualInt(4, archive_entry_ctime(ae));
152	assertEqualInt(40, archive_entry_ctime_nsec(ae));
153	assert(archive_entry_mtime_is_set(ae));
154	assertEqualInt(5, archive_entry_mtime(ae));
155	assertEqualInt(50, archive_entry_mtime_nsec(ae));
156	assertEqualString("file2", archive_entry_pathname(ae));
157	assert((S_IFREG | 0755) == archive_entry_mode(ae));
158	assertEqualInt(8, archive_entry_size(ae));
159	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
160	assertEqualMem(buff2, "12345678", 8);
161
162	/*
163	 * Read "file3"
164	 */
165	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
166	assertEqualInt(2, archive_entry_atime(ae));
167	assertEqualInt(20, archive_entry_atime_nsec(ae));
168	assertEqualInt(3, archive_entry_birthtime(ae));
169	assertEqualInt(30, archive_entry_birthtime_nsec(ae));
170	assertEqualInt(4, archive_entry_ctime(ae));
171	assertEqualInt(40, archive_entry_ctime_nsec(ae));
172	assertEqualInt(5, archive_entry_mtime(ae));
173	assertEqualInt(50, archive_entry_mtime_nsec(ae));
174	assertEqualString("file3", archive_entry_pathname(ae));
175	assert((S_IFREG | 0755) == archive_entry_mode(ae));
176	assertEqualInt(1024008, archive_entry_size(ae));
177	assertEqualInt(1, archive_entry_sparse_reset(ae));
178	assertEqualInt(ARCHIVE_OK,
179	    archive_entry_sparse_next(ae, &offset, &length));
180	assertEqualInt(1024000, offset);
181	assertEqualInt(8, length);
182	for (i = 0; i < 1024000; i += 1024) {
183		int j;
184		assertEqualIntA(a, 1024, archive_read_data(a, nulls, 1024));
185		for (j = 0; j < 1024; j++)
186			assertEqualInt(0, nulls[j]);
187	}
188	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
189	assertEqualMem(buff2, "12345678", 8);
190
191	/*
192	 * Verify the end of the archive.
193	 */
194	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
195	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
196	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
197
198	free(buff);
199}
200