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
27 static char buff2[64];
28
DEFINE_TEST(test_write_format_pax)29 DEFINE_TEST(test_write_format_pax)
30 {
31 size_t buffsize = 1000000;
32 char *buff;
33 struct archive_entry *ae;
34 struct archive *a;
35 size_t used;
36 int i;
37 char nulls[1024];
38 int64_t offset, length;
39
40 buff = malloc(buffsize); /* million bytes of work area */
41 assert(buff != NULL);
42
43 /* Create a new archive in memory. */
44 assert((a = archive_write_new()) != NULL);
45 assertA(0 == archive_write_set_format_pax(a));
46 assertA(0 == archive_write_add_filter_none(a));
47 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
48
49 /*
50 * "file" has a bunch of attributes and 8 bytes of data.
51 */
52 assert((ae = archive_entry_new()) != NULL);
53 archive_entry_set_atime(ae, 2, 20);
54 archive_entry_set_birthtime(ae, 3, 30);
55 archive_entry_set_ctime(ae, 4, 40);
56 archive_entry_set_mtime(ae, 5, 50);
57 archive_entry_copy_pathname(ae, "file");
58 archive_entry_set_mode(ae, S_IFREG | 0755);
59 archive_entry_set_size(ae, 8);
60 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
61 archive_entry_free(ae);
62 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
63
64 /*
65 * "file2" is similar but has birthtime later than mtime.
66 */
67 assert((ae = archive_entry_new()) != NULL);
68 archive_entry_set_atime(ae, 2, 20);
69 archive_entry_set_birthtime(ae, 8, 80);
70 archive_entry_set_ctime(ae, 4, 40);
71 archive_entry_set_mtime(ae, 5, 50);
72 archive_entry_copy_pathname(ae, "file2");
73 archive_entry_set_mode(ae, S_IFREG | 0755);
74 archive_entry_set_size(ae, 8);
75 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
76 archive_entry_free(ae);
77 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
78
79 /*
80 * "file3" is sparse file and has hole size of which is
81 * 1024000 bytes, and has 8 bytes data after the hole.
82 *
83 * Pad the filename to make it larger than the ustar limit.
84 * It should still read back correctly.
85 */
86 assert((ae = archive_entry_new()) != NULL);
87 archive_entry_set_atime(ae, 2, 20);
88 archive_entry_set_birthtime(ae, 3, 30);
89 archive_entry_set_ctime(ae, 4, 40);
90 archive_entry_set_mtime(ae, 5, 50);
91 archive_entry_copy_pathname(ae, "file3"
92 "_123456789_123456789_123456789_123456789_123456789"
93 "_123456789_123456789_123456789_123456789_123456789"
94 "_123456789_123456789_123456789_123456789_123456789");
95 archive_entry_set_mode(ae, S_IFREG | 0755);
96 archive_entry_set_size(ae, 1024008);
97 archive_entry_sparse_add_entry(ae, 1024000, 8);
98 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
99 archive_entry_free(ae);
100 memset(nulls, 0, sizeof(nulls));
101 for (i = 0; i < 1024000; i += 1024)
102 /* write hole data, which won't be stored into an archive file. */
103 assertEqualIntA(a, 1024, archive_write_data(a, nulls, 1024));
104 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
105
106 /*
107 * "file4" is similar to "file1" but has a large uid, large gid,
108 * uname and gname are longer than 32 characters
109 */
110 assert((ae = archive_entry_new()) != NULL);
111 archive_entry_set_atime(ae, 2, 20);
112 archive_entry_set_birthtime(ae, 3, 30);
113 archive_entry_set_ctime(ae, 4, 40);
114 archive_entry_set_mtime(ae, 5, 50);
115 archive_entry_copy_pathname(ae, "file4");
116 archive_entry_set_mode(ae, S_IFREG | 0755);
117 archive_entry_set_size(ae, 8);
118 archive_entry_copy_uname(ae,
119 "long-uname123456789012345678901234567890");
120 archive_entry_copy_gname(ae,
121 "long-gname123456789012345678901234567890");
122 archive_entry_set_uid(ae, 536870912);
123 archive_entry_set_gid(ae, 536870913);
124 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
125 archive_entry_free(ae);
126 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
127
128 /*
129 * XXX TODO XXX Archive directory, other file types.
130 * Archive extended attributes, ACLs, other metadata.
131 * Verify they get read back correctly.
132 */
133
134 /* Close out the archive. */
135 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
136 assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
137
138 /*
139 *
140 * Now, read the data back.
141 *
142 */
143 assert((a = archive_read_new()) != NULL);
144 assertEqualIntA(a, 0, archive_read_support_format_all(a));
145 assertEqualIntA(a, 0, archive_read_support_filter_all(a));
146 assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
147
148 /*
149 * Read "file"
150 */
151 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
152 assertEqualInt(2, archive_entry_atime(ae));
153 assertEqualInt(20, archive_entry_atime_nsec(ae));
154 assertEqualInt(3, archive_entry_birthtime(ae));
155 assertEqualInt(30, archive_entry_birthtime_nsec(ae));
156 assertEqualInt(4, archive_entry_ctime(ae));
157 assertEqualInt(40, archive_entry_ctime_nsec(ae));
158 assertEqualInt(5, archive_entry_mtime(ae));
159 assertEqualInt(50, archive_entry_mtime_nsec(ae));
160 assertEqualString("file", archive_entry_pathname(ae));
161 assert((S_IFREG | 0755) == archive_entry_mode(ae));
162 assertEqualInt(8, archive_entry_size(ae));
163 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
164 assertEqualMem(buff2, "12345678", 8);
165
166 /*
167 * Read "file2"
168 */
169 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
170 assert(archive_entry_atime_is_set(ae));
171 assertEqualInt(2, archive_entry_atime(ae));
172 assertEqualInt(20, archive_entry_atime_nsec(ae));
173 /* Birthtime > mtime above, so it doesn't get stored at all. */
174 assert(!archive_entry_birthtime_is_set(ae));
175 assertEqualInt(0, archive_entry_birthtime(ae));
176 assertEqualInt(0, archive_entry_birthtime_nsec(ae));
177 assert(archive_entry_ctime_is_set(ae));
178 assertEqualInt(4, archive_entry_ctime(ae));
179 assertEqualInt(40, archive_entry_ctime_nsec(ae));
180 assert(archive_entry_mtime_is_set(ae));
181 assertEqualInt(5, archive_entry_mtime(ae));
182 assertEqualInt(50, archive_entry_mtime_nsec(ae));
183 assertEqualString("file2", archive_entry_pathname(ae));
184 assert((S_IFREG | 0755) == archive_entry_mode(ae));
185 assertEqualInt(8, archive_entry_size(ae));
186 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
187 assertEqualMem(buff2, "12345678", 8);
188
189 /*
190 * Read "file3"
191 */
192 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
193 assertEqualInt(2, archive_entry_atime(ae));
194 assertEqualInt(20, archive_entry_atime_nsec(ae));
195 assertEqualInt(3, archive_entry_birthtime(ae));
196 assertEqualInt(30, archive_entry_birthtime_nsec(ae));
197 assertEqualInt(4, archive_entry_ctime(ae));
198 assertEqualInt(40, archive_entry_ctime_nsec(ae));
199 assertEqualInt(5, archive_entry_mtime(ae));
200 assertEqualInt(50, archive_entry_mtime_nsec(ae));
201 assertEqualString("file3"
202 "_123456789_123456789_123456789_123456789_123456789"
203 "_123456789_123456789_123456789_123456789_123456789"
204 "_123456789_123456789_123456789_123456789_123456789",
205 archive_entry_pathname(ae));
206 assert((S_IFREG | 0755) == archive_entry_mode(ae));
207 assertEqualInt(1024008, archive_entry_size(ae));
208 assertEqualInt(1, archive_entry_sparse_reset(ae));
209 assertEqualInt(ARCHIVE_OK,
210 archive_entry_sparse_next(ae, &offset, &length));
211 assertEqualInt(1024000, offset);
212 assertEqualInt(8, length);
213 for (i = 0; i < 1024000; i += 1024) {
214 int j;
215 assertEqualIntA(a, 1024, archive_read_data(a, nulls, 1024));
216 for (j = 0; j < 1024; j++)
217 assertEqualInt(0, nulls[j]);
218 }
219 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
220 assertEqualMem(buff2, "12345678", 8);
221
222 /*
223 * Read "file4
224 */
225 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
226 assertEqualInt(2, archive_entry_atime(ae));
227 assertEqualInt(20, archive_entry_atime_nsec(ae));
228 assertEqualInt(3, archive_entry_birthtime(ae));
229 assertEqualInt(30, archive_entry_birthtime_nsec(ae));
230 assertEqualInt(4, archive_entry_ctime(ae));
231 assertEqualInt(40, archive_entry_ctime_nsec(ae));
232 assertEqualInt(5, archive_entry_mtime(ae));
233 assertEqualInt(50, archive_entry_mtime_nsec(ae));
234 assertEqualString("file4", archive_entry_pathname(ae));
235 assertEqualString("long-uname123456789012345678901234567890",
236 archive_entry_uname(ae));
237 assertEqualString("long-gname123456789012345678901234567890",
238 archive_entry_gname(ae));
239 assertEqualInt(536870912, archive_entry_uid(ae));
240 assertEqualInt(536870913, archive_entry_gid(ae));
241 assert((S_IFREG | 0755) == archive_entry_mode(ae));
242 assertEqualInt(8, archive_entry_size(ae));
243 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
244 assertEqualMem(buff2, "12345678", 8);
245
246 /*
247 * Verify the end of the archive.
248 */
249 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
250 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
251 assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
252
253 free(buff);
254 }
255