2 * Copyright (c) 2003-2010 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$");
29 group_cleanup(void *d)
32 assertEqualInt(*mp, 0x13579);
37 group_lookup(void *d, const char *name, int64_t g)
43 assertEqualInt(*mp, 0x13579);
44 if (strcmp(name, "FOOGROUP"))
53 assertEqualInt(*mp, 0x1234);
58 user_lookup(void *d, const char *name, int64_t u)
64 assertEqualInt(*mp, 0x1234);
65 if (strcmp("FOO", name) == 0)
70 DEFINE_TEST(test_write_disk_lookup)
73 int gmagic = 0x13579, umagic = 0x1234;
76 assert((a = archive_write_disk_new()) != NULL);
78 /* Default uname/gname lookups always return ID. */
79 assertEqualInt(0, archive_write_disk_gid(a, "", 0));
80 assertEqualInt(12, archive_write_disk_gid(a, "root", 12));
81 assertEqualInt(12, archive_write_disk_gid(a, "wheel", 12));
82 assertEqualInt(0, archive_write_disk_uid(a, "", 0));
83 assertEqualInt(18, archive_write_disk_uid(a, "root", 18));
85 /* Register some weird lookup functions. */
86 assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a,
87 &gmagic, &group_lookup, &group_cleanup));
88 /* Verify that our new function got called. */
89 assertEqualInt(73, archive_write_disk_gid(a, "FOOGROUP", 8));
90 assertEqualInt(1, archive_write_disk_gid(a, "NOTFOOGROUP", 8));
93 assertEqualInt(ARCHIVE_OK,
94 archive_write_disk_set_group_lookup(a, NULL, NULL, NULL));
95 /* Ensure our cleanup function got called. */
96 assertEqualInt(gmagic, 0x2468);
98 /* Same thing with uname lookup.... */
99 assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a,
100 &umagic, &user_lookup, &user_cleanup));
101 assertEqualInt(2, archive_write_disk_uid(a, "FOO", 0));
102 assertEqualInt(74, archive_write_disk_uid(a, "NOTFOO", 1));
103 assertEqualInt(ARCHIVE_OK,
104 archive_write_disk_set_user_lookup(a, NULL, NULL, NULL));
105 assertEqualInt(umagic, 0x2345);
107 /* Try the standard lookup functions. */
108 if (archive_write_disk_set_standard_lookup(a) != ARCHIVE_OK) {
109 skipping("standard uname/gname lookup");
111 /* Try a few common names for group #0. */
112 id = archive_write_disk_gid(a, "wheel", 8);
114 id = archive_write_disk_gid(a, "root", 8);
115 failure("Unable to verify lookup of group #0");
116 #if defined(_WIN32) && !defined(__CYGWIN__)
117 /* Not yet implemented on Windows. */
118 assertEqualInt(8, id);
120 assertEqualInt(0, id);
123 /* Try a few common names for user #0. */
124 id = archive_write_disk_uid(a, "root", 8);
125 failure("Unable to verify lookup of user #0");
126 #if defined(_WIN32) && !defined(__CYGWIN__)
127 /* Not yet implemented on Windows. */
128 assertEqualInt(8, id);
130 assertEqualInt(0, id);
134 /* Deregister again and verify the default lookups again. */
135 assertEqualInt(ARCHIVE_OK,
136 archive_write_disk_set_group_lookup(a, NULL, NULL, NULL));
137 assertEqualInt(ARCHIVE_OK,
138 archive_write_disk_set_user_lookup(a, NULL, NULL, NULL));
139 assertEqualInt(0, archive_write_disk_gid(a, "", 0));
140 assertEqualInt(0, archive_write_disk_uid(a, "", 0));
142 /* Re-register our custom handlers. */
145 assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a,
146 &gmagic, &group_lookup, &group_cleanup));
147 assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a,
148 &umagic, &user_lookup, &user_cleanup));
150 /* Destroy the archive. */
151 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
153 /* Verify our cleanup functions got called. */
154 assertEqualInt(gmagic, 0x2468);
155 assertEqualInt(umagic, 0x2345);