1/*-
2 * Copyright (c) 2003-2007 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
28
29static int
30is_hex(const char *p, size_t l)
31{
32	while (l > 0) {
33		if (*p >= '0' && *p <= '9') {
34			/* Ascii digit */
35		} else if (*p >= 'a' && *p <= 'f') {
36			/* lowercase letter a-f */
37		} else {
38			/* Not hex. */
39			return (0);
40		}
41		--l;
42		++p;
43	}
44	return (1);
45}
46
47/*
48 * Detailed verification that cpio 'newc' archives are written with
49 * the correct format.
50 */
51DEFINE_TEST(test_write_format_cpio_newc)
52{
53	struct archive *a;
54	struct archive_entry *entry;
55	char *buff, *e, *file;
56	size_t buffsize = 100000;
57	size_t used;
58
59	buff = malloc(buffsize);
60
61	/* Create a new archive in memory. */
62	assert((a = archive_write_new()) != NULL);
63	assertEqualIntA(a, 0, archive_write_set_format_cpio_newc(a));
64	assertEqualIntA(a, 0, archive_write_add_filter_none(a));
65	assertEqualIntA(a, 0, archive_write_open_memory(a, buff, buffsize, &used));
66
67	/*
68	 * Add various files to it.
69	 * TODO: Extend this to cover more filetypes.
70	 */
71
72	/* Regular file */
73	assert((entry = archive_entry_new()) != NULL);
74	archive_entry_set_mtime(entry, 1, 10);
75	archive_entry_set_pathname(entry, "file");
76	archive_entry_set_mode(entry, S_IFREG | 0664);
77	archive_entry_set_size(entry, 10);
78	archive_entry_set_uid(entry, 80);
79	archive_entry_set_gid(entry, 90);
80	archive_entry_set_dev(entry, 12);
81	archive_entry_set_ino(entry, 89);
82	archive_entry_set_nlink(entry, 1);
83	assertEqualIntA(a, 0, archive_write_header(a, entry));
84	archive_entry_free(entry);
85	assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
86
87	/* Directory */
88	assert((entry = archive_entry_new()) != NULL);
89	archive_entry_set_mtime(entry, 2, 20);
90	archive_entry_set_pathname(entry, "dir");
91	archive_entry_set_mode(entry, S_IFDIR | 0775);
92	archive_entry_set_size(entry, 10);
93	archive_entry_set_nlink(entry, 2);
94	assertEqualIntA(a, 0, archive_write_header(a, entry));
95	archive_entry_free(entry);
96	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
97
98	/* Symlink */
99	assert((entry = archive_entry_new()) != NULL);
100	archive_entry_set_mtime(entry, 3, 30);
101	archive_entry_set_pathname(entry, "lnk");
102	archive_entry_set_mode(entry, 0664);
103	archive_entry_set_filetype(entry, AE_IFLNK);
104	archive_entry_set_size(entry, 0);
105	archive_entry_set_uid(entry, 83);
106	archive_entry_set_gid(entry, 93);
107	archive_entry_set_dev(entry, 13);
108	archive_entry_set_ino(entry, 88);
109	archive_entry_set_nlink(entry, 1);
110	archive_entry_set_symlink(entry,"a");
111	assertEqualIntA(a, 0, archive_write_header(a, entry));
112	archive_entry_free(entry);
113
114	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
115
116	/*
117	 * Verify the archive format.
118	 */
119	e = buff;
120
121	/* First entry is "file" */
122	file = e;
123	assert(is_hex(e, 110)); /* Entire header is hex digits. */
124	assertEqualMem(e + 0, "070701", 6); /* Magic */
125	assert(memcmp(e + 6, "00000000", 8) != 0); /* ino != 0 */
126	assertEqualMem(e + 14, "000081b4", 8); /* Mode */
127	assertEqualMem(e + 22, "00000050", 8); /* uid */
128	assertEqualMem(e + 30, "0000005a", 8); /* gid */
129	assertEqualMem(e + 38, "00000001", 8); /* nlink */
130	assertEqualMem(e + 46, "00000001", 8); /* mtime */
131	assertEqualMem(e + 54, "0000000a", 8); /* File size */
132	assertEqualMem(e + 62, "00000000", 8); /* devmajor */
133	assertEqualMem(e + 70, "0000000c", 8); /* devminor */
134	assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
135	assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
136	assertEqualMem(e + 94, "00000005", 8); /* Name size */
137	assertEqualMem(e + 102, "00000000", 8); /* CRC */
138	assertEqualMem(e + 110, "file\0\0", 6); /* Name contents */
139	assertEqualMem(e + 116, "1234567890", 10); /* File body */
140	assertEqualMem(e + 126, "\0\0", 2); /* Pad to multiple of 4 */
141	e += 128; /* Must be multiple of four here! */
142
143	/* Second entry is "dir" */
144	assert(is_hex(e, 110));
145	assertEqualMem(e + 0, "070701", 6); /* Magic */
146	assertEqualMem(e + 6, "00000000", 8); /* ino */
147	assertEqualMem(e + 14, "000041fd", 8); /* Mode */
148	assertEqualMem(e + 22, "00000000", 8); /* uid */
149	assertEqualMem(e + 30, "00000000", 8); /* gid */
150	assertEqualMem(e + 38, "00000002", 8); /* nlink */
151	assertEqualMem(e + 46, "00000002", 8); /* mtime */
152	assertEqualMem(e + 54, "00000000", 8); /* File size */
153	assertEqualMem(e + 62, "00000000", 8); /* devmajor */
154	assertEqualMem(e + 70, "00000000", 8); /* devminor */
155	assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
156	assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
157	assertEqualMem(e + 94, "00000004", 8); /* Name size */
158	assertEqualMem(e + 102, "00000000", 8); /* CRC */
159	assertEqualMem(e + 110, "dir\0", 4); /* name */
160	assertEqualMem(e + 114, "\0\0", 2); /* Pad to multiple of 4 */
161	e += 116; /* Must be multiple of four here! */
162
163	/* Third entry is "lnk" */
164	assert(is_hex(e, 110)); /* Entire header is hex digits. */
165	assertEqualMem(e + 0, "070701", 6); /* Magic */
166	assert(memcmp(e + 6, file + 6, 8) != 0); /* ino != file ino */
167	assert(memcmp(e + 6, "00000000", 8) != 0); /* ino != 0 */
168	assertEqualMem(e + 14, "0000a1b4", 8); /* Mode */
169	assertEqualMem(e + 22, "00000053", 8); /* uid */
170	assertEqualMem(e + 30, "0000005d", 8); /* gid */
171	assertEqualMem(e + 38, "00000001", 8); /* nlink */
172	assertEqualMem(e + 46, "00000003", 8); /* mtime */
173	assertEqualMem(e + 54, "00000001", 8); /* File size */
174	assertEqualMem(e + 62, "00000000", 8); /* devmajor */
175	assertEqualMem(e + 70, "0000000d", 8); /* devminor */
176	assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
177	assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
178	assertEqualMem(e + 94, "00000004", 8); /* Name size */
179	assertEqualMem(e + 102, "00000000", 8); /* CRC */
180	assertEqualMem(e + 110, "lnk\0\0\0", 6); /* Name contents */
181	assertEqualMem(e + 116, "a\0\0\0", 4); /* File body + pad */
182	e += 120; /* Must be multiple of four here! */
183
184	/* TODO: Verify other types of entries. */
185
186	/* Last entry is end-of-archive marker. */
187	assert(is_hex(e, 76));
188	assertEqualMem(e + 0, "070701", 6); /* Magic */
189	assertEqualMem(e + 6, "00000000", 8); /* ino */
190	assertEqualMem(e + 14, "00000000", 8); /* Mode */
191	assertEqualMem(e + 22, "00000000", 8); /* uid */
192	assertEqualMem(e + 30, "00000000", 8); /* gid */
193	assertEqualMem(e + 38, "00000001", 8); /* nlink */
194	assertEqualMem(e + 46, "00000000", 8); /* mtime */
195	assertEqualMem(e + 54, "00000000", 8); /* File size */
196	assertEqualMem(e + 62, "00000000", 8); /* devmajor */
197	assertEqualMem(e + 70, "00000000", 8); /* devminor */
198	assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
199	assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
200	assertEqualMem(e + 94, "0000000b", 8); /* Name size */
201	assertEqualMem(e + 102, "00000000", 8); /* CRC */
202	assertEqualMem(e + 110, "TRAILER!!!\0", 11); /* Name */
203	assertEqualMem(e + 121, "\0\0\0", 3); /* Pad to multiple of 4 bytes */
204	e += 124; /* Must be multiple of four here! */
205
206	assertEqualInt((int)used, e - buff);
207
208	free(buff);
209}
210