1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003-2009 Tim Kientzle 5 * All rights reserved. 6 */ 7 #include "test.h" 8 9 #include "../cpio.h" 10 #include "err.h" 11 12 #if !defined(_WIN32) 13 #define ROOT "root" 14 static const int root_uids[] = { 0 }; 15 static const int root_gids[] = { 0, 1 }; 16 #elif defined(__CYGWIN__) 17 /* On cygwin, the Administrator user most likely exists (unless 18 * it has been renamed or is in a non-English localization), but 19 * its primary group membership depends on how the user set up 20 * their /etc/passwd. Likely values are 513 (None), 545 (Users), 21 * or 544 (Administrators). Just check for one of those... 22 * TODO: Handle non-English localizations... e.g. French 'Administrateur' 23 * Use CreateWellKnownSID() and LookupAccountName()? 24 */ 25 #define ROOT "Administrator" 26 static const int root_uids[] = { 500 }; 27 static const int root_gids[] = { 513, 545, 544 }; 28 #endif 29 30 #if defined(ROOT) 31 static int 32 int_in_list(int i, const int *l, size_t n) 33 { 34 while (n-- > 0) 35 if (*l++ == i) 36 return (1); 37 failure("%d", i); 38 return (0); 39 } 40 41 static void 42 free_cpio_owner(struct cpio_owner *owner) { 43 owner->uid = -1; 44 owner->gid = -1; 45 free(owner->uname); 46 free(owner->gname); 47 } 48 #endif 49 50 DEFINE_TEST(test_owner_parse) 51 { 52 #if !defined(ROOT) 53 skipping("No uid/gid configuration for this OS"); 54 #else 55 struct cpio_owner owner; 56 const char *errstr; 57 58 assert(0 == owner_parse(ROOT, &owner, &errstr)); 59 assert(int_in_list(owner.uid, root_uids, 60 sizeof(root_uids)/sizeof(root_uids[0]))); 61 assertEqualInt(-1, owner.gid); 62 free_cpio_owner(&owner); 63 64 assert(0 == owner_parse(ROOT ":", &owner, &errstr)); 65 assert(int_in_list(owner.uid, root_uids, 66 sizeof(root_uids)/sizeof(root_uids[0]))); 67 assert(int_in_list(owner.gid, root_gids, 68 sizeof(root_gids)/sizeof(root_gids[0]))); 69 free_cpio_owner(&owner); 70 71 assert(0 == owner_parse(ROOT ".", &owner, &errstr)); 72 assert(int_in_list(owner.uid, root_uids, 73 sizeof(root_uids)/sizeof(root_uids[0]))); 74 assert(int_in_list(owner.gid, root_gids, 75 sizeof(root_gids)/sizeof(root_gids[0]))); 76 free_cpio_owner(&owner); 77 78 assert(0 == owner_parse("111", &owner, &errstr)); 79 assertEqualInt(111, owner.uid); 80 assertEqualInt(-1, owner.gid); 81 free_cpio_owner(&owner); 82 83 assert(0 == owner_parse("112:", &owner, &errstr)); 84 assertEqualInt(112, owner.uid); 85 /* Can't assert gid, since we don't know gid for user #112. */ 86 free_cpio_owner(&owner); 87 88 assert(0 == owner_parse("113.", &owner, &errstr)); 89 assertEqualInt(113, owner.uid); 90 /* Can't assert gid, since we don't know gid for user #113. */ 91 free_cpio_owner(&owner); 92 93 assert(0 == owner_parse(":114", &owner, &errstr)); 94 assertEqualInt(-1, owner.uid); 95 assertEqualInt(114, owner.gid); 96 free_cpio_owner(&owner); 97 98 assert(0 == owner_parse(".115", &owner, &errstr)); 99 assertEqualInt(-1, owner.uid); 100 assertEqualInt(115, owner.gid); 101 free_cpio_owner(&owner); 102 103 assert(0 == owner_parse("116:117", &owner, &errstr)); 104 assertEqualInt(116, owner.uid); 105 assertEqualInt(117, owner.gid); 106 free_cpio_owner(&owner); 107 108 /* 109 * TODO: Lookup current user/group name, build strings and 110 * use those to verify username/groupname lookups for ordinary 111 * users. 112 */ 113 114 errstr = NULL; 115 assert(0 != owner_parse(":nonexistentgroup", &owner, &errstr)); 116 assertEqualString(errstr, "Couldn't lookup group ``nonexistentgroup''"); 117 free_cpio_owner(&owner); 118 119 errstr = NULL; 120 assert(0 != owner_parse(ROOT ":nonexistentgroup", &owner, &errstr)); 121 assertEqualString(errstr, "Couldn't lookup group ``nonexistentgroup''"); 122 free_cpio_owner(&owner); 123 124 errstr = NULL; 125 assert(0 != owner_parse("nonexistentuser:nonexistentgroup", &owner, 126 &errstr)); 127 assertEqualString(errstr, "Couldn't lookup user ``nonexistentuser''"); 128 free_cpio_owner(&owner); 129 #endif 130 } 131