2
* Copyright (c) 2003-2009 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: head/lib/libarchive/test/test_read_disk.c 201247 2009-12-30 05:59:21Z kientzle $");
29
gname_cleanup(void *d)
32
assertEqualInt(*mp, 0x13579);
37
gname_lookup(void *d, gid_t g)
40
assertEqualInt(*mp, 0x13579);
43
return ("NOTFOOGROUP");
47
uname_cleanup(void *d)
50
assertEqualInt(*mp, 0x1234);
55
uname_lookup(void *d, uid_t u)
58
assertEqualInt(*mp, 0x1234);
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.
70
static const char *zero_groups[] = {
75
DEFINE_TEST(test_read_disk)
78
int gmagic = 0x13579, umagic = 0x1234;
82
assert((a = archive_read_disk_new()) != NULL);
84
/* Default uname/gname lookups always return NULL. */
85
assert(archive_read_disk_gname(a, 0) == NULL);
86
assert(archive_read_disk_uname(a, 0) == NULL);
88
/* Register some weird lookup functions. */
89
assertEqualInt(ARCHIVE_OK, archive_read_disk_set_gname_lookup(a,
90
&gmagic, &gname_lookup, &gname_cleanup));
91
/* Verify that our new function got called. */
92
assertEqualString(archive_read_disk_gname(a, 0), "NOTFOOGROUP");
93
assertEqualString(archive_read_disk_gname(a, 1), "FOOGROUP");
96
assertEqualInt(ARCHIVE_OK,
97
archive_read_disk_set_gname_lookup(a, NULL, NULL, NULL));
98
/* Ensure our cleanup function got called. */
99
assertEqualInt(gmagic, 0x2468);
101
/* Same thing with uname lookup.... */
102
assertEqualInt(ARCHIVE_OK, archive_read_disk_set_uname_lookup(a,
103
&umagic, &uname_lookup, &uname_cleanup));
104
assertEqualString(archive_read_disk_uname(a, 0), "NOTFOO");
105
assertEqualString(archive_read_disk_uname(a, 1), "FOO");
106
assertEqualInt(ARCHIVE_OK,
107
archive_read_disk_set_uname_lookup(a, NULL, NULL, NULL));
108
assertEqualInt(umagic, 0x2345);
110
/* Try the standard lookup functions. */
111
if (archive_read_disk_set_standard_lookup(a) != ARCHIVE_OK) {
112
skipping("standard uname/gname lookup");
114
#if defined(__CYGWIN__) || defined(__HAIKU__)
115
/* Some platforms don't have predictable names for
116
* uid=0, so we skip this part of the test. */
117
skipping("standard uname/gname lookup");
119
p = zero_groups[0]; /* avoid unused warnings */
121
/* XXX Someday, we may need to generalize this the
122
* same way we generalized the group name check below.
123
* That's needed only if we encounter a system where
124
* uid 0 is not "root". XXX */
125
assertEqualString(archive_read_disk_uname(a, 0), "root");
127
/* Get the group name for group 0 and see if it makes sense. */
128
p = archive_read_disk_gname(a, 0);
129
if (assert(p != NULL)) {
131
while (i < sizeof(zero_groups)/sizeof(zero_groups[0])) {
132
if (strcmp(zero_groups[i], p) == 0)
136
if (i == sizeof(zero_groups)/sizeof(zero_groups[0])) {
137
/* If you get a failure here, either
138
* archive_read_disk_gname() isn't working or
139
* your system uses a different name for group
140
* number zero. If the latter, please add a
141
* new entry to the zero_groups[] array above.
143
failure("group 0 didn't have any of the expected names");
144
assertEqualString(p, zero_groups[0]);
150
/* Deregister again and verify the default lookups again. */
151
assertEqualInt(ARCHIVE_OK,
152
archive_read_disk_set_gname_lookup(a, NULL, NULL, NULL));
153
assertEqualInt(ARCHIVE_OK,
154
archive_read_disk_set_uname_lookup(a, NULL, NULL, NULL));
155
assert(archive_read_disk_gname(a, 0) == NULL);
156
assert(archive_read_disk_uname(a, 0) == NULL);
158
/* Re-register our custom handlers. */
161
assertEqualInt(ARCHIVE_OK, archive_read_disk_set_gname_lookup(a,
162
&gmagic, &gname_lookup, &gname_cleanup));
163
assertEqualInt(ARCHIVE_OK, archive_read_disk_set_uname_lookup(a,
164
&umagic, &uname_lookup, &uname_cleanup));
166
/* Destroy the archive. */
167
assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
169
/* Verify our cleanup functions got called. */
170
assertEqualInt(gmagic, 0x2468);
171
assertEqualInt(umagic, 0x2345);