1 /*-
2  * Copyright (c) 2003-2010 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
group_cleanup(void * d)28 group_cleanup(void *d)
29 {
30 	int *mp = d;
31 	assertEqualInt(*mp, 0x13579);
32 	*mp = 0x2468;
33 }
34 
35 static int64_t
group_lookup(void * d,const char * name,int64_t g)36 group_lookup(void *d, const char *name, int64_t g)
37 {
38 	int *mp = d;
39 
40 	(void)g; /* UNUSED */
41 
42 	assertEqualInt(*mp, 0x13579);
43 	if (strcmp(name, "FOOGROUP"))
44 		return (1);
45 	return (73);
46 }
47 
48 static void
user_cleanup(void * d)49 user_cleanup(void *d)
50 {
51 	int *mp = d;
52 	assertEqualInt(*mp, 0x1234);
53 	*mp = 0x2345;
54 }
55 
56 static int64_t
user_lookup(void * d,const char * name,int64_t u)57 user_lookup(void *d, const char *name, int64_t u)
58 {
59 	int *mp = d;
60 
61 	(void)u; /* UNUSED */
62 
63 	assertEqualInt(*mp, 0x1234);
64 	if (strcmp("FOO", name) == 0)
65 		return (2);
66 	return (74);
67 }
68 
DEFINE_TEST(test_write_disk_lookup)69 DEFINE_TEST(test_write_disk_lookup)
70 {
71 	struct archive *a;
72 	int gmagic = 0x13579, umagic = 0x1234;
73 	int64_t id;
74 
75 	assert((a = archive_write_disk_new()) != NULL);
76 
77 	/* Default uname/gname lookups always return ID. */
78 	assertEqualInt(0, archive_write_disk_gid(a, "", 0));
79 	assertEqualInt(12, archive_write_disk_gid(a, "root", 12));
80 	assertEqualInt(12, archive_write_disk_gid(a, "wheel", 12));
81 	assertEqualInt(0, archive_write_disk_uid(a, "", 0));
82 	assertEqualInt(18, archive_write_disk_uid(a, "root", 18));
83 
84 	/* Register some weird lookup functions. */
85 	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a,
86 		       &gmagic, &group_lookup, &group_cleanup));
87 	/* Verify that our new function got called. */
88 	assertEqualInt(73, archive_write_disk_gid(a, "FOOGROUP", 8));
89 	assertEqualInt(1, archive_write_disk_gid(a, "NOTFOOGROUP", 8));
90 
91 	/* De-register. */
92 	assertEqualInt(ARCHIVE_OK,
93 		       archive_write_disk_set_group_lookup(a, NULL, NULL, NULL));
94 	/* Ensure our cleanup function got called. */
95 	assertEqualInt(gmagic, 0x2468);
96 
97 	/* Same thing with uname lookup.... */
98 	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a,
99 			   &umagic, &user_lookup, &user_cleanup));
100 	assertEqualInt(2, archive_write_disk_uid(a, "FOO", 0));
101 	assertEqualInt(74, archive_write_disk_uid(a, "NOTFOO", 1));
102 	assertEqualInt(ARCHIVE_OK,
103 	    archive_write_disk_set_user_lookup(a, NULL, NULL, NULL));
104 	assertEqualInt(umagic, 0x2345);
105 
106 	/* Try the standard lookup functions. */
107 	if (archive_write_disk_set_standard_lookup(a) != ARCHIVE_OK) {
108 		skipping("standard uname/gname lookup");
109 	} else {
110 		/* Try a few common names for group #0. */
111 		id = archive_write_disk_gid(a, "wheel", 8);
112 		if (id != 0)
113 			id = archive_write_disk_gid(a, "root", 8);
114 		failure("Unable to verify lookup of group #0");
115 #if defined(_WIN32) && !defined(__CYGWIN__)
116 		/* Not yet implemented on Windows. */
117 		assertEqualInt(8, id);
118 #else
119 		assertEqualInt(0, id);
120 #endif
121 
122 		/* Try a few common names for user #0. */
123 		id = archive_write_disk_uid(a, "root", 8);
124 		failure("Unable to verify lookup of user #0");
125 #if defined(_WIN32) && !defined(__CYGWIN__)
126 		/* Not yet implemented on Windows. */
127 		assertEqualInt(8, id);
128 #else
129 		assertEqualInt(0, id);
130 #endif
131 	}
132 
133 	/* Deregister again and verify the default lookups again. */
134 	assertEqualInt(ARCHIVE_OK,
135 	    archive_write_disk_set_group_lookup(a, NULL, NULL, NULL));
136 	assertEqualInt(ARCHIVE_OK,
137 	    archive_write_disk_set_user_lookup(a, NULL, NULL, NULL));
138 	assertEqualInt(0, archive_write_disk_gid(a, "", 0));
139 	assertEqualInt(0, archive_write_disk_uid(a, "", 0));
140 
141 	/* Re-register our custom handlers. */
142 	gmagic = 0x13579;
143 	umagic = 0x1234;
144 	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a,
145 			   &gmagic, &group_lookup, &group_cleanup));
146 	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a,
147 		      &umagic, &user_lookup, &user_cleanup));
148 
149 	/* Destroy the archive. */
150 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
151 
152 	/* Verify our cleanup functions got called. */
153 	assertEqualInt(gmagic, 0x2468);
154 	assertEqualInt(umagic, 0x2345);
155 }
156