]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_write_disk_lookup.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_write_disk_lookup.c
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
28 static void
29 group_cleanup(void *d)
30 {
31         int *mp = d;
32         assertEqualInt(*mp, 0x13579);
33         *mp = 0x2468;
34 }
35
36 static int64_t
37 group_lookup(void *d, const char *name, int64_t g)
38 {
39         int *mp = d;
40
41         (void)g; /* UNUSED */
42
43         assertEqualInt(*mp, 0x13579);
44         if (strcmp(name, "FOOGROUP"))
45                 return (1);
46         return (73);
47 }
48
49 static void
50 user_cleanup(void *d)
51 {
52         int *mp = d;
53         assertEqualInt(*mp, 0x1234);
54         *mp = 0x2345;
55 }
56
57 static int64_t
58 user_lookup(void *d, const char *name, int64_t u)
59 {
60         int *mp = d;
61
62         (void)u; /* UNUSED */
63
64         assertEqualInt(*mp, 0x1234);
65         if (strcmp("FOO", name) == 0)
66                 return (2);
67         return (74);
68 }
69
70 DEFINE_TEST(test_write_disk_lookup)
71 {
72         struct archive *a;
73         int gmagic = 0x13579, umagic = 0x1234;
74         int64_t id;
75
76         assert((a = archive_write_disk_new()) != NULL);
77
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));
84
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));
91
92         /* De-register. */
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);
97
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);
106
107         /* Try the standard lookup functions. */
108         if (archive_write_disk_set_standard_lookup(a) != ARCHIVE_OK) {
109                 skipping("standard uname/gname lookup");
110         } else {
111                 /* Try a few common names for group #0. */
112                 id = archive_write_disk_gid(a, "wheel", 8);
113                 if (id != 0)
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);
119 #else
120                 assertEqualInt(0, id);
121 #endif
122
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);
129 #else
130                 assertEqualInt(0, id);
131 #endif
132         }
133
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));
141
142         /* Re-register our custom handlers. */
143         gmagic = 0x13579;
144         umagic = 0x1234;
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));
149
150         /* Destroy the archive. */
151         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
152
153         /* Verify our cleanup functions got called. */
154         assertEqualInt(gmagic, 0x2468);
155         assertEqualInt(umagic, 0x2345);
156 }