1 /*-
2 * Copyright (c) 2003-2009 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
27 static void
gname_cleanup(void * d)28 gname_cleanup(void *d)
29 {
30 int *mp = d;
31 assertEqualInt(*mp, 0x13579);
32 *mp = 0x2468;
33 }
34
35 static const char *
gname_lookup(void * d,int64_t g)36 gname_lookup(void *d, int64_t g)
37 {
38 int *mp = d;
39 assertEqualInt(*mp, 0x13579);
40 if (g == 1)
41 return ("FOOGROUP");
42 return ("NOTFOOGROUP");
43 }
44
45 static void
uname_cleanup(void * d)46 uname_cleanup(void *d)
47 {
48 int *mp = d;
49 assertEqualInt(*mp, 0x1234);
50 *mp = 0x2345;
51 }
52
53 static const char *
uname_lookup(void * d,int64_t u)54 uname_lookup(void *d, int64_t u)
55 {
56 int *mp = d;
57 assertEqualInt(*mp, 0x1234);
58 if (u == 1)
59 return ("FOO");
60 return ("NOTFOO");
61 }
62
63 #if !defined(__CYGWIN__) && !defined(__HAIKU__)
64 /* We test GID lookup by looking up the name of group number zero and
65 * checking it against the following list. If your system uses a
66 * different conventional name for group number zero, please extend
67 * this array and send us a patch. As always, please keep this list
68 * sorted alphabetically.
69 */
70 static const char *zero_groups[] = {
71 "root", /* Linux */
72 "wheel" /* BSD */
73 };
74 #endif
75
DEFINE_TEST(test_read_disk)76 DEFINE_TEST(test_read_disk)
77 {
78 struct archive *a;
79 int gmagic = 0x13579, umagic = 0x1234;
80 #if !defined(__CYGWIN__) && !defined(__HAIKU__)
81 const char *p;
82 size_t i;
83 #endif
84
85 assert((a = archive_read_disk_new()) != NULL);
86
87 /* Default uname/gname lookups always return NULL. */
88 assert(archive_read_disk_gname(a, 0) == NULL);
89 assert(archive_read_disk_uname(a, 0) == NULL);
90
91 /* Register some weird lookup functions. */
92 assertEqualInt(ARCHIVE_OK, archive_read_disk_set_gname_lookup(a,
93 &gmagic, &gname_lookup, &gname_cleanup));
94 /* Verify that our new function got called. */
95 assertEqualString(archive_read_disk_gname(a, 0), "NOTFOOGROUP");
96 assertEqualString(archive_read_disk_gname(a, 1), "FOOGROUP");
97
98 /* De-register. */
99 assertEqualInt(ARCHIVE_OK,
100 archive_read_disk_set_gname_lookup(a, NULL, NULL, NULL));
101 /* Ensure our cleanup function got called. */
102 assertEqualInt(gmagic, 0x2468);
103
104 /* Same thing with uname lookup.... */
105 assertEqualInt(ARCHIVE_OK, archive_read_disk_set_uname_lookup(a,
106 &umagic, &uname_lookup, &uname_cleanup));
107 assertEqualString(archive_read_disk_uname(a, 0), "NOTFOO");
108 assertEqualString(archive_read_disk_uname(a, 1), "FOO");
109 assertEqualInt(ARCHIVE_OK,
110 archive_read_disk_set_uname_lookup(a, NULL, NULL, NULL));
111 assertEqualInt(umagic, 0x2345);
112
113 /* Try the standard lookup functions. */
114 if (archive_read_disk_set_standard_lookup(a) != ARCHIVE_OK) {
115 skipping("standard uname/gname lookup");
116 } else {
117 #if defined(__CYGWIN__) || defined(__HAIKU__)
118 /* Some platforms don't have predictable names for
119 * uid=0, so we skip this part of the test. */
120 skipping("standard uname/gname lookup");
121 #else
122 /* XXX Someday, we may need to generalize this the
123 * same way we generalized the group name check below.
124 * That's needed only if we encounter a system where
125 * uid 0 is not "root". XXX */
126 assertEqualString(archive_read_disk_uname(a, 0), "root");
127
128 /* Get the group name for group 0 and see if it makes sense. */
129 p = archive_read_disk_gname(a, 0);
130 assert(p != NULL);
131 if (p != NULL) {
132 i = 0;
133 while (i < sizeof(zero_groups)/sizeof(zero_groups[0])) {
134 if (strcmp(zero_groups[i], p) == 0)
135 break;
136 ++i;
137 }
138 if (i == sizeof(zero_groups)/sizeof(zero_groups[0])) {
139 /* If you get a failure here, either
140 * archive_read_disk_gname() isn't working or
141 * your system uses a different name for group
142 * number zero. If the latter, please add a
143 * new entry to the zero_groups[] array above.
144 */
145 failure("group 0 didn't have any of the expected names");
146 assertEqualString(p, zero_groups[0]);
147 }
148 }
149 #endif
150 }
151
152 /* Deregister again and verify the default lookups again. */
153 assertEqualInt(ARCHIVE_OK,
154 archive_read_disk_set_gname_lookup(a, NULL, NULL, NULL));
155 assertEqualInt(ARCHIVE_OK,
156 archive_read_disk_set_uname_lookup(a, NULL, NULL, NULL));
157 assert(archive_read_disk_gname(a, 0) == NULL);
158 assert(archive_read_disk_uname(a, 0) == NULL);
159
160 /* Re-register our custom handlers. */
161 gmagic = 0x13579;
162 umagic = 0x1234;
163 assertEqualInt(ARCHIVE_OK, archive_read_disk_set_gname_lookup(a,
164 &gmagic, &gname_lookup, &gname_cleanup));
165 assertEqualInt(ARCHIVE_OK, archive_read_disk_set_uname_lookup(a,
166 &umagic, &uname_lookup, &uname_cleanup));
167
168 /* Destroy the archive. */
169 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
170
171 /* Verify our cleanup functions got called. */
172 assertEqualInt(gmagic, 0x2468);
173 assertEqualInt(umagic, 0x2345);
174 }
175