github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/core/scc/lscc/deployedcc_infoprovider_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package lscc_test 8 9 import ( 10 "testing" 11 12 "github.com/golang/protobuf/proto" 13 "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/hyperledger/fabric/core/common/ccprovider" 15 "github.com/hyperledger/fabric/core/common/privdata" 16 "github.com/hyperledger/fabric/core/ledger" 17 "github.com/hyperledger/fabric/core/scc/lscc" 18 "github.com/hyperledger/fabric/core/scc/lscc/mock" 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func TestNamespaces(t *testing.T) { 23 ccInfoProvdier := &lscc.DeployedCCInfoProvider{} 24 namespaces := ccInfoProvdier.Namespaces() 25 assert.Len(t, namespaces, 1) 26 assert.Equal(t, "lscc", namespaces[0]) 27 } 28 29 func TestChaincodeInfo(t *testing.T) { 30 cc1 := &ledger.DeployedChaincodeInfo{ 31 Name: "cc1", 32 Version: "cc1_version", 33 Hash: []byte("cc1_hash"), 34 } 35 36 cc2 := &ledger.DeployedChaincodeInfo{ 37 Name: "cc2", 38 Version: "cc2_version", 39 Hash: []byte("cc2_hash"), 40 ExplicitCollectionConfigPkg: prepapreCollectionConfigPkg([]string{"cc2_coll1", "cc2_coll2"}), 41 } 42 43 mockQE := prepareMockQE(t, []*ledger.DeployedChaincodeInfo{cc1, cc2}) 44 ccInfoProvdier := &lscc.DeployedCCInfoProvider{} 45 46 ccInfo1, err := ccInfoProvdier.ChaincodeInfo("", "cc1", mockQE) 47 assert.NoError(t, err) 48 expectedCCInfo1 := cc1 49 expectedCCInfo1.IsLegacy = true 50 assert.Equal(t, expectedCCInfo1, ccInfo1) 51 52 ccInfo2, err := ccInfoProvdier.ChaincodeInfo("", "cc2", mockQE) 53 assert.NoError(t, err) 54 assert.Equal(t, cc2.Name, ccInfo2.Name) 55 assert.True(t, proto.Equal(cc2.ExplicitCollectionConfigPkg, ccInfo2.ExplicitCollectionConfigPkg)) 56 57 ccInfo3, err := ccInfoProvdier.ChaincodeInfo("", "cc3", mockQE) 58 assert.NoError(t, err) 59 assert.Nil(t, ccInfo3) 60 } 61 62 func TestCollectionInfo(t *testing.T) { 63 cc1 := &ledger.DeployedChaincodeInfo{ 64 Name: "cc1", 65 Version: "cc1_version", 66 Hash: []byte("cc1_hash"), 67 } 68 69 cc2 := &ledger.DeployedChaincodeInfo{ 70 Name: "cc2", 71 Version: "cc2_version", 72 Hash: []byte("cc2_hash"), 73 ExplicitCollectionConfigPkg: prepapreCollectionConfigPkg([]string{"cc2_coll1", "cc2_coll2"}), 74 } 75 76 mockQE := prepareMockQE(t, []*ledger.DeployedChaincodeInfo{cc1, cc2}) 77 ccInfoProvdier := &lscc.DeployedCCInfoProvider{} 78 79 collInfo1, err := ccInfoProvdier.CollectionInfo("", "cc1", "non-existing-coll-in-cc1", mockQE) 80 assert.NoError(t, err) 81 assert.Nil(t, collInfo1) 82 83 collInfo2, err := ccInfoProvdier.CollectionInfo("", "cc2", "cc2_coll1", mockQE) 84 assert.NoError(t, err) 85 assert.Equal(t, "cc2_coll1", collInfo2.Name) 86 87 collInfo3, err := ccInfoProvdier.CollectionInfo("", "cc2", "non-existing-coll-in-cc2", mockQE) 88 assert.NoError(t, err) 89 assert.Nil(t, collInfo3) 90 91 ccPkg1, err := ccInfoProvdier.AllCollectionsConfigPkg("", "cc1", mockQE) 92 assert.NoError(t, err) 93 assert.Nil(t, ccPkg1) 94 95 ccPkg2, err := ccInfoProvdier.AllCollectionsConfigPkg("", "cc2", mockQE) 96 assert.NoError(t, err) 97 assert.Equal(t, prepapreCollectionConfigPkg([]string{"cc2_coll1", "cc2_coll2"}), ccPkg2) 98 } 99 100 func prepareMockQE(t *testing.T, deployedChaincodes []*ledger.DeployedChaincodeInfo) *mock.QueryExecutor { 101 mockQE := &mock.QueryExecutor{} 102 lsccTable := map[string][]byte{} 103 for _, cc := range deployedChaincodes { 104 chaincodeData := &ccprovider.ChaincodeData{Name: cc.Name, Version: cc.Version, Id: cc.Hash} 105 chaincodeDataBytes, err := proto.Marshal(chaincodeData) 106 assert.NoError(t, err) 107 lsccTable[cc.Name] = chaincodeDataBytes 108 109 if cc.ExplicitCollectionConfigPkg != nil { 110 collConfigPkgByte, err := proto.Marshal(cc.ExplicitCollectionConfigPkg) 111 assert.NoError(t, err) 112 lsccTable[privdata.BuildCollectionKVSKey(cc.Name)] = collConfigPkgByte 113 } 114 } 115 116 mockQE.GetStateStub = func(ns, key string) ([]byte, error) { 117 return lsccTable[key], nil 118 } 119 return mockQE 120 } 121 122 func prepapreCollectionConfigPkg(collNames []string) *peer.CollectionConfigPackage { 123 pkg := &peer.CollectionConfigPackage{} 124 for _, collName := range collNames { 125 sCollConfig := &peer.CollectionConfig_StaticCollectionConfig{ 126 StaticCollectionConfig: &peer.StaticCollectionConfig{ 127 Name: collName, 128 }, 129 } 130 config := &peer.CollectionConfig{Payload: sCollConfig} 131 pkg.Config = append(pkg.Config, config) 132 } 133 return pkg 134 }