forked from mikespook/gorbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
117 lines (104 loc) · 2.72 KB
/
helper_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
package gorbac
import (
"errors"
"testing"
)
var (
pAll = NewPermission("permission-all")
pNone = NewPermission("permission-none")
)
func TestPrepareCircle(t *testing.T) {
rbac = New[string]()
assert(t, rA.Assign(pA))
assert(t, rB.Assign(pB))
assert(t, rC.Assign(pC))
assert(t, rA.Assign(pAll))
assert(t, rB.Assign(pAll))
assert(t, rC.Assign(pAll))
assert(t, rbac.Add(rA))
assert(t, rbac.Add(rB))
assert(t, rbac.Add(rC))
assert(t, rbac.SetParent("role-a", "role-b"))
assert(t, rbac.SetParent("role-b", "role-c"))
assert(t, rbac.SetParent("role-c", "role-a"))
}
func TestInherCircle(t *testing.T) {
if err := InherCircle(rbac); err == nil {
t.Fatal("There should be a circle inheritance.")
} else {
t.Log(err)
}
}
func TestInherNormal(t *testing.T) {
assert(t, rbac.RemoveParent("role-c", "role-a"))
if err := InherCircle(rbac); err != nil {
t.Fatal(err)
}
}
func TestAllGranted(t *testing.T) {
// All roles have pAll
roles := []string{"role-a", "role-b", "role-c"}
if !AllGranted(rbac, roles, pAll, nil) {
t.Errorf("All roles(%v) were expected having %s, but they weren't.", roles, pAll)
}
if AllGranted(rbac, roles, pA, nil) {
t.Errorf("Not all roles(%v) were expected having %s, but they were.", roles, pA)
}
}
func TestAnyGranted(t *testing.T) {
// rA roles have pA
roles := []string{"role-a", "role-b", "role-c"}
if !AnyGranted(rbac, roles, pA, nil) {
t.Errorf("One of roles(%v) was expected having %s, but it wasn't.", roles, pA)
}
if AnyGranted(rbac, roles, pNone, nil) {
t.Errorf("None of roles(%v) were expected having %s, but it was.", roles, pNone)
}
}
func TestWalk(t *testing.T) {
if err := Walk(rbac, nil); err != nil {
t.Errorf("Unexpected error: %s", err)
}
h := func(r Role[string], parents []string) error {
t.Logf("Role: %v", r.ID)
permissions := make([]string, 0)
for _, p := range r.Permissions() {
permissions = append(permissions, p.ID())
}
t.Logf("Permission: %v", permissions)
t.Logf("Parents: %v", parents)
return nil
}
if err := Walk(rbac, h); err != nil {
t.Errorf("Unexpected error: %s", err)
}
he := func(r Role[string], parents []string) error {
return errors.New("Expected error")
}
if err := Walk(rbac, he); err == nil {
t.Errorf("Expected error, got nil")
}
}
func BenchmarkInherCircle(b *testing.B) {
rbac = New[string]()
rbac.Add(rA)
rbac.Add(rB)
rbac.Add(rC)
rbac.SetParent("role-a", "role-b")
rbac.SetParent("role-b", "role-c")
rbac.SetParent("role-c", "role-a")
for i := 0; i < b.N; i++ {
InherCircle(rbac)
}
}
func BenchmarkInherNormal(b *testing.B) {
rbac = New[string]()
rbac.Add(rA)
rbac.Add(rB)
rbac.Add(rC)
rbac.SetParent("role-a", "role-b")
rbac.SetParent("role-b", "role-c")
for i := 0; i < b.N; i++ {
InherCircle(rbac)
}
}