]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/cpio/test/test_format_newc.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / cpio / test / test_format_newc.c
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 static int
29 is_hex(const char *p, size_t l)
30 {
31         while (l > 0) {
32                 if ((*p >= '0' && *p <= '9')
33                     || (*p >= 'a' && *p <= 'f')
34                     || (*p >= 'A' && *p <= 'F'))
35                 {
36                         --l;
37                         ++p;
38                 } else
39                         return (0);
40
41         }
42         return (1);
43 }
44
45 static int
46 from_hex(const char *p, size_t l)
47 {
48         int r = 0;
49
50         while (l > 0) {
51                 r *= 16;
52                 if (*p >= 'a' && *p <= 'f')
53                         r += *p + 10 - 'a';
54                 else if (*p >= 'A' && *p <= 'F')
55                         r += *p + 10 - 'A';
56                 else
57                         r += *p - '0';
58                 --l;
59                 ++p;
60         }
61         return (r);
62 }
63
64 DEFINE_TEST(test_format_newc)
65 {
66         int fd, list;
67         int r;
68         int devmajor, devminor, ino, gid;
69         time_t t, t2, now;
70         char *p, *e;
71         size_t s, fs, ns;
72         mode_t oldmask;
73
74         oldmask = umask(0);
75
76         /*
77          * Create an assortment of files.
78          * TODO: Extend this to cover more filetypes.
79          */
80         list = open("list", O_CREAT | O_WRONLY, 0644);
81
82         /* "file1" */
83         fd = open("file1", O_CREAT | O_WRONLY, 0644);
84         assert(fd >= 0);
85         assertEqualInt(10, write(fd, "123456789", 10));
86         close(fd);
87         assertEqualInt(6, write(list, "file1\n", 6));
88
89         /* "hardlink" */
90         assertEqualInt(0, link("file1", "hardlink"));
91         assertEqualInt(9, write(list, "hardlink\n", 9));
92
93         /* Another hardlink, but this one won't be archived. */
94         assertEqualInt(0, link("file1", "hardlink2"));
95
96         /* "symlink" */
97         assertEqualInt(0, symlink("file1", "symlink"));
98         assertEqualInt(8, write(list, "symlink\n", 8));
99
100         /* "dir" */
101         assertEqualInt(0, mkdir("dir", 0775));
102         assertEqualInt(4, write(list, "dir\n", 4));
103
104         /* Record some facts about what we just created: */
105         now = time(NULL); /* They were all created w/in last two seconds. */
106
107         /* Use the cpio program to create an archive. */
108         close(list);
109         r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
110             testprog);
111         if (!assertEqualInt(r, 0))
112                 return;
113
114         /* Verify that nothing went to stderr. */
115         assertTextFileContents("2 blocks\n", "newc.err");
116
117         /* Verify that stdout is a well-formed cpio file in "newc" format. */
118         p = slurpfile(&s, "newc.out");
119         assertEqualInt(s, 1024);
120         e = p;
121
122         /*
123          * Some of these assertions could be stronger, but it's
124          * a little tricky because they depend on the local environment.
125          */
126
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 */
134 #else
135         assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
136 #endif
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);
150         fs += 3 & -fs;
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);
157         ns += 3 & (-ns - 2);
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. */
162         e += 110 + fs + ns;
163
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 */
172 #endif
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 */
182 #else
183         assertEqualMem(e + 54, "00000005", 8); /* File size */
184 #endif
185         fs = from_hex(e + 54, 8);
186         fs += 3 & -fs;
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);
193         ns += 3 & (-ns - 2);
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 */
198 #endif
199         e += 110 + fs + ns;
200
201         /* "dir" */
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 */
208 #else
209         assertEqualInt(0x41fd, from_hex(e + 14, 8)); /* Mode */
210 #endif
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 */
215 #endif
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);
221         fs += 3 & -fs;
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);
228         ns += 3 & (-ns - 2);
229         assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
230         assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
231         e += 110 + fs + ns;
232
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 */
243 #else
244         assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
245 #endif
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);
254         fs += 3 & -fs;
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);
261         ns += 3 & (-ns - 2);
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 */
265         e += 110 + ns + fs;
266
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 */
284
285         free(p);
286
287         umask(oldmask);
288 }