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 assertFileContents("2 blocks\n", 9, "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 assertEqualInt(0x81a4, from_hex(e
+ 14, 8)); /* Mode */
132 assertEqualInt(from_hex(e
+ 22, 8), getuid()); /* uid */
133 gid
= from_hex(e
+ 30, 8); /* gid */
134 assertEqualMem(e
+ 38, "00000003", 8); /* nlink */
135 t
= from_hex(e
+ 46, 8); /* mtime */
136 failure("t=0x%08x now=0x%08x=%d", t
, now
, now
);
137 assert(t
<= now
); /* File wasn't created in future. */
138 failure("t=0x%08x now - 2=0x%08x = %d", t
, now
- 2, now
- 2);
139 assert(t
>= now
- 2); /* File was created w/in last 2 secs. */
140 failure("newc format stores body only with last appearance of a link\n"
141 " first appearance should be empty, so this file size\n"
142 " field should be zero");
143 assertEqualInt(0, from_hex(e
+ 54, 8)); /* File size */
144 devmajor
= from_hex(e
+ 62, 8); /* devmajor */
145 devminor
= from_hex(e
+ 70, 8); /* devminor */
146 assert(is_hex(e
+ 78, 8)); /* rdevmajor */
147 assert(is_hex(e
+ 86, 8)); /* rdevminor */
148 assertEqualMem(e
+ 94, "00000006", 8); /* Name size */
149 assertEqualInt(0, from_hex(e
+ 102, 8)); /* check field */
150 assertEqualMem(e
+ 110, "file1\0", 6); /* Name contents */
151 /* Since there's another link, no file contents here. */
152 /* But add in file size so that an error here doesn't cascade. */
153 e
+= 116 + from_hex(e
+ 54, 8) + (3 & -from_hex(e
+ 54, 8));
154 /* "symlink" pointing to "file1" */
155 assert(is_hex(e
, 110));
156 assertEqualMem(e
+ 0, "070701", 6); /* Magic */
157 assert(is_hex(e
+ 6, 8)); /* ino */
158 assertEqualInt(0xa1ff, from_hex(e
+ 14, 8)); /* Mode */
159 assertEqualInt(from_hex(e
+ 22, 8), getuid()); /* uid */
160 assertEqualInt(gid
, from_hex(e
+ 30, 8)); /* gid */
161 assertEqualMem(e
+ 38, "00000001", 8); /* nlink */
162 t2
= from_hex(e
+ 46, 8); /* mtime */
163 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t
, t2
);
164 assert(t2
== t
|| t2
== t
+ 1); /* Almost same as first entry. */
165 assertEqualMem(e
+ 54, "00000005", 8); /* File size */
166 assertEqualInt(devmajor
, from_hex(e
+ 62, 8)); /* devmajor */
167 assertEqualInt(devminor
, from_hex(e
+ 70, 8)); /* devminor */
168 assert(is_hex(e
+ 78, 8)); /* rdevmajor */
169 assert(is_hex(e
+ 86, 8)); /* rdevminor */
170 assertEqualMem(e
+ 94, "00000008", 8); /* Name size */
171 assertEqualInt(0, from_hex(e
+ 102, 8)); /* check field */
172 assertEqualMem(e
+ 110, "symlink\0\0\0", 10); /* Name contents */
173 assertEqualMem(e
+ 120, "file1\0\0\0", 8); /* symlink target */
174 e
+= 120 + from_hex(e
+ 54, 8) + (3 & -from_hex(e
+ 54, 8));
177 assert(is_hex(e
, 110));
178 assertEqualMem(e
+ 0, "070701", 6); /* Magic */
179 assert(is_hex(e
+ 6, 8)); /* ino */
180 assertEqualInt(0x41fd, from_hex(e
+ 14, 8)); /* Mode */
181 assertEqualInt(from_hex(e
+ 22, 8), getuid()); /* uid */
182 assertEqualInt(gid
, from_hex(e
+ 30, 8)); /* gid */
183 assertEqualMem(e
+ 38, "00000002", 8); /* nlink */
184 t2
= from_hex(e
+ 46, 8); /* mtime */
185 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t
, t2
);
186 assert(t2
== t
|| t2
== t
+ 1); /* Almost same as first entry. */
187 assertEqualMem(e
+ 54, "00000000", 8); /* File size */
188 assertEqualInt(devmajor
, from_hex(e
+ 62, 8)); /* devmajor */
189 assertEqualInt(devminor
, from_hex(e
+ 70, 8)); /* devminor */
190 assert(is_hex(e
+ 78, 8)); /* rdevmajor */
191 assert(is_hex(e
+ 86, 8)); /* rdevminor */
192 assertEqualMem(e
+ 94, "00000004", 8); /* Name size */
193 assertEqualInt(0, from_hex(e
+ 102, 8)); /* check field */
194 assertEqualMem(e
+ 110, "dir\0\0\0", 6); /* Name contents */
197 /* TODO: Verify other types of entries. */
199 /* Hardlink identical to "file1" */
200 /* Since we only wrote two of the three links to this
201 * file, this link should get deferred by the hardlink logic. */
202 assert(is_hex(e
, 110));
203 assertEqualMem(e
+ 0, "070701", 6); /* Magic */
204 failure("If these aren't the same, then the hardlink detection failed to match them.");
205 assertEqualInt(ino
, from_hex(e
+ 6, 8)); /* ino */
206 assertEqualInt(0x81a4, from_hex(e
+ 14, 8)); /* Mode */
207 assertEqualInt(from_hex(e
+ 22, 8), getuid()); /* uid */
208 assertEqualInt(gid
, from_hex(e
+ 30, 8)); /* gid */
209 assertEqualMem(e
+ 38, "00000003", 8); /* nlink */
210 t2
= from_hex(e
+ 46, 8); /* mtime */
211 failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t
, t2
);
212 assert(t2
== t
|| t2
== t
+ 1); /* Almost same as first entry. */
213 assertEqualInt(10, from_hex(e
+ 54, 8)); /* File size */
214 assertEqualInt(devmajor
, from_hex(e
+ 62, 8)); /* devmajor */
215 assertEqualInt(devminor
, from_hex(e
+ 70, 8)); /* devminor */
216 assert(is_hex(e
+ 78, 8)); /* rdevmajor */
217 assert(is_hex(e
+ 86, 8)); /* rdevminor */
218 assertEqualMem(e
+ 94, "00000009", 8); /* Name size */
219 assertEqualInt(0, from_hex(e
+ 102, 8)); /* check field */
220 assertEqualMem(e
+ 110, "hardlink\0\0", 10); /* Name contents */
221 assertEqualMem(e
+ 120, "123456789\0\0\0", 12); /* File contents */
222 e
+= 120 + from_hex(e
+ 54, 8) + (3 & -from_hex(e
+ 54, 8));
224 /* Last entry is end-of-archive marker. */
225 assert(is_hex(e
, 110));
226 assertEqualMem(e
+ 0, "070701", 6); /* Magic */
227 assertEqualMem(e
+ 8, "00000000", 8); /* ino */
228 assertEqualMem(e
+ 14, "00000000", 8); /* mode */
229 assertEqualMem(e
+ 22, "00000000", 8); /* uid */
230 assertEqualMem(e
+ 30, "00000000", 8); /* gid */
231 assertEqualMem(e
+ 38, "00000001", 8); /* nlink */
232 assertEqualMem(e
+ 46, "00000000", 8); /* mtime */
233 assertEqualMem(e
+ 54, "00000000", 8); /* size */
234 assertEqualMem(e
+ 62, "00000000", 8); /* devmajor */
235 assertEqualMem(e
+ 70, "00000000", 8); /* devminor */
236 assertEqualMem(e
+ 78, "00000000", 8); /* rdevmajor */
237 assertEqualMem(e
+ 86, "00000000", 8); /* rdevminor */
238 assertEqualInt(11, from_hex(e
+ 94, 8)); /* name size */
239 assertEqualMem(e
+ 102, "00000000", 8); /* check field */
240 assertEqualMem(e
+ 110, "TRAILER!!!\0\0", 12); /* Name */