1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#include "test.h"
27
28static int
29is_null(const char *p, size_t l)
30{
31	while (l > 0) {
32		if (*p != '\0')
33			return (0);
34		--l;
35		++p;
36	}
37	return (1);
38}
39
40/* Verify the contents, then erase them to NUL bytes. */
41/* Tar requires all "unused" bytes be set to NUL; this allows us
42 * to easily verify that by invoking is_null() over the entire header
43 * after verifying each field. */
44#define myAssertEqualMem(a,b,s) assertEqualMem(a, b, s); memset(a, 0, s)
45
46/*
47 * Detailed verification that 'v7tar' archives are written with
48 * the correct format.
49 */
50DEFINE_TEST(test_write_format_tar_v7tar)
51{
52	struct archive *a;
53	struct archive_entry *entry;
54	char *buff, *e;
55	size_t buffsize = 100000;
56	size_t used;
57	int i;
58	char f99[100];
59	char f100[101];
60
61	for (i = 0; i < 99; ++i)
62		f99[i] = 'a' + i % 26;
63	f99[99] = '\0';
64
65	for (i = 0; i < 100; ++i)
66		f100[i] = 'A' + i % 26;
67	f100[100] = '\0';
68
69	buff = malloc(buffsize);
70
71	/* Create a new archive in memory. */
72	assert((a = archive_write_new()) != NULL);
73	assertEqualIntA(a, ARCHIVE_OK,
74	    archive_write_set_format_v7tar(a));
75	assertEqualIntA(a, ARCHIVE_OK,
76	    archive_write_add_filter_none(a));
77	assertEqualIntA(a, ARCHIVE_OK,
78	    archive_write_open_memory(a, buff, buffsize, &used));
79
80	/*
81	 * Add various files to it.
82	 * TODO: Extend this to cover more filetypes.
83	 */
84
85	/* "file" with 10 bytes of content */
86	assert((entry = archive_entry_new()) != NULL);
87	archive_entry_set_mtime(entry, 1, 10);
88	archive_entry_set_pathname(entry, "file");
89	archive_entry_set_mode(entry, S_IFREG | 0664);
90	archive_entry_set_size(entry, 10);
91	archive_entry_set_uid(entry, 80);
92	archive_entry_set_gid(entry, 90);
93	archive_entry_set_dev(entry, 12);
94	archive_entry_set_ino(entry, 89);
95	archive_entry_set_nlink(entry, 2);
96	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
97	archive_entry_free(entry);
98	assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
99
100	/* Hardlink to "file" with 10 bytes of content */
101	assert((entry = archive_entry_new()) != NULL);
102	archive_entry_set_mtime(entry, 1, 10);
103	archive_entry_set_pathname(entry, "linkfile");
104	archive_entry_set_hardlink(entry, "file");
105	archive_entry_set_mode(entry, S_IFREG | 0664);
106	archive_entry_set_size(entry, 10);
107	archive_entry_set_uid(entry, 80);
108	archive_entry_set_gid(entry, 90);
109	archive_entry_set_dev(entry, 12);
110	archive_entry_set_ino(entry, 89);
111	archive_entry_set_nlink(entry, 2);
112	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
113	archive_entry_free(entry);
114	/* Write of data to dir should fail == zero bytes get written. */
115	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
116
117	/* "dir" */
118	assert((entry = archive_entry_new()) != NULL);
119	archive_entry_set_mtime(entry, 2, 20);
120	archive_entry_set_pathname(entry, "dir");
121	archive_entry_set_mode(entry, S_IFDIR | 0775);
122	archive_entry_set_size(entry, 10);
123	archive_entry_set_nlink(entry, 2);
124	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
125	archive_entry_free(entry);
126	/* Write of data to dir should fail == zero bytes get written. */
127	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
128
129	/* "symlink" pointing to "file" */
130	assert((entry = archive_entry_new()) != NULL);
131	archive_entry_set_mtime(entry, 3, 30);
132	archive_entry_set_pathname(entry, "symlink");
133	archive_entry_set_mode(entry, 0664);
134	archive_entry_set_filetype(entry, AE_IFLNK);
135	archive_entry_set_symlink(entry,"file");
136	archive_entry_set_size(entry, 0);
137	archive_entry_set_uid(entry, 88);
138	archive_entry_set_gid(entry, 98);
139	archive_entry_set_dev(entry, 12);
140	archive_entry_set_ino(entry, 90);
141	archive_entry_set_nlink(entry, 1);
142	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
143	archive_entry_free(entry);
144
145	/* file with 99-char filename. */
146	assert((entry = archive_entry_new()) != NULL);
147	archive_entry_set_mtime(entry, 1, 10);
148	archive_entry_set_pathname(entry, f99);
149	archive_entry_set_mode(entry, S_IFREG | 0664);
150	archive_entry_set_size(entry, 0);
151	archive_entry_set_uid(entry, 82);
152	archive_entry_set_gid(entry, 93);
153	archive_entry_set_dev(entry, 102);
154	archive_entry_set_ino(entry, 7);
155	archive_entry_set_nlink(entry, 1);
156	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
157	archive_entry_free(entry);
158
159	/* file with 100-char filename. */
160	assert((entry = archive_entry_new()) != NULL);
161	archive_entry_set_mtime(entry, 1, 10);
162	archive_entry_set_pathname(entry, f100);
163	archive_entry_set_mode(entry, S_IFREG | 0664);
164	archive_entry_set_size(entry, 0);
165	archive_entry_set_uid(entry, 82);
166	archive_entry_set_gid(entry, 93);
167	archive_entry_set_dev(entry, 102);
168	archive_entry_set_ino(entry, 7);
169	archive_entry_set_nlink(entry, 1);
170	failure("100-char filename should be rejected");
171	assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, entry));
172	archive_entry_free(entry);
173
174	/* Close out the archive. */
175	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
176
177	/*
178	 * Verify the archive format.
179	 */
180	e = buff;
181
182	/* "file" */
183	myAssertEqualMem(e + 0, "file", 5); /* Filename */
184	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
185	myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
186	myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
187	myAssertEqualMem(e + 124, "00000000012 ", 12); /* size */
188	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
189	myAssertEqualMem(e + 148, "005335\0 ", 8); /* checksum */
190	myAssertEqualMem(e + 156, "", 1); /* linkflag */
191	myAssertEqualMem(e + 157, "", 1); /* linkname */
192	assert(is_null(e + 0, 512));
193	myAssertEqualMem(e + 512, "1234567890", 10);
194	assert(is_null(e + 512, 512));
195	e += 1024;
196
197	/* hardlink to "file" */
198	myAssertEqualMem(e + 0, "linkfile", 9); /* Filename */
199	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
200	myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
201	myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
202	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
203	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
204	myAssertEqualMem(e + 148, "007131\0 ", 8); /* checksum */
205	myAssertEqualMem(e + 156, "1", 1); /* linkflag */
206	myAssertEqualMem(e + 157, "file", 5); /* linkname */
207	assert(is_null(e + 0, 512));
208	e += 512;
209
210	/* "dir" */
211	myAssertEqualMem(e + 0, "dir/", 4); /* Filename */
212	myAssertEqualMem(e + 100, "000775 ", 8); /* mode */
213	myAssertEqualMem(e + 108, "000000 ", 8); /* uid */
214	myAssertEqualMem(e + 116, "000000 ", 8); /* gid */
215	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
216	myAssertEqualMem(e + 136, "00000000002 ", 12); /* mtime */
217	myAssertEqualMem(e + 148, "005243\0 ", 8); /* checksum */
218	myAssertEqualMem(e + 156, "", 1); /* typeflag */
219	myAssertEqualMem(e + 157, "", 1); /* linkname */
220	assert(is_null(e + 0, 512));
221	e += 512;
222
223	/* "symlink" pointing to "file" */
224	myAssertEqualMem(e + 0, "symlink", 8); /* Filename */
225	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
226	myAssertEqualMem(e + 108, "000130 ", 8); /* uid */
227	myAssertEqualMem(e + 116, "000142 ", 8); /* gid */
228	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
229	myAssertEqualMem(e + 136, "00000000003 ", 12); /* mtime */
230	myAssertEqualMem(e + 148, "007027\0 ", 8); /* checksum */
231	myAssertEqualMem(e + 156, "2", 1); /* linkflag */
232	myAssertEqualMem(e + 157, "file", 5); /* linkname */
233	assert(is_null(e + 0, 512));
234	e += 512;
235
236	/* File with 99-char filename */
237	myAssertEqualMem(e + 0, f99, 100); /* Filename */
238	myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
239	myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
240	myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
241	myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
242	myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
243	myAssertEqualMem(e + 148, "031543\0 ", 8); /* checksum */
244	myAssertEqualMem(e + 156, "", 1); /* linkflag */
245	myAssertEqualMem(e + 157, "", 1); /* linkname */
246	assert(is_null(e + 0, 512));
247	e += 512;
248
249	/* TODO: Verify other types of entries. */
250
251	/* Last entry is end-of-archive marker. */
252	assert(is_null(e, 1024));
253	e += 1024;
254
255	assertEqualInt((int)used, e - buff);
256
257	free(buff);
258}
259