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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stddef.h>
#include "alloc-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "tests.h"
#include "tmpfile-util.h"
#include "uid-range.h"
#include "user-util.h"
#include "virt.h"
TEST(uid_range) {
_cleanup_(uid_range_freep) UidRange *p = NULL;
uid_t search;
assert_se(uid_range_covers(p, 0, 0));
assert_se(!uid_range_covers(p, 0, 1));
assert_se(!uid_range_covers(p, 100, UINT32_MAX));
assert_se(uid_range_add_str(&p, "500-999") >= 0);
assert_se(p);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 500);
assert_se(p->entries[0].nr == 500);
assert_se(!uid_range_contains(p, 499));
assert_se(uid_range_contains(p, 500));
assert_se(uid_range_contains(p, 999));
assert_se(!uid_range_contains(p, 1000));
assert_se(!uid_range_covers(p, 100, 150));
assert_se(!uid_range_covers(p, 400, 200));
assert_se(!uid_range_covers(p, 499, 1));
assert_se(uid_range_covers(p, 500, 1));
assert_se(uid_range_covers(p, 501, 10));
assert_se(uid_range_covers(p, 999, 1));
assert_se(!uid_range_covers(p, 999, 2));
assert_se(!uid_range_covers(p, 1000, 1));
assert_se(!uid_range_covers(p, 1000, 100));
assert_se(!uid_range_covers(p, 1001, 100));
search = UID_INVALID;
assert_se(uid_range_next_lower(p, &search));
assert_se(search == 999);
assert_se(uid_range_next_lower(p, &search));
assert_se(search == 998);
search = 501;
assert_se(uid_range_next_lower(p, &search));
assert_se(search == 500);
assert_se(uid_range_next_lower(p, &search) == -EBUSY);
assert_se(uid_range_add_str(&p, "1000") >= 0);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 500);
assert_se(p->entries[0].nr == 501);
assert_se(uid_range_add_str(&p, "30-40") >= 0);
assert_se(p->n_entries == 2);
assert_se(p->entries[0].start == 30);
assert_se(p->entries[0].nr == 11);
assert_se(p->entries[1].start == 500);
assert_se(p->entries[1].nr == 501);
assert_se(uid_range_add_str(&p, "60-70") >= 0);
assert_se(p->n_entries == 3);
assert_se(p->entries[0].start == 30);
assert_se(p->entries[0].nr == 11);
assert_se(p->entries[1].start == 60);
assert_se(p->entries[1].nr == 11);
assert_se(p->entries[2].start == 500);
assert_se(p->entries[2].nr == 501);
assert_se(uid_range_add_str(&p, "20-2000") >= 0);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 20);
assert_se(p->entries[0].nr == 1981);
assert_se(uid_range_add_str(&p, "2002") >= 0);
assert_se(p->n_entries == 2);
assert_se(p->entries[0].start == 20);
assert_se(p->entries[0].nr == 1981);
assert_se(p->entries[1].start == 2002);
assert_se(p->entries[1].nr == 1);
assert_se(uid_range_add_str(&p, "2001") >= 0);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 20);
assert_se(p->entries[0].nr == 1983);
}
TEST(load_userns) {
_cleanup_(uid_range_freep) UidRange *p = NULL;
_cleanup_(unlink_and_freep) char *fn = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
r = uid_range_load_userns(&p, NULL);
if (r < 0 && ERRNO_IS_NOT_SUPPORTED(r))
return;
assert_se(r >= 0);
assert_se(uid_range_contains(p, getuid()));
r = running_in_userns();
if (r == 0) {
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 0);
assert_se(p->entries[0].nr == UINT32_MAX);
assert_se(uid_range_covers(p, 0, UINT32_MAX));
}
assert_se(fopen_temporary_child(NULL, &f, &fn) >= 0);
fputs("0 0 20\n"
"100 0 20\n", f);
assert_se(fflush_and_check(f) >= 0);
p = uid_range_free(p);
assert_se(uid_range_load_userns(&p, fn) >= 0);
assert_se(uid_range_contains(p, 0));
assert_se(uid_range_contains(p, 19));
assert_se(!uid_range_contains(p, 20));
assert_se(!uid_range_contains(p, 99));
assert_se(uid_range_contains(p, 100));
assert_se(uid_range_contains(p, 119));
assert_se(!uid_range_contains(p, 120));
}
TEST(uid_range_coalesce) {
_cleanup_(uid_range_freep) UidRange *p = NULL;
for (size_t i = 0; i < 10; i++) {
assert_se(uid_range_add_internal(&p, i * 10, 10, /* coalesce = */ false) >= 0);
assert_se(uid_range_add_internal(&p, i * 10 + 5, 10, /* coalesce = */ false) >= 0);
}
assert_se(uid_range_add_internal(&p, 100, 1, /* coalesce = */ true) >= 0);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 0);
assert_se(p->entries[0].nr == 105);
p = uid_range_free(p);
for (size_t i = 0; i < 10; i++) {
assert_se(uid_range_add_internal(&p, (10 - i) * 10, 10, /* coalesce = */ false) >= 0);
assert_se(uid_range_add_internal(&p, (10 - i) * 10 + 5, 10, /* coalesce = */ false) >= 0);
}
assert_se(uid_range_add_internal(&p, 100, 1, /* coalesce = */ true) >= 0);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 10);
assert_se(p->entries[0].nr == 105);
p = uid_range_free(p);
for (size_t i = 0; i < 10; i++) {
assert_se(uid_range_add_internal(&p, i * 10, 10, /* coalesce = */ false) >= 0);
assert_se(uid_range_add_internal(&p, i * 10 + 5, 10, /* coalesce = */ false) >= 0);
assert_se(uid_range_add_internal(&p, (10 - i) * 10, 10, /* coalesce = */ false) >= 0);
assert_se(uid_range_add_internal(&p, (10 - i) * 10 + 5, 10, /* coalesce = */ false) >= 0);
}
assert_se(uid_range_add_internal(&p, 100, 1, /* coalesce = */ true) >= 0);
assert_se(p->n_entries == 1);
assert_se(p->entries[0].start == 0);
assert_se(p->entries[0].nr == 115);
}
DEFINE_TEST_MAIN(LOG_DEBUG);
|