github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/policy/mocks/mocks.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mocks 18 19 import ( 20 "bytes" 21 22 "fmt" 23 24 "errors" 25 26 "github.com/hyperledger/fabric/common/policies" 27 "github.com/hyperledger/fabric/msp" 28 "github.com/hyperledger/fabric/protos/common" 29 mspproto "github.com/hyperledger/fabric/protos/msp" 30 ) 31 32 type MockChannelPolicyManagerGetter struct { 33 Managers map[string]policies.Manager 34 } 35 36 func (c *MockChannelPolicyManagerGetter) Manager(channelID string) (policies.Manager, bool) { 37 return c.Managers[channelID], true 38 } 39 40 type MockChannelPolicyManager struct { 41 MockPolicy policies.Policy 42 } 43 44 func (m *MockChannelPolicyManager) GetPolicy(id string) (policies.Policy, bool) { 45 return m.MockPolicy, true 46 } 47 48 func (m *MockChannelPolicyManager) Manager(path []string) (policies.Manager, bool) { 49 panic("Not implemented") 50 } 51 52 func (m *MockChannelPolicyManager) BasePath() string { 53 panic("Not implemented") 54 } 55 56 func (m *MockChannelPolicyManager) PolicyNames() []string { 57 panic("Not implemented") 58 } 59 60 type MockPolicy struct { 61 Deserializer msp.IdentityDeserializer 62 } 63 64 // Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy 65 func (m *MockPolicy) Evaluate(signatureSet []*common.SignedData) error { 66 fmt.Printf("Evaluate [%s], [% x], [% x]\n", string(signatureSet[0].Identity), string(signatureSet[0].Data), string(signatureSet[0].Signature)) 67 identity, err := m.Deserializer.DeserializeIdentity(signatureSet[0].Identity) 68 if err != nil { 69 return err 70 } 71 72 return identity.Verify(signatureSet[0].Data, signatureSet[0].Signature) 73 } 74 75 type MockIdentityDeserializer struct { 76 Identity []byte 77 Msg []byte 78 } 79 80 func (d *MockIdentityDeserializer) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) { 81 fmt.Printf("[DeserializeIdentity] id : [%s], [%s]\n", string(serializedIdentity), string(d.Identity)) 82 if bytes.Equal(d.Identity, serializedIdentity) { 83 fmt.Printf("GOT : [%s], [%s]\n", string(serializedIdentity), string(d.Identity)) 84 return &MockIdentity{identity: d.Identity, msg: d.Msg}, nil 85 } 86 87 return nil, errors.New("Invalid Identity") 88 } 89 90 type MockIdentity struct { 91 identity []byte 92 msg []byte 93 } 94 95 func (id *MockIdentity) SatisfiesPrincipal(p *mspproto.MSPPrincipal) error { 96 fmt.Printf("[SatisfiesPrincipal] id : [%s], [%s]\n", string(id.identity), string(p.Principal)) 97 if !bytes.Equal(id.identity, p.Principal) { 98 return fmt.Errorf("Different identities [% x]!=[% x]", id.identity, p.Principal) 99 } 100 return nil 101 } 102 103 func (id *MockIdentity) GetIdentifier() *msp.IdentityIdentifier { 104 return &msp.IdentityIdentifier{Mspid: "mock", Id: "mock"} 105 } 106 107 func (id *MockIdentity) GetMSPIdentifier() string { 108 return "mock" 109 } 110 111 func (id *MockIdentity) Validate() error { 112 return nil 113 } 114 115 func (id *MockIdentity) GetOrganizationalUnits() []*msp.OUIdentifier { 116 return nil 117 } 118 119 func (id *MockIdentity) Verify(msg []byte, sig []byte) error { 120 fmt.Printf("VERIFY [% x], [% x], [% x]\n", string(id.msg), string(msg), string(sig)) 121 if bytes.Equal(id.msg, msg) { 122 if bytes.Equal(msg, sig) { 123 return nil 124 } 125 } 126 127 return errors.New("Invalid Signature") 128 } 129 130 func (id *MockIdentity) Serialize() ([]byte, error) { 131 return []byte("cert"), nil 132 } 133 134 type MockMSPPrincipalGetter struct { 135 Principal []byte 136 } 137 138 func (m *MockMSPPrincipalGetter) Get(role string) (*mspproto.MSPPrincipal, error) { 139 return &mspproto.MSPPrincipal{Principal: m.Principal}, nil 140 }