xref: /freebsd/contrib/libarchive/cpio/test/test_owner_parse.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
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 #include "../cpio.h"
29 #include "err.h"
30 
31 #if !defined(_WIN32)
32 #define ROOT "root"
33 static const int root_uids[] = { 0 };
34 static const int root_gids[] = { 0, 1 };
35 #elif defined(__CYGWIN__)
36 /* On cygwin, the Administrator user most likely exists (unless
37  * it has been renamed or is in a non-English localization), but
38  * its primary group membership depends on how the user set up
39  * their /etc/passwd. Likely values are 513 (None), 545 (Users),
40  * or 544 (Administrators). Just check for one of those...
41  * TODO: Handle non-English localizations... e.g. French 'Administrateur'
42  *       Use CreateWellKnownSID() and LookupAccountName()?
43  */
44 #define ROOT "Administrator"
45 static const int root_uids[] = { 500 };
46 static const int root_gids[] = { 513, 545, 544 };
47 #endif
48 
49 #if defined(ROOT)
50 static int
51 int_in_list(int i, const int *l, size_t n)
52 {
53 	while (n-- > 0)
54 		if (*l++ == i)
55 			return (1);
56 	failure("%d", i);
57 	return (0);
58 }
59 #endif
60 
61 DEFINE_TEST(test_owner_parse)
62 {
63 #if !defined(ROOT)
64 	skipping("No uid/gid configuration for this OS");
65 #else
66 	int uid, gid;
67 
68 	assert(NULL == owner_parse(ROOT, &uid, &gid));
69 	assert(int_in_list(uid, root_uids,
70 		sizeof(root_uids)/sizeof(root_uids[0])));
71 	assertEqualInt(-1, gid);
72 
73 
74 	assert(NULL == owner_parse(ROOT ":", &uid, &gid));
75 	assert(int_in_list(uid, root_uids,
76 		sizeof(root_uids)/sizeof(root_uids[0])));
77 	assert(int_in_list(gid, root_gids,
78 		sizeof(root_gids)/sizeof(root_gids[0])));
79 
80 	assert(NULL == owner_parse(ROOT ".", &uid, &gid));
81 	assert(int_in_list(uid, root_uids,
82 		sizeof(root_uids)/sizeof(root_uids[0])));
83 	assert(int_in_list(gid, root_gids,
84 		sizeof(root_gids)/sizeof(root_gids[0])));
85 
86 	assert(NULL == owner_parse("111", &uid, &gid));
87 	assertEqualInt(111, uid);
88 	assertEqualInt(-1, gid);
89 
90 	assert(NULL == owner_parse("112:", &uid, &gid));
91 	assertEqualInt(112, uid);
92 	/* Can't assert gid, since we don't know gid for user #112. */
93 
94 	assert(NULL == owner_parse("113.", &uid, &gid));
95 	assertEqualInt(113, uid);
96 	/* Can't assert gid, since we don't know gid for user #113. */
97 
98 	assert(NULL == owner_parse(":114", &uid, &gid));
99 	assertEqualInt(-1, uid);
100 	assertEqualInt(114, gid);
101 
102 	assert(NULL == owner_parse(".115", &uid, &gid));
103 	assertEqualInt(-1, uid);
104 	assertEqualInt(115, gid);
105 
106 	assert(NULL == owner_parse("116:117", &uid, &gid));
107 	assertEqualInt(116, uid);
108 	assertEqualInt(117, gid);
109 
110 	/*
111 	 * TODO: Lookup current user/group name, build strings and
112 	 * use those to verify username/groupname lookups for ordinary
113 	 * users.
114 	 */
115 
116 	assert(NULL != owner_parse(":nonexistentgroup", &uid, &gid));
117 	assert(NULL != owner_parse(ROOT ":nonexistentgroup", &uid, &gid));
118 	assert(NULL !=
119 	    owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid));
120 #endif
121 }
122