forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockconfig.go
83 lines (59 loc) · 2.5 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
/*
Copyright SecureKey Technologies Inc., Unchain B.V. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mockfab
import (
tls "crypto/tls"
x509 "crypto/x509"
"time"
"github.com/golang/mock/gomock"
"github.com/x-chain/fabric-sdk-go/pkg/common/providers/fab"
"github.com/pkg/errors"
)
// GoodCert is a mock of a good certificate
var GoodCert = &x509.Certificate{Raw: []byte{0, 1, 2}}
// BadCert is a mock of a bad certificate
var BadCert = &x509.Certificate{Raw: []byte{1, 2}}
// TLSCert is a mock of a tls.Certificate{}
var TLSCert = tls.Certificate{Certificate: [][]byte{{3}, {4}}}
// CertPool is a mock of a *x509.CertPool
var CertPool = x509.NewCertPool()
// ErrorMessage is a mock error message
const ErrorMessage = "default error message"
// DefaultMockConfig returns a default mock config for testing
func DefaultMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
config := NewMockEndpointConfig(mockCtrl)
config.EXPECT().TLSCACertPool().Return(&MockCertPool{CertPool: CertPool}).AnyTimes()
config.EXPECT().Timeout(fab.PeerConnection).Return(time.Second * 5).AnyTimes()
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{TLSCert}).AnyTimes()
return config
}
// CustomMockConfig returns a custom mock config with custom certpool for testing
func CustomMockConfig(mockCtrl *gomock.Controller, certPool *x509.CertPool) *MockEndpointConfig {
config := NewMockEndpointConfig(mockCtrl)
config.EXPECT().TLSCACertPool().Return(&MockCertPool{CertPool: certPool}).AnyTimes()
config.EXPECT().Timeout(fab.PeerConnection).Return(time.Second * 5).AnyTimes()
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{TLSCert}).AnyTimes()
return config
}
// BadTLSClientMockConfig returns a mock config for testing with TLSClientCerts() that always returns an error
func BadTLSClientMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
config := NewMockEndpointConfig(mockCtrl)
config.EXPECT().TLSCACertPool().Return(&MockCertPool{Err: errors.New(ErrorMessage)}).AnyTimes()
config.EXPECT().Timeout(fab.PeerConnection).Return(time.Second * 5).AnyTimes()
config.EXPECT().TLSClientCerts().Return(nil).AnyTimes()
return config
}
//MockCertPool for unit tests to mock CertPool
type MockCertPool struct {
CertPool *x509.CertPool
Err error
}
//Get mock implementation of fab CertPool.Get()
func (c *MockCertPool) Get() (*x509.CertPool, error) {
return c.CertPool, c.Err
}
//Add mock impl of adding certs to cert pool queue
func (c *MockCertPool) Add(certs ...*x509.Certificate) {
}