github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/configtxlator/sanitycheck/sanitycheck_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 sanitycheck
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/hyperledger/fabric/bccsp/factory"
    23  	"github.com/hyperledger/fabric/common/cauthdsl"
    24  	"github.com/hyperledger/fabric/common/config"
    25  	"github.com/hyperledger/fabric/common/configtx"
    26  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    27  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    28  	cb "github.com/hyperledger/fabric/protos/common"
    29  	mspprotos "github.com/hyperledger/fabric/protos/msp"
    30  	"github.com/hyperledger/fabric/protos/utils"
    31  
    32  	"github.com/golang/protobuf/proto"
    33  	"github.com/stretchr/testify/assert"
    34  )
    35  
    36  var (
    37  	insecureConfig  *cb.Config
    38  	singleMSPConfig *cb.Config
    39  )
    40  
    41  func init() {
    42  	factory.InitFactories(nil)
    43  
    44  	insecureConf := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
    45  	insecureGB := provisional.New(insecureConf).GenesisBlockForChannel(provisional.TestChainID)
    46  	insecureCtx := utils.ExtractEnvelopeOrPanic(insecureGB, 0)
    47  	insecureConfig = configtx.UnmarshalConfigEnvelopeOrPanic(utils.UnmarshalPayloadOrPanic(insecureCtx.Payload).Data).Config
    48  
    49  	singleMSPConf := genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile)
    50  	singleMSPGB := provisional.New(singleMSPConf).GenesisBlockForChannel(provisional.TestChainID)
    51  	singleMSPCtx := utils.ExtractEnvelopeOrPanic(singleMSPGB, 0)
    52  	singleMSPConfig = configtx.UnmarshalConfigEnvelopeOrPanic(utils.UnmarshalPayloadOrPanic(singleMSPCtx.Payload).Data).Config
    53  }
    54  
    55  func TestSimpleCheck(t *testing.T) {
    56  	result, err := Check(insecureConfig)
    57  	assert.NoError(t, err, "Simple empty config")
    58  	assert.Equal(t, &Messages{}, result)
    59  }
    60  
    61  func TestOneMSPCheck(t *testing.T) {
    62  	result, err := Check(singleMSPConfig)
    63  	assert.NoError(t, err, "Simple single MSP config")
    64  	assert.Equal(t, &Messages{}, result)
    65  }
    66  
    67  func TestEmptyConfigCheck(t *testing.T) {
    68  	result, err := Check(&cb.Config{})
    69  	assert.NoError(t, err, "Simple single MSP config")
    70  	assert.Empty(t, result.ElementErrors)
    71  	assert.Empty(t, result.ElementWarnings)
    72  	assert.NotEmpty(t, result.GeneralErrors)
    73  }
    74  
    75  func TestWrongMSPID(t *testing.T) {
    76  	localConfig := proto.Clone(insecureConfig).(*cb.Config)
    77  	policyName := "foo"
    78  	localConfig.ChannelGroup.Groups[config.OrdererGroupKey].Policies[policyName] = &cb.ConfigPolicy{
    79  		Policy: &cb.Policy{
    80  			Type:  int32(cb.Policy_SIGNATURE),
    81  			Value: utils.MarshalOrPanic(cauthdsl.SignedByMspAdmin("MissingOrg")),
    82  		},
    83  	}
    84  	result, err := Check(localConfig)
    85  	assert.NoError(t, err, "Simple empty config")
    86  	assert.Empty(t, result.GeneralErrors)
    87  	assert.Empty(t, result.ElementErrors)
    88  	assert.Len(t, result.ElementWarnings, 1)
    89  	assert.Equal(t, ".groups."+config.OrdererGroupKey+".policies."+policyName, result.ElementWarnings[0].Path)
    90  }
    91  
    92  func TestCorruptRolePrincipal(t *testing.T) {
    93  	localConfig := proto.Clone(insecureConfig).(*cb.Config)
    94  	policyName := "foo"
    95  	sigPolicy := cauthdsl.SignedByMspAdmin("MissingOrg")
    96  	sigPolicy.Identities[0].Principal = []byte("garbage which corrupts the evaluation")
    97  	localConfig.ChannelGroup.Policies[policyName] = &cb.ConfigPolicy{
    98  		Policy: &cb.Policy{
    99  			Type:  int32(cb.Policy_SIGNATURE),
   100  			Value: utils.MarshalOrPanic(sigPolicy),
   101  		},
   102  	}
   103  	result, err := Check(localConfig)
   104  	assert.NoError(t, err, "Simple empty config")
   105  	assert.Empty(t, result.GeneralErrors)
   106  	assert.Empty(t, result.ElementWarnings)
   107  	assert.Len(t, result.ElementErrors, 1)
   108  	assert.Equal(t, ".policies."+policyName, result.ElementErrors[0].Path)
   109  }
   110  
   111  func TestCorruptOUPrincipal(t *testing.T) {
   112  	localConfig := proto.Clone(insecureConfig).(*cb.Config)
   113  	policyName := "foo"
   114  	sigPolicy := cauthdsl.SignedByMspAdmin("MissingOrg")
   115  	sigPolicy.Identities[0].PrincipalClassification = mspprotos.MSPPrincipal_ORGANIZATION_UNIT
   116  	sigPolicy.Identities[0].Principal = []byte("garbage which corrupts the evaluation")
   117  	localConfig.ChannelGroup.Policies[policyName] = &cb.ConfigPolicy{
   118  		Policy: &cb.Policy{
   119  			Type:  int32(cb.Policy_SIGNATURE),
   120  			Value: utils.MarshalOrPanic(sigPolicy),
   121  		},
   122  	}
   123  	result, err := Check(localConfig)
   124  	assert.NoError(t, err, "Simple empty config")
   125  	assert.Empty(t, result.GeneralErrors)
   126  	assert.Empty(t, result.ElementWarnings)
   127  	assert.Len(t, result.ElementErrors, 1)
   128  	assert.Equal(t, ".policies."+policyName, result.ElementErrors[0].Path)
   129  }