github.com/kaituanwang/hyperledger@v2.0.1+incompatible/discovery/support/chaincode/support_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package chaincode
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	"github.com/hyperledger/fabric-protos-go/common"
    14  	"github.com/hyperledger/fabric-protos-go/msp"
    15  	"github.com/hyperledger/fabric/common/chaincode"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  type mockMetadataRetriever struct {
    20  	res *chaincode.Metadata
    21  }
    22  
    23  func (r *mockMetadataRetriever) Metadata(channel string, cc string, loadCollections bool) *chaincode.Metadata {
    24  	return r.res
    25  }
    26  
    27  func TestSupport(t *testing.T) {
    28  	emptySignaturePolicyEnvelope, _ := proto.Marshal(&common.SignaturePolicyEnvelope{})
    29  	ccmd1 := &chaincode.Metadata{Policy: emptySignaturePolicyEnvelope}
    30  	notEmptySignaturePolicyEnvelope, _ := proto.Marshal(&common.SignaturePolicyEnvelope{
    31  		Rule:       &common.SignaturePolicy{},
    32  		Identities: []*msp.MSPPrincipal{{}},
    33  	})
    34  	ccmd2 := &chaincode.Metadata{Policy: notEmptySignaturePolicyEnvelope}
    35  
    36  	tests := []struct {
    37  		name        string
    38  		input       *chaincode.Metadata
    39  		shouldBeNil bool
    40  	}{
    41  		{
    42  			name:        "Nil instantiatedChaincode",
    43  			input:       nil,
    44  			shouldBeNil: true,
    45  		},
    46  		{
    47  			name:        "Invalid policy bytes",
    48  			input:       &chaincode.Metadata{Policy: []byte{1, 2, 3}},
    49  			shouldBeNil: true,
    50  		},
    51  		{
    52  			name:        "Empty signature policy envelope",
    53  			input:       ccmd1,
    54  			shouldBeNil: true,
    55  		},
    56  		{
    57  			name:        "Not Empty signature policy envelope",
    58  			input:       ccmd2,
    59  			shouldBeNil: false,
    60  		},
    61  	}
    62  
    63  	for _, test := range tests {
    64  		test := test
    65  		t.Run(test.name, func(t *testing.T) {
    66  			sup := NewDiscoverySupport(&mockMetadataRetriever{res: test.input})
    67  			res := sup.PoliciesByChaincode("", "")
    68  			if test.shouldBeNil {
    69  				assert.Nil(t, res)
    70  			} else {
    71  				assert.NotNil(t, res)
    72  			}
    73  		})
    74  	}
    75  }