forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsp_test.go
161 lines (135 loc) · 3.16 KB
/
msp_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
package msp
import (
"os"
"reflect"
"testing"
)
var localMsp MSP
func TestNoopMSP(t *testing.T) {
noopmsp := NewNoopMsp()
id, err := noopmsp.GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetSigningIdentity should have succeeded")
return
}
serializedID, err := id.Serialize()
if err != nil {
t.Fatalf("Serialize should have succeeded")
return
}
idBack, err := noopmsp.DeserializeIdentity(serializedID)
if err != nil {
t.Fatalf("DeserializeIdentity should have succeeded")
return
}
msg := []byte("foo")
sig, err := id.Sign(msg)
if err != nil {
t.Fatalf("Sign should have succeeded")
return
}
err = id.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}
err = idBack.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}
}
func TestMSPSetupBad(t *testing.T) {
_, err := GetLocalMspConfig("barf")
if err == nil {
t.Fatalf("Setup should have failed on an invalid config file")
return
}
}
func TestMSPSetupGood(t *testing.T) {
conf, err := GetLocalMspConfig("./sampleconfig/")
if err != nil {
t.Fatalf("Setup should have succeeded, got err %s instead", err)
return
}
localMsp, err = NewBccspMsp()
if err != nil {
t.Fatalf("Constructor for msp should have succeeded, got err %s instead", err)
return
}
err = localMsp.Setup(conf)
if err != nil {
t.Fatalf("Setup for msp should have succeeded, got err %s instead", err)
return
}
}
func TestGetIdentities(t *testing.T) {
_, err := localMsp.GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetDefaultSigningIdentity failed with err %s", err)
return
}
}
func TestSerializeIdentities(t *testing.T) {
id, err := localMsp.GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetSigningIdentity should have succeeded, got err %s", err)
return
}
serializedID, err := id.Serialize()
if err != nil {
t.Fatalf("Serialize should have succeeded, got err %s", err)
return
}
idBack, err := localMsp.DeserializeIdentity(serializedID)
if err != nil {
t.Fatalf("DeserializeIdentity should have succeeded, got err %s", err)
return
}
err = localMsp.Validate(idBack)
if err != nil {
t.Fatalf("The identity should be valid, got err %s", err)
return
}
if !reflect.DeepEqual(id.GetPublicVersion(), idBack) {
t.Fatalf("Identities should be equal (%s) (%s)", id, idBack)
return
}
}
func TestSignAndVerify(t *testing.T) {
id, err := localMsp.GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetSigningIdentity should have succeeded")
return
}
serializedID, err := id.Serialize()
if err != nil {
t.Fatalf("Serialize should have succeeded")
return
}
idBack, err := localMsp.DeserializeIdentity(serializedID)
if err != nil {
t.Fatalf("DeserializeIdentity should have succeeded")
return
}
msg := []byte("foo")
sig, err := id.Sign(msg)
if err != nil {
t.Fatalf("Sign should have succeeded")
return
}
err = id.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}
err = idBack.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}
}
func TestMain(m *testing.M) {
retVal := m.Run()
os.Exit(retVal)
}