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