Skip to content

Commit

Permalink
[FAB-3451] Move noopmsp in mocks
Browse files Browse the repository at this point in the history
This change set moves the implementation of the noop version of MSP in
common/mocks/msp/ as decided for all mocks.

Change-Id: I411d50f1b869d84192dc3837ecd335036638e71e
Signed-off-by: Alessandro Sorniotti <ale.linux@sopit.net>
  • Loading branch information
ale-linux committed Apr 28, 2017
1 parent ecc29dd commit e644262
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 75 deletions.
4 changes: 2 additions & 2 deletions common/configtx/tool/configtxgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
mmsp "github.com/hyperledger/fabric/common/mocks/msp"
logging "github.com/op/go-logging"
)

Expand All @@ -56,7 +56,7 @@ func doOutputBlock(config *genesisconfig.Profile, channelID string, outputBlock
func doOutputChannelCreateTx(conf *genesisconfig.Profile, channelID string, outputChannelCreateTx string) error {
logger.Info("Generating new channel configtx")
// TODO, use actual MSP eventually
signer, err := msp.NewNoopMsp().GetDefaultSigningIdentity()
signer, err := mmsp.NewNoopMsp().GetDefaultSigningIdentity()
if err != nil {
return fmt.Errorf("Error getting signing identity: %s", err)
}
Expand Down
48 changes: 20 additions & 28 deletions msp/noopmsp.go → common/mocks/msp/noopmsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,83 @@ limitations under the License.
package msp

import (
m "github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/msp"
)

type noopmsp struct {
}

func NewNoopMsp() MSP {
mspLogger.Infof("Creating no-op MSP instance")
// NewNoopMsp returns a no-op implementation of the MSP inteface
func NewNoopMsp() m.MSP {
return &noopmsp{}
}

func (msp *noopmsp) Setup(*msp.MSPConfig) error {
return nil
}

func (msp *noopmsp) GetType() ProviderType {
func (msp *noopmsp) GetType() m.ProviderType {
return 0
}

func (msp *noopmsp) GetIdentifier() (string, error) {
return "NOOP", nil
}

func (msp *noopmsp) GetSigningIdentity(identifier *IdentityIdentifier) (SigningIdentity, error) {
mspLogger.Infof("Obtaining signing identity for %s", identifier)
func (msp *noopmsp) GetSigningIdentity(identifier *m.IdentityIdentifier) (m.SigningIdentity, error) {
id, _ := newNoopSigningIdentity()
return id, nil
}

func (msp *noopmsp) GetDefaultSigningIdentity() (SigningIdentity, error) {
mspLogger.Infof("Obtaining default signing identity")
func (msp *noopmsp) GetDefaultSigningIdentity() (m.SigningIdentity, error) {
id, _ := newNoopSigningIdentity()
return id, nil
}

// GetRootCerts returns the root certificates for this MSP
func (msp *noopmsp) GetRootCerts() []Identity {
func (msp *noopmsp) GetRootCerts() []m.Identity {
return nil
}

// GetIntermediateCerts returns the intermediate root certificates for this MSP
func (msp *noopmsp) GetIntermediateCerts() []Identity {
func (msp *noopmsp) GetIntermediateCerts() []m.Identity {
return nil
}

func (msp *noopmsp) DeserializeIdentity(serializedID []byte) (Identity, error) {
mspLogger.Infof("Obtaining identity for %s", string(serializedID))
func (msp *noopmsp) DeserializeIdentity(serializedID []byte) (m.Identity, error) {
id, _ := newNoopIdentity()
return id, nil
}

func (msp *noopmsp) Validate(id Identity) error {
func (msp *noopmsp) Validate(id m.Identity) error {
return nil
}

func (msp *noopmsp) SatisfiesPrincipal(id Identity, principal *msp.MSPPrincipal) error {
func (msp *noopmsp) SatisfiesPrincipal(id m.Identity, principal *msp.MSPPrincipal) error {
return nil
}

type noopidentity struct {
}

func newNoopIdentity() (Identity, error) {
mspLogger.Infof("Creating no-op identity instance")
func newNoopIdentity() (m.Identity, error) {
return &noopidentity{}, nil
}

func (id *noopidentity) SatisfiesPrincipal(*msp.MSPPrincipal) error {
return nil
}

func (id *noopidentity) GetIdentifier() *IdentityIdentifier {
return &IdentityIdentifier{Mspid: "NOOP", Id: "Bob"}
func (id *noopidentity) GetIdentifier() *m.IdentityIdentifier {
return &m.IdentityIdentifier{Mspid: "NOOP", Id: "Bob"}
}

func (id *noopidentity) GetMSPIdentifier() string {
return "MSPID"
}

func (id *noopidentity) Validate() error {
mspLogger.Infof("Identity is valid")
return nil
}

Expand All @@ -106,46 +102,42 @@ func (id *noopidentity) GetOrganizationalUnits() []msp.FabricOUIdentifier {
}

func (id *noopidentity) Verify(msg []byte, sig []byte) error {
mspLogger.Infof("Signature is valid")
return nil
}

func (id *noopidentity) VerifyOpts(msg []byte, sig []byte, opts SignatureOpts) error {
func (id *noopidentity) VerifyOpts(msg []byte, sig []byte, opts m.SignatureOpts) error {
return nil
}

func (id *noopidentity) VerifyAttributes(proof []byte, spec *AttributeProofSpec) error {
func (id *noopidentity) VerifyAttributes(proof []byte, spec *m.AttributeProofSpec) error {
return nil
}

func (id *noopidentity) Serialize() ([]byte, error) {
mspLogger.Infof("Serialinzing identity")
return []byte("cert"), nil
}

type noopsigningidentity struct {
noopidentity
}

func newNoopSigningIdentity() (SigningIdentity, error) {
mspLogger.Infof("Creating no-op signing identity instance")
func newNoopSigningIdentity() (m.SigningIdentity, error) {
return &noopsigningidentity{}, nil
}

func (id *noopsigningidentity) Sign(msg []byte) ([]byte, error) {
mspLogger.Infof("signing message")
return []byte("signature"), nil
}

func (id *noopsigningidentity) SignOpts(msg []byte, opts SignatureOpts) ([]byte, error) {
func (id *noopsigningidentity) SignOpts(msg []byte, opts m.SignatureOpts) ([]byte, error) {
return nil, nil
}

func (id *noopsigningidentity) GetAttributeProof(spec *AttributeProofSpec) (proof []byte, err error) {
func (id *noopsigningidentity) GetAttributeProof(spec *m.AttributeProofSpec) (proof []byte, err error) {
return nil, nil
}

func (id *noopsigningidentity) GetPublicVersion() Identity {
func (id *noopsigningidentity) GetPublicVersion() m.Identity {
return id
}

Expand Down
60 changes: 60 additions & 0 deletions common/mocks/msp/noopmsp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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 msp

import "testing"

func TestNoopMSP(t *testing.T) {
noopmsp := NewNoopMsp()

id, err := noopmsp.GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetSigningIdentity should have succeeded")
return
}

serializedID, err := id.Serialize()
if err != nil {
t.Fatalf("Serialize should have succeeded")
return
}

idBack, err := noopmsp.DeserializeIdentity(serializedID)
if err != nil {
t.Fatalf("DeserializeIdentity should have succeeded")
return
}

msg := []byte("foo")
sig, err := id.Sign(msg)
if err != nil {
t.Fatalf("Sign should have succeeded")
return
}

err = id.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}

err = idBack.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}
}
3 changes: 2 additions & 1 deletion core/common/validation/fullflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

mmsp "github.com/hyperledger/fabric/common/mocks/msp"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
Expand Down Expand Up @@ -275,7 +276,7 @@ func TestBadProp(t *testing.T) {
}

// get a bad signing identity
badSigner, err := msp.NewNoopMsp().GetDefaultSigningIdentity()
badSigner, err := mmsp.NewNoopMsp().GetDefaultSigningIdentity()
if err != nil {
t.Fatal("Couldn't get noop signer")
return
Expand Down
3 changes: 2 additions & 1 deletion events/producer/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

mmsp "github.com/hyperledger/fabric/common/mocks/msp"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/msp/mgmt"
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestSignedEvent(t *testing.T) {
}

// get a bad signing identity
badSigner, err := msp.NewNoopMsp().GetDefaultSigningIdentity()
badSigner, err := mmsp.NewNoopMsp().GetDefaultSigningIdentity()
if err != nil {
t.Fatal("couldn't get noop signer")
return
Expand Down
41 changes: 0 additions & 41 deletions msp/msp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNoopMSP(t *testing.T) {
noopmsp := NewNoopMsp()

id, err := noopmsp.GetDefaultSigningIdentity()
if err != nil {
t.Fatalf("GetSigningIdentity should have succeeded")
return
}

serializedID, err := id.Serialize()
if err != nil {
t.Fatalf("Serialize should have succeeded")
return
}

idBack, err := noopmsp.DeserializeIdentity(serializedID)
if err != nil {
t.Fatalf("DeserializeIdentity should have succeeded")
return
}

msg := []byte("foo")
sig, err := id.Sign(msg)
if err != nil {
t.Fatalf("Sign should have succeeded")
return
}

err = id.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}

err = idBack.Verify(msg, sig)
if err != nil {
t.Fatalf("The signature should be valid")
return
}
}

func TestMSPSetupBad(t *testing.T) {
_, err := GetLocalMspConfig("barf", nil, "DEFAULT")
if err == nil {
Expand Down
3 changes: 2 additions & 1 deletion orderer/multichain/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"errors"

mmsp "github.com/hyperledger/fabric/common/mocks/msp"
logging "github.com/op/go-logging"
"github.com/stretchr/testify/assert"
)
Expand All @@ -46,7 +47,7 @@ func init() {
conf = genesisconfig.Load(genesisconfig.SampleInsecureProfile)
logging.SetLevel(logging.DEBUG, "")
genesisBlock = provisional.New(conf).GenesisBlock()
mockSigningIdentity, _ = msp.NewNoopMsp().GetDefaultSigningIdentity()
mockSigningIdentity, _ = mmsp.NewNoopMsp().GetDefaultSigningIdentity()
}

func mockCrypto() *mockCryptoHelper {
Expand Down
3 changes: 2 additions & 1 deletion protos/testutils/txtestutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"

mmsp "github.com/hyperledger/fabric/common/mocks/msp"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/msp/mgmt/testtools"
Expand Down Expand Up @@ -85,7 +86,7 @@ var sigId msp.SigningIdentity
// ConstructUnsingedTxEnv creates a Transaction envelope from given inputs
func ConstructUnsingedTxEnv(chainID string, ccid *pb.ChaincodeID, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
if mspLcl == nil {
mspLcl = msp.NewNoopMsp()
mspLcl = mmsp.NewNoopMsp()
sigId, _ = mspLcl.GetDefaultSigningIdentity()
}

Expand Down

0 comments on commit e644262

Please sign in to comment.