]>
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 | ||
31 | struct Client me; | |
32 | ||
33 | static void cleanup(void) | |
34 | { | |
35 | privilegeset_prepare_rehash(); | |
36 | privilegeset_cleanup_rehash(); | |
37 | } | |
38 | ||
39 | static void test_privset_membership(void) | |
40 | { | |
41 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo bar", 0); | |
42 | ||
43 | is_bool(true, privilegeset_in_set(set, "foo"), MSG); | |
44 | is_bool(true, privilegeset_in_set(set, "bar"), MSG); | |
45 | ||
46 | is_bool(false, privilegeset_in_set(set, "qux"), MSG); | |
47 | ||
48 | cleanup(); | |
49 | } | |
50 | ||
51 | static void test_privset_extend(void) | |
52 | { | |
53 | struct PrivilegeSet *parent = privilegeset_set_new("parent", "foo bar", 0); | |
54 | struct PrivilegeSet *child = privilegeset_extend(parent, "child", "qux", 0); | |
55 | ||
56 | is_bool(true, privilegeset_in_set(child, "foo"), MSG); | |
57 | is_bool(true, privilegeset_in_set(child, "bar"), MSG); | |
58 | ||
59 | is_bool(false, privilegeset_in_set(parent, "qux"), MSG); | |
60 | is_bool(true, privilegeset_in_set(child, "qux"), MSG); | |
61 | ||
62 | cleanup(); | |
63 | } | |
64 | ||
65 | static void test_privset_persistence(void) | |
66 | { | |
67 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo", 0); | |
68 | privilegeset_ref(set); | |
69 | ||
70 | /* should survive rehash since it's referenced, but become empty */ | |
71 | privilegeset_prepare_rehash(); | |
72 | privilegeset_cleanup_rehash(); | |
73 | is_bool(false, privilegeset_in_set(set, "foo"), MSG); | |
74 | ||
75 | /* and have its contents replaced by the equal name */ | |
76 | privilegeset_set_new("test", "bar", 0); | |
77 | is_bool(true, privilegeset_in_set(set, "bar"), MSG); | |
78 | ||
79 | privilegeset_unref(set); | |
80 | cleanup(); | |
81 | } | |
82 | ||
83 | static void test_privset_diff(void) | |
84 | { | |
85 | struct PrivilegeSet *old = privilegeset_set_new("old", "foo bar", 0); | |
86 | struct PrivilegeSet *new = privilegeset_set_new("new", "foo qux", 0); | |
9962f625 | 87 | struct privset_diff diff = privilegeset_diff(old, new); |
ae54b3a7 | 88 | |
9962f625 EK |
89 | is_bool(true, privilegeset_in_set(diff.unchanged, "foo"), MSG); |
90 | is_bool(false, privilegeset_in_set(diff.added, "foo"), MSG); | |
91 | is_bool(false, privilegeset_in_set(diff.removed, "foo"), MSG); | |
ae54b3a7 | 92 | |
9962f625 EK |
93 | is_bool(false, privilegeset_in_set(diff.unchanged, "bar"), MSG); |
94 | is_bool(false, privilegeset_in_set(diff.added, "bar"), MSG); | |
95 | is_bool(true, privilegeset_in_set(diff.removed, "bar"), MSG); | |
ae54b3a7 | 96 | |
9962f625 EK |
97 | is_bool(false, privilegeset_in_set(diff.unchanged, "qux"), MSG); |
98 | is_bool(true, privilegeset_in_set(diff.added, "qux"), MSG); | |
99 | is_bool(false, privilegeset_in_set(diff.removed, "qux"), MSG); | |
ae54b3a7 EK |
100 | |
101 | cleanup(); | |
102 | } | |
103 | ||
104 | static void test_privset_diff_rehash(void) | |
105 | { | |
106 | struct PrivilegeSet *set = privilegeset_set_new("test", "foo bar", 0); | |
9962f625 | 107 | struct privset_diff diff; |
ae54b3a7 EK |
108 | privilegeset_ref(set); |
109 | ||
110 | privilegeset_prepare_rehash(); | |
111 | ||
112 | /* should have changed from foo, bar to nothing, i.e. -foo -bar */ | |
9962f625 | 113 | diff = privilegeset_diff(set->shadow, set); |
ae54b3a7 | 114 | |
9962f625 EK |
115 | is_bool(false, privilegeset_in_set(diff.unchanged, "foo"), MSG); |
116 | is_bool(false, privilegeset_in_set(diff.added, "foo"), MSG); | |
117 | is_bool(true, privilegeset_in_set(diff.removed, "foo"), MSG); | |
ae54b3a7 | 118 | |
9962f625 EK |
119 | is_bool(false, privilegeset_in_set(diff.unchanged, "bar"), MSG); |
120 | is_bool(false, privilegeset_in_set(diff.added, "bar"), MSG); | |
121 | is_bool(true, privilegeset_in_set(diff.removed, "bar"), MSG); | |
ae54b3a7 EK |
122 | |
123 | privilegeset_set_new("test", "foo qux", 0); | |
9962f625 | 124 | diff = privilegeset_diff(set->shadow, set); |
ae54b3a7 EK |
125 | |
126 | /* should have changed from foo, bar to foo, qux, i.e. =foo -bar +qux */ | |
9962f625 EK |
127 | is_bool(true, privilegeset_in_set(diff.unchanged, "foo"), MSG); |
128 | is_bool(false, privilegeset_in_set(diff.added, "foo"), MSG); | |
129 | is_bool(false, privilegeset_in_set(diff.removed, "foo"), MSG); | |
ae54b3a7 | 130 | |
9962f625 EK |
131 | is_bool(false, privilegeset_in_set(diff.unchanged, "bar"), MSG); |
132 | is_bool(false, privilegeset_in_set(diff.added, "bar"), MSG); | |
133 | is_bool(true, privilegeset_in_set(diff.removed, "bar"), MSG); | |
ae54b3a7 | 134 | |
9962f625 EK |
135 | is_bool(false, privilegeset_in_set(diff.unchanged, "qux"), MSG); |
136 | is_bool(true, privilegeset_in_set(diff.added, "qux"), MSG); | |
137 | is_bool(false, privilegeset_in_set(diff.removed, "qux"), MSG); | |
ae54b3a7 EK |
138 | |
139 | privilegeset_cleanup_rehash(); | |
140 | ||
141 | privilegeset_unref(set); | |
142 | cleanup(); | |
143 | } | |
144 | ||
145 | int main(int argc, char *argv[]) | |
146 | { | |
147 | plan_lazy(); | |
148 | ||
149 | test_privset_membership(); | |
150 | test_privset_extend(); | |
151 | test_privset_persistence(); | |
152 | test_privset_diff(); | |
153 | test_privset_diff_rehash(); | |
154 | ||
155 | return 0; | |
156 | } |