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
157
158
|
/*-
* Copyright (c) 2008-2011 Robert N. M. Watson
* Copyright (c) 2011 Jonathan Anderson
* 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*
* $FreeBSD$
*/
#ifndef CAP_TEST_H
#define CAP_TEST_H
#include <err.h>
/*
* Define a file required by a test. The test can't complete without the file,
* so if we don't have it, just die.
*/
#define REQUIRE(fd) do { \
if ((fd) < 0) \
err(-1, "%s:%d: Missing required file '%s'", \
__FILE__, __LINE__, #fd); \
} while (0)
/* Whether a test passed or failed. */
#define PASSED 0
#define FAILED 1
/* A test has failed; print a message and clear the 'success' flag. */
#define FAIL(...) do { \
warn(__VA_ARGS__); \
success = FAILED; \
} while (0)
/* As above, but do not print the errno message. */
#define FAILX(...) do { \
warnx(__VA_ARGS__); \
success = FAILED; \
} while (0)
/* Like an assertion, but don't kill the test, just fail and keep going. */
#define CHECK(condition) do { \
if (!(condition)) \
FAILX("%s:%d: Assertion '%s' failed", \
__func__, __LINE__, #condition); \
} while (0)
/* Make sure that a system call's return value is >= 0. */
#define CHECK_SYSCALL_SUCCEEDS(syscall, ...) do { \
if (syscall(__VA_ARGS__) < 0) \
FAIL("%s() at line %d: %s failed", \
__func__, __LINE__, #syscall); \
} while (0)
/* Make sure that a system call fails with the correct errno. */
#define CHECK_SYSCALL_FAILS(expected_errno, syscall, ...) do { \
if (syscall(__VA_ARGS__) < 0) { \
if (errno != expected_errno) \
FAIL("%s() at line %d: %s", \
__func__, __LINE__, #syscall); \
} else { \
FAILX("%s() at line %d: %s succeeded; it should've failed", \
__func__, __LINE__, #syscall); \
} \
} while (0)
/* Make sure that a system call fails, but not with a particular errno. */
#define CHECK_SYSCALL_FAILS_BUT_NOT_WITH(bad_errno, syscall, ...) do { \
if (syscall(__VA_ARGS__) < 0) { \
if (errno == bad_errno) \
FAIL("%s() at line %d: %s", \
__func__, __LINE__, #syscall); \
} else { \
FAILX("%s() at line %d: %s succeeded; it should've failed", \
__func__, __LINE__, #syscall); \
} \
} while (0)
/* A system call should fail with ECAPMODE. */
#define CHECK_CAPMODE(...) \
CHECK_SYSCALL_FAILS(ECAPMODE, __VA_ARGS__)
/* A system call should fail, but not with ECAPMODE. */
#define CHECK_NOT_CAPMODE(...) \
CHECK_SYSCALL_FAILS_BUT_NOT_WITH(ECAPMODE, __VA_ARGS__)
/* A system call should fail with ENOTCAPABLE. */
#define CHECK_NOTCAPABLE(...) \
CHECK_SYSCALL_FAILS(ENOTCAPABLE, __VA_ARGS__)
/* Ensure that 'rights' are a subset of 'max'. */
#define CHECK_RIGHTS(rights, max) do { \
if ((success == PASSED) && (rights != max)) \
FAILX("Rights of opened file (%jx) > maximum (%jx)", \
(cap_rights_t) rights, (cap_rights_t) max); \
} while (0)
/* Create a capability from a file descriptor, make sure it succeeds. */
#define MAKE_CAPABILITY(to, from, rights) do { \
cap_rights_t _rights; \
REQUIRE(to = cap_new(from, rights)); \
CHECK_SYSCALL_SUCCEEDS(cap_getrights, to, &_rights); \
if ((success == PASSED) && (_rights != (rights))) \
FAILX("New capability's rights (%jx) != %jx", \
_rights, (cap_rights_t) (rights)); \
} while (0)
/*
* A top-level test should take no arguments and return an integer value,
* either PASSED or FAILED.
*
* Errors such as SIGSEGV will be caught and interpreted as FAILED.
*/
typedef int (*test_function)(void);
/* Information about a test. */
struct test {
char *t_name;
test_function t_run;
int t_result;
};
/*
* Run a test in a child process so that cap_enter(2) doesn't mess up
* subsequent tests.
*/
int execute(int id, struct test*);
int test_capmode(void);
int test_capabilities(void);
int test_fcntl(void);
int test_pdfork(void);
int test_pdkill(void);
int test_pdwait(void);
int test_relative(void);
int test_sysctl(void);
#endif /* CAP_TEST_H */
|