@@ -18,6 +18,7 @@ package cauthdsl
18
18
19
19
import (
20
20
"bytes"
21
+ "fmt"
21
22
"testing"
22
23
23
24
"github.com/golang/protobuf/proto"
@@ -27,71 +28,87 @@ import (
27
28
var invalidSignature = []byte ("badsigned" )
28
29
var validSignature = []byte ("signed" )
29
30
var signers = [][]byte {[]byte ("signer0" ), []byte ("signer1" )}
31
+ var msgs = [][]byte {nil , nil }
30
32
31
33
type mockCryptoHelper struct {
32
34
}
33
35
34
- func (mch * mockCryptoHelper ) VerifySignature (msg []byte , id []byte , signature []byte ) bool {
35
- return bytes .Equal (signature , validSignature )
36
+ func (mch * mockCryptoHelper ) VerifySignature (sd * cb.SignedData ) error {
37
+ if ! bytes .Equal (sd .Signature , validSignature ) {
38
+ return fmt .Errorf ("Bad signature" )
39
+ }
40
+ return nil
41
+ }
42
+
43
+ func toSignedData (data [][]byte , identities [][]byte , signatures [][]byte ) []* cb.SignedData {
44
+ signedData := make ([]* cb.SignedData , len (data ))
45
+ for i := range signedData {
46
+ signedData [i ] = & cb.SignedData {
47
+ Data : data [i ],
48
+ Identity : identities [i ],
49
+ Signature : signatures [i ],
50
+ }
51
+ }
52
+ return signedData
36
53
}
37
54
38
55
func TestSimpleSignature (t * testing.T ) {
39
56
mch := & mockCryptoHelper {}
40
57
policy := Envelope (SignedBy (0 ), signers )
41
58
42
- spe , err := NewSignaturePolicyEvaluator (policy , mch )
59
+ spe , err := compile (policy . Policy , policy . Identities , mch )
43
60
if err != nil {
44
61
t .Fatalf ("Could not create a new SignaturePolicyEvaluator using the given policy, crypto-helper: %s" , err )
45
62
}
46
63
47
- if ! spe . Authenticate ( nil , [][] byte { signers [0 ]}, [][] byte { validSignature }) {
48
- t .Error ("Expected authentication to succeed with valid signatures" )
64
+ if ! spe ([] * cb. SignedData { & cb. SignedData { Identity : signers [0 ], Signature : validSignature } }) {
65
+ t .Errorf ("Expected authentication to succeed with valid signatures" )
49
66
}
50
- if spe . Authenticate ( nil , [][] byte { signers [0 ]}, [][] byte { invalidSignature }) {
51
- t .Error ("Expected authentication to fail given the invalid signature" )
67
+ if spe ([] * cb. SignedData { & cb. SignedData { Identity : signers [0 ], Signature : invalidSignature } }) {
68
+ t .Errorf ("Expected authentication to fail given the invalid signature" )
52
69
}
53
- if spe . Authenticate ( nil , [][] byte { signers [1 ]}, [][] byte { validSignature }) {
54
- t .Error ("Expected authentication to fail because signers[1] is not authorized in the policy, despite his valid signature" )
70
+ if spe ([] * cb. SignedData { & cb. SignedData { Identity : signers [1 ], Signature : validSignature } }) {
71
+ t .Errorf ("Expected authentication to fail because signers[1] is not authorized in the policy, despite his valid signature" )
55
72
}
56
73
}
57
74
58
75
func TestMultipleSignature (t * testing.T ) {
59
76
mch := & mockCryptoHelper {}
60
77
policy := Envelope (And (SignedBy (0 ), SignedBy (1 )), signers )
61
78
62
- spe , err := NewSignaturePolicyEvaluator (policy , mch )
79
+ spe , err := compile (policy . Policy , policy . Identities , mch )
63
80
if err != nil {
64
81
t .Fatalf ("Could not create a new SignaturePolicyEvaluator using the given policy, crypto-helper: %s" , err )
65
82
}
66
83
67
- if ! spe . Authenticate ( nil , signers , [][]byte {validSignature , validSignature }) {
68
- t .Error ("Expected authentication to succeed with valid signatures" )
84
+ if ! spe ( toSignedData ( msgs , signers , [][]byte {validSignature , validSignature }) ) {
85
+ t .Errorf ("Expected authentication to succeed with valid signatures" )
69
86
}
70
- if spe . Authenticate ( nil , signers , [][]byte {validSignature , invalidSignature }) {
71
- t .Error ("Expected authentication to fail given one of two invalid signatures" )
87
+ if spe ( toSignedData ( msgs , signers , [][]byte {validSignature , invalidSignature }) ) {
88
+ t .Errorf ("Expected authentication to fail given one of two invalid signatures" )
72
89
}
73
- if spe . Authenticate ( nil , [][]byte {signers [0 ], signers [0 ]}, [][]byte {validSignature , validSignature }) {
74
- t .Error ("Expected authentication to fail because although there were two valid signatures, one was duplicated" )
90
+ if spe ( toSignedData ( msgs , [][]byte {signers [0 ], signers [0 ]}, [][]byte {validSignature , validSignature }) ) {
91
+ t .Errorf ("Expected authentication to fail because although there were two valid signatures, one was duplicated" )
75
92
}
76
93
}
77
94
78
95
func TestComplexNestedSignature (t * testing.T ) {
79
96
mch := & mockCryptoHelper {}
80
97
policy := Envelope (And (Or (And (SignedBy (0 ), SignedBy (1 )), And (SignedBy (0 ), SignedBy (0 ))), SignedBy (0 )), signers )
81
98
82
- spe , err := NewSignaturePolicyEvaluator (policy , mch )
99
+ spe , err := compile (policy . Policy , policy . Identities , mch )
83
100
if err != nil {
84
101
t .Fatalf ("Could not create a new SignaturePolicyEvaluator using the given policy, crypto-helper: %s" , err )
85
102
}
86
103
87
- if ! spe . Authenticate ( nil , signers , [][]byte {validSignature , validSignature }) {
88
- t .Error ("Expected authentication to succeed with valid signatures" )
104
+ if ! spe ( toSignedData ( msgs , signers , [][]byte {validSignature , validSignature }) ) {
105
+ t .Errorf ("Expected authentication to succeed with valid signatures" )
89
106
}
90
- if spe . Authenticate ( nil , signers , [][]byte {invalidSignature , validSignature }) {
91
- t .Error ("Expected authentication failure as only the signature of signer[1] was valid" )
107
+ if spe ( toSignedData ( msgs , signers , [][]byte {invalidSignature , validSignature }) ) {
108
+ t .Errorf ("Expected authentication failure as only the signature of signer[1] was valid" )
92
109
}
93
- if ! spe . Authenticate ( nil , [][]byte {signers [0 ], signers [0 ]}, [][]byte {validSignature , validSignature }) {
94
- t .Error ("Expected authentication to succeed because the rule allows duplicated signatures for signer[0]" )
110
+ if ! spe ( toSignedData ( msgs , [][]byte {signers [0 ], signers [0 ]}, [][]byte {validSignature , validSignature }) ) {
111
+ t .Errorf ("Expected authentication to succeed because the rule allows duplicated signatures for signer[0]" )
95
112
}
96
113
}
97
114
@@ -102,7 +119,7 @@ func TestNegatively(t *testing.T) {
102
119
b , _ := proto .Marshal (rpolicy )
103
120
policy := & cb.SignaturePolicyEnvelope {}
104
121
_ = proto .Unmarshal (b , policy )
105
- _ , err := NewSignaturePolicyEvaluator (policy , mch )
122
+ _ , err := compile (policy . Policy , policy . Identities , mch )
106
123
if err == nil {
107
124
t .Fatal ("Should have errored compiling because the Type field was nil" )
108
125
}
0 commit comments