This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrights_test.go
302 lines (264 loc) · 7.17 KB
/
rights_test.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package aci
import (
"fmt"
"testing"
)
func TestAllow(t *testing.T) {
G := Allow(ReadAccess, CompareAccess)
t.Logf("WHAT THE FUCK %d\n", G.permission.rights.cast().Int())
want := `allow(read,compare)`
got := G.String()
if want != got {
t.Errorf("%s failed: want '%s', got '%s'", t.Name(), want, got)
return
}
}
func TestAllow_all(t *testing.T) {
G := Allow(ReadAccess,
CompareAccess,
SearchAccess,
ImportAccess,
ExportAccess,
SelfWriteAccess,
DeleteAccess,
AddAccess,
WriteAccess)
want := `allow(all)`
got := G.String()
if want != got {
t.Errorf("%s failed: want '%s', got '%s'", t.Name(), want, got)
return
}
}
/*
This example demonstrates the granting (allowance) of read, compare and search access.
*/
func ExampleAllow() {
// allow read, compare and search privileges ...
G := Allow(ReadAccess, CompareAccess, SearchAccess)
// order is always fixed for string representation
// regardless of order-of-input ...
fmt.Printf("%s", G)
// Output: allow(read,search,compare)
}
func TestDeny(t *testing.T) {
G := Deny(AllAccess)
want := `deny(all)`
got := G.String()
if want != got {
t.Errorf("%s failed: want '%s', got '%s'", t.Name(), want, got)
return
}
}
func ExampleRight_String() {
// iterate all of the known Right definitions
// defined as constants in this package.
for idx, privilege := range []Right{
NoAccess,
ReadAccess,
WriteAccess,
AddAccess,
DeleteAccess,
SearchAccess,
CompareAccess,
SelfWriteAccess,
ProxyAccess,
ImportAccess,
ExportAccess,
AllAccess, // does NOT include proxy access !
} {
fmt.Printf("Privilege %02d/%d: %s (bit:%d)\n", idx+1, 12, privilege, int(privilege))
}
// Output:
// Privilege 01/12: none (bit:0)
// Privilege 02/12: read (bit:1)
// Privilege 03/12: write (bit:2)
// Privilege 04/12: add (bit:4)
// Privilege 05/12: delete (bit:8)
// Privilege 06/12: search (bit:16)
// Privilege 07/12: compare (bit:32)
// Privilege 08/12: selfwrite (bit:64)
// Privilege 09/12: proxy (bit:128)
// Privilege 10/12: import (bit:256)
// Privilege 11/12: export (bit:512)
// Privilege 12/12: all (bit:895)
}
/*
This example demonstrates the withholding (denial) of all privileges except proxy.
*/
func ExampleDeny() {
// deny everything (this does not include proxy privilege)
D := Deny(AllAccess)
fmt.Printf("%s", D)
// Output: deny(all)
}
func ExamplePermission_IsZero() {
var priv Permission
fmt.Printf("Privileges are undefined: %t", priv.IsZero())
// Output: Privileges are undefined: true
}
func ExamplePermission_Valid() {
var priv Permission
fmt.Printf("%T is ready for use: %t", priv, priv.Valid() == nil)
// Output: aci.Permission is ready for use: false
}
func ExamplePermission_Disposition() {
priv := Allow(`read`, `write`, `compare`, `selfwrite`)
fmt.Printf("%s", priv.Disposition())
// Output: allow
}
func ExamplePermission_String() {
priv := Allow(`read`, `write`, `compare`, `selfwrite`)
fmt.Printf("%s", priv)
// Output: allow(read,write,compare,selfwrite)
}
func ExamplePermission_Shift() {
var priv Permission = Allow() // you MUST initialize Permission explicitly using Allow or Deny funcs
priv.Shift(ReadAccess, ProxyAccess)
fmt.Printf("Allows proxy: %t", priv.Positive(`proxy`))
// Output: Allows proxy: true
}
func ExamplePermission_Len() {
var priv Permission = Deny() // you MUST initialize Permission explicitly using Allow or Deny funcs
priv.Shift(ReadAccess, WriteAccess, CompareAccess, SearchAccess, ProxyAccess)
fmt.Printf("Number of privileges denied: %d", priv.Len())
// Output: Number of privileges denied: 5
}
func ExamplePermission_Positive() {
var priv Permission = Deny(`read`, `write`, `proxy`, `search`) // you MUST initialize Permission explicitly using Allow or Deny funcs
fmt.Printf("Forbids read access: %t", priv.Positive(`read`))
// Output: Forbids read access: true
}
func ExamplePermission_Unshift() {
var priv Permission = Deny() // you MUST initialize Permission explicitly using Allow or Deny funcs
priv.Shift(ReadAccess, WriteAccess, CompareAccess, SearchAccess, ProxyAccess)
priv.Unshift(CompareAccess) // remove the negated compare privilege
fmt.Printf("Forbids compare: %t", priv.Positive(`compare`))
// Output: Forbids compare: false
}
func TestRights_bogus(t *testing.T) {
var p Permission
if err := p.Valid(); err == nil {
t.Errorf("%s failed: invalid %T returned no validity error",
t.Name(), p)
return
}
if p.String() != badPerm {
t.Errorf("%s failed: invalid %T returned no bogus string warning",
t.Name(), p)
return
}
p.Unshift(`all`)
p.Shift(-1985) //underflow
p.Shift(45378297659) //overflow
if !p.IsZero() {
t.Errorf("%s failed: overflow or underflow shift value accepted for %T",
t.Name(), p)
return
}
p.Unshift(-5) //underflow
p.Unshift(134559) //overflow
if !p.IsZero() {
t.Errorf("%s failed: overflow or underflow unshift value accepted for %T",
t.Name(), p)
return
}
}
func TestRights_lrShift(t *testing.T) {
var p Permission = Allow(NoAccess)
if !p.Positive(0) || !p.Positive(`none`) || !p.positive(NoAccess) {
t.Errorf("%s failed: cannot identify 'none' permission", t.Name())
return
}
// three iterations, one per supported
// Right type
for i := 0; i < 3; i++ {
// iterate each of the rights in the
// rights/names map
for k, v := range rightsMap {
if k == 0 {
continue
}
term, typ := testGetRightsTermType(i, k, v)
shifters := map[int]func(...any) Permission{
0: p.Shift,
1: p.Unshift,
}
for j := 0; j < len(shifters); j++ {
mode, phase := testGetRightsPhase(j)
if shifters[j](term); p.Positive(term) != phase {
t.Errorf("%s failed: %T %s %s failed [key:%d; term:%v] (value:%v)",
t.Name(), p, typ, mode, k, term, p)
return
}
}
}
}
}
func testGetRightsPhase(j int) (mode string, phase bool) {
mode = `shift`
if phase = (0 == j); !phase {
mode = `un` + mode
}
return
}
func testGetRightsTermType(i int, k Right, v string) (term any, typ string) {
term = k // default
switch i {
case 1:
term = v // string name (e.g.: read)
case 2:
term = Right(k) // Right
}
typ = sprintf("%T", term) // label for err
return
}
func TestPermission_codecov(t *testing.T) {
if ReadAccess.Compare(WriteAccess) {
t.Errorf("%s failed: %s wrongly equal to %s",
t.Name(), ReadAccess, WriteAccess)
return
}
var p Permission
_ = p.Len()
_ = p.Valid()
_ = p.Disposition()
_ = p.Shift()
_ = p.Shift(nil)
_ = p.Positive(nil)
_ = p.Shift(4)
_ = p.Shift(4547887935)
_ = p.Shift(-45478879)
_ = p.Unshift()
_ = p.Unshift(nil)
_ = p.Unshift(4)
_ = p.Unshift(4547887935)
_ = p.Unshift(-45478879)
_ = p.Parse(`alow(red,rite)`)
_ = p.Disposition()
_ = p.Positive(ProxyAccess)
p = Allow(ReadAccess, WriteAccess)
d := Deny(ReadAccess, WriteAccess)
_ = p.Unshift()
_ = p.Unshift(nil)
_ = p.Unshift(4)
_ = p.Shift(4547887935)
_ = p.Positive(4547887935)
_ = p.Shift(-45478879)
_ = p.Unshift(4547887935)
_ = p.Unshift(-45478879)
_ = d.Unshift()
_ = d.Unshift(nil)
_ = d.Unshift(4)
_ = d.Shift(4547887935)
_ = d.Positive(4547887935)
_ = d.Shift(-45478879)
_ = d.Unshift(4547887935)
if p.Compare(d) {
t.Errorf("%s failed: %s wrongly equal to %s",
t.Name(), p, d)
return
}
p.permission = new(permission)
_ = p.Valid()
}