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_octal(const char *p
, size_t l
)
32 if (*p
< '0' || *p
> '7')
41 from_octal(const char *p
, size_t l
)
54 DEFINE_TEST(test_option_c
)
67 * Create an assortment of files.
68 * TODO: Extend this to cover more filetypes.
70 filelist
= open("filelist", O_CREAT
| O_WRONLY
, 0644);
73 fd
= open("file", O_CREAT
| O_WRONLY
, 0644);
75 assertEqualInt(10, write(fd
, "123456789", 10));
77 assertEqualInt(5, write(filelist
, "file\n", 5));
80 assertEqualInt(0, symlink("file", "symlink"));
81 assertEqualInt(8, write(filelist
, "symlink\n", 8));
84 assertEqualInt(0, mkdir("dir", 0775));
85 /* Record some facts about what we just created: */
86 now
= time(NULL
); /* They were all created w/in last two seconds. */
87 assertEqualInt(4, write(filelist
, "dir\n", 4));
89 /* Use the cpio program to create an archive. */
91 r
= systemf("%s -oc <filelist >basic.out 2>basic.err", testprog
);
92 /* Verify that nothing went to stderr. */
93 assertFileContents("1 block\n", 8, "basic.err");
95 /* Assert that the program finished. */
96 failure("%s -oc crashed", testprog
);
97 if (!assertEqualInt(r
, 0))
100 /* Verify that stdout is a well-formed cpio file in "odc" format. */
101 p
= slurpfile(&s
, "basic.out");
102 assertEqualInt(s
, 512);
106 * Some of these assertions could be stronger, but it's
107 * a little tricky because they depend on the local environment.
110 /* First entry is "file" */
111 assert(is_octal(e
, 76)); /* Entire header is octal digits. */
112 assertEqualMem(e
+ 0, "070707", 6); /* Magic */
113 assert(is_octal(e
+ 6, 6)); /* dev */
114 dev
= from_octal(e
+ 6, 6);
115 assert(is_octal(e
+ 12, 6)); /* ino */
116 ino
= from_octal(e
+ 12, 6);
117 assertEqualMem(e
+ 18, "100644", 6); /* Mode */
118 assertEqualInt(from_octal(e
+ 24, 6), getuid()); /* uid */
119 assert(is_octal(e
+ 30, 6)); /* gid */
120 gid
= from_octal(e
+ 30, 6);
121 assertEqualMem(e
+ 36, "000001", 6); /* nlink */
122 failure("file entries should not have rdev set (dev field was 0%o)",
124 assertEqualMem(e
+ 42, "000000", 6); /* rdev */
125 t
= from_octal(e
+ 48, 11); /* mtime */
126 assert(t
<= now
); /* File wasn't created in future. */
127 assert(t
>= now
- 2); /* File was created w/in last 2 secs. */
128 assertEqualMem(e
+ 59, "000005", 6); /* Name size */
129 assertEqualMem(e
+ 65, "00000000012", 11); /* File size */
130 assertEqualMem(e
+ 76, "file\0", 5); /* Name contents */
131 assertEqualMem(e
+ 81, "123456789\0", 10); /* File contents */
134 /* Second entry is "symlink" pointing to "file" */
135 assert(is_octal(e
, 76)); /* Entire header is octal digits. */
136 assertEqualMem(e
+ 0, "070707", 6); /* Magic */
137 assertEqualInt(dev
, from_octal(e
+ 6, 6)); /* dev */
138 assert(dev
!= from_octal(e
+ 12, 6)); /* ino */
139 assertEqualMem(e
+ 18, "120777", 6); /* Mode */
140 assertEqualInt(from_octal(e
+ 24, 6), getuid()); /* uid */
141 assertEqualInt(gid
, from_octal(e
+ 30, 6)); /* gid */
142 assertEqualMem(e
+ 36, "000001", 6); /* nlink */
143 failure("file entries should have rdev == 0 (dev was 0%o)",
144 from_octal(e
+ 6, 6));
145 assertEqualMem(e
+ 42, "000000", 6); /* rdev */
146 t
= from_octal(e
+ 48, 11); /* mtime */
147 assert(t
<= now
); /* File wasn't created in future. */
148 assert(t
>= now
- 2); /* File was created w/in last 2 secs. */
149 assertEqualMem(e
+ 59, "000010", 6); /* Name size */
150 assertEqualMem(e
+ 65, "00000000004", 11); /* File size */
151 assertEqualMem(e
+ 76, "symlink\0", 8); /* Name contents */
152 assertEqualMem(e
+ 84, "file", 4); /* Symlink target. */
155 /* Second entry is "dir" */
156 assert(is_octal(e
, 76));
157 assertEqualMem(e
+ 0, "070707", 6); /* Magic */
158 /* Dev should be same as first entry. */
159 assert(is_octal(e
+ 6, 6)); /* dev */
160 assertEqualInt(dev
, from_octal(e
+ 6, 6));
161 /* Ino must be different from first entry. */
162 assert(is_octal(e
+ 12, 6)); /* ino */
163 assert(dev
!= from_octal(e
+ 12, 6));
164 assertEqualMem(e
+ 18, "040775", 6); /* Mode */
165 assertEqualInt(from_octal(e
+ 24, 6), getuid()); /* uid */
166 /* Gid should be same as first entry. */
167 assert(is_octal(e
+ 30, 6)); /* gid */
168 assertEqualInt(gid
, from_octal(e
+ 30, 6));
169 assertEqualMem(e
+ 36, "000002", 6); /* Nlink */
170 t
= from_octal(e
+ 48, 11); /* mtime */
171 assert(t
<= now
); /* File wasn't created in future. */
172 assert(t
>= now
- 2); /* File was created w/in last 2 secs. */
173 assertEqualMem(e
+ 59, "000004", 6); /* Name size */
174 assertEqualMem(e
+ 65, "00000000000", 11); /* File size */
175 assertEqualMem(e
+ 76, "dir\0", 4); /* name */
178 /* TODO: Verify other types of entries. */
180 /* Last entry is end-of-archive marker. */
181 assert(is_octal(e
, 76));
182 assertEqualMem(e
+ 0, "070707", 6); /* Magic */
183 assertEqualMem(e
+ 6, "000000", 6); /* dev */
184 assertEqualMem(e
+ 12, "000000", 6); /* ino */
185 assertEqualMem(e
+ 18, "000000", 6); /* Mode */
186 assertEqualMem(e
+ 24, "000000", 6); /* uid */
187 assertEqualMem(e
+ 30, "000000", 6); /* gid */
188 assertEqualMem(e
+ 36, "000001", 6); /* Nlink */
189 assertEqualMem(e
+ 42, "000000", 6); /* rdev */
190 assertEqualMem(e
+ 48, "00000000000", 11); /* mtime */
191 assertEqualMem(e
+ 59, "000013", 6); /* Name size */
192 assertEqualMem(e
+ 65, "00000000000", 11); /* File size */
193 assertEqualMem(e
+ 76, "TRAILER!!!\0", 11); /* Name */