github.com/true-sqn/fabric@v2.1.1+incompatible/core/cclifecycle/util_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package cclifecycle_test 8 9 import ( 10 "bytes" 11 "testing" 12 13 "github.com/golang/protobuf/proto" 14 "github.com/hyperledger/fabric-protos-go/common" 15 "github.com/hyperledger/fabric/common/chaincode" 16 cc "github.com/hyperledger/fabric/core/cclifecycle" 17 "github.com/hyperledger/fabric/core/cclifecycle/mocks" 18 "github.com/hyperledger/fabric/core/common/ccprovider" 19 "github.com/pkg/errors" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/mock" 22 ) 23 24 func TestChaincodeInspection(t *testing.T) { 25 acceptAll := func(cc chaincode.Metadata) bool { 26 return true 27 } 28 29 policy := &common.SignaturePolicyEnvelope{} 30 policy.Rule = &common.SignaturePolicy{ 31 Type: &common.SignaturePolicy_NOutOf_{ 32 NOutOf: &common.SignaturePolicy_NOutOf{ 33 N: int32(2), 34 }, 35 }, 36 } 37 policyBytes, _ := proto.Marshal(policy) 38 39 cc1 := &ccprovider.ChaincodeData{Name: "cc1", Version: "1.0", Policy: policyBytes, Id: []byte{42}} 40 cc1Bytes, _ := proto.Marshal(cc1) 41 acceptSpecificCCId := func(cc chaincode.Metadata) bool { 42 // cc1.Id above is 42 and not 24 43 return bytes.Equal(cc.Id, []byte{24}) 44 } 45 46 var corruptBytes []byte 47 corruptBytes = append(corruptBytes, cc1Bytes...) 48 corruptBytes = append(corruptBytes, 0) 49 50 cc2 := &ccprovider.ChaincodeData{Name: "cc2", Version: "1.1"} 51 cc2Bytes, _ := proto.Marshal(cc2) 52 53 tests := []struct { 54 name string 55 expected chaincode.MetadataSet 56 returnedCCBytes []byte 57 queryErr error 58 queriedChaincodes []string 59 filter func(cc chaincode.Metadata) bool 60 }{ 61 { 62 name: "No chaincodes installed", 63 expected: nil, 64 filter: acceptAll, 65 queriedChaincodes: []string{}, 66 }, 67 { 68 name: "Failure on querying the ledger", 69 queryErr: errors.New("failed querying ledger"), 70 filter: acceptAll, 71 queriedChaincodes: []string{"cc1", "cc2"}, 72 expected: nil, 73 }, 74 { 75 name: "The data in LSCC is corrupt", 76 returnedCCBytes: corruptBytes, 77 filter: acceptAll, 78 queriedChaincodes: []string{"cc1"}, 79 expected: nil, 80 }, 81 { 82 name: "The chaincode is listed as a different chaincode in the ledger", 83 returnedCCBytes: cc2Bytes, 84 filter: acceptAll, 85 queriedChaincodes: []string{"cc1"}, 86 expected: nil, 87 }, 88 { 89 name: "Chaincode doesn't exist", 90 returnedCCBytes: nil, 91 filter: acceptAll, 92 queriedChaincodes: []string{"cc1"}, 93 expected: nil, 94 }, 95 { 96 name: "Select a specific chaincode", 97 returnedCCBytes: cc1Bytes, 98 queriedChaincodes: []string{"cc1", "cc2"}, 99 filter: acceptSpecificCCId, 100 }, 101 { 102 name: "Green path", 103 returnedCCBytes: cc1Bytes, 104 filter: acceptAll, 105 queriedChaincodes: []string{"cc1", "cc2"}, 106 expected: chaincode.MetadataSet([]chaincode.Metadata{ 107 { 108 Id: []byte{42}, 109 Name: "cc1", 110 Version: "1.0", 111 Policy: policyBytes, 112 }, 113 { 114 Name: "cc2", 115 Version: "1.1", 116 }, 117 }), 118 }, 119 } 120 121 for _, test := range tests { 122 test := test 123 t.Run(test.name, func(t *testing.T) { 124 query := &mocks.Query{} 125 query.On("Done") 126 query.On("GetState", mock.Anything, mock.Anything).Return(test.returnedCCBytes, test.queryErr).Once() 127 query.On("GetState", mock.Anything, mock.Anything).Return(cc2Bytes, nil).Once() 128 ccInfo, err := cc.DeployedChaincodes(query, test.filter, false, test.queriedChaincodes...) 129 if test.queryErr != nil { 130 assert.Error(t, err) 131 } else { 132 assert.NoError(t, err) 133 } 134 assert.Equal(t, test.expected, ccInfo) 135 }) 136 } 137 }