]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/cpio/test/test_option_c.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / libarchive / 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 #if !defined(_WIN32) || defined(__CYGWIN__)
55 static int
56 nlinks(const char *p)
57 {
58         struct stat st;
59         assertEqualInt(0, stat(p, &st));
60         return st.st_nlink;
61 }
62 #endif
63
64 DEFINE_TEST(test_option_c)
65 {
66         FILE *filelist;
67         int r;
68         int uid = -1;
69         int dev, ino, gid;
70         time_t t, now;
71         char *p, *e;
72         size_t s;
73
74         assertUmask(0);
75
76 #if !defined(_WIN32)
77         uid = getuid();
78 #endif
79
80         /*
81          * Create an assortment of files.
82          * TODO: Extend this to cover more filetypes.
83          */
84         filelist = fopen("filelist", "w");
85
86         /* "file" */
87         assertMakeFile("file", 0644, "1234567890");
88         fprintf(filelist, "file\n");
89
90         /* "symlink" */
91         if (canSymlink()) {
92                 assertMakeSymlink("symlink", "file");
93                 fprintf(filelist, "symlink\n");
94         }
95
96         /* "dir" */
97         assertMakeDir("dir", 0775);
98         /* Record some facts about what we just created: */
99         now = time(NULL); /* They were all created w/in last two seconds. */
100         fprintf(filelist, "dir\n");
101
102         /* Use the cpio program to create an archive. */
103         fclose(filelist);
104         r = systemf("%s -oc <filelist >basic.out 2>basic.err", testprog);
105         /* Verify that nothing went to stderr. */
106         assertTextFileContents("1 block\n", "basic.err");
107
108         /* Assert that the program finished. */
109         failure("%s -oc crashed", testprog);
110         if (!assertEqualInt(r, 0))
111                 return;
112
113         /* Verify that stdout is a well-formed cpio file in "odc" format. */
114         p = slurpfile(&s, "basic.out");
115         assertEqualInt(s, 512);
116         e = p;
117
118         /*
119          * Some of these assertions could be stronger, but it's
120          * a little tricky because they depend on the local environment.
121          */
122
123         /* First entry is "file" */
124         assert(is_octal(e, 76)); /* Entire header is octal digits. */
125         assertEqualMem(e + 0, "070707", 6); /* Magic */
126         assert(is_octal(e + 6, 6)); /* dev */
127         dev = from_octal(e + 6, 6);
128         assert(is_octal(e + 12, 6)); /* ino */
129         ino = from_octal(e + 12, 6);
130 #if defined(_WIN32) && !defined(__CYGWIN__)
131         /* Group members bits and others bits do not work. */
132         assertEqualMem(e + 18, "100666", 6); /* Mode */
133 #else
134         assertEqualMem(e + 18, "100644", 6); /* Mode */
135 #endif
136         if (uid < 0)
137                 uid = from_octal(e + 24, 6);
138         assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
139         assert(is_octal(e + 30, 6)); /* gid */
140         gid = from_octal(e + 30, 6);
141         assertEqualMem(e + 36, "000001", 6); /* nlink */
142         failure("file entries should not have rdev set (dev field was 0%o)",
143             dev);
144         assertEqualMem(e + 42, "000000", 6); /* rdev */
145         t = from_octal(e + 48, 11); /* mtime */
146         assert(t <= now); /* File wasn't created in future. */
147         assert(t >= now - 2); /* File was created w/in last 2 secs. */
148         assertEqualMem(e + 59, "000005", 6); /* Name size */
149         assertEqualMem(e + 65, "00000000012", 11); /* File size */
150         assertEqualMem(e + 76, "file\0", 5); /* Name contents */
151         assertEqualMem(e + 81, "1234567890", 10); /* File contents */
152         e += 91;
153
154         /* "symlink" pointing to "file" */
155         if (canSymlink()) {
156                 assert(is_octal(e, 76)); /* Entire header is octal digits. */
157                 assertEqualMem(e + 0, "070707", 6); /* Magic */
158                 assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */
159                 assert(ino != from_octal(e + 12, 6)); /* ino */
160 #if !defined(_WIN32) || defined(__CYGWIN__)
161                 /* On Windows, symbolic link and group members bits and
162                  * others bits do not work. */
163                 assertEqualMem(e + 18, "120777", 6); /* Mode */
164 #endif
165                 assertEqualInt(from_octal(e + 24, 6), uid); /* uid */
166                 assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */
167                 assertEqualMem(e + 36, "000001", 6); /* nlink */
168                 failure("file entries should have rdev == 0 (dev was 0%o)",
169                     from_octal(e + 6, 6));
170                 assertEqualMem(e + 42, "000000", 6); /* rdev */
171                 t = from_octal(e + 48, 11); /* mtime */
172                 assert(t <= now); /* File wasn't created in future. */
173                 assert(t >= now - 2); /* File was created w/in last 2 secs. */
174                 assertEqualMem(e + 59, "000010", 6); /* Name size */
175                 assertEqualMem(e + 65, "00000000004", 11); /* File size */
176                 assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
177                 assertEqualMem(e + 84, "file", 4); /* Symlink target. */
178                 e += 88;
179         }
180
181         /* "dir" */
182         assert(is_octal(e, 76));
183         assertEqualMem(e + 0, "070707", 6); /* Magic */
184         /* Dev should be same as first entry. */
185         assert(is_octal(e + 6, 6)); /* dev */
186         assertEqualInt(dev, from_octal(e + 6, 6));
187         /* Ino must be different from first entry. */
188         assert(is_octal(e + 12, 6)); /* ino */
189         assert(ino != from_octal(e + 12, 6));
190 #if defined(_WIN32) && !defined(__CYGWIN__)
191         /* Group members bits and others bits do not work. */
192         assertEqualMem(e + 18, "040777", 6); /* Mode */
193 #else
194         /* Accept 042775 to accommodate systems where sgid bit propagates. */
195         if (memcmp(e + 18, "042775", 6) != 0)
196                 assertEqualMem(e + 18, "040775", 6); /* Mode */
197 #endif
198         assertEqualInt(uid, from_octal(e + 24, 6)); /* uid */
199         /* Gid should be same as first entry. */
200         assert(is_octal(e + 30, 6)); /* gid */
201         assertEqualInt(gid, from_octal(e + 30, 6));
202
203 #if !defined(_WIN32) || defined(__CYGWIN__)
204         assertEqualInt(nlinks("dir"), from_octal(e + 36, 6)); /* Nlink */
205 #endif
206
207         t = from_octal(e + 48, 11); /* mtime */
208         assert(t <= now); /* File wasn't created in future. */
209         assert(t >= now - 2); /* File was created w/in last 2 secs. */
210         assertEqualMem(e + 59, "000004", 6); /* Name size */
211         assertEqualMem(e + 65, "00000000000", 11); /* File size */
212         assertEqualMem(e + 76, "dir\0", 4); /* name */
213         e += 80;
214
215         /* TODO: Verify other types of entries. */
216
217         /* Last entry is end-of-archive marker. */
218         assert(is_octal(e, 76));
219         assertEqualMem(e + 0, "070707", 6); /* Magic */
220         assertEqualMem(e + 6, "000000", 6); /* dev */
221         assertEqualMem(e + 12, "000000", 6); /* ino */
222         assertEqualMem(e + 18, "000000", 6); /* Mode */
223         assertEqualMem(e + 24, "000000", 6); /* uid */
224         assertEqualMem(e + 30, "000000", 6); /* gid */
225         assertEqualMem(e + 36, "000001", 6); /* Nlink */
226         assertEqualMem(e + 42, "000000", 6); /* rdev */
227         assertEqualMem(e + 48, "00000000000", 11); /* mtime */
228         assertEqualMem(e + 59, "000013", 6); /* Name size */
229         assertEqualMem(e + 65, "00000000000", 11); /* File size */
230         assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
231
232         free(p);
233 }