test_owner_parse.c revision 311042
1324145Smm/*- 2324145Smm * Copyright (c) 2003-2009 Tim Kientzle 3324145Smm * All rights reserved. 4324145Smm * 5324145Smm * Redistribution and use in source and binary forms, with or without 6324145Smm * modification, are permitted provided that the following conditions 7324145Smm * are met: 8324145Smm * 1. Redistributions of source code must retain the above copyright 9324145Smm * notice, this list of conditions and the following disclaimer. 10324145Smm * 2. Redistributions in binary form must reproduce the above copyright 11324145Smm * notice, this list of conditions and the following disclaimer in the 12324145Smm * documentation and/or other materials provided with the distribution. 13324145Smm * 14324145Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15324145Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16324145Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17324145Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18324145Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19324145Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20324145Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21324145Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22324145Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23324145Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24324145Smm */ 25324145Smm#include "test.h" 26324145Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/cpio/test/test_owner_parse.c 311042 2017-01-02 01:43:11Z mm $"); 27324145Smm 28324145Smm#include "../cpio.h" 29324145Smm#include "err.h" 30324145Smm 31324145Smm#if !defined(_WIN32) 32324145Smm#define ROOT "root" 33324145Smmstatic const int root_uids[] = { 0 }; 34324145Smmstatic const int root_gids[] = { 0, 1 }; 35324145Smm#elif defined(__CYGWIN__) 36324145Smm/* On cygwin, the Administrator user most likely exists (unless 37324145Smm * it has been renamed or is in a non-English localization), but 38324145Smm * its primary group membership depends on how the user set up 39324145Smm * their /etc/passwd. Likely values are 513 (None), 545 (Users), 40324145Smm * or 544 (Administrators). Just check for one of those... 41324145Smm * TODO: Handle non-English localizations... e.g. French 'Administrateur' 42324145Smm * Use CreateWellKnownSID() and LookupAccountName()? 43324145Smm */ 44324145Smm#define ROOT "Administrator" 45324145Smmstatic const int root_uids[] = { 500 }; 46324145Smmstatic const int root_gids[] = { 513, 545, 544 }; 47324145Smm#endif 48324145Smm 49324145Smm#if defined(ROOT) 50324145Smmstatic int 51324145Smmint_in_list(int i, const int *l, size_t n) 52324145Smm{ 53324145Smm while (n-- > 0) 54324145Smm if (*l++ == i) 55324145Smm return (1); 56324145Smm failure("%d", i); 57324145Smm return (0); 58324145Smm} 59324145Smm#endif 60324145Smm 61324145SmmDEFINE_TEST(test_owner_parse) 62324145Smm{ 63324145Smm#if !defined(ROOT) 64324145Smm skipping("No uid/gid configuration for this OS"); 65324145Smm#else 66324145Smm int uid, gid; 67324145Smm 68324145Smm assert(NULL == owner_parse(ROOT, &uid, &gid)); 69324145Smm assert(int_in_list(uid, root_uids, 70324145Smm sizeof(root_uids)/sizeof(root_uids[0]))); 71324145Smm assertEqualInt(-1, gid); 72324145Smm 73324145Smm 74324145Smm assert(NULL == owner_parse(ROOT ":", &uid, &gid)); 75324145Smm assert(int_in_list(uid, root_uids, 76324145Smm sizeof(root_uids)/sizeof(root_uids[0]))); 77324145Smm assert(int_in_list(gid, root_gids, 78324145Smm sizeof(root_gids)/sizeof(root_gids[0]))); 79324145Smm 80324145Smm assert(NULL == owner_parse(ROOT ".", &uid, &gid)); 81324145Smm assert(int_in_list(uid, root_uids, 82324145Smm sizeof(root_uids)/sizeof(root_uids[0]))); 83324145Smm assert(int_in_list(gid, root_gids, 84324145Smm sizeof(root_gids)/sizeof(root_gids[0]))); 85324145Smm 86324145Smm assert(NULL == owner_parse("111", &uid, &gid)); 87324145Smm assertEqualInt(111, uid); 88324145Smm assertEqualInt(-1, gid); 89324145Smm 90324145Smm assert(NULL == owner_parse("112:", &uid, &gid)); 91324145Smm assertEqualInt(112, uid); 92324145Smm /* Can't assert gid, since we don't know gid for user #112. */ 93324145Smm 94324145Smm assert(NULL == owner_parse("113.", &uid, &gid)); 95324145Smm assertEqualInt(113, uid); 96324145Smm /* Can't assert gid, since we don't know gid for user #113. */ 97324145Smm 98324145Smm assert(NULL == owner_parse(":114", &uid, &gid)); 99324145Smm assertEqualInt(-1, uid); 100324145Smm assertEqualInt(114, gid); 101324145Smm 102324145Smm assert(NULL == owner_parse(".115", &uid, &gid)); 103324145Smm assertEqualInt(-1, uid); 104324145Smm assertEqualInt(115, gid); 105324145Smm 106324145Smm assert(NULL == owner_parse("116:117", &uid, &gid)); 107324145Smm assertEqualInt(116, uid); 108324145Smm assertEqualInt(117, gid); 109324145Smm 110324145Smm /* 111324145Smm * TODO: Lookup current user/group name, build strings and 112324145Smm * use those to verify username/groupname lookups for ordinary 113324145Smm * users. 114324145Smm */ 115324145Smm 116324145Smm assert(NULL != owner_parse(":nonexistentgroup", &uid, &gid)); 117324145Smm assert(NULL != owner_parse(ROOT ":nonexistentgroup", &uid, &gid)); 118324145Smm assert(NULL != 119324145Smm owner_parse("nonexistentuser:nonexistentgroup", &uid, &gid)); 120324145Smm#endif 121324145Smm} 122362134Smm