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
/
levels_test.go
312 lines (269 loc) · 8.07 KB
/
levels_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
303
304
305
306
307
308
309
310
311
312
package aci
import (
"fmt"
"testing"
)
func ExampleLevel_String() {
fmt.Printf("%s", Level8)
// Output: 8
}
func ExampleInheritance_BRM() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
brm := inh.BRM()
fmt.Printf("%d available comparison operator methods", brm.Len())
// Output: 2 available comparison operator methods
}
func ExampleInheritance_String() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("%s", inh)
// Output: parent[1,3].manager#uid=frank,ou=People,dc=example,dc=com
}
func ExampleInheritance_Valid() {
var inh Inheritance
fmt.Printf("%T.Valid: %t", inh, inh.Valid() == nil)
// Output: aci.Inheritance.Valid: false
}
func ExampleInheritance_Eq() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("%s", inh.Eq())
// Output: userattr = "parent[1,3].manager#uid=frank,ou=People,dc=example,dc=com"
}
func ExampleInheritance_Ne() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("%s", inh.Ne())
// Output: userattr != "parent[1,3].manager#uid=frank,ou=People,dc=example,dc=com"
}
func ExampleInheritance_IsZero() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("%t", inh.IsZero())
// Output: false
}
func ExampleInheritance_Len() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("Number of levels: %d", inh.Len())
// Output: Number of levels: 2
}
func ExampleInheritance_Positive() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("Level 5 positive? %t", inh.Positive(5))
// Output: Level 5 positive? false
}
func ExampleInheritance_Shift() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
inh.Shift(7) // add one more we forgot
fmt.Printf("Number of levels: %d", inh.Len())
// Output: Number of levels: 3
}
func ExampleInheritance_Unshift() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3, 8)
inh.Unshift(1) // we changed our mind
inh.Unshift(`1`) // alternatively
inh.Unshift(Level1) // alternatively
fmt.Printf("Number of levels: %d", inh.Len())
// Output: Number of levels: 2
}
func ExampleInheritance_Keyword() {
var inh Inheritance
//fmt.Printf("Keyword found: %t", inh.Keyword().String() != ``)
// Would return: Keyword found: false
attr := AT(`manager`)
// we'll use the userattr keyword (bestowed
// by UAT), and for a value we'll just give
// it an explicit bind type (USERDN). If it
// is preferable to use groupattr keyword,
// simply supplant UAT with GAT func.
uat := UAT(attr, USERDN)
inh = Inherit(uat, 6, 7) // levels 6 & 7
fmt.Printf("Keyword: %s", inh.Keyword())
// Output: Keyword: userattr
}
func ExampleInheritance_Positive_byString() {
attr := AT(`manager`)
// we'll use the userattr keyword (bestowed
// by UAT), and for a value we'll just give
// it an explicit bind type (USERDN). If it
// is preferable to use groupattr keyword,
// simply supplant UAT with GAT func.
uat := UAT(attr, USERDN)
inh := Inherit(uat, 6, 7) // levels 6 & 7
fmt.Printf("Level 6 positive? %t", inh.Positive(`6`))
// Output: Level 6 positive? true
}
func TestInheritance(t *testing.T) {
inh := Inherit(UAT(AT(`manager`), USERDN), Level0, Level1, Level2, Level8)
want := `userattr = "parent[0,1,2,8].manager#USERDN"`
got := inh.Eq()
if want != got.String() {
t.Errorf("%s failed: want '%s', got '%s'", t.Name(), want, got)
return
}
}
func ExampleInherit_uSERDN() {
attr := AT(`manager`)
uat := UAT(attr, AV(`USERDN`))
inh := Inherit(uat, 0, 1, 2, 8)
fmt.Printf("%s", inh.Eq())
// Output: userattr = "parent[0,1,2,8].manager#USERDN"
}
func ExampleInherit_uAT() {
attr := AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh := Inherit(uat, 1, 3)
fmt.Printf("%s", inh.Eq())
// Output: userattr = "parent[1,3].manager#uid=frank,ou=People,dc=example,dc=com"
}
func ExampleInherit_gAT() {
attr := AT(`owner`)
gat := GAT(attr, USERDN)
inh := Inherit(gat, 3, 4)
fmt.Printf("%s", inh.Eq())
// Output: groupattr = "parent[3,4].owner#USERDN"
}
/*
This example demonstrates the SHA-1 hash comparison between two (2)
Inheritance instances using the Compare method.
*/
func ExampleInheritance_Compare() {
attr := AT(`owner`)
gat := GAT(attr, USERDN)
inh1 := Inherit(gat, 3, 4)
attr = AT(`manager`)
uat := UAT(attr, AV(`uid=frank,ou=People,dc=example,dc=com`))
inh2 := Inherit(uat, 1, 3)
fmt.Printf("Hashes are equal: %t", inh1.Compare(inh2))
// Output: Hashes are equal: false
}
/*
This example demonstrates the SHA-1 hash comparison between two (2)
Level instances using the Compare method.
*/
func ExampleLevel_Compare() {
fmt.Printf("Hashes are equal: %t", Level8.Compare(Level7))
// Output: Hashes are equal: false
}
func TestLevels_bogus(t *testing.T) {
var inh Inheritance
if err := inh.Valid(); err == nil {
t.Errorf("%s failed: invalid %T returned no validity error",
t.Name(), inh)
return
}
if inh.String() != badInheritance {
t.Errorf("%s failed: invalid %T returned no bogus inheritance warning",
t.Name(), inh)
return
}
if inh.Eq() != badBindRule {
t.Errorf("%s failed: invalid %T returned unexpected %T instance during equality bindrule creation",
t.Name(), inh, badBindRule)
return
}
if inh.Ne() != badBindRule {
t.Errorf("%s failed: invalid %T returned unexpected %T instance during negated equality bindrule creation",
t.Name(), inh, badBindRule)
return
}
if !inh.IsZero() {
t.Errorf("%s failed: bogus %T is non-zero",
t.Name(), inh)
return
}
for idx, rawng := range []string{
`100.manager#USERDN`,
`parent[100].manager#USERDN`,
`parent[].manager#SELFDN`,
`parent[4]#ROLEDN`,
`parent[-1,20,3,476,5,666,7,666,9]?manager#LDAPURI`,
`parent[0]].owner#GROUPDN`,
`Parent[1,3,5,7)owner]#LDAPURI`,
`parent[1,3,5,7)owner#LDAPURI`,
`parent[1,2,3,4].squatcobbler`,
``,
} {
i, err := parseInheritance(rawng)
if err == nil {
t.Errorf("%s failed [idx:%d]: parsing of bogus %T definition returned no error (%s)",
t.Name(), idx, i, rawng)
return
}
if i.String() != badInheritance {
t.Errorf("%s failed [idx:%d]: %T parsing attempt failed; want '%s', got '%s'",
t.Name(), idx, i, badInheritance, i)
return
}
}
}
func TestInheritance_parse(t *testing.T) {
for _, raw := range []string{
`parent[0,5,9].manager#USERDN`,
`parent[1].manager#SELFDN`,
`parent[4].terminated#ROLEDN`,
`parent[0,1,2,3,4,5,6,7,8,9].manager#LDAPURI`,
`parent[0].owner#GROUPDN`,
} {
i, err := parseInheritance(raw)
if err != nil {
t.Errorf("%s failed: %T parsing attempt failed; %v",
t.Name(), i, err)
return
}
if raw != i.String() {
t.Errorf("%s failed: %T parsing attempt failed; want '%s', got '%s'",
t.Name(), i, raw, i)
return
}
want := sprintf("( userattr = %q )", raw)
equality := i.Eq().Paren()
if got := equality.String(); want != got {
t.Errorf("%s failed: %T equality creation error; want '%s', got '%s'",
t.Name(), i, want, got)
return
}
negation := i.Ne().Paren()
want = sprintf("( userattr != %q )", raw)
if got := negation.String(); want != got {
t.Errorf("%s failed: %T negated equality creation error; want '%s', got '%s'",
t.Name(), i, want, got)
return
}
}
}
func TestInheritance_codecov(t *testing.T) {
var inh Inheritance
_ = inh.Positive(`4`)
_ = inh.Keyword()
_ = inh.String()
_ = inh.Shift(1370)
_ = inh.Shift(`farts`)
_ = inh.Shift(-100)
_ = inh.Shift(3.14159)
_ = inh.Unshift(1370)
_ = inh.Unshift(`farts`)
_ = inh.Unshift(-100)
_ = inh.Unshift(3.14159)
_ = inh.Positive(`fart`)
_ = inh.Positive(100000)
_ = inh.Positive(-1)
_ = inh.Positive(4)
_ = inh.Positive("something awful")
_ = inh.Positive(Level(^uint16(0)))
_ = inh.Positive(3.14159)
}