2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
29 is_hex(const char *p, size_t l)
32 if ((*p >= '0' && *p <= '9')
33 || (*p >= 'a' && *p <= 'f')
34 || (*p >= 'A' && *p <= 'F'))
46 from_hex(const char *p, size_t l)
52 if (*p >= 'a' && *p <= 'f')
54 else if (*p >= 'A' && *p <= 'F')
64 DEFINE_TEST(test_format_newc)
68 int devmajor, devminor, ino, gid;
77 * Create an assortment of files.
78 * TODO: Extend this to cover more filetypes.
80 list = open("list", O_CREAT | O_WRONLY, 0644);
83 fd = open("file1", O_CREAT | O_WRONLY, 0644);
85 assertEqualInt(10, write(fd, "123456789", 10));
87 assertEqualInt(6, write(list, "file1\n", 6));
90 assertEqualInt(0, link("file1", "hardlink"));
91 assertEqualInt(9, write(list, "hardlink\n", 9));
93 /* Another hardlink, but this one won't be archived. */
94 assertEqualInt(0, link("file1", "hardlink2"));
97 assertEqualInt(0, symlink("file1", "symlink"));
98 assertEqualInt(8, write(list, "symlink\n", 8));
101 assertEqualInt(0, mkdir("dir", 0775));
102 assertEqualInt(4, write(list, "dir\n", 4));
104 /* Record some facts about what we just created: */
105 now = time(NULL); /* They were all created w/in last two seconds. */
107 /* Use the cpio program to create an archive. */
109 r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
111 if (!assertEqualInt(r, 0))
114 /* Verify that nothing went to stderr. */
115 assertTextFileContents("2 blocks\n", "newc.err");
117 /* Verify that stdout is a well-formed cpio file in "newc" format. */
118 p = slurpfile(&s, "newc.out");
119 assertEqualInt(s, 1024);
123 * Some of these assertions could be stronger, but it's
124 * a little tricky because they depend on the local environment.
127 /* First entry is "file1" */
128 assert(is_hex(e, 110)); /* Entire header is octal digits. */
129 assertEqualMem(e + 0, "070701", 6); /* Magic */
130 ino = from_hex(e + 6, 8); /* ino */
131 #if defined(_WIN32) && !defined(__CYGWIN__)
132 /* Group members bits and others bits do not work. */
133 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
135 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
137 assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
138 gid = from_hex(e + 30, 8); /* gid */
139 assertEqualMem(e + 38, "00000003", 8); /* nlink */
140 t = from_hex(e + 46, 8); /* mtime */
141 failure("t=0x%08x now=0x%08x=%d", t, now, now);
142 assert(t <= now); /* File wasn't created in future. */
143 failure("t=0x%08x now - 2=0x%08x = %d", t, now - 2, now - 2);
144 assert(t >= now - 2); /* File was created w/in last 2 secs. */
145 failure("newc format stores body only with last appearance of a link\n"
146 " first appearance should be empty, so this file size\n"
147 " field should be zero");
148 assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
149 fs = from_hex(e + 54, 8);
151 devmajor = from_hex(e + 62, 8); /* devmajor */
152 devminor = from_hex(e + 70, 8); /* devminor */
153 assert(is_hex(e + 78, 8)); /* rdevmajor */
154 assert(is_hex(e + 86, 8)); /* rdevminor */
155 assertEqualMem(e + 94, "00000006", 8); /* Name size */
156 ns = from_hex(e + 94, 8);
158 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
159 assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
160 /* Since there's another link, no file contents here. */
161 /* But add in file size so that an error here doesn't cascade. */
164 /* "symlink" pointing to "file1" */
165 assert(is_hex(e, 110));
166 assertEqualMem(e + 0, "070701", 6); /* Magic */
167 assert(is_hex(e + 6, 8)); /* ino */
168 #if !defined(_WIN32) || defined(__CYGWIN__)
169 /* On Windows, symbolic link and group members bits and
170 * others bits do not work. */
171 assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
173 assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
174 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
175 assertEqualMem(e + 38, "00000001", 8); /* nlink */
176 t2 = from_hex(e + 46, 8); /* mtime */
177 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
178 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
179 #if defined(_WIN32) && !defined(__CYGWIN__)
180 /* Symbolic link does not work. */
181 assertEqualMem(e + 54, "0000000a", 8); /* File size */
183 assertEqualMem(e + 54, "00000005", 8); /* File size */
185 fs = from_hex(e + 54, 8);
187 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
188 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
189 assert(is_hex(e + 78, 8)); /* rdevmajor */
190 assert(is_hex(e + 86, 8)); /* rdevminor */
191 assertEqualMem(e + 94, "00000008", 8); /* Name size */
192 ns = from_hex(e + 94, 8);
194 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
195 assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
196 #if !defined(_WIN32) || defined(__CYGWIN__)
197 assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
202 assert(is_hex(e, 110));
203 assertEqualMem(e + 0, "070701", 6); /* Magic */
204 assert(is_hex(e + 6, 8)); /* ino */
205 #if defined(_WIN32) && !defined(__CYGWIN__)
206 /* Group members bits and others bits do not work. */
207 assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */
209 assertEqualInt(0x41fd, from_hex(e + 14, 8)); /* Mode */
211 assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
212 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
213 #ifndef NLINKS_INACCURATE_FOR_DIRS
214 assertEqualMem(e + 38, "00000002", 8); /* nlink */
216 t2 = from_hex(e + 46, 8); /* mtime */
217 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
218 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
219 assertEqualMem(e + 54, "00000000", 8); /* File size */
220 fs = from_hex(e + 54, 8);
222 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
223 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
224 assert(is_hex(e + 78, 8)); /* rdevmajor */
225 assert(is_hex(e + 86, 8)); /* rdevminor */
226 assertEqualMem(e + 94, "00000004", 8); /* Name size */
227 ns = from_hex(e + 94, 8);
229 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
230 assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
233 /* Hardlink identical to "file1" */
234 /* Since we only wrote two of the three links to this
235 * file, this link should get deferred by the hardlink logic. */
236 assert(is_hex(e, 110));
237 assertEqualMem(e + 0, "070701", 6); /* Magic */
238 failure("If these aren't the same, then the hardlink detection failed to match them.");
239 assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */
240 #if defined(_WIN32) && !defined(__CYGWIN__)
241 /* Group members bits and others bits do not work. */
242 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
244 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
246 assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
247 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
248 assertEqualMem(e + 38, "00000003", 8); /* nlink */
249 t2 = from_hex(e + 46, 8); /* mtime */
250 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
251 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
252 assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
253 fs = from_hex(e + 54, 8);
255 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
256 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
257 assert(is_hex(e + 78, 8)); /* rdevmajor */
258 assert(is_hex(e + 86, 8)); /* rdevminor */
259 assertEqualMem(e + 94, "00000009", 8); /* Name size */
260 ns = from_hex(e + 94, 8);
262 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
263 assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */
264 assertEqualMem(e + 110 + ns, "123456789\0\0\0", 12); /* File contents */
267 /* Last entry is end-of-archive marker. */
268 assert(is_hex(e, 110));
269 assertEqualMem(e + 0, "070701", 6); /* Magic */
270 assertEqualMem(e + 8, "00000000", 8); /* ino */
271 assertEqualMem(e + 14, "00000000", 8); /* mode */
272 assertEqualMem(e + 22, "00000000", 8); /* uid */
273 assertEqualMem(e + 30, "00000000", 8); /* gid */
274 assertEqualMem(e + 38, "00000001", 8); /* nlink */
275 assertEqualMem(e + 46, "00000000", 8); /* mtime */
276 assertEqualMem(e + 54, "00000000", 8); /* size */
277 assertEqualMem(e + 62, "00000000", 8); /* devmajor */
278 assertEqualMem(e + 70, "00000000", 8); /* devminor */
279 assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */
280 assertEqualMem(e + 86, "00000000", 8); /* rdevminor */
281 assertEqualInt(11, from_hex(e + 94, 8)); /* name size */
282 assertEqualMem(e + 102, "00000000", 8); /* check field */
283 assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */