From 844eb7bb1db8e3558669ca178dd6db0390a1cb41 Mon Sep 17 00:00:00 2001 From: Angelo De Caro Date: Fri, 21 Apr 2017 17:49:15 +0200 Subject: [PATCH] Improved test coverage for msp/mgmt This change-set improves the test coverage of the msp/mgmt package Change-Id: Iee1a5b57ce5175adee315d9c7a6041cb1cb33aac Signed-off-by: Angelo De Caro --- msp/mgmt/deserializer_test.go | 86 +++++++++++++++++++++++++++++++++++ msp/mgmt/principal.go | 1 - msp/mgmt/principal_test.go | 55 ++++++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 msp/mgmt/deserializer_test.go create mode 100644 msp/mgmt/principal_test.go diff --git a/msp/mgmt/deserializer_test.go b/msp/mgmt/deserializer_test.go new file mode 100644 index 00000000000..e8a89941c9f --- /dev/null +++ b/msp/mgmt/deserializer_test.go @@ -0,0 +1,86 @@ +/* +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 mgmt + +import ( + "fmt" + "os" + "testing" + + msp2 "github.com/hyperledger/fabric/common/config/msp" + "github.com/hyperledger/fabric/msp" + "github.com/stretchr/testify/assert" +) + +func TestNewDeserializersManager(t *testing.T) { + assert.NotNil(t, NewDeserializersManager()) +} + +func TestMspDeserializersManager_Deserialize(t *testing.T) { + m := NewDeserializersManager() + + i, err := GetLocalMSP().GetDefaultSigningIdentity() + assert.NoError(t, err) + raw, err := i.Serialize() + assert.NoError(t, err) + + i2, err := m.Deserialize(raw) + assert.NoError(t, err) + assert.NotNil(t, i2) + assert.NotNil(t, i2.IdBytes) + assert.Equal(t, m.GetLocalMSPIdentifier(), i2.Mspid) +} + +func TestMspDeserializersManager_GetChannelDeserializers(t *testing.T) { + m := NewDeserializersManager() + + deserializers := m.GetChannelDeserializers() + assert.NotNil(t, deserializers) +} + +func TestMspDeserializersManager_GetLocalDeserializer(t *testing.T) { + m := NewDeserializersManager() + + i, err := GetLocalMSP().GetDefaultSigningIdentity() + assert.NoError(t, err) + raw, err := i.Serialize() + assert.NoError(t, err) + + i2, err := m.GetLocalDeserializer().DeserializeIdentity(raw) + assert.NoError(t, err) + assert.NotNil(t, i2) + assert.Equal(t, m.GetLocalMSPIdentifier(), i2.GetMSPIdentifier()) +} + +func TestMain(m *testing.M) { + var err error + testConf, err := msp.GetLocalMspConfig("../sampleconfig/", nil, "DEFAULT") + if err != nil { + fmt.Printf("Setup should have succeeded, got err %s instead", err) + os.Exit(-1) + } + + err = GetLocalMSP().Setup(testConf) + if err != nil { + fmt.Printf("Setup for msp should have succeeded, got err %s instead", err) + os.Exit(-1) + } + + XXXSetMSPManager("foo", &msp2.MSPConfigHandler{MSPManager: msp.NewMSPManager()}) + retVal := m.Run() + os.Exit(retVal) +} diff --git a/msp/mgmt/principal.go b/msp/mgmt/principal.go index 2c918aa0248..f5040abacc5 100644 --- a/msp/mgmt/principal.go +++ b/msp/mgmt/principal.go @@ -48,7 +48,6 @@ func (m *localMSPPrincipalGetter) Get(role string) (*msp.MSPPrincipal, error) { return nil, fmt.Errorf("Could not extract local msp identifier [%s]", err) } - // TODO: put the constants in some more appropriate place switch role { case Admins: principalBytes, err := proto.Marshal(&msp.MSPRole{Role: msp.MSPRole_ADMIN, MspIdentifier: mspid}) diff --git a/msp/mgmt/principal_test.go b/msp/mgmt/principal_test.go new file mode 100644 index 00000000000..2d88b5e3df6 --- /dev/null +++ b/msp/mgmt/principal_test.go @@ -0,0 +1,55 @@ +/* +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 mgmt + +import ( + "testing" + + "github.com/golang/protobuf/proto" + "github.com/hyperledger/fabric/protos/msp" + "github.com/stretchr/testify/assert" +) + +func TestNewLocalMSPPrincipalGetter(t *testing.T) { + assert.NotNil(t, NewLocalMSPPrincipalGetter()) +} + +func TestLocalMSPPrincipalGetter_Get(t *testing.T) { + m := NewDeserializersManager() + g := NewLocalMSPPrincipalGetter() + + _, err := g.Get("") + assert.Error(t, err) + + p, err := g.Get(Admins) + assert.NoError(t, err) + assert.NotNil(t, p) + assert.Equal(t, msp.MSPPrincipal_ROLE, p.PrincipalClassification) + role := &msp.MSPRole{} + proto.Unmarshal(p.Principal, role) + assert.Equal(t, m.GetLocalMSPIdentifier(), role.MspIdentifier) + assert.Equal(t, msp.MSPRole_ADMIN, role.Role) + + p, err = g.Get(Members) + assert.NoError(t, err) + assert.NotNil(t, p) + assert.Equal(t, msp.MSPPrincipal_ROLE, p.PrincipalClassification) + role = &msp.MSPRole{} + proto.Unmarshal(p.Principal, role) + assert.Equal(t, m.GetLocalMSPIdentifier(), role.MspIdentifier) + assert.Equal(t, msp.MSPRole_MEMBER, role.Role) +}