github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/config/msp/config_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package msp
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/hyperledger/fabric/common/cauthdsl"
    23  	"github.com/hyperledger/fabric/core/config"
    24  	"github.com/hyperledger/fabric/msp"
    25  	cb "github.com/hyperledger/fabric/protos/common"
    26  	mspprotos "github.com/hyperledger/fabric/protos/msp"
    27  	"github.com/hyperledger/fabric/protos/utils"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestMSPConfigManager(t *testing.T) {
    32  	mspDir, err := config.GetDevMspDir()
    33  	assert.NoError(t, err)
    34  	conf, err := msp.GetLocalMspConfig(mspDir, nil, "DEFAULT")
    35  	assert.NoError(t, err)
    36  
    37  	// test success:
    38  
    39  	// begin/propose/commit
    40  	mspCH := NewMSPConfigHandler()
    41  
    42  	assert.Panics(t, func() {
    43  		mspCH.PreCommit(t)
    44  	}, "Expected panic calling PreCommit before beginning transaction")
    45  	assert.Panics(t, func() {
    46  		mspCH.CommitProposals(t)
    47  	}, "Expected panic calling CommitProposals before beginning transaction")
    48  	assert.Panics(t, func() {
    49  		_, err = mspCH.ProposeMSP(t, conf)
    50  	}, "Expected panic calling ProposeMSP before beginning transaction")
    51  
    52  	mspCH.BeginConfig(t)
    53  	_, err = mspCH.ProposeMSP(t, conf)
    54  	assert.NoError(t, err)
    55  	mspCH.PreCommit(t)
    56  	mspCH.CommitProposals(t)
    57  
    58  	msps, err := mspCH.GetMSPs()
    59  	assert.NoError(t, err)
    60  
    61  	if msps == nil || len(msps) == 0 {
    62  		t.Fatalf("There are no MSPS in the manager")
    63  	}
    64  
    65  	mspCH.BeginConfig(t)
    66  	_, err = mspCH.ProposeMSP(t, conf)
    67  	mspCH.RollbackProposals(t)
    68  
    69  	// test failure
    70  	// begin/propose/commit
    71  	mspCH.BeginConfig(t)
    72  	_, err = mspCH.ProposeMSP(t, conf)
    73  	assert.NoError(t, err)
    74  	_, err = mspCH.ProposeMSP(t, &mspprotos.MSPConfig{Config: []byte("BARF!")})
    75  	assert.Error(t, err)
    76  	_, err = mspCH.ProposeMSP(t, &mspprotos.MSPConfig{Type: int32(10)})
    77  	assert.Panics(t, func() {
    78  		mspCH.BeginConfig(t)
    79  	}, "Expected panic calling BeginConfig multiple times for same transaction")
    80  }
    81  
    82  func TestTemplates(t *testing.T) {
    83  	mspDir, err := config.GetDevMspDir()
    84  	assert.NoError(t, err)
    85  	mspConf, err := msp.GetLocalMspConfig(mspDir, nil, "DEFAULT")
    86  	assert.NoError(t, err)
    87  
    88  	expectedMSPValue := &cb.ConfigValue{
    89  		Value: utils.MarshalOrPanic(mspConf),
    90  	}
    91  	configGroup := TemplateGroupMSP([]string{"TestPath"}, mspConf)
    92  	testGroup, ok := configGroup.Groups["TestPath"]
    93  	assert.Equal(t, true, ok, "Failed to find group key")
    94  	assert.Equal(t, expectedMSPValue, testGroup.Values[MSPKey], "MSPKey did not match expected value")
    95  
    96  	configGroup = TemplateGroupMSPWithAdminRolePrincipal([]string{"TestPath"}, mspConf, false)
    97  	expectedPolicyValue := utils.MarshalOrPanic(cauthdsl.SignedByMspMember("DEFAULT"))
    98  	actualPolicyValue := configGroup.Groups["TestPath"].Policies[AdminsPolicyKey].Policy.Value
    99  	assert.Equal(t, expectedPolicyValue, actualPolicyValue, "Expected SignedByMspMemberPolicy")
   100  
   101  	mspConf = &mspprotos.MSPConfig{}
   102  	assert.Panics(t, func() {
   103  		configGroup = TemplateGroupMSPWithAdminRolePrincipal([]string{"TestPath"}, mspConf, false)
   104  	}, "Expected panic with bad msp config")
   105  	mspConf.Type = int32(10)
   106  	assert.Panics(t, func() {
   107  		configGroup = TemplateGroupMSPWithAdminRolePrincipal([]string{"TestPath"}, mspConf, false)
   108  	}, "Expected panic with bad msp config")
   109  
   110  }