forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockconfig.go
321 lines (257 loc) · 8.31 KB
/
mockconfig.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
313
314
315
316
317
318
319
320
321
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"crypto/tls"
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"crypto/x509"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/test/mockfab"
"github.com/pkg/errors"
)
// MockConfig ...
type MockConfig struct {
tlsEnabled bool
mutualTLSEnabled bool
errorCase bool
customNetworkPeerCfg []fab.NetworkPeer
customPeerCfg *fab.PeerConfig
customOrdererCfg *fab.OrdererConfig
customRandomOrdererCfg *fab.OrdererConfig
EvtServiceType fab.EventServiceType
}
// NewMockCryptoConfig ...
func NewMockCryptoConfig() core.CryptoSuiteConfig {
return &MockConfig{}
}
// NewMockEndpointConfig ...
func NewMockEndpointConfig() fab.EndpointConfig {
return &MockConfig{}
}
// NewMockIdentityConfig ...
func NewMockIdentityConfig() msp.IdentityConfig {
return &MockConfig{}
}
// NewMockCryptoConfigCustomized ...
func NewMockCryptoConfigCustomized(tlsEnabled, mutualTLSEnabled, errorCase bool) core.CryptoSuiteConfig {
return &MockConfig{tlsEnabled: tlsEnabled, mutualTLSEnabled: mutualTLSEnabled, errorCase: errorCase}
}
// NewMockEndpointConfigCustomized ...
func NewMockEndpointConfigCustomized(tlsEnabled, mutualTLSEnabled, errorCase bool) fab.EndpointConfig {
return &MockConfig{tlsEnabled: tlsEnabled, mutualTLSEnabled: mutualTLSEnabled, errorCase: errorCase}
}
// NewMockIdentityConfigCustomized ...
func NewMockIdentityConfigCustomized(tlsEnabled, mutualTLSEnabled, errorCase bool) msp.IdentityConfig {
return &MockConfig{tlsEnabled: tlsEnabled, mutualTLSEnabled: mutualTLSEnabled, errorCase: errorCase}
}
// Client ...
func (c *MockConfig) Client() *msp.ClientConfig {
clientConfig := msp.ClientConfig{}
clientConfig.CredentialStore = msp.CredentialStoreType{
Path: "/tmp/fabsdkgo_test/store",
}
if c.mutualTLSEnabled {
key := endpoint.TLSConfig{Path: "../../../test/fixtures/config/mutual_tls/client_sdk_go-key.pem"}
cert := endpoint.TLSConfig{Path: "../../../test/fixtures/config/mutual_tls/client_sdk_go.pem"}
err := key.LoadBytes()
if err != nil {
panic(err)
}
err = cert.LoadBytes()
if err != nil {
panic(err)
}
clientConfig.TLSKey = key.Bytes()
clientConfig.TLSCert = cert.Bytes()
}
return &clientConfig
}
// CAConfig not implemented
func (c *MockConfig) CAConfig(org string) (*msp.CAConfig, bool) {
caConfig := msp.CAConfig{
CAName: "org1",
}
return &caConfig, true
}
//CAServerCerts Read configuration option for the server certificates for given org
func (c *MockConfig) CAServerCerts(org string) ([][]byte, bool) {
return nil, false
}
//CAClientKey Read configuration option for the fabric CA client key for given org
func (c *MockConfig) CAClientKey(org string) ([]byte, bool) {
return nil, false
}
//CAClientCert Read configuration option for the fabric CA client cert for given org
func (c *MockConfig) CAClientCert(org string) ([]byte, bool) {
return nil, false
}
//Timeout not implemented
func (c *MockConfig) Timeout(arg fab.TimeoutType) time.Duration {
return time.Second * 10
}
// PeersConfig Retrieves the fabric peers from the config file provided
func (c *MockConfig) PeersConfig(org string) ([]fab.PeerConfig, bool) {
return nil, false
}
// PeerConfig Retrieves a specific peer from the configuration by org and name
func (c *MockConfig) PeerConfig(nameOrURL string) (*fab.PeerConfig, bool) {
if nameOrURL == "invalid" {
return nil, false
}
if c.customPeerCfg != nil {
return c.customPeerCfg, true
}
cfg := fab.PeerConfig{
URL: "example.com",
}
return &cfg, true
}
// TLSCACertPool ...
func (c *MockConfig) TLSCACertPool() fab.CertPool {
if c.errorCase {
return &mockfab.MockCertPool{Err: errors.New("just to test error scenario")}
}
return &mockfab.MockCertPool{CertPool: x509.NewCertPool()}
}
// TcertBatchSize ...
func (c *MockConfig) TcertBatchSize() int {
return 0
}
// SecurityAlgorithm ...
func (c *MockConfig) SecurityAlgorithm() string {
return "SHA2"
}
// SecurityLevel ...
func (c *MockConfig) SecurityLevel() int {
return 256
}
//SecurityProviderLibPath will be set only if provider is PKCS11
func (c *MockConfig) SecurityProviderLibPath() string {
return ""
}
// OrderersConfig returns a list of defined orderers
func (c *MockConfig) OrderersConfig() []fab.OrdererConfig {
oConfig, _ := c.OrdererConfig("")
return []fab.OrdererConfig{*oConfig}
}
//SetCustomNetworkPeerCfg sets custom orderer config for unit-tests
func (c *MockConfig) SetCustomNetworkPeerCfg(customNetworkPeerCfg []fab.NetworkPeer) {
c.customNetworkPeerCfg = customNetworkPeerCfg
}
//SetCustomPeerCfg sets custom orderer config for unit-tests
func (c *MockConfig) SetCustomPeerCfg(customPeerCfg *fab.PeerConfig) {
c.customPeerCfg = customPeerCfg
}
//SetCustomOrdererCfg sets custom orderer config for unit-tests
func (c *MockConfig) SetCustomOrdererCfg(customOrdererCfg *fab.OrdererConfig) {
c.customOrdererCfg = customOrdererCfg
}
//SetCustomRandomOrdererCfg sets custom random orderer config for unit-tests
func (c *MockConfig) SetCustomRandomOrdererCfg(customRandomOrdererCfg *fab.OrdererConfig) {
c.customRandomOrdererCfg = customRandomOrdererCfg
}
// OrdererConfig not implemented
func (c *MockConfig) OrdererConfig(name string) (*fab.OrdererConfig, bool) {
if name == "Invalid" {
return nil, false
}
if c.customOrdererCfg != nil {
return c.customOrdererCfg, true
}
oConfig := fab.OrdererConfig{
URL: "example.com",
}
return &oConfig, true
}
// KeyStorePath ...
func (c *MockConfig) KeyStorePath() string {
return "/tmp/fabsdkgo_test"
}
// CredentialStorePath ...
func (c *MockConfig) CredentialStorePath() string {
return "/tmp/userstore"
}
// CAKeyStorePath not implemented
func (c *MockConfig) CAKeyStorePath() string {
return "/tmp/fabsdkgo_test"
}
// CryptoConfigPath ...
func (c *MockConfig) CryptoConfigPath() string {
return ""
}
// NetworkConfig not implemented
func (c *MockConfig) NetworkConfig() *fab.NetworkConfig {
return nil
}
// ChannelConfig returns the channel configuration
func (c *MockConfig) ChannelConfig(name string) (*fab.ChannelEndpointConfig, bool) {
return &fab.ChannelEndpointConfig{Policies: fab.ChannelPolicies{}}, true
}
// ChannelPeers returns the channel peers configuration
func (c *MockConfig) ChannelPeers(name string) ([]fab.ChannelPeer, bool) {
if name == "noChannelPeers" {
return nil, false
}
peerChCfg := fab.PeerChannelConfig{EndorsingPeer: true, ChaincodeQuery: true, LedgerQuery: true, EventSource: true}
if name == "noEndpoints" {
peerChCfg = fab.PeerChannelConfig{EndorsingPeer: false, ChaincodeQuery: false, LedgerQuery: false, EventSource: false}
}
mockPeer := fab.ChannelPeer{PeerChannelConfig: peerChCfg, NetworkPeer: fab.NetworkPeer{PeerConfig: fab.PeerConfig{URL: "example.com"}}}
return []fab.ChannelPeer{mockPeer}, true
}
// ChannelOrderers returns a list of channel orderers
func (c *MockConfig) ChannelOrderers(name string) ([]fab.OrdererConfig, bool) {
if name == "Invalid" {
return nil, false
}
oConfig, _ := c.OrdererConfig("")
return []fab.OrdererConfig{*oConfig}, true
}
// NetworkPeers returns the mock network peers configuration
func (c *MockConfig) NetworkPeers() []fab.NetworkPeer {
return c.customNetworkPeerCfg
}
// SecurityProvider ...
func (c *MockConfig) SecurityProvider() string {
return "sw"
}
// SecurityProviderLabel ...
func (c *MockConfig) SecurityProviderLabel() string {
return ""
}
//SecurityProviderPin ...
func (c *MockConfig) SecurityProviderPin() string {
return ""
}
//SoftVerify flag
func (c *MockConfig) SoftVerify() bool {
return false
}
// IsSecurityEnabled ...
func (c *MockConfig) IsSecurityEnabled() bool {
return false
}
// TLSClientCerts ...
func (c *MockConfig) TLSClientCerts() []tls.Certificate {
return nil
}
// EventServiceType returns the type of event service client to use
func (c *MockConfig) EventServiceType() fab.EventServiceType {
return c.EvtServiceType
}
// Lookup gets the Value from config file by Key
func (c *MockConfig) Lookup(key string) (interface{}, bool) {
if key == "invalid" {
return nil, false
}
value, ok := c.Lookup(key)
if !ok {
return nil, false
}
return value, true
}