Skip to content

Commit

Permalink
[FAB-2361] Create local signer mocks
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 18, 2017
1 parent 2ecb22a commit 42fba98
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 24 deletions.
46 changes: 46 additions & 0 deletions common/mocks/crypto/localsigner.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions common/mocks/crypto/localsigner_test.go
Original file line number Diff line number Diff line change
@@ -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{})
}
8 changes: 4 additions & 4 deletions orderer/multichain/chainsupport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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}
Expand All @@ -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)

Expand Down
23 changes: 11 additions & 12 deletions orderer/multichain/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
11 changes: 3 additions & 8 deletions orderer/multichain/systemchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,6 +35,7 @@ import (
)

type mockSupport struct {
*mockcrypto.LocalSigner
mpm *mockpolicies.Manager
msc *mockconfigvaluesorderer.SharedConfig
chainID string
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 42fba98

Please sign in to comment.