github.com/yimialmonte/fabric@v2.1.1+incompatible/core/policy/application_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package policy 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-protos-go/common" 13 "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/hyperledger/fabric/common/cauthdsl" 15 "github.com/hyperledger/fabric/common/policydsl" 16 "github.com/hyperledger/fabric/core/policy/mocks" 17 "github.com/hyperledger/fabric/msp" 18 "github.com/hyperledger/fabric/protoutil" 19 "github.com/pkg/errors" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/mock" 22 ) 23 24 func TestComponentIntegrationSignaturePolicyEnv(t *testing.T) { 25 idds := &mocks.IdentityDeserializer{} 26 id := &mocks.Identity{} 27 28 spp := &cauthdsl.EnvelopeBasedPolicyProvider{ 29 Deserializer: idds, 30 } 31 ev := &ApplicationPolicyEvaluator{ 32 signaturePolicyProvider: spp, 33 } 34 35 spenv := policydsl.SignedByMspMember("msp") 36 mspenv := protoutil.MarshalOrPanic(&peer.ApplicationPolicy{ 37 Type: &peer.ApplicationPolicy_SignaturePolicy{ 38 SignaturePolicy: spenv, 39 }, 40 }) 41 42 idds.On("DeserializeIdentity", []byte("guess who")).Return(id, nil) 43 id.On("GetIdentifier").Return(&msp.IdentityIdentifier{Id: "id", Mspid: "msp"}) 44 id.On("SatisfiesPrincipal", mock.Anything).Return(nil) 45 id.On("Verify", []byte("batti"), []byte("lei")).Return(nil) 46 err := ev.Evaluate(mspenv, []*protoutil.SignedData{{ 47 Identity: []byte("guess who"), 48 Data: []byte("batti"), 49 Signature: []byte("lei"), 50 }}) 51 assert.NoError(t, err) 52 } 53 54 func TestEvaluator(t *testing.T) { 55 okEval := &mocks.Policy{} 56 nokEval := &mocks.Policy{} 57 58 okEval.On("EvaluateSignedData", mock.Anything).Return(nil) 59 nokEval.On("EvaluateSignedData", mock.Anything).Return(errors.New("bad bad")) 60 61 spp := &mocks.SignaturePolicyProvider{} 62 cpp := &mocks.ChannelPolicyReferenceProvider{} 63 ev := &ApplicationPolicyEvaluator{ 64 signaturePolicyProvider: spp, 65 channelPolicyReferenceProvider: cpp, 66 } 67 68 // SCENARIO: bad policy argument 69 70 err := ev.Evaluate([]byte("bad bad"), nil) 71 assert.Error(t, err) 72 assert.Contains(t, err.Error(), "failed to unmarshal ApplicationPolicy bytes") 73 74 // SCENARIO: bad policy type 75 76 err = ev.Evaluate([]byte{}, nil) 77 assert.Error(t, err) 78 assert.Contains(t, err.Error(), "unsupported policy type") 79 80 // SCENARIO: signature policy supplied - good and bad path 81 82 spenv := &common.SignaturePolicyEnvelope{} 83 mspenv := protoutil.MarshalOrPanic(&peer.ApplicationPolicy{ 84 Type: &peer.ApplicationPolicy_SignaturePolicy{ 85 SignaturePolicy: spenv, 86 }, 87 }) 88 spp.On("NewPolicy", spenv).Return(okEval, nil).Once() 89 err = ev.Evaluate(mspenv, nil) 90 assert.NoError(t, err) 91 spp.On("NewPolicy", spenv).Return(nokEval, nil).Once() 92 err = ev.Evaluate(mspenv, nil) 93 assert.Error(t, err) 94 assert.Contains(t, err.Error(), "bad bad") 95 spp.On("NewPolicy", spenv).Return(nil, errors.New("bad policy")).Once() 96 err = ev.Evaluate(mspenv, nil) 97 assert.Error(t, err) 98 assert.Contains(t, err.Error(), "bad policy") 99 100 // SCENARIO: channel ref policy supplied - good and bad path 101 102 chrefstr := "Quo usque tandem abutere, Catilina, patientia nostra?" 103 chrefstrEnv := protoutil.MarshalOrPanic(&peer.ApplicationPolicy{ 104 Type: &peer.ApplicationPolicy_ChannelConfigPolicyReference{ 105 ChannelConfigPolicyReference: chrefstr, 106 }, 107 }) 108 cpp.On("NewPolicy", chrefstr).Return(okEval, nil).Once() 109 err = ev.Evaluate(chrefstrEnv, nil) 110 assert.NoError(t, err) 111 cpp.On("NewPolicy", chrefstr).Return(nokEval, nil).Once() 112 err = ev.Evaluate(chrefstrEnv, nil) 113 assert.Error(t, err) 114 assert.Contains(t, err.Error(), "bad bad") 115 cpp.On("NewPolicy", chrefstr).Return(nil, errors.New("bad policy")).Once() 116 err = ev.Evaluate(chrefstrEnv, nil) 117 assert.Error(t, err) 118 assert.Contains(t, err.Error(), "bad policy") 119 } 120 121 func TestChannelPolicyReference(t *testing.T) { 122 mcpmg := &mocks.ChannelPolicyManagerGetter{} 123 mcpmg.On("Manager", "channel").Return(nil, false).Once() 124 ape, err := New(nil, "channel", mcpmg) 125 assert.Error(t, err) 126 assert.Nil(t, ape) 127 assert.Contains(t, err.Error(), "failed to retrieve policy manager for channel") 128 129 mm := &mocks.PolicyManager{} 130 mcpmg.On("Manager", "channel").Return(mm, true).Once() 131 ape, err = New(nil, "channel", mcpmg) 132 assert.NoError(t, err) 133 assert.NotNil(t, ape) 134 135 mcpmg.On("Manager", "channel").Return(mm, true) 136 137 mp := &mocks.Policy{} 138 mp.On("EvaluateSignedData", mock.Anything).Return(nil) 139 mm.On("GetPolicy", "As the sun breaks above the ground").Return(mp, true) 140 err = ape.evaluateChannelConfigPolicyReference("As the sun breaks above the ground", nil) 141 assert.NoError(t, err) 142 143 mm.On("GetPolicy", "An old man stands on the hill").Return(nil, false) 144 err = ape.evaluateChannelConfigPolicyReference("An old man stands on the hill", nil) 145 assert.Error(t, err) 146 assert.Contains(t, err.Error(), "failed to retrieve policy for reference") 147 }