test_owner_parse.c revision 232153
1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2003-2009 Tim Kientzle
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15221828Sgrehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16221828Sgrehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17221828Sgrehan * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18221828Sgrehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19221828Sgrehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20221828Sgrehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21221828Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22221828Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23221828Sgrehan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24221828Sgrehan */
25221828Sgrehan#include "test.h"
26221828Sgrehan__FBSDID("$FreeBSD: head/contrib/libarchive/cpio/test/test_owner_parse.c 232153 2012-02-25 10:58:02Z mm $");
27221828Sgrehan
28221828Sgrehan#include "../cpio.h"
29221828Sgrehan#include "err.h"
30221828Sgrehan
31221828Sgrehan#if !defined(_WIN32)
32221828Sgrehan#define ROOT "root"
33221828Sgrehanstatic const int root_uids[] = { 0 };
34221828Sgrehanstatic const int root_gids[] = { 0, 1 };
35221828Sgrehan#elif defined(__CYGWIN__)
36259081Sneel/* On cygwin, the Administrator user most likely exists (unless
37221828Sgrehan * it has been renamed or is in a non-English localization), but
38221828Sgrehan * its primary group membership depends on how the user set up
39221828Sgrehan * their /etc/passwd. Likely values are 513 (None), 545 (Users),
40221828Sgrehan * or 544 (Administrators). Just check for one of those...
41257018Sneel * TODO: Handle non-English localizations...e.g. French 'Administrateur'
42256176Sneel *       Use CreateWellKnownSID() and LookupAccountName()?
43221828Sgrehan */
44221828Sgrehan#define ROOT "Administrator"
45221828Sgrehanstatic const int root_uids[] = { 500 };
46221828Sgrehanstatic const int root_gids[] = { 513, 545, 544 };
47221828Sgrehan#endif
48242404Sgrehan
49256176Sneel#if defined(ROOT)
50221828Sgrehanstatic int
51221828Sgrehanint_in_list(int i, const int *l, size_t n)
52221828Sgrehan{
53221828Sgrehan	while (n-- > 0)
54244167Sgrehan		if (*l++ == i)
55243327Sgrehan			return (1);
56221828Sgrehan	failure("%d", i);
57221828Sgrehan	return (0);
58261268Sjhb}
59241744Sgrehan#endif
60221828Sgrehan
61242131SgrehanDEFINE_TEST(test_owner_parse)
62221828Sgrehan{
63266125Sjhb#if !defined(ROOT)
64257293Sneel	skipping("No uid/gid configuration for this OS");
65262744Stychon#else
66221828Sgrehan	int uid, gid;
67240912Sneel
68253181Sgrehan	assert(NULL == owner_parse(ROOT, &uid, &gid));
69221828Sgrehan	assert(int_in_list(uid, root_uids,
70221828Sgrehan		sizeof(root_uids)/sizeof(root_uids[0])));
71221828Sgrehan	assertEqualInt(-1, gid);
72221828Sgrehan
73221828Sgrehan
74221828Sgrehan	assert(NULL == owner_parse(ROOT ":", &uid, &gid));
75221828Sgrehan	assert(int_in_list(uid, root_uids,
76268777Sneel		sizeof(root_uids)/sizeof(root_uids[0])));
77221828Sgrehan	assert(int_in_list(gid, root_gids,
78221828Sgrehan		sizeof(root_gids)/sizeof(root_gids[0])));
79221828Sgrehan
80221828Sgrehan	assert(NULL == owner_parse(ROOT ".", &uid, &gid));
81262744Stychon	assert(int_in_list(uid, root_uids,
82221828Sgrehan		sizeof(root_uids)/sizeof(root_uids[0])));
83262236Sneel	assert(int_in_list(gid, root_gids,
84256711Sgrehan		sizeof(root_gids)/sizeof(root_gids[0])));
85262236Sneel
86221828Sgrehan	assert(NULL == owner_parse("111", &uid, &gid));
87222105Sgrehan	assertEqualInt(111, uid);
88259635Sneel	assertEqualInt(-1, gid);
89222105Sgrehan
90243327Sgrehan	assert(NULL == owner_parse("112:", &uid, &gid));
91243327Sgrehan	assertEqualInt(112, uid);
92221828Sgrehan	/* Can't assert gid, since we don't know gid for user #112. */
93221828Sgrehan
94221828Sgrehan	assert(NULL == owner_parse("113.", &uid, &gid));
95263432Sneel	assertEqualInt(113, uid);
96221828Sgrehan	/* Can't assert gid, since we don't know gid for user #113. */
97221828Sgrehan
98221828Sgrehan	assert(NULL == owner_parse(":114", &uid, &gid));
99269042Sneel	assertEqualInt(-1, uid);
100221828Sgrehan	assertEqualInt(114, gid);
101256062Sgrehan
102221828Sgrehan	assert(NULL == owner_parse(".115", &uid, &gid));
103221828Sgrehan	assertEqualInt(-1, uid);
104221828Sgrehan	assertEqualInt(115, gid);
105221828Sgrehan
106221828Sgrehan	assert(NULL == owner_parse("116:117", &uid, &gid));
107256072Sneel	assertEqualInt(116, uid);
108221828Sgrehan	assertEqualInt(117, gid);
109221828Sgrehan
110221828Sgrehan	/*
111221828Sgrehan	 * TODO: Lookup current user/group name, build strings and
112221828Sgrehan	 * use those to verify username/groupname lookups for ordinary
113221828Sgrehan	 * users.
114221828Sgrehan	 */
115221828Sgrehan
116221828Sgrehan	assert(NULL != owner_parse(":nonexistentgroup", &uid, &gid));
117221828Sgrehan	assert(NULL != owner_parse(ROOT ":nonexistentgroup", &uid, &gid));
118265376Sneel	assert(NULL !=
119265376Sneel	    owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid));
120221828Sgrehan#endif
121221828Sgrehan}
122221828Sgrehan