]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/cpio/test/test_option_c.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / cpio / test / test_option_c.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_octal(const char *p, size_t l)
30 {
31         while (l > 0) {
32                 if (*p < '0' || *p > '7')
33                         return (0);
34                 --l;
35                 ++p;
36         }
37         return (1);
38 }
39
40 static int
41 from_octal(const char *p, size_t l)
42 {
43         int r = 0;
44
45         while (l > 0) {
46                 r *= 8;
47                 r += *p - '0';
48                 --l;
49                 ++p;
50         }
51         return (r);
52 }
53
54 DEFINE_TEST(test_option_c)
55 {
56         int fd, filelist;
57         int r;
58         int dev, ino, gid;
59         time_t t, now;
60         char *p, *e;
61         size_t s;
62         mode_t oldmask;
63
64         oldmask = umask(0);
65
66         /*
67          * Create an assortment of files.
68          * TODO: Extend this to cover more filetypes.
69          */
70         filelist = open("filelist", O_CREAT | O_WRONLY, 0644);
71
72         /* "file" */
73         fd = open("file", O_CREAT | O_WRONLY, 0644);
74         assert(fd >= 0);
75         assertEqualInt(10, write(fd, "123456789", 10));
76         close(fd);
77         assertEqualInt(5, write(filelist, "file\n", 5));
78
79         /* "symlink" */
80         assertEqualInt(0, symlink("file", "symlink"));
81         assertEqualInt(8, write(filelist, "symlink\n", 8));
82
83         /* "dir" */
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));
88
89         /* Use the cpio program to create an archive. */
90         close(filelist);
91         r = systemf("%s -oc <filelist >basic.out 2>basic.err", testprog);
92         /* Verify that nothing went to stderr. */
93         assertTextFileContents("1 block\n", "basic.err");
94
95         /* Assert that the program finished. */
96         failure("%s -oc crashed", testprog);
97         if (!assertEqualInt(r, 0))
98                 return;
99
100         /* Verify that stdout is a well-formed cpio file in "odc" format. */
101         p = slurpfile(&s, "basic.out");
102         assertEqualInt(s, 512);
103         e = p;
104
105         /*
106          * Some of these assertions could be stronger, but it's
107          * a little tricky because they depend on the local environment.
108          */
109
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 #if defined(_WIN32) && !defined(__CYGWIN__)
118         /* Group members bits and others bits do not work. */ 
119         assertEqualMem(e + 18, "100666", 6); /* Mode */
120 #else
121         assertEqualMem(e + 18, "100644", 6); /* Mode */
122 #endif
123         assertEqualInt(from_octal(e + 24, 6), getuid()); /* uid */
124         assert(is_octal(e + 30, 6)); /* gid */
125         gid = from_octal(e + 30, 6);
126         assertEqualMem(e + 36, "000001", 6); /* nlink */
127         failure("file entries should not have rdev set (dev field was 0%o)",
128             dev);
129         assertEqualMem(e + 42, "000000", 6); /* rdev */
130         t = from_octal(e + 48, 11); /* mtime */
131         assert(t <= now); /* File wasn't created in future. */
132         assert(t >= now - 2); /* File was created w/in last 2 secs. */
133         assertEqualMem(e + 59, "000005", 6); /* Name size */
134         assertEqualMem(e + 65, "00000000012", 11); /* File size */
135         assertEqualMem(e + 76, "file\0", 5); /* Name contents */
136         assertEqualMem(e + 81, "123456789\0", 10); /* File contents */
137         e += 91;
138
139         /* Second entry is "symlink" pointing to "file" */
140         assert(is_octal(e, 76)); /* Entire header is octal digits. */
141         assertEqualMem(e + 0, "070707", 6); /* Magic */
142         assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */
143         assert(dev != from_octal(e + 12, 6)); /* ino */
144 #if !defined(_WIN32) || defined(__CYGWIN__)
145         /* On Windows, symbolic link and group members bits and 
146          * others bits do not work. */ 
147         assertEqualMem(e + 18, "120777", 6); /* Mode */
148 #endif
149         assertEqualInt(from_octal(e + 24, 6), getuid()); /* uid */
150         assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */
151         assertEqualMem(e + 36, "000001", 6); /* nlink */
152         failure("file entries should have rdev == 0 (dev was 0%o)",
153             from_octal(e + 6, 6));
154         assertEqualMem(e + 42, "000000", 6); /* rdev */
155         t = from_octal(e + 48, 11); /* mtime */
156         assert(t <= now); /* File wasn't created in future. */
157         assert(t >= now - 2); /* File was created w/in last 2 secs. */
158         assertEqualMem(e + 59, "000010", 6); /* Name size */
159 #if defined(_WIN32) && !defined(__CYGWIN__)
160         /* On Windows, symbolic link does not work. */
161         assertEqualMem(e + 65, "00000000012", 11); /* File size */
162 #else
163         assertEqualMem(e + 65, "00000000004", 11); /* File size */
164 #endif
165         assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
166 #if defined(_WIN32) && !defined(__CYGWIN__)
167         /* On Windows, symbolic link does not work. */
168         assertEqualMem(e + 84, "123456789\0", 10); /* File contents. */
169         e += 94;
170 #else
171         assertEqualMem(e + 84, "file", 4); /* Symlink target. */
172         e += 88;
173 #endif
174
175         /* Second entry is "dir" */
176         assert(is_octal(e, 76));
177         assertEqualMem(e + 0, "070707", 6); /* Magic */
178         /* Dev should be same as first entry. */
179         assert(is_octal(e + 6, 6)); /* dev */
180         assertEqualInt(dev, from_octal(e + 6, 6));
181         /* Ino must be different from first entry. */
182         assert(is_octal(e + 12, 6)); /* ino */
183         assert(dev != from_octal(e + 12, 6));
184 #if defined(_WIN32) && !defined(__CYGWIN__)
185         /* Group members bits and others bits do not work. */ 
186         assertEqualMem(e + 18, "040777", 6); /* Mode */
187 #else
188         assertEqualMem(e + 18, "040775", 6); /* Mode */
189 #endif
190         assertEqualInt(from_octal(e + 24, 6), getuid()); /* uid */
191         /* Gid should be same as first entry. */
192         assert(is_octal(e + 30, 6)); /* gid */
193         assertEqualInt(gid, from_octal(e + 30, 6));
194 #ifndef NLINKS_INACCURATE_FOR_DIRS
195         assertEqualMem(e + 36, "000002", 6); /* Nlink */
196 #endif
197         t = from_octal(e + 48, 11); /* mtime */
198         assert(t <= now); /* File wasn't created in future. */
199         assert(t >= now - 2); /* File was created w/in last 2 secs. */
200         assertEqualMem(e + 59, "000004", 6); /* Name size */
201         assertEqualMem(e + 65, "00000000000", 11); /* File size */
202         assertEqualMem(e + 76, "dir\0", 4); /* name */
203         e += 80;
204
205         /* TODO: Verify other types of entries. */
206
207         /* Last entry is end-of-archive marker. */
208         assert(is_octal(e, 76));
209         assertEqualMem(e + 0, "070707", 6); /* Magic */
210         assertEqualMem(e + 6, "000000", 6); /* dev */
211         assertEqualMem(e + 12, "000000", 6); /* ino */
212         assertEqualMem(e + 18, "000000", 6); /* Mode */
213         assertEqualMem(e + 24, "000000", 6); /* uid */
214         assertEqualMem(e + 30, "000000", 6); /* gid */
215         assertEqualMem(e + 36, "000001", 6); /* Nlink */
216         assertEqualMem(e + 42, "000000", 6); /* rdev */
217         assertEqualMem(e + 48, "00000000000", 11); /* mtime */
218         assertEqualMem(e + 59, "000013", 6); /* Name size */
219         assertEqualMem(e + 65, "00000000000", 11); /* File size */
220         assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
221
222         free(p);
223
224         umask(oldmask);
225 }