test_owner_parse.c revision 232153
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: head/contrib/libarchive/cpio/test/test_owner_parse.c 232153 2012-02-25 10:58:02Z mm $"); 27228753Smm 28228753Smm#include "../cpio.h" 29228753Smm#include "err.h" 30228753Smm 31228753Smm#if !defined(_WIN32) 32228753Smm#define ROOT "root" 33232153Smmstatic const int root_uids[] = { 0 }; 34232153Smmstatic const int root_gids[] = { 0, 1 }; 35228753Smm#elif defined(__CYGWIN__) 36228753Smm/* On cygwin, the Administrator user most likely exists (unless 37228753Smm * it has been renamed or is in a non-English localization), but 38228753Smm * its primary group membership depends on how the user set up 39228753Smm * their /etc/passwd. Likely values are 513 (None), 545 (Users), 40228753Smm * or 544 (Administrators). Just check for one of those... 41228753Smm * TODO: Handle non-English localizations...e.g. French 'Administrateur' 42228753Smm * Use CreateWellKnownSID() and LookupAccountName()? 43228753Smm */ 44228753Smm#define ROOT "Administrator" 45232153Smmstatic const int root_uids[] = { 500 }; 46232153Smmstatic const int root_gids[] = { 513, 545, 544 }; 47228753Smm#endif 48228753Smm 49228753Smm#if defined(ROOT) 50228753Smmstatic int 51232153Smmint_in_list(int i, const int *l, size_t n) 52228753Smm{ 53228753Smm while (n-- > 0) 54228753Smm if (*l++ == i) 55228753Smm return (1); 56228753Smm failure("%d", i); 57228753Smm return (0); 58228753Smm} 59228753Smm#endif 60228753Smm 61228753SmmDEFINE_TEST(test_owner_parse) 62228753Smm{ 63228753Smm#if !defined(ROOT) 64228753Smm skipping("No uid/gid configuration for this OS"); 65228753Smm#else 66228753Smm int uid, gid; 67228753Smm 68228753Smm assert(NULL == owner_parse(ROOT, &uid, &gid)); 69228753Smm assert(int_in_list(uid, root_uids, 70228753Smm sizeof(root_uids)/sizeof(root_uids[0]))); 71228753Smm assertEqualInt(-1, gid); 72228753Smm 73228753Smm 74228753Smm assert(NULL == owner_parse(ROOT ":", &uid, &gid)); 75228753Smm assert(int_in_list(uid, root_uids, 76228753Smm sizeof(root_uids)/sizeof(root_uids[0]))); 77228753Smm assert(int_in_list(gid, root_gids, 78228753Smm sizeof(root_gids)/sizeof(root_gids[0]))); 79228753Smm 80228753Smm assert(NULL == owner_parse(ROOT ".", &uid, &gid)); 81228753Smm assert(int_in_list(uid, root_uids, 82228753Smm sizeof(root_uids)/sizeof(root_uids[0]))); 83228753Smm assert(int_in_list(gid, root_gids, 84228753Smm sizeof(root_gids)/sizeof(root_gids[0]))); 85228753Smm 86228753Smm assert(NULL == owner_parse("111", &uid, &gid)); 87228753Smm assertEqualInt(111, uid); 88228753Smm assertEqualInt(-1, gid); 89228753Smm 90228753Smm assert(NULL == owner_parse("112:", &uid, &gid)); 91228753Smm assertEqualInt(112, uid); 92228753Smm /* Can't assert gid, since we don't know gid for user #112. */ 93228753Smm 94228753Smm assert(NULL == owner_parse("113.", &uid, &gid)); 95228753Smm assertEqualInt(113, uid); 96228753Smm /* Can't assert gid, since we don't know gid for user #113. */ 97228753Smm 98228753Smm assert(NULL == owner_parse(":114", &uid, &gid)); 99228753Smm assertEqualInt(-1, uid); 100228753Smm assertEqualInt(114, gid); 101228753Smm 102228753Smm assert(NULL == owner_parse(".115", &uid, &gid)); 103228753Smm assertEqualInt(-1, uid); 104228753Smm assertEqualInt(115, gid); 105228753Smm 106228753Smm assert(NULL == owner_parse("116:117", &uid, &gid)); 107228753Smm assertEqualInt(116, uid); 108228753Smm assertEqualInt(117, gid); 109228753Smm 110228753Smm /* 111228753Smm * TODO: Lookup current user/group name, build strings and 112228753Smm * use those to verify username/groupname lookups for ordinary 113228753Smm * users. 114228753Smm */ 115228753Smm 116228753Smm assert(NULL != owner_parse(":nonexistentgroup", &uid, &gid)); 117228753Smm assert(NULL != owner_parse(ROOT ":nonexistentgroup", &uid, &gid)); 118228753Smm assert(NULL != 119228753Smm owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid)); 120228753Smm#endif 121228753Smm} 122