github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/cauthdsl/policy_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package cauthdsl 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/golang/protobuf/proto" 14 cb "github.com/hyperledger/fabric-protos-go/common" 15 "github.com/hyperledger/fabric/common/policies" 16 "github.com/hyperledger/fabric/protoutil" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 var acceptAllPolicy *cb.Policy 21 var rejectAllPolicy *cb.Policy 22 23 func init() { 24 acceptAllPolicy = makePolicySource(true) 25 rejectAllPolicy = makePolicySource(false) 26 } 27 28 // The proto utils has become a dumping ground of cyclic imports, it's easier to define this locally 29 func marshalOrPanic(msg proto.Message) []byte { 30 data, err := proto.Marshal(msg) 31 if err != nil { 32 panic(fmt.Errorf("Error marshaling messages: %s, %s", msg, err)) 33 } 34 return data 35 } 36 37 func makePolicySource(policyResult bool) *cb.Policy { 38 var policyData *cb.SignaturePolicyEnvelope 39 if policyResult { 40 policyData = AcceptAllPolicy 41 } else { 42 policyData = RejectAllPolicy 43 } 44 return &cb.Policy{ 45 Type: int32(cb.Policy_SIGNATURE), 46 Value: marshalOrPanic(policyData), 47 } 48 } 49 50 func providerMap() map[int32]policies.Provider { 51 r := make(map[int32]policies.Provider) 52 r[int32(cb.Policy_SIGNATURE)] = NewPolicyProvider(&mockDeserializer{}) 53 return r 54 } 55 56 func TestAccept(t *testing.T) { 57 policyID := "policyID" 58 m, err := policies.NewManagerImpl("test", providerMap(), &cb.ConfigGroup{ 59 Policies: map[string]*cb.ConfigPolicy{ 60 policyID: {Policy: acceptAllPolicy}, 61 }, 62 }) 63 assert.NoError(t, err) 64 assert.NotNil(t, m) 65 66 policy, ok := m.GetPolicy(policyID) 67 assert.True(t, ok, "Should have found policy which was just added, but did not") 68 err = policy.EvaluateSignedData([]*protoutil.SignedData{{Identity: []byte("identity"), Data: []byte("data"), Signature: []byte("sig")}}) 69 assert.NoError(t, err, "Should not have errored evaluating an acceptAll policy") 70 } 71 72 func TestReject(t *testing.T) { 73 policyID := "policyID" 74 m, err := policies.NewManagerImpl("test", providerMap(), &cb.ConfigGroup{ 75 Policies: map[string]*cb.ConfigPolicy{ 76 policyID: {Policy: rejectAllPolicy}, 77 }, 78 }) 79 assert.NoError(t, err) 80 assert.NotNil(t, m) 81 policy, ok := m.GetPolicy(policyID) 82 assert.True(t, ok, "Should have found policy which was just added, but did not") 83 err = policy.EvaluateSignedData([]*protoutil.SignedData{{Identity: []byte("identity"), Data: []byte("data"), Signature: []byte("sig")}}) 84 assert.Error(t, err, "Should have errored evaluating an rejectAll policy") 85 } 86 87 func TestRejectOnUnknown(t *testing.T) { 88 m, err := policies.NewManagerImpl("test", providerMap(), &cb.ConfigGroup{}) 89 assert.NoError(t, err) 90 assert.NotNil(t, m) 91 policy, ok := m.GetPolicy("FakePolicyID") 92 assert.False(t, ok, "Should not have found policy which was never added, but did") 93 err = policy.EvaluateSignedData([]*protoutil.SignedData{{Identity: []byte("identity"), Data: []byte("data"), Signature: []byte("sig")}}) 94 assert.Error(t, err, "Should have errored evaluating the default policy") 95 } 96 97 func TestNewPolicyErrorCase(t *testing.T) { 98 provider := NewPolicyProvider(nil) 99 100 pol1, msg1, err1 := provider.NewPolicy([]byte{0}) 101 assert.Nil(t, pol1) 102 assert.Nil(t, msg1) 103 assert.EqualError(t, err1, "Error unmarshaling to SignaturePolicy: proto: common.SignaturePolicyEnvelope: illegal tag 0 (wire type 0)") 104 105 sigPolicy2 := &cb.SignaturePolicyEnvelope{Version: -1} 106 data2 := marshalOrPanic(sigPolicy2) 107 pol2, msg2, err2 := provider.NewPolicy(data2) 108 assert.Nil(t, pol2) 109 assert.Nil(t, msg2) 110 assert.EqualError(t, err2, "This evaluator only understands messages of version 0, but version was -1") 111 112 pol3, msg3, err3 := provider.NewPolicy([]byte{}) 113 assert.Nil(t, pol3) 114 assert.Nil(t, msg3) 115 assert.EqualError(t, err3, "Empty policy element") 116 117 var pol4 *policy 118 err4 := pol4.EvaluateSignedData([]*protoutil.SignedData{}) 119 assert.EqualError(t, err4, "no such policy") 120 } 121 122 func TestEnvelopeBasedPolicyProvider(t *testing.T) { 123 pp := &EnvelopeBasedPolicyProvider{Deserializer: &mockDeserializer{}} 124 p, err := pp.NewPolicy(nil) 125 assert.Nil(t, p) 126 assert.Error(t, err, "invalid arguments") 127 128 p, err = pp.NewPolicy(&cb.SignaturePolicyEnvelope{}) 129 assert.Nil(t, p) 130 assert.Error(t, err, "Empty policy element") 131 132 p, err = pp.NewPolicy(SignedByMspPeer("primus inter pares")) 133 assert.NotNil(t, p) 134 assert.NoError(t, err) 135 } 136 137 func TestConverter(t *testing.T) { 138 p := policy{} 139 140 cp, err := p.Convert() 141 assert.Nil(t, cp) 142 assert.Error(t, err) 143 assert.Contains(t, err.Error(), "nil policy field") 144 145 p.signaturePolicyEnvelope = RejectAllPolicy 146 147 cp, err = p.Convert() 148 assert.NotNil(t, cp) 149 assert.NoError(t, err) 150 assert.Equal(t, cp, RejectAllPolicy) 151 }