test_owner_parse.c revision 228753
1228753Smm/*- 2228753Smm * Copyright (c) 2003-2009 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm#include "test.h" 26228753Smm__FBSDID("$FreeBSD$"); 27228753Smm 28228753Smm#include "../cpio.h" 29228753Smm#include "err.h" 30228753Smm 31228753Smm#if !defined(_WIN32) 32228753Smm#define ROOT "root" 33228753Smmstatic int root_uids[] = { 0 }; 34228753Smm/* Solaris 9 root has gid 1 (other) */ 35228753Smmstatic int root_gids[] = { 0, 1 }; 36228753Smm#elif defined(__CYGWIN__) 37228753Smm/* On cygwin, the Administrator user most likely exists (unless 38228753Smm * it has been renamed or is in a non-English localization), but 39228753Smm * its primary group membership depends on how the user set up 40228753Smm * their /etc/passwd. Likely values are 513 (None), 545 (Users), 41228753Smm * or 544 (Administrators). Just check for one of those... 42228753Smm * TODO: Handle non-English localizations...e.g. French 'Administrateur' 43228753Smm * Use CreateWellKnownSID() and LookupAccountName()? 44228753Smm */ 45228753Smm#define ROOT "Administrator" 46228753Smmstatic int root_uids[] = { 500 }; 47228753Smmstatic int root_gids[] = { 513, 545, 544 }; 48228753Smm#endif 49228753Smm 50228753Smm#if defined(ROOT) 51228753Smmstatic int 52228753Smmint_in_list(int i, int *l, size_t n) 53228753Smm{ 54228753Smm while (n-- > 0) 55228753Smm if (*l++ == i) 56228753Smm return (1); 57228753Smm failure("%d", i); 58228753Smm return (0); 59228753Smm} 60228753Smm#endif 61228753Smm 62228753SmmDEFINE_TEST(test_owner_parse) 63228753Smm{ 64228753Smm#if !defined(ROOT) 65228753Smm skipping("No uid/gid configuration for this OS"); 66228753Smm#else 67228753Smm int uid, gid; 68228753Smm 69228753Smm assert(NULL == owner_parse(ROOT, &uid, &gid)); 70228753Smm assert(int_in_list(uid, root_uids, 71228753Smm sizeof(root_uids)/sizeof(root_uids[0]))); 72228753Smm assertEqualInt(-1, gid); 73228753Smm 74228753Smm 75228753Smm assert(NULL == owner_parse(ROOT ":", &uid, &gid)); 76228753Smm assert(int_in_list(uid, root_uids, 77228753Smm sizeof(root_uids)/sizeof(root_uids[0]))); 78228753Smm assert(int_in_list(gid, root_gids, 79228753Smm sizeof(root_gids)/sizeof(root_gids[0]))); 80228753Smm 81228753Smm assert(NULL == owner_parse(ROOT ".", &uid, &gid)); 82228753Smm assert(int_in_list(uid, root_uids, 83228753Smm sizeof(root_uids)/sizeof(root_uids[0]))); 84228753Smm assert(int_in_list(gid, root_gids, 85228753Smm sizeof(root_gids)/sizeof(root_gids[0]))); 86228753Smm 87228753Smm assert(NULL == owner_parse("111", &uid, &gid)); 88228753Smm assertEqualInt(111, uid); 89228753Smm assertEqualInt(-1, gid); 90228753Smm 91228753Smm assert(NULL == owner_parse("112:", &uid, &gid)); 92228753Smm assertEqualInt(112, uid); 93228753Smm /* Can't assert gid, since we don't know gid for user #112. */ 94228753Smm 95228753Smm assert(NULL == owner_parse("113.", &uid, &gid)); 96228753Smm assertEqualInt(113, uid); 97228753Smm /* Can't assert gid, since we don't know gid for user #113. */ 98228753Smm 99228753Smm assert(NULL == owner_parse(":114", &uid, &gid)); 100228753Smm assertEqualInt(-1, uid); 101228753Smm assertEqualInt(114, gid); 102228753Smm 103228753Smm assert(NULL == owner_parse(".115", &uid, &gid)); 104228753Smm assertEqualInt(-1, uid); 105228753Smm assertEqualInt(115, gid); 106228753Smm 107228753Smm assert(NULL == owner_parse("116:117", &uid, &gid)); 108228753Smm assertEqualInt(116, uid); 109228753Smm assertEqualInt(117, gid); 110228753Smm 111228753Smm /* 112228753Smm * TODO: Lookup current user/group name, build strings and 113228753Smm * use those to verify username/groupname lookups for ordinary 114228753Smm * users. 115228753Smm */ 116228753Smm 117228753Smm assert(NULL != owner_parse(":nonexistentgroup", &uid, &gid)); 118228753Smm assert(NULL != owner_parse(ROOT ":nonexistentgroup", &uid, &gid)); 119228753Smm assert(NULL != 120228753Smm owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid)); 121228753Smm#endif 122228753Smm} 123