github.com/ewagmig/fabric@v2.1.1+incompatible/common/channelconfig/msp_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	"testing"
    11  
    12  	mspprotos "github.com/hyperledger/fabric-protos-go/msp"
    13  	"github.com/hyperledger/fabric/bccsp/factory"
    14  	"github.com/hyperledger/fabric/bccsp/sw"
    15  	"github.com/hyperledger/fabric/core/config/configtest"
    16  	"github.com/hyperledger/fabric/msp"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestMSPConfigManager(t *testing.T) {
    21  	mspDir := configtest.GetDevMspDir()
    22  	conf, err := msp.GetLocalMspConfig(mspDir, nil, "SampleOrg")
    23  	assert.NoError(t, err)
    24  
    25  	// test success:
    26  
    27  	mspVers := []msp.MSPVersion{msp.MSPv1_0, msp.MSPv1_1}
    28  
    29  	for _, ver := range mspVers {
    30  		mspCH := NewMSPConfigHandler(ver, factory.GetDefault())
    31  
    32  		_, err = mspCH.ProposeMSP(conf)
    33  		assert.NoError(t, err)
    34  
    35  		mgr, err := mspCH.CreateMSPManager()
    36  		assert.NoError(t, err)
    37  		assert.NotNil(t, mgr)
    38  
    39  		msps, err := mgr.GetMSPs()
    40  		assert.NoError(t, err)
    41  
    42  		if len(msps) == 0 {
    43  			t.Fatalf("There are no MSPS in the manager")
    44  		}
    45  
    46  		for _, mspInst := range msps {
    47  			assert.Equal(t, mspInst.GetVersion(), msp.MSPVersion(ver))
    48  		}
    49  	}
    50  }
    51  
    52  func TestMSPConfigFailure(t *testing.T) {
    53  	cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    54  	assert.NoError(t, err)
    55  	mspCH := NewMSPConfigHandler(msp.MSPv1_0, cryptoProvider)
    56  
    57  	// begin/propose/commit
    58  	t.Run("Bad proto", func(t *testing.T) {
    59  		_, err := mspCH.ProposeMSP(&mspprotos.MSPConfig{Config: []byte("BARF!")})
    60  		assert.Error(t, err)
    61  	})
    62  
    63  	t.Run("Bad MSP Type", func(t *testing.T) {
    64  		_, err := mspCH.ProposeMSP(&mspprotos.MSPConfig{Type: int32(10)})
    65  		assert.Error(t, err)
    66  	})
    67  }