Skip to content

Commit 42fba98

Browse files
author
Jason Yellick
committed
[FAB-2361] Create local signer mocks
https://jira.hyperledger.org/browse/FAB-2361 In preparation for splitting the config update processing out of the multichain package, the signing mocks need to be extracted and moved to the common mocks. Change-Id: Id6ee786337f8ad20b54951bf69d3ad95b6f1352c Signed-off-by: Jason Yellick <[email protected]>
1 parent 2ecb22a commit 42fba98

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

common/mocks/crypto/localsigner.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package crypto
18+
19+
import (
20+
cb "github.com/hyperledger/fabric/protos/common"
21+
)
22+
23+
// FakeLocalSigner is a signer which already has identity an nonce set to fake values
24+
var FakeLocalSigner = &LocalSigner{
25+
Identity: []byte("IdentityBytes"),
26+
Nonce: []byte("NonceValue"),
27+
}
28+
29+
// LocalSigner is a mock implmeentation of crypto.LocalSigner
30+
type LocalSigner struct {
31+
Identity []byte
32+
Nonce []byte
33+
}
34+
35+
// Sign returns the msg, nil
36+
func (ls *LocalSigner) Sign(msg []byte) ([]byte, error) {
37+
return msg, nil
38+
}
39+
40+
// NewSignatureHeader returns a new signature header, nil
41+
func (ls *LocalSigner) NewSignatureHeader() (*cb.SignatureHeader, error) {
42+
return &cb.SignatureHeader{
43+
Creator: ls.Identity,
44+
Nonce: ls.Nonce,
45+
}, nil
46+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package crypto
18+
19+
import (
20+
"testing"
21+
22+
crypto "github.com/hyperledger/fabric/common/crypto"
23+
)
24+
25+
func TestLocalSignerInterface(t *testing.T) {
26+
_ = crypto.LocalSigner(&LocalSigner{})
27+
}

orderer/multichain/chainsupport_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (mc *mockCommitter) Commit() {
6565
func TestCommitConfig(t *testing.T) {
6666
ml := &mockLedgerReadWriter{}
6767
cm := &mockconfigtx.Manager{}
68-
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}}
68+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
6969
txs := []*cb.Envelope{makeNormalTx("foo", 0), makeNormalTx("bar", 1)}
7070
committers := []filter.Committer{&mockCommitter{}, &mockCommitter{}}
7171
block := cs.CreateNextBlock(txs)
@@ -90,7 +90,7 @@ func TestCommitConfig(t *testing.T) {
9090
func TestWriteBlockSignatures(t *testing.T) {
9191
ml := &mockLedgerReadWriter{}
9292
cm := &mockconfigtx.Manager{}
93-
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}}
93+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
9494

9595
if utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, nil), cb.BlockMetadataIndex_SIGNATURES) == nil {
9696
t.Fatalf("Block should have block signature")
@@ -100,7 +100,7 @@ func TestWriteBlockSignatures(t *testing.T) {
100100
func TestWriteBlockOrdererMetadata(t *testing.T) {
101101
ml := &mockLedgerReadWriter{}
102102
cm := &mockconfigtx.Manager{}
103-
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}}
103+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
104104

105105
value := []byte("foo")
106106
expected := &cb.Metadata{Value: value}
@@ -118,7 +118,7 @@ func TestWriteBlockOrdererMetadata(t *testing.T) {
118118
func TestWriteLastConfig(t *testing.T) {
119119
ml := &mockLedgerReadWriter{}
120120
cm := &mockconfigtx.Manager{}
121-
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}}
121+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()}
122122

123123
expected := uint64(0)
124124

orderer/multichain/manager_test.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/hyperledger/fabric/common/configtx"
2525
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
2626
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
27+
mockcrypto "github.com/hyperledger/fabric/common/mocks/crypto"
2728
ordererledger "github.com/hyperledger/fabric/orderer/ledger"
2829
ramledger "github.com/hyperledger/fabric/orderer/ledger/ram"
2930
cb "github.com/hyperledger/fabric/protos/common"
@@ -44,18 +45,16 @@ func init() {
4445
genesisBlock = provisional.New(conf).GenesisBlock()
4546
}
4647

47-
type mockCryptoHelper struct{}
48-
49-
func (xxx mockCryptoHelper) VerifySignature(sd *cb.SignedData) error {
50-
return nil
48+
func mockCrypto() *mockCryptoHelper {
49+
return &mockCryptoHelper{LocalSigner: mockcrypto.FakeLocalSigner}
5150
}
5251

53-
func (xxx mockCryptoHelper) NewSignatureHeader() (*cb.SignatureHeader, error) {
54-
return &cb.SignatureHeader{}, nil
52+
type mockCryptoHelper struct {
53+
*mockcrypto.LocalSigner
5554
}
5655

57-
func (xxx mockCryptoHelper) Sign(message []byte) ([]byte, error) {
58-
return message, nil
56+
func (mch mockCryptoHelper) VerifySignature(sd *cb.SignedData) error {
57+
return nil
5958
}
6059

6160
func NewRAMLedgerAndFactory(maxSize int) (ordererledger.Factory, ordererledger.ReadWriter) {
@@ -129,7 +128,7 @@ func TestNoSystemChain(t *testing.T) {
129128
consenters := make(map[string]Consenter)
130129
consenters[conf.Orderer.OrdererType] = &mockConsenter{}
131130

132-
NewManagerImpl(lf, consenters, &mockCryptoHelper{})
131+
NewManagerImpl(lf, consenters, mockCrypto())
133132
}
134133

135134
// This test essentially brings the entire system up and is ultimately what main.go will replicate
@@ -139,7 +138,7 @@ func TestManagerImpl(t *testing.T) {
139138
consenters := make(map[string]Consenter)
140139
consenters[conf.Orderer.OrdererType] = &mockConsenter{}
141140

142-
manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{})
141+
manager := NewManagerImpl(lf, consenters, mockCrypto())
143142

144143
_, ok := manager.GetChain("Fake")
145144
if ok {
@@ -185,7 +184,7 @@ func TestSignatureFilter(t *testing.T) {
185184
consenters := make(map[string]Consenter)
186185
consenters[conf.Orderer.OrdererType] = &mockConsenter{}
187186

188-
manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{})
187+
manager := NewManagerImpl(lf, consenters, mockCrypto())
189188

190189
cs, ok := manager.GetChain(provisional.TestChainID)
191190

@@ -222,7 +221,7 @@ func TestNewChain(t *testing.T) {
222221
consenters := make(map[string]Consenter)
223222
consenters[conf.Orderer.OrdererType] = &mockConsenter{}
224223

225-
manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{})
224+
manager := NewManagerImpl(lf, consenters, mockCrypto())
226225

227226
generator := provisional.New(conf)
228227
channelTemplate := generator.ChannelTemplate()

orderer/multichain/systemchain_test.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel"
2727
mockconfigvalueschannel "github.com/hyperledger/fabric/common/mocks/configvalues/channel"
2828
mockconfigvaluesorderer "github.com/hyperledger/fabric/common/mocks/configvalues/channel/orderer"
29+
mockcrypto "github.com/hyperledger/fabric/common/mocks/crypto"
2930
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
3031
"github.com/hyperledger/fabric/common/policies"
3132
"github.com/hyperledger/fabric/orderer/common/filter"
@@ -34,6 +35,7 @@ import (
3435
)
3536

3637
type mockSupport struct {
38+
*mockcrypto.LocalSigner
3739
mpm *mockpolicies.Manager
3840
msc *mockconfigvaluesorderer.SharedConfig
3941
chainID string
@@ -43,6 +45,7 @@ type mockSupport struct {
4345

4446
func newMockSupport(chainID string) *mockSupport {
4547
return &mockSupport{
48+
LocalSigner: mockcrypto.FakeLocalSigner,
4649
mpm: &mockpolicies.Manager{},
4750
msc: &mockconfigvaluesorderer.SharedConfig{},
4851
chainID: chainID,
@@ -71,14 +74,6 @@ func (ms *mockSupport) ChannelConfig() configvalueschannel.ConfigReader {
7174
return ms.chainConfig
7275
}
7376

74-
func (ms *mockSupport) Sign(data []byte) ([]byte, error) {
75-
return data, nil
76-
}
77-
78-
func (ms *mockSupport) NewSignatureHeader() (*cb.SignatureHeader, error) {
79-
return &cb.SignatureHeader{}, nil
80-
}
81-
8277
type mockChainCreator struct {
8378
newChains []*cb.Envelope
8479
ms *mockSupport

0 commit comments

Comments
 (0)