github.com/ewagmig/fabric@v2.1.1+incompatible/core/chaincode/lifecycle/metadata_provider_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package lifecycle_test 8 9 import ( 10 "github.com/hyperledger/fabric-protos-go/common" 11 "github.com/hyperledger/fabric-protos-go/msp" 12 pb "github.com/hyperledger/fabric-protos-go/peer" 13 lb "github.com/hyperledger/fabric-protos-go/peer/lifecycle" 14 "github.com/hyperledger/fabric/common/chaincode" 15 "github.com/hyperledger/fabric/common/policydsl" 16 "github.com/hyperledger/fabric/core/chaincode/lifecycle" 17 "github.com/hyperledger/fabric/core/chaincode/lifecycle/mock" 18 "github.com/pkg/errors" 19 20 "github.com/hyperledger/fabric/protoutil" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("MetadataProvider", func() { 26 var ( 27 fakeChaincodeInfoProvider *mock.ChaincodeInfoProvider 28 fakeLegacyMetadataProvider *mock.LegacyMetadataProvider 29 fakeChannelPolicyReferenceProvider *mock.ChannelPolicyReferenceProvider 30 fakeConvertedPolicy *mock.ConvertiblePolicy 31 metadataProvider *lifecycle.MetadataProvider 32 ccInfo *lifecycle.LocalChaincodeInfo 33 ) 34 35 BeforeEach(func() { 36 fakeChaincodeInfoProvider = &mock.ChaincodeInfoProvider{} 37 ccInfo = &lifecycle.LocalChaincodeInfo{ 38 Definition: &lifecycle.ChaincodeDefinition{ 39 Sequence: 1, 40 EndorsementInfo: &lb.ChaincodeEndorsementInfo{ 41 Version: "cc-version", 42 }, 43 ValidationInfo: &lb.ChaincodeValidationInfo{ 44 ValidationParameter: protoutil.MarshalOrPanic(&pb.ApplicationPolicy{ 45 Type: &pb.ApplicationPolicy_SignaturePolicy{ 46 SignaturePolicy: policydsl.AcceptAllPolicy, 47 }, 48 }), 49 }, 50 Collections: &pb.CollectionConfigPackage{}, 51 }, 52 } 53 fakeChaincodeInfoProvider.ChaincodeInfoReturns(ccInfo, nil) 54 55 legacyCCMetadata := &chaincode.Metadata{ 56 Name: "legacy-cc", 57 Version: "legacy-version", 58 Policy: []byte("legacy-policy"), 59 CollectionPolicies: nil, 60 CollectionsConfig: &pb.CollectionConfigPackage{}, 61 } 62 fakeLegacyMetadataProvider = &mock.LegacyMetadataProvider{} 63 fakeLegacyMetadataProvider.MetadataReturns(legacyCCMetadata) 64 65 metadataProvider = lifecycle.NewMetadataProvider(fakeChaincodeInfoProvider, fakeLegacyMetadataProvider, nil) 66 }) 67 68 It("returns metadata using the ChaincodeInfoProvider (SignaturePolicyEnvelope case)", func() { 69 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1") 70 Expect(metadata).To(Equal( 71 &chaincode.Metadata{ 72 Name: "cc-name", 73 Version: "1", 74 Policy: policydsl.MarshaledAcceptAllPolicy, 75 CollectionPolicies: map[string][]byte{}, 76 CollectionsConfig: &pb.CollectionConfigPackage{}, 77 }, 78 )) 79 }) 80 81 Context("when the chaincode is not found by the ChaincodeInfoProvider", func() { 82 BeforeEach(func() { 83 fakeChaincodeInfoProvider.ChaincodeInfoReturns(nil, errors.New("scrumtrulescent")) 84 }) 85 86 It("returns metadata using the LegacyMetadataProvider", func() { 87 metadata := metadataProvider.Metadata("testchannel", "legacy-cc", "col1") 88 Expect(metadata).To(Equal( 89 &chaincode.Metadata{ 90 Name: "legacy-cc", 91 Version: "legacy-version", 92 Policy: []byte("legacy-policy"), 93 CollectionPolicies: nil, 94 CollectionsConfig: &pb.CollectionConfigPackage{}, 95 }, 96 )) 97 }) 98 }) 99 100 Context("when the policy is bad", func() { 101 BeforeEach(func() { 102 ccInfo.Definition.ValidationInfo.ValidationParameter = []byte{0, 1, 2} 103 fakeChaincodeInfoProvider.ChaincodeInfoReturns(ccInfo, nil) 104 }) 105 106 It("returns nil", func() { 107 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1") 108 Expect(metadata).To(BeNil()) 109 }) 110 }) 111 112 Context("when the policy is of the channel reference type", func() { 113 BeforeEach(func() { 114 ccInfo.Definition.ValidationInfo.ValidationParameter = protoutil.MarshalOrPanic( 115 &pb.ApplicationPolicy{ 116 Type: &pb.ApplicationPolicy_ChannelConfigPolicyReference{ 117 ChannelConfigPolicyReference: "barf", 118 }, 119 }) 120 fakeChaincodeInfoProvider.ChaincodeInfoReturns(ccInfo, nil) 121 122 fakeConvertedPolicy = &mock.ConvertiblePolicy{} 123 fakeConvertedPolicy.ConvertReturns(policydsl.AcceptAllPolicy, nil) 124 125 fakeChannelPolicyReferenceProvider = &mock.ChannelPolicyReferenceProvider{} 126 fakeChannelPolicyReferenceProvider.NewPolicyReturns(fakeConvertedPolicy, nil) 127 metadataProvider.ChannelPolicyReferenceProvider = fakeChannelPolicyReferenceProvider 128 }) 129 130 It("returns metadata after translating the policy", func() { 131 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1") 132 Expect(metadata).To(Equal( 133 &chaincode.Metadata{ 134 Name: "cc-name", 135 Version: "1", 136 Policy: policydsl.MarshaledAcceptAllPolicy, 137 CollectionPolicies: map[string][]byte{}, 138 CollectionsConfig: &pb.CollectionConfigPackage{}, 139 }, 140 )) 141 }) 142 143 Context("when NewPolicy returns an error", func() { 144 BeforeEach(func() { 145 fakeChannelPolicyReferenceProvider = &mock.ChannelPolicyReferenceProvider{} 146 fakeChannelPolicyReferenceProvider.NewPolicyReturns(nil, errors.New("go away")) 147 metadataProvider.ChannelPolicyReferenceProvider = fakeChannelPolicyReferenceProvider 148 }) 149 150 It("returns nil", func() { 151 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1") 152 Expect(metadata).To(BeNil()) 153 }) 154 }) 155 156 Context("when the policy is not convertible", func() { 157 BeforeEach(func() { 158 fakeChannelPolicyReferenceProvider = &mock.ChannelPolicyReferenceProvider{} 159 fakeChannelPolicyReferenceProvider.NewPolicyReturns(&mock.InconvertiblePolicy{}, nil) 160 metadataProvider.ChannelPolicyReferenceProvider = fakeChannelPolicyReferenceProvider 161 }) 162 163 It("returns nil", func() { 164 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1") 165 Expect(metadata).To(BeNil()) 166 }) 167 }) 168 169 Context("when conversion fails", func() { 170 BeforeEach(func() { 171 fakeConvertedPolicy = &mock.ConvertiblePolicy{} 172 fakeConvertedPolicy.ConvertReturns(nil, errors.New("go away")) 173 174 fakeChannelPolicyReferenceProvider = &mock.ChannelPolicyReferenceProvider{} 175 fakeChannelPolicyReferenceProvider.NewPolicyReturns(fakeConvertedPolicy, nil) 176 metadataProvider.ChannelPolicyReferenceProvider = fakeChannelPolicyReferenceProvider 177 }) 178 179 It("returns nil", func() { 180 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1") 181 Expect(metadata).To(BeNil()) 182 }) 183 }) 184 }) 185 186 Context("when collection endorsement policies exist", func() { 187 var ( 188 expectedSignaturePolicy *common.SignaturePolicyEnvelope 189 expectedCollectionConfigPackage *pb.CollectionConfigPackage 190 ) 191 192 BeforeEach(func() { 193 expectedSignaturePolicy = &common.SignaturePolicyEnvelope{ 194 Identities: []*msp.MSPPrincipal{ 195 { 196 Principal: []byte("test"), 197 }, 198 }, 199 } 200 201 expectedCollectionConfigPackage = &pb.CollectionConfigPackage{ 202 Config: []*pb.CollectionConfig{ 203 { 204 Payload: &pb.CollectionConfig_StaticCollectionConfig{ 205 StaticCollectionConfig: &pb.StaticCollectionConfig{ 206 Name: "col1", 207 EndorsementPolicy: &pb.ApplicationPolicy{ 208 Type: &pb.ApplicationPolicy_SignaturePolicy{ 209 SignaturePolicy: expectedSignaturePolicy, 210 }, 211 }, 212 }, 213 }, 214 }, 215 { 216 Payload: &pb.CollectionConfig_StaticCollectionConfig{ 217 StaticCollectionConfig: &pb.StaticCollectionConfig{ 218 Name: "col2", 219 EndorsementPolicy: &pb.ApplicationPolicy{ 220 Type: &pb.ApplicationPolicy_SignaturePolicy{ 221 SignaturePolicy: expectedSignaturePolicy, 222 }, 223 }, 224 }, 225 }, 226 }, 227 { 228 Payload: &pb.CollectionConfig_StaticCollectionConfig{ 229 StaticCollectionConfig: &pb.StaticCollectionConfig{ 230 Name: "col3", 231 // col3 has no endorsement policy 232 EndorsementPolicy: nil, 233 }, 234 }, 235 }, 236 }, 237 } 238 ccInfo = &lifecycle.LocalChaincodeInfo{ 239 Definition: &lifecycle.ChaincodeDefinition{ 240 Sequence: 1, 241 EndorsementInfo: &lb.ChaincodeEndorsementInfo{ 242 Version: "cc-version", 243 }, 244 ValidationInfo: &lb.ChaincodeValidationInfo{ 245 ValidationParameter: protoutil.MarshalOrPanic(&pb.ApplicationPolicy{ 246 Type: &pb.ApplicationPolicy_SignaturePolicy{ 247 SignaturePolicy: policydsl.AcceptAllPolicy, 248 }, 249 }), 250 }, 251 Collections: expectedCollectionConfigPackage, 252 }, 253 } 254 fakeChaincodeInfoProvider.ChaincodeInfoReturns(ccInfo, nil) 255 }) 256 257 It("returns metadata that includes the collection policies", func() { 258 metadata := metadataProvider.Metadata("testchannel", "cc-name", "col1", "col2", "col3") 259 Expect(metadata).To(Equal( 260 &chaincode.Metadata{ 261 Name: "cc-name", 262 Version: "1", 263 Policy: policydsl.MarshaledAcceptAllPolicy, 264 CollectionPolicies: map[string][]byte{ 265 "col1": protoutil.MarshalOrPanic(expectedSignaturePolicy), 266 "col2": protoutil.MarshalOrPanic(expectedSignaturePolicy), 267 // col3 has no endorsement policy 268 }, 269 CollectionsConfig: expectedCollectionConfigPackage, 270 }, 271 )) 272 }) 273 }) 274 })