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