From 42fba989b52b0c5a326e2692a5b9d206d2e8cc03 Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Sat, 18 Feb 2017 16:21:37 -0500 Subject: [PATCH] [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 --- common/mocks/crypto/localsigner.go | 46 +++++++++++++++++++++++++ common/mocks/crypto/localsigner_test.go | 27 +++++++++++++++ orderer/multichain/chainsupport_test.go | 8 ++--- orderer/multichain/manager_test.go | 23 ++++++------- orderer/multichain/systemchain_test.go | 11 ++---- 5 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 common/mocks/crypto/localsigner.go create mode 100644 common/mocks/crypto/localsigner_test.go diff --git a/common/mocks/crypto/localsigner.go b/common/mocks/crypto/localsigner.go new file mode 100644 index 00000000000..662b075af34 --- /dev/null +++ b/common/mocks/crypto/localsigner.go @@ -0,0 +1,46 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package crypto + +import ( + cb "github.com/hyperledger/fabric/protos/common" +) + +// FakeLocalSigner is a signer which already has identity an nonce set to fake values +var FakeLocalSigner = &LocalSigner{ + Identity: []byte("IdentityBytes"), + Nonce: []byte("NonceValue"), +} + +// LocalSigner is a mock implmeentation of crypto.LocalSigner +type LocalSigner struct { + Identity []byte + Nonce []byte +} + +// Sign returns the msg, nil +func (ls *LocalSigner) Sign(msg []byte) ([]byte, error) { + return msg, nil +} + +// NewSignatureHeader returns a new signature header, nil +func (ls *LocalSigner) NewSignatureHeader() (*cb.SignatureHeader, error) { + return &cb.SignatureHeader{ + Creator: ls.Identity, + Nonce: ls.Nonce, + }, nil +} diff --git a/common/mocks/crypto/localsigner_test.go b/common/mocks/crypto/localsigner_test.go new file mode 100644 index 00000000000..774e5994eb6 --- /dev/null +++ b/common/mocks/crypto/localsigner_test.go @@ -0,0 +1,27 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package crypto + +import ( + "testing" + + crypto "github.com/hyperledger/fabric/common/crypto" +) + +func TestLocalSignerInterface(t *testing.T) { + _ = crypto.LocalSigner(&LocalSigner{}) +} diff --git a/orderer/multichain/chainsupport_test.go b/orderer/multichain/chainsupport_test.go index ff2f8df86b9..711a3085a14 100644 --- a/orderer/multichain/chainsupport_test.go +++ b/orderer/multichain/chainsupport_test.go @@ -65,7 +65,7 @@ func (mc *mockCommitter) Commit() { func TestCommitConfig(t *testing.T) { ml := &mockLedgerReadWriter{} cm := &mockconfigtx.Manager{} - cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}} + cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()} txs := []*cb.Envelope{makeNormalTx("foo", 0), makeNormalTx("bar", 1)} committers := []filter.Committer{&mockCommitter{}, &mockCommitter{}} block := cs.CreateNextBlock(txs) @@ -90,7 +90,7 @@ func TestCommitConfig(t *testing.T) { func TestWriteBlockSignatures(t *testing.T) { ml := &mockLedgerReadWriter{} cm := &mockconfigtx.Manager{} - cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}} + cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()} if utils.GetMetadataFromBlockOrPanic(cs.WriteBlock(cb.NewBlock(0, nil), nil, nil), cb.BlockMetadataIndex_SIGNATURES) == nil { t.Fatalf("Block should have block signature") @@ -100,7 +100,7 @@ func TestWriteBlockSignatures(t *testing.T) { func TestWriteBlockOrdererMetadata(t *testing.T) { ml := &mockLedgerReadWriter{} cm := &mockconfigtx.Manager{} - cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}} + cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()} value := []byte("foo") expected := &cb.Metadata{Value: value} @@ -118,7 +118,7 @@ func TestWriteBlockOrdererMetadata(t *testing.T) { func TestWriteLastConfig(t *testing.T) { ml := &mockLedgerReadWriter{} cm := &mockconfigtx.Manager{} - cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &mockCryptoHelper{}} + cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: mockCrypto()} expected := uint64(0) diff --git a/orderer/multichain/manager_test.go b/orderer/multichain/manager_test.go index 9101cbab116..1c325d1921c 100644 --- a/orderer/multichain/manager_test.go +++ b/orderer/multichain/manager_test.go @@ -24,6 +24,7 @@ import ( "github.com/hyperledger/fabric/common/configtx" genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig" "github.com/hyperledger/fabric/common/configtx/tool/provisional" + mockcrypto "github.com/hyperledger/fabric/common/mocks/crypto" ordererledger "github.com/hyperledger/fabric/orderer/ledger" ramledger "github.com/hyperledger/fabric/orderer/ledger/ram" cb "github.com/hyperledger/fabric/protos/common" @@ -44,18 +45,16 @@ func init() { genesisBlock = provisional.New(conf).GenesisBlock() } -type mockCryptoHelper struct{} - -func (xxx mockCryptoHelper) VerifySignature(sd *cb.SignedData) error { - return nil +func mockCrypto() *mockCryptoHelper { + return &mockCryptoHelper{LocalSigner: mockcrypto.FakeLocalSigner} } -func (xxx mockCryptoHelper) NewSignatureHeader() (*cb.SignatureHeader, error) { - return &cb.SignatureHeader{}, nil +type mockCryptoHelper struct { + *mockcrypto.LocalSigner } -func (xxx mockCryptoHelper) Sign(message []byte) ([]byte, error) { - return message, nil +func (mch mockCryptoHelper) VerifySignature(sd *cb.SignedData) error { + return nil } func NewRAMLedgerAndFactory(maxSize int) (ordererledger.Factory, ordererledger.ReadWriter) { @@ -129,7 +128,7 @@ func TestNoSystemChain(t *testing.T) { consenters := make(map[string]Consenter) consenters[conf.Orderer.OrdererType] = &mockConsenter{} - NewManagerImpl(lf, consenters, &mockCryptoHelper{}) + NewManagerImpl(lf, consenters, mockCrypto()) } // 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) { consenters := make(map[string]Consenter) consenters[conf.Orderer.OrdererType] = &mockConsenter{} - manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{}) + manager := NewManagerImpl(lf, consenters, mockCrypto()) _, ok := manager.GetChain("Fake") if ok { @@ -185,7 +184,7 @@ func TestSignatureFilter(t *testing.T) { consenters := make(map[string]Consenter) consenters[conf.Orderer.OrdererType] = &mockConsenter{} - manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{}) + manager := NewManagerImpl(lf, consenters, mockCrypto()) cs, ok := manager.GetChain(provisional.TestChainID) @@ -222,7 +221,7 @@ func TestNewChain(t *testing.T) { consenters := make(map[string]Consenter) consenters[conf.Orderer.OrdererType] = &mockConsenter{} - manager := NewManagerImpl(lf, consenters, &mockCryptoHelper{}) + manager := NewManagerImpl(lf, consenters, mockCrypto()) generator := provisional.New(conf) channelTemplate := generator.ChannelTemplate() diff --git a/orderer/multichain/systemchain_test.go b/orderer/multichain/systemchain_test.go index abcee2fb763..2ca7f8310c2 100644 --- a/orderer/multichain/systemchain_test.go +++ b/orderer/multichain/systemchain_test.go @@ -26,6 +26,7 @@ import ( configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel" mockconfigvalueschannel "github.com/hyperledger/fabric/common/mocks/configvalues/channel" mockconfigvaluesorderer "github.com/hyperledger/fabric/common/mocks/configvalues/channel/orderer" + mockcrypto "github.com/hyperledger/fabric/common/mocks/crypto" mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/orderer/common/filter" @@ -34,6 +35,7 @@ import ( ) type mockSupport struct { + *mockcrypto.LocalSigner mpm *mockpolicies.Manager msc *mockconfigvaluesorderer.SharedConfig chainID string @@ -43,6 +45,7 @@ type mockSupport struct { func newMockSupport(chainID string) *mockSupport { return &mockSupport{ + LocalSigner: mockcrypto.FakeLocalSigner, mpm: &mockpolicies.Manager{}, msc: &mockconfigvaluesorderer.SharedConfig{}, chainID: chainID, @@ -71,14 +74,6 @@ func (ms *mockSupport) ChannelConfig() configvalueschannel.ConfigReader { return ms.chainConfig } -func (ms *mockSupport) Sign(data []byte) ([]byte, error) { - return data, nil -} - -func (ms *mockSupport) NewSignatureHeader() (*cb.SignatureHeader, error) { - return &cb.SignatureHeader{}, nil -} - type mockChainCreator struct { newChains []*cb.Envelope ms *mockSupport