summaryrefslogtreecommitdiffstats
path: root/contrib/libarchive/libarchive/test/test_write_disk_lookup.c
blob: cabdae8ddc4355b6128208dae07d4c73100e71e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*-
 * Copyright (c) 2003-2010 Tim Kientzle
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "test.h"
__FBSDID("$FreeBSD$");

static void
group_cleanup(void *d)
{
	int *mp = d;
	assertEqualInt(*mp, 0x13579);
	*mp = 0x2468;
}

static int64_t
group_lookup(void *d, const char *name, int64_t g)
{
	int *mp = d;

	(void)g; /* UNUSED */

	assertEqualInt(*mp, 0x13579);
	if (strcmp(name, "FOOGROUP"))
		return (1);
	return (73);
}

static void
user_cleanup(void *d)
{
	int *mp = d;
	assertEqualInt(*mp, 0x1234);
	*mp = 0x2345;
}

static int64_t
user_lookup(void *d, const char *name, int64_t u)
{
	int *mp = d;

	(void)u; /* UNUSED */

	assertEqualInt(*mp, 0x1234);
	if (strcmp("FOO", name) == 0)
		return (2);
	return (74);
}

DEFINE_TEST(test_write_disk_lookup)
{
	struct archive *a;
	int gmagic = 0x13579, umagic = 0x1234;
	int64_t id;

	assert((a = archive_write_disk_new()) != NULL);

	/* Default uname/gname lookups always return ID. */
	assertEqualInt(0, archive_write_disk_gid(a, "", 0));
	assertEqualInt(12, archive_write_disk_gid(a, "root", 12));
	assertEqualInt(12, archive_write_disk_gid(a, "wheel", 12));
	assertEqualInt(0, archive_write_disk_uid(a, "", 0));
	assertEqualInt(18, archive_write_disk_uid(a, "root", 18));

	/* Register some weird lookup functions. */
	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a,
		       &gmagic, &group_lookup, &group_cleanup));
	/* Verify that our new function got called. */
	assertEqualInt(73, archive_write_disk_gid(a, "FOOGROUP", 8));
	assertEqualInt(1, archive_write_disk_gid(a, "NOTFOOGROUP", 8));

	/* De-register. */
	assertEqualInt(ARCHIVE_OK,
		       archive_write_disk_set_group_lookup(a, NULL, NULL, NULL));
	/* Ensure our cleanup function got called. */
	assertEqualInt(gmagic, 0x2468);

	/* Same thing with uname lookup.... */
	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a,
			   &umagic, &user_lookup, &user_cleanup));
	assertEqualInt(2, archive_write_disk_uid(a, "FOO", 0));
	assertEqualInt(74, archive_write_disk_uid(a, "NOTFOO", 1));
	assertEqualInt(ARCHIVE_OK,
	    archive_write_disk_set_user_lookup(a, NULL, NULL, NULL));
	assertEqualInt(umagic, 0x2345);

	/* Try the standard lookup functions. */
	if (archive_write_disk_set_standard_lookup(a) != ARCHIVE_OK) {
		skipping("standard uname/gname lookup");
	} else {
		/* Try a few common names for group #0. */
		id = archive_write_disk_gid(a, "wheel", 8);
		if (id != 0)
			id = archive_write_disk_gid(a, "root", 8);
		failure("Unable to verify lookup of group #0");
#if defined(_WIN32) && !defined(__CYGWIN__)
		/* Not yet implemented on Windows. */
		assertEqualInt(8, id);
#else
		assertEqualInt(0, id);
#endif

		/* Try a few common names for user #0. */
		id = archive_write_disk_uid(a, "root", 8);
		failure("Unable to verify lookup of user #0");
#if defined(_WIN32) && !defined(__CYGWIN__)
		/* Not yet implemented on Windows. */
		assertEqualInt(8, id);
#else
		assertEqualInt(0, id);
#endif
	}

	/* Deregister again and verify the default lookups again. */
	assertEqualInt(ARCHIVE_OK,
	    archive_write_disk_set_group_lookup(a, NULL, NULL, NULL));
	assertEqualInt(ARCHIVE_OK,
	    archive_write_disk_set_user_lookup(a, NULL, NULL, NULL));
	assertEqualInt(0, archive_write_disk_gid(a, "", 0));
	assertEqualInt(0, archive_write_disk_uid(a, "", 0));

	/* Re-register our custom handlers. */
	gmagic = 0x13579;
	umagic = 0x1234;
	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_group_lookup(a,
			   &gmagic, &group_lookup, &group_cleanup));
	assertEqualInt(ARCHIVE_OK, archive_write_disk_set_user_lookup(a,
		      &umagic, &user_lookup, &user_cleanup));

	/* Destroy the archive. */
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));

	/* Verify our cleanup functions got called. */
	assertEqualInt(gmagic, 0x2468);
	assertEqualInt(umagic, 0x2345);
}
OpenPOWER on IntegriCloud