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