1251881Speter/*- 2251881Speter * Copyright (c) 2003-2010 Tim Kientzle 3251881Speter * All rights reserved. 4251881Speter * 5251881Speter * Redistribution and use in source and binary forms, with or without 6251881Speter * modification, are permitted provided that the following conditions 7251881Speter * are met: 8251881Speter * 1. Redistributions of source code must retain the above copyright 9251881Speter * notice, this list of conditions and the following disclaimer. 10251881Speter * 2. Redistributions in binary form must reproduce the above copyright 11251881Speter * notice, this list of conditions and the following disclaimer in the 12251881Speter * documentation and/or other materials provided with the distribution. 13251881Speter * 14251881Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15251881Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16251881Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17251881Speter * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18251881Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19251881Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20251881Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21251881Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22251881Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23251881Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24251881Speter */ 25251881Speter#include "test.h" 26251881Speter__FBSDID("$FreeBSD$"); 27251881Speter 28251881Speterstatic void 29251881Spetergroup_cleanup(void *d) 30251881Speter{ 31251881Speter int *mp = d; 32251881Speter assertEqualInt(*mp, 0x13579); 33251881Speter *mp = 0x2468; 34251881Speter} 35251881Speter 36251881Speterstatic int64_t 37251881Spetergroup_lookup(void *d, const char *name, int64_t g) 38251881Speter{ 39251881Speter int *mp = d; 40251881Speter 41251881Speter (void)g; /* UNUSED */ 42251881Speter 43251881Speter assertEqualInt(*mp, 0x13579); 44251881Speter if (strcmp(name, "FOOGROUP")) 45251881Speter return (1); 46251881Speter return (73); 47251881Speter} 48251881Speter 49251881Speterstatic void 50251881Speteruser_cleanup(void *d) 51251881Speter{ 52251881Speter int *mp = d; 53251881Speter assertEqualInt(*mp, 0x1234); 54251881Speter *mp = 0x2345; 55251881Speter} 56251881Speter 57251881Speterstatic int64_t 58251881Speteruser_lookup(void *d, const char *name, int64_t u) 59251881Speter{ 60251881Speter int *mp = d; 61251881Speter 62251881Speter (void)u; /* UNUSED */ 63251881Speter 64251881Speter assertEqualInt(*mp, 0x1234); 65251881Speter if (strcmp("FOO", name) == 0) 66251881Speter return (2); 67251881Speter return (74); 68251881Speter} 69251881Speter 70251881SpeterDEFINE_TEST(test_write_disk_lookup) 71251881Speter{ 72251881Speter struct archive *a; 73251881Speter int gmagic = 0x13579, umagic = 0x1234; 74251881Speter int64_t id; 75251881Speter 76251881Speter assert((a = archive_write_disk_new()) != NULL); 77251881Speter 78251881Speter /* Default uname/gname lookups always return ID. */ 79251881Speter assertEqualInt(0, archive_write_disk_gid(a, "", 0)); 80251881Speter assertEqualInt(12, archive_write_disk_gid(a, "root", 12)); 81251881Speter assertEqualInt(12, archive_write_disk_gid(a, "wheel", 12)); 82251881Speter assertEqualInt(0, archive_write_disk_uid(a, "", 0)); 83251881Speter assertEqualInt(18, archive_write_disk_uid(a, "root", 18)); 84251881Speter 85251881Speter /* Register some weird lookup functions. */ 86251881Speter assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a, 87251881Speter &gmagic, &group_lookup, &group_cleanup)); 88251881Speter /* Verify that our new function got called. */ 89251881Speter assertEqualInt(73, archive_write_disk_gid(a, "FOOGROUP", 8)); 90251881Speter assertEqualInt(1, archive_write_disk_gid(a, "NOTFOOGROUP", 8)); 91251881Speter 92251881Speter /* De-register. */ 93251881Speter assertEqualInt(ARCHIVE_OK, 94251881Speter archive_write_disk_set_group_lookup(a, NULL, NULL, NULL)); 95251881Speter /* Ensure our cleanup function got called. */ 96251881Speter assertEqualInt(gmagic, 0x2468); 97251881Speter 98251881Speter /* Same thing with uname lookup.... */ 99251881Speter assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a, 100251881Speter &umagic, &user_lookup, &user_cleanup)); 101251881Speter assertEqualInt(2, archive_write_disk_uid(a, "FOO", 0)); 102251881Speter assertEqualInt(74, archive_write_disk_uid(a, "NOTFOO", 1)); 103251881Speter assertEqualInt(ARCHIVE_OK, 104251881Speter archive_write_disk_set_user_lookup(a, NULL, NULL, NULL)); 105251881Speter assertEqualInt(umagic, 0x2345); 106251881Speter 107251881Speter /* Try the standard lookup functions. */ 108251881Speter if (archive_write_disk_set_standard_lookup(a) != ARCHIVE_OK) { 109251881Speter skipping("standard uname/gname lookup"); 110251881Speter } else { 111251881Speter /* Try a few common names for group #0. */ 112251881Speter id = archive_write_disk_gid(a, "wheel", 8); 113251881Speter if (id != 0) 114251881Speter id = archive_write_disk_gid(a, "root", 8); 115251881Speter failure("Unable to verify lookup of group #0"); 116251881Speter#if defined(_WIN32) && !defined(__CYGWIN__) 117251881Speter /* Not yet implemented on Windows. */ 118251881Speter assertEqualInt(8, id); 119251881Speter#else 120251881Speter assertEqualInt(0, id); 121#endif 122 123 /* Try a few common names for user #0. */ 124 id = archive_write_disk_uid(a, "root", 8); 125 failure("Unable to verify lookup of user #0"); 126#if defined(_WIN32) && !defined(__CYGWIN__) 127 /* Not yet implemented on Windows. */ 128 assertEqualInt(8, id); 129#else 130 assertEqualInt(0, id); 131#endif 132 } 133 134 /* Deregister again and verify the default lookups again. */ 135 assertEqualInt(ARCHIVE_OK, 136 archive_write_disk_set_group_lookup(a, NULL, NULL, NULL)); 137 assertEqualInt(ARCHIVE_OK, 138 archive_write_disk_set_user_lookup(a, NULL, NULL, NULL)); 139 assertEqualInt(0, archive_write_disk_gid(a, "", 0)); 140 assertEqualInt(0, archive_write_disk_uid(a, "", 0)); 141 142 /* Re-register our custom handlers. */ 143 gmagic = 0x13579; 144 umagic = 0x1234; 145 assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a, 146 &gmagic, &group_lookup, &group_cleanup)); 147 assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a, 148 &umagic, &user_lookup, &user_cleanup)); 149 150 /* Destroy the archive. */ 151 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 152 153 /* Verify our cleanup functions got called. */ 154 assertEqualInt(gmagic, 0x2468); 155 assertEqualInt(umagic, 0x2345); 156} 157