github.com/true-sqn/fabric@v2.1.1+incompatible/msp/mgmt/mgmt_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package mgmt
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/hyperledger/fabric/bccsp"
    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 TestGetManagerForChains(t *testing.T) {
    21  	// MSPManager for channel does not exist prior to this call
    22  	mspMgr1 := GetManagerForChain("test")
    23  	// ensure MSPManager is set
    24  	if mspMgr1 == nil {
    25  		t.Fatal("mspMgr1 fail")
    26  	}
    27  
    28  	// MSPManager for channel now exists
    29  	mspMgr2 := GetManagerForChain("test")
    30  	// ensure MSPManager returned matches the first result
    31  	if mspMgr2 != mspMgr1 {
    32  		t.Fatal("mspMgr2 != mspMgr1 fail")
    33  	}
    34  }
    35  
    36  func TestGetManagerForChains_usingMSPConfigHandlers(t *testing.T) {
    37  	XXXSetMSPManager("foo", msp.NewMSPManager())
    38  	msp2 := GetManagerForChain("foo")
    39  	// return value should be set because the MSPManager was initialized
    40  	if msp2 == nil {
    41  		t.FailNow()
    42  	}
    43  }
    44  
    45  func TestGetIdentityDeserializer(t *testing.T) {
    46  	cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    47  	assert.NoError(t, err)
    48  
    49  	XXXSetMSPManager("baz", msp.NewMSPManager())
    50  	ids := GetIdentityDeserializer("baz", cryptoProvider)
    51  	assert.NotNil(t, ids)
    52  	ids = GetIdentityDeserializer("", cryptoProvider)
    53  	assert.NotNil(t, ids)
    54  }
    55  
    56  func TestGetLocalSigningIdentityOrPanic(t *testing.T) {
    57  	cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    58  	assert.NoError(t, err)
    59  
    60  	sid := GetLocalSigningIdentityOrPanic(cryptoProvider)
    61  	assert.NotNil(t, sid)
    62  }
    63  
    64  func TestUpdateLocalMspCache(t *testing.T) {
    65  	// reset localMsp to force it to be initialized on the first call
    66  	localMsp = nil
    67  
    68  	cryptoProvider := factory.GetDefault()
    69  
    70  	// first call should initialize local MSP and returned the cached version
    71  	firstMsp := GetLocalMSP(cryptoProvider)
    72  	// second call should return the same
    73  	secondMsp := GetLocalMSP(cryptoProvider)
    74  	// third call should return the same
    75  	thirdMsp := GetLocalMSP(cryptoProvider)
    76  
    77  	// the same (non-cached if not patched) instance
    78  	if thirdMsp != secondMsp {
    79  		t.Fatalf("thirdMsp != secondMsp")
    80  	}
    81  	// first (cached) and second (non-cached) different unless patched
    82  	if firstMsp != secondMsp {
    83  		t.Fatalf("firstMsp != secondMsp")
    84  	}
    85  }
    86  
    87  func TestNewMSPMgmtMgr(t *testing.T) {
    88  	cryptoProvider, err := LoadMSPSetupForTesting()
    89  	assert.Nil(t, err)
    90  
    91  	// test for nonexistent channel
    92  	mspMgmtMgr := GetManagerForChain("fake")
    93  
    94  	id := GetLocalSigningIdentityOrPanic(cryptoProvider)
    95  	assert.NotNil(t, id)
    96  
    97  	serializedID, err := id.Serialize()
    98  	if err != nil {
    99  		t.Fatalf("Serialize should have succeeded, got err %s", err)
   100  		return
   101  	}
   102  
   103  	idBack, err := mspMgmtMgr.DeserializeIdentity(serializedID)
   104  	assert.Error(t, err)
   105  	assert.Contains(t, err.Error(), "channel doesn't exist")
   106  	assert.Nil(t, idBack, "deserialized identity should have been nil")
   107  
   108  	// test for existing channel
   109  	mspMgmtMgr = GetManagerForChain("testchannelid")
   110  
   111  	id = GetLocalSigningIdentityOrPanic(cryptoProvider)
   112  	assert.NotNil(t, id)
   113  
   114  	serializedID, err = id.Serialize()
   115  	if err != nil {
   116  		t.Fatalf("Serialize should have succeeded, got err %s", err)
   117  		return
   118  	}
   119  
   120  	idBack, err = mspMgmtMgr.DeserializeIdentity(serializedID)
   121  	assert.NoError(t, err)
   122  	assert.NotNil(t, idBack, "deserialized identity should not have been nil")
   123  }
   124  
   125  func LoadMSPSetupForTesting() (bccsp.BCCSP, error) {
   126  	dir := configtest.GetDevMspDir()
   127  	conf, err := msp.GetLocalMspConfig(dir, nil, "SampleOrg")
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	cryptoProvider := factory.GetDefault()
   133  
   134  	err = GetLocalMSP(cryptoProvider).Setup(conf)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	err = GetManagerForChain("testchannelid").Setup([]msp.MSP{GetLocalMSP(cryptoProvider)})
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	return cryptoProvider, nil
   145  }