]>
Commit | Line | Data |
---|---|---|
ae54b3a7 EK |
1 | /* |
2 | * Copyright (C) 2020 Ed Kellett | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
17 | * USA | |
18 | */ | |
19 | #include <stdio.h> | |
20 | #include <string.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | #include "tap/basic.h" | |
24 | ||
25 | #include "stdinc.h" | |
26 | #include "client.h" | |
27 | #include "privilege.h" | |
28 | ||
29 | #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ | |
30 | ||
df4fead0 EK |
31 | void privilegeset_add_privs(struct PrivilegeSet *dst, const char *privs); |
32 | ||
ae54b3a7 EK |
33 | struct Client me; |
34 | ||
35 | static void cleanup(void) | |
36 | { | |
37 | privilegeset_prepare_rehash(); | |
38 | privilegeset_cleanup_rehash(); | |
39 | } | |
40 | ||
41 | static void test_privset_membership(void) | |
42 | { | |
43 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo bar", 0); | |
44 | ||
45 | is_bool(true, privilegeset_in_set(set, "foo"), MSG); | |
46 | is_bool(true, privilegeset_in_set(set, "bar"), MSG); | |
47 | ||
48 | is_bool(false, privilegeset_in_set(set, "qux"), MSG); | |
49 | ||
50 | cleanup(); | |
51 | } | |
52 | ||
df4fead0 EK |
53 | static void test_privset_add(void) |
54 | { | |
55 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo bar", 0); | |
56 | privilegeset_add_privs(set, "baz qux"); | |
57 | ||
58 | is_bool(true, privilegeset_in_set(set, "foo"), MSG); | |
59 | is_bool(true, privilegeset_in_set(set, "bar"), MSG); | |
60 | is_bool(true, privilegeset_in_set(set, "baz"), MSG); | |
61 | is_bool(true, privilegeset_in_set(set, "qux"), MSG); | |
62 | ||
63 | is_bool(false, privilegeset_in_set(set, "frob"), MSG); | |
64 | ||
65 | cleanup(); | |
66 | } | |
67 | ||
ae54b3a7 EK |
68 | static void test_privset_extend(void) |
69 | { | |
70 | struct PrivilegeSet *parent = privilegeset_set_new("parent", "foo bar", 0); | |
71 | struct PrivilegeSet *child = privilegeset_extend(parent, "child", "qux", 0); | |
72 | ||
73 | is_bool(true, privilegeset_in_set(child, "foo"), MSG); | |
74 | is_bool(true, privilegeset_in_set(child, "bar"), MSG); | |
75 | ||
76 | is_bool(false, privilegeset_in_set(parent, "qux"), MSG); | |
77 | is_bool(true, privilegeset_in_set(child, "qux"), MSG); | |
78 | ||
79 | cleanup(); | |
80 | } | |
81 | ||
82 | static void test_privset_persistence(void) | |
83 | { | |
84 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo", 0); | |
85 | privilegeset_ref(set); | |
86 | ||
87 | /* should survive rehash since it's referenced, but become empty */ | |
88 | privilegeset_prepare_rehash(); | |
89 | privilegeset_cleanup_rehash(); | |
90 | is_bool(false, privilegeset_in_set(set, "foo"), MSG); | |
91 | ||
92 | /* and have its contents replaced by the equal name */ | |
93 | privilegeset_set_new("test", "bar", 0); | |
94 | is_bool(true, privilegeset_in_set(set, "bar"), MSG); | |
95 | ||
96 | privilegeset_unref(set); | |
97 | cleanup(); | |
98 | } | |
99 | ||
100 | static void test_privset_diff(void) | |
101 | { | |
102 | struct PrivilegeSet *old = privilegeset_set_new("old", "foo bar", 0); | |
103 | struct PrivilegeSet *new = privilegeset_set_new("new", "foo qux", 0); | |
9962f625 | 104 | struct privset_diff diff = privilegeset_diff(old, new); |
ae54b3a7 | 105 | |
9962f625 EK |
106 | is_bool(true, privilegeset_in_set(diff.unchanged, "foo"), MSG); |
107 | is_bool(false, privilegeset_in_set(diff.added, "foo"), MSG); | |
108 | is_bool(false, privilegeset_in_set(diff.removed, "foo"), MSG); | |
ae54b3a7 | 109 | |
9962f625 EK |
110 | is_bool(false, privilegeset_in_set(diff.unchanged, "bar"), MSG); |
111 | is_bool(false, privilegeset_in_set(diff.added, "bar"), MSG); | |
112 | is_bool(true, privilegeset_in_set(diff.removed, "bar"), MSG); | |
ae54b3a7 | 113 | |
9962f625 EK |
114 | is_bool(false, privilegeset_in_set(diff.unchanged, "qux"), MSG); |
115 | is_bool(true, privilegeset_in_set(diff.added, "qux"), MSG); | |
116 | is_bool(false, privilegeset_in_set(diff.removed, "qux"), MSG); | |
ae54b3a7 EK |
117 | |
118 | cleanup(); | |
119 | } | |
120 | ||
121 | static void test_privset_diff_rehash(void) | |
122 | { | |
123 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo bar", 0); | |
9962f625 | 124 | struct privset_diff diff; |
ae54b3a7 EK |
125 | privilegeset_ref(set); |
126 | ||
127 | privilegeset_prepare_rehash(); | |
128 | ||
129 | /* should have changed from foo, bar to nothing, i.e. -foo -bar */ | |
9962f625 | 130 | diff = privilegeset_diff(set->shadow, set); |
ae54b3a7 | 131 | |
9962f625 EK |
132 | is_bool(false, privilegeset_in_set(diff.unchanged, "foo"), MSG); |
133 | is_bool(false, privilegeset_in_set(diff.added, "foo"), MSG); | |
134 | is_bool(true, privilegeset_in_set(diff.removed, "foo"), MSG); | |
ae54b3a7 | 135 | |
9962f625 EK |
136 | is_bool(false, privilegeset_in_set(diff.unchanged, "bar"), MSG); |
137 | is_bool(false, privilegeset_in_set(diff.added, "bar"), MSG); | |
138 | is_bool(true, privilegeset_in_set(diff.removed, "bar"), MSG); | |
ae54b3a7 EK |
139 | |
140 | privilegeset_set_new("test", "foo qux", 0); | |
9962f625 | 141 | diff = privilegeset_diff(set->shadow, set); |
ae54b3a7 EK |
142 | |
143 | /* should have changed from foo, bar to foo, qux, i.e. =foo -bar +qux */ | |
9962f625 EK |
144 | is_bool(true, privilegeset_in_set(diff.unchanged, "foo"), MSG); |
145 | is_bool(false, privilegeset_in_set(diff.added, "foo"), MSG); | |
146 | is_bool(false, privilegeset_in_set(diff.removed, "foo"), MSG); | |
ae54b3a7 | 147 | |
9962f625 EK |
148 | is_bool(false, privilegeset_in_set(diff.unchanged, "bar"), MSG); |
149 | is_bool(false, privilegeset_in_set(diff.added, "bar"), MSG); | |
150 | is_bool(true, privilegeset_in_set(diff.removed, "bar"), MSG); | |
ae54b3a7 | 151 | |
9962f625 EK |
152 | is_bool(false, privilegeset_in_set(diff.unchanged, "qux"), MSG); |
153 | is_bool(true, privilegeset_in_set(diff.added, "qux"), MSG); | |
154 | is_bool(false, privilegeset_in_set(diff.removed, "qux"), MSG); | |
ae54b3a7 EK |
155 | |
156 | privilegeset_cleanup_rehash(); | |
157 | ||
158 | privilegeset_unref(set); | |
159 | cleanup(); | |
160 | } | |
161 | ||
162 | int main(int argc, char *argv[]) | |
163 | { | |
164 | plan_lazy(); | |
165 | ||
166 | test_privset_membership(); | |
df4fead0 | 167 | test_privset_add(); |
ae54b3a7 EK |
168 | test_privset_extend(); |
169 | test_privset_persistence(); | |
170 | test_privset_diff(); | |
171 | test_privset_diff_rehash(); | |
172 | ||
173 | return 0; | |
174 | } |