Skip to content

Commit

Permalink
Merge "Improved test coverage for msp/mgmt"
Browse files Browse the repository at this point in the history
  • Loading branch information
mastersingh24 authored and Gerrit Code Review committed Apr 22, 2017
2 parents 0991057 + 844eb7b commit 0fd4f6f
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
86 changes: 86 additions & 0 deletions msp/mgmt/deserializer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 0 additions & 1 deletion msp/mgmt/principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
55 changes: 55 additions & 0 deletions msp/mgmt/principal_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0fd4f6f

Please sign in to comment.