test_write_format_gnutar.c revision 232153
1/*-
2 * Copyright (c) 2010 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
30/* Some names 1026 characters long */
31static const char *longfilename = "abcdefghijklmnopqrstuvwxyz"
32    "12345678901234567890123456789012345678901234567890"
33    "12345678901234567890123456789012345678901234567890"
34    "12345678901234567890123456789012345678901234567890"
35    "12345678901234567890123456789012345678901234567890"
36    "12345678901234567890123456789012345678901234567890"
37    "12345678901234567890123456789012345678901234567890"
38    "12345678901234567890123456789012345678901234567890"
39    "12345678901234567890123456789012345678901234567890"
40    "12345678901234567890123456789012345678901234567890"
41    "12345678901234567890123456789012345678901234567890"
42    "12345678901234567890123456789012345678901234567890"
43    "12345678901234567890123456789012345678901234567890"
44    "12345678901234567890123456789012345678901234567890"
45    "12345678901234567890123456789012345678901234567890"
46    "12345678901234567890123456789012345678901234567890"
47    "12345678901234567890123456789012345678901234567890"
48    "12345678901234567890123456789012345678901234567890"
49    "12345678901234567890123456789012345678901234567890"
50    "12345678901234567890123456789012345678901234567890"
51    "12345678901234567890123456789012345678901234567890";
52
53static const char *longlinkname = "Xabcdefghijklmnopqrstuvwxyz"
54    "12345678901234567890123456789012345678901234567890"
55    "12345678901234567890123456789012345678901234567890"
56    "12345678901234567890123456789012345678901234567890"
57    "12345678901234567890123456789012345678901234567890"
58    "12345678901234567890123456789012345678901234567890"
59    "12345678901234567890123456789012345678901234567890"
60    "12345678901234567890123456789012345678901234567890"
61    "12345678901234567890123456789012345678901234567890"
62    "12345678901234567890123456789012345678901234567890"
63    "12345678901234567890123456789012345678901234567890"
64    "12345678901234567890123456789012345678901234567890"
65    "12345678901234567890123456789012345678901234567890"
66    "12345678901234567890123456789012345678901234567890"
67    "12345678901234567890123456789012345678901234567890"
68    "12345678901234567890123456789012345678901234567890"
69    "12345678901234567890123456789012345678901234567890"
70    "12345678901234567890123456789012345678901234567890"
71    "12345678901234567890123456789012345678901234567890"
72    "12345678901234567890123456789012345678901234567890"
73    "12345678901234567890123456789012345678901234567890";
74
75static const char *longhardlinkname = "Yabcdefghijklmnopqrstuvwxyz"
76    "12345678901234567890123456789012345678901234567890"
77    "12345678901234567890123456789012345678901234567890"
78    "12345678901234567890123456789012345678901234567890"
79    "12345678901234567890123456789012345678901234567890"
80    "12345678901234567890123456789012345678901234567890"
81    "12345678901234567890123456789012345678901234567890"
82    "12345678901234567890123456789012345678901234567890"
83    "12345678901234567890123456789012345678901234567890"
84    "12345678901234567890123456789012345678901234567890"
85    "12345678901234567890123456789012345678901234567890"
86    "12345678901234567890123456789012345678901234567890"
87    "12345678901234567890123456789012345678901234567890"
88    "12345678901234567890123456789012345678901234567890"
89    "12345678901234567890123456789012345678901234567890"
90    "12345678901234567890123456789012345678901234567890"
91    "12345678901234567890123456789012345678901234567890"
92    "12345678901234567890123456789012345678901234567890"
93    "12345678901234567890123456789012345678901234567890"
94    "12345678901234567890123456789012345678901234567890"
95    "12345678901234567890123456789012345678901234567890";
96
97
98DEFINE_TEST(test_write_format_gnutar)
99{
100	size_t buffsize = 1000000;
101	char *buff;
102	struct archive_entry *ae;
103	struct archive *a;
104	size_t used;
105
106	buff = malloc(buffsize); /* million bytes of work area */
107	assert(buff != NULL);
108
109	/* Create a new archive in memory. */
110	assert((a = archive_write_new()) != NULL);
111	assertA(0 == archive_write_set_format_gnutar(a));
112	assertA(0 == archive_write_set_compression_none(a));
113	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
114
115	/*
116	 * "file" has a bunch of attributes and 8 bytes of data.
117	 */
118	assert((ae = archive_entry_new()) != NULL);
119	archive_entry_set_atime(ae, 2, 20);
120	archive_entry_set_birthtime(ae, 3, 30);
121	archive_entry_set_ctime(ae, 4, 40);
122	archive_entry_set_mtime(ae, 5, 50);
123	archive_entry_copy_pathname(ae, "file");
124	archive_entry_set_mode(ae, S_IFREG | 0755);
125	archive_entry_set_size(ae, 8);
126	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
127	archive_entry_free(ae);
128	assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
129
130	/*
131	 * A file with a very long name
132	 */
133	assert((ae = archive_entry_new()) != NULL);
134	archive_entry_copy_pathname(ae, longfilename);
135	archive_entry_set_mode(ae, S_IFREG | 0755);
136	archive_entry_set_size(ae, 8);
137	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
138	archive_entry_free(ae);
139	assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
140
141	/*
142	 * A hardlink to the above file.
143	 */
144	assert((ae = archive_entry_new()) != NULL);
145	archive_entry_copy_pathname(ae, longhardlinkname);
146	archive_entry_copy_hardlink(ae, longfilename);
147	archive_entry_set_mode(ae, S_IFREG | 0755);
148	archive_entry_set_size(ae, 8);
149	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
150	archive_entry_free(ae);
151
152	/*
153	 * A symlink to the above file.
154	 */
155	assert((ae = archive_entry_new()) != NULL);
156	archive_entry_copy_pathname(ae, longlinkname);
157	archive_entry_copy_symlink(ae, longfilename);
158	archive_entry_set_mode(ae, AE_IFLNK | 0755);
159	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
160	archive_entry_free(ae);
161
162	/* TODO: support GNU tar sparse format and test it here. */
163	/* See test_write_format_pax for an example of testing sparse files. */
164
165	/* Close out the archive. */
166	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
167	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
168
169	/*
170	 * Some basic verification of the low-level format.
171	 */
172
173	/* Verify GNU tar magic/version fields */
174	assertEqualMem(buff + 257, "ustar  \0", 8);
175
176	assertEqualInt(14336, used);
177
178	/*
179	 *
180	 * Now, read the data back.
181	 *
182	 */
183	assert((a = archive_read_new()) != NULL);
184	assertEqualIntA(a, 0, archive_read_support_format_all(a));
185	assertEqualIntA(a, 0, archive_read_support_filter_all(a));
186	assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
187
188	/*
189	 * Read "file"
190	 */
191	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
192	assert(!archive_entry_atime_is_set(ae));
193	assert(!archive_entry_birthtime_is_set(ae));
194	assert(!archive_entry_ctime_is_set(ae));
195	assertEqualInt(5, archive_entry_mtime(ae));
196	assertEqualInt(0, archive_entry_mtime_nsec(ae));
197	assertEqualString("file", archive_entry_pathname(ae));
198	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
199	assertEqualInt(8, archive_entry_size(ae));
200	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
201	assertEqualMem(buff2, "12345678", 8);
202
203	/*
204	 * Read file with very long name.
205	 */
206	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
207	assertEqualString(longfilename, archive_entry_pathname(ae));
208	assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
209	assertEqualInt(8, archive_entry_size(ae));
210	assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
211	assertEqualMem(buff2, "abcdefgh", 8);
212
213	/*
214	 * Read hardlink.
215	 */
216	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
217	assertEqualString(longhardlinkname, archive_entry_pathname(ae));
218	assertEqualString(longfilename, archive_entry_hardlink(ae));
219
220	/*
221	 * Read symlink.
222	 */
223	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
224	assertEqualString(longlinkname, archive_entry_pathname(ae));
225	assertEqualString(longfilename, archive_entry_symlink(ae));
226	assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
227
228	/*
229	 * Verify the end of the archive.
230	 */
231	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
232	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
233	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
234
235	free(buff);
236}
237