github.com/true-sqn/fabric@v2.1.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/hyperledger/fabric-protos-go/common"
    13  	"github.com/hyperledger/fabric-protos-go/msp"
    14  	"github.com/hyperledger/fabric/common/chaincode"
    15  	"github.com/hyperledger/fabric/common/policies"
    16  	"github.com/hyperledger/fabric/common/policies/inquire"
    17  	"github.com/hyperledger/fabric/protoutil"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  type mockMetadataRetriever struct {
    22  	res *chaincode.Metadata
    23  }
    24  
    25  func (r *mockMetadataRetriever) Metadata(channel string, cc string, collections ...string) *chaincode.Metadata {
    26  	return r.res
    27  }
    28  
    29  func TestSupport(t *testing.T) {
    30  	emptySignaturePolicyEnvelope := &common.SignaturePolicyEnvelope{}
    31  	ccmd1 := &chaincode.Metadata{Policy: protoutil.MarshalOrPanic(emptySignaturePolicyEnvelope)}
    32  	notEmptySignaturePolicyEnvelope := &common.SignaturePolicyEnvelope{
    33  		Rule:       &common.SignaturePolicy{},
    34  		Identities: []*msp.MSPPrincipal{{Principal: []byte("principal-1")}},
    35  	}
    36  	ccmd2 := &chaincode.Metadata{Policy: protoutil.MarshalOrPanic(notEmptySignaturePolicyEnvelope)}
    37  	notEmptySignaturePolicyEnvelope2 := &common.SignaturePolicyEnvelope{
    38  		Rule:       &common.SignaturePolicy{},
    39  		Identities: []*msp.MSPPrincipal{{Principal: []byte("principal-2")}},
    40  	}
    41  	ccmd3 := &chaincode.Metadata{Policy: protoutil.MarshalOrPanic(notEmptySignaturePolicyEnvelope),
    42  		CollectionPolicies: map[string][]byte{"col1": protoutil.MarshalOrPanic(notEmptySignaturePolicyEnvelope2)}}
    43  
    44  	tests := []struct {
    45  		name           string
    46  		input          *chaincode.Metadata
    47  		collNames      []string
    48  		expectedReturn []policies.InquireablePolicy
    49  	}{
    50  		{
    51  			name:           "Nil instantiatedChaincode",
    52  			input:          nil,
    53  			collNames:      nil,
    54  			expectedReturn: nil,
    55  		},
    56  		{
    57  			name:           "Invalid policy bytes",
    58  			input:          &chaincode.Metadata{Policy: []byte{1, 2, 3}},
    59  			collNames:      nil,
    60  			expectedReturn: nil,
    61  		},
    62  		{
    63  			name:           "Empty signature policy envelope",
    64  			input:          ccmd1,
    65  			collNames:      nil,
    66  			expectedReturn: nil,
    67  		},
    68  		{
    69  			name:           "Not Empty signature policy envelope",
    70  			input:          ccmd2,
    71  			collNames:      nil,
    72  			expectedReturn: []policies.InquireablePolicy{inquire.NewInquireableSignaturePolicy(notEmptySignaturePolicyEnvelope)},
    73  		},
    74  		{
    75  			name:           "Not Empty signature policy envelopes with existing collection policy",
    76  			input:          ccmd3,
    77  			collNames:      []string{"col1"},
    78  			expectedReturn: []policies.InquireablePolicy{inquire.NewInquireableSignaturePolicy(notEmptySignaturePolicyEnvelope2)},
    79  		},
    80  	}
    81  
    82  	for _, test := range tests {
    83  		test := test
    84  		t.Run(test.name, func(t *testing.T) {
    85  			sup := NewDiscoverySupport(&mockMetadataRetriever{res: test.input})
    86  			res := sup.PoliciesByChaincode("", "", test.collNames...)
    87  			assert.Equal(t, len(res), len(test.expectedReturn))
    88  			for i := 0; i < len(test.expectedReturn); i++ {
    89  				assert.Equal(t, res[i].SatisfiedBy(), test.expectedReturn[i].SatisfiedBy())
    90  			}
    91  		})
    92  	}
    93  }