@@ -17,53 +17,92 @@ import (
17
17
"github.com/stretchr/testify/assert"
18
18
)
19
19
20
- func TestSatisfiedBy (t * testing.T ) {
21
- p1 , err := cauthdsl .FromString ("OR(AND('A.member', 'B.member'), 'C.member', AND('A.member', 'D.member'))" )
22
- assert .NoError (t , err )
20
+ type testCase struct {
21
+ name string
22
+ policy string
23
+ expected map [string ]struct {}
24
+ principals []* msp.MSPPrincipal
25
+ }
23
26
27
+ func createPrincipals (orgNames ... string ) []* msp.MSPPrincipal {
24
28
principals := make ([]* msp.MSPPrincipal , 0 )
25
-
26
- mspId := func (principal * msp.MSPPrincipal ) string {
27
- role := & msp.MSPRole {}
28
- proto .Unmarshal (principal .Principal , role )
29
- return role .MspIdentifier
30
- }
31
-
32
29
appendPrincipal := func (orgName string ) {
33
30
principals = append (principals , & msp.MSPPrincipal {
34
31
PrincipalClassification : msp .MSPPrincipal_ROLE ,
35
32
Principal : utils .MarshalOrPanic (& msp.MSPRole {Role : msp .MSPRole_MEMBER , MspIdentifier : orgName })})
36
33
}
34
+ for _ , org := range orgNames {
35
+ appendPrincipal (org )
36
+ }
37
+ return principals
38
+ }
37
39
38
- appendPrincipal ("A" )
39
- appendPrincipal ("B" )
40
- appendPrincipal ("C" )
41
- appendPrincipal ("A" )
42
- appendPrincipal ("D" )
40
+ var cases = []testCase {
41
+ {
42
+ name : "orOfAnds" ,
43
+ policy : "OR(AND('A.member', 'B.member'), 'C.member', AND('A.member', 'D.member'))" ,
44
+ expected : map [string ]struct {}{
45
+ fmt .Sprintf ("%v" , []string {"A" , "B" }): {},
46
+ fmt .Sprintf ("%v" , []string {"C" }): {},
47
+ fmt .Sprintf ("%v" , []string {"A" , "D" }): {},
48
+ },
49
+ principals : createPrincipals ("A" , "B" , "C" , "D" , "A" ),
50
+ },
51
+ {
52
+ name : "andOfOrs" ,
53
+ policy : "AND('A.member', 'C.member', OR('B.member', 'D.member'))" ,
54
+ expected : map [string ]struct {}{
55
+ fmt .Sprintf ("%v" , []string {"A" , "C" , "B" }): {},
56
+ fmt .Sprintf ("%v" , []string {"A" , "C" , "D" }): {},
57
+ },
58
+ principals : createPrincipals ("A" , "C" , "B" , "D" ),
59
+ },
60
+ {
61
+ name : "orOfOrs" ,
62
+ policy : "OR('A.member', OR('B.member', 'C.member'))" ,
63
+ expected : map [string ]struct {}{
64
+ fmt .Sprintf ("%v" , []string {"A" }): {},
65
+ fmt .Sprintf ("%v" , []string {"B" }): {},
66
+ fmt .Sprintf ("%v" , []string {"C" }): {},
67
+ },
68
+ principals : createPrincipals ("A" , "B" , "C" ),
69
+ },
70
+ {
71
+ name : "andOfAnds" ,
72
+ policy : "AND('A.member', AND('B.member', 'C.member'), AND('D.member','A.member'))" ,
73
+ expected : map [string ]struct {}{
74
+ fmt .Sprintf ("%v" , []string {"A" , "B" , "C" , "D" , "A" }): {},
75
+ },
76
+ principals : createPrincipals ("A" , "B" , "C" , "D" ),
77
+ },
78
+ }
43
79
44
- ip := NewInquireableSignaturePolicy (p1 )
45
- satisfiedBy := ip .SatisfiedBy ()
80
+ func TestSatisfiedBy (t * testing.T ) {
46
81
47
- expected := map [ string ] struct {} {
48
- fmt . Sprintf ( "%v" , [] string { "A" , "B" }): {},
49
- fmt . Sprintf ( "%v" , [] string { "C" }): {},
50
- fmt . Sprintf ( "%v" , [] string { "A" , "D" }): {},
82
+ mspId := func ( principal * msp. MSPPrincipal ) string {
83
+ role := & msp. MSPRole {}
84
+ proto . Unmarshal ( principal . Principal , role )
85
+ return role . MspIdentifier
51
86
}
52
87
53
- actual := make (map [string ]struct {})
54
- for _ , ps := range satisfiedBy {
55
- var principals []string
56
- for _ , principal := range ps {
57
- principals = append (principals , mspId (principal ))
58
- }
59
- actual [fmt .Sprintf ("%v" , principals )] = struct {}{}
60
- }
88
+ for _ , test := range cases {
89
+ t .Run (test .name , func (t * testing.T ) {
90
+ p , err := cauthdsl .FromString (test .policy )
91
+ assert .NoError (t , err )
61
92
62
- assert .Equal (t , expected , actual )
93
+ ip := NewInquireableSignaturePolicy (p )
94
+ satisfiedBy := ip .SatisfiedBy ()
63
95
64
- // Bad path: Remove an identity and re-try
65
- p1 .Identities = p1 .Identities [1 :]
66
- ip = NewInquireableSignaturePolicy (p1 )
67
- satisfiedBy = ip .SatisfiedBy ()
68
- assert .Nil (t , satisfiedBy )
96
+ actual := make (map [string ]struct {})
97
+ for _ , ps := range satisfiedBy {
98
+ var principals []string
99
+ for _ , principal := range ps {
100
+ principals = append (principals , mspId (principal ))
101
+ }
102
+ actual [fmt .Sprintf ("%v" , principals )] = struct {}{}
103
+ }
104
+
105
+ assert .Equal (t , test .expected , actual )
106
+ })
107
+ }
69
108
}
0 commit comments