github.com/ewagmig/fabric@v2.1.1+incompatible/core/policy/mocks/mocks.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package mocks
     8  
     9  import (
    10  	"bytes"
    11  	"errors"
    12  	"fmt"
    13  	"time"
    14  
    15  	mspproto "github.com/hyperledger/fabric-protos-go/msp"
    16  	"github.com/hyperledger/fabric/common/policies"
    17  	"github.com/hyperledger/fabric/msp"
    18  	"github.com/hyperledger/fabric/protoutil"
    19  )
    20  
    21  type MockChannelPolicyManagerGetter struct {
    22  	Managers map[string]policies.Manager
    23  }
    24  
    25  func (c *MockChannelPolicyManagerGetter) Manager(channelID string) policies.Manager {
    26  	return c.Managers[channelID]
    27  }
    28  
    29  type MockChannelPolicyManager struct {
    30  	MockPolicy policies.Policy
    31  }
    32  
    33  func (m *MockChannelPolicyManager) GetPolicy(id string) (policies.Policy, bool) {
    34  	return m.MockPolicy, true
    35  }
    36  
    37  func (m *MockChannelPolicyManager) Manager(path []string) (policies.Manager, bool) {
    38  	panic("Not implemented")
    39  }
    40  
    41  type MockPolicy struct {
    42  	Deserializer msp.IdentityDeserializer
    43  }
    44  
    45  // EvaluateSignedData takes a set of SignedData and evaluates whether this set of signatures satisfies the policy
    46  func (m *MockPolicy) EvaluateSignedData(signatureSet []*protoutil.SignedData) error {
    47  	fmt.Printf("Evaluate [%s], [% x], [% x]\n", string(signatureSet[0].Identity), string(signatureSet[0].Data), string(signatureSet[0].Signature))
    48  	identity, err := m.Deserializer.DeserializeIdentity(signatureSet[0].Identity)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return identity.Verify(signatureSet[0].Data, signatureSet[0].Signature)
    54  }
    55  
    56  // EvaluateIdentities takes an array of identities and evaluates whether
    57  // they satisfy the policy
    58  func (m *MockPolicy) EvaluateIdentities(identities []msp.Identity) error {
    59  	return nil
    60  }
    61  
    62  type MockIdentityDeserializer struct {
    63  	Identity []byte
    64  	Msg      []byte
    65  }
    66  
    67  func (d *MockIdentityDeserializer) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
    68  	fmt.Printf("[DeserializeIdentity] id : [%s], [%s]\n", string(serializedIdentity), string(d.Identity))
    69  	if bytes.Equal(d.Identity, serializedIdentity) {
    70  		fmt.Printf("GOT : [%s], [%s]\n", string(serializedIdentity), string(d.Identity))
    71  		return &MockIdentity{identity: d.Identity, msg: d.Msg}, nil
    72  	}
    73  
    74  	return nil, errors.New("Invalid Identity")
    75  }
    76  
    77  func (d *MockIdentityDeserializer) IsWellFormed(_ *mspproto.SerializedIdentity) error {
    78  	return nil
    79  }
    80  
    81  type MockIdentity struct {
    82  	identity []byte
    83  	msg      []byte
    84  }
    85  
    86  func (id *MockIdentity) Anonymous() bool {
    87  	panic("implement me")
    88  }
    89  
    90  func (id *MockIdentity) SatisfiesPrincipal(p *mspproto.MSPPrincipal) error {
    91  	fmt.Printf("[SatisfiesPrincipal] id : [%s], [%s]\n", string(id.identity), string(p.Principal))
    92  	if !bytes.Equal(id.identity, p.Principal) {
    93  		return fmt.Errorf("Different identities [% x]!=[% x]", id.identity, p.Principal)
    94  	}
    95  	return nil
    96  }
    97  
    98  func (id *MockIdentity) ExpiresAt() time.Time {
    99  	return time.Time{}
   100  }
   101  
   102  func (id *MockIdentity) GetIdentifier() *msp.IdentityIdentifier {
   103  	return &msp.IdentityIdentifier{Mspid: "mock", Id: "mock"}
   104  }
   105  
   106  func (id *MockIdentity) GetMSPIdentifier() string {
   107  	return "mock"
   108  }
   109  
   110  func (id *MockIdentity) Validate() error {
   111  	return nil
   112  }
   113  
   114  func (id *MockIdentity) GetOrganizationalUnits() []*msp.OUIdentifier {
   115  	return nil
   116  }
   117  
   118  func (id *MockIdentity) Verify(msg []byte, sig []byte) error {
   119  	fmt.Printf("VERIFY [% x], [% x], [% x]\n", string(id.msg), string(msg), string(sig))
   120  	if bytes.Equal(id.msg, msg) {
   121  		if bytes.Equal(msg, sig) {
   122  			return nil
   123  		}
   124  	}
   125  
   126  	return errors.New("Invalid Signature")
   127  }
   128  
   129  func (id *MockIdentity) Serialize() ([]byte, error) {
   130  	return []byte("cert"), nil
   131  }
   132  
   133  type MockMSPPrincipalGetter struct {
   134  	Principal []byte
   135  }
   136  
   137  func (m *MockMSPPrincipalGetter) Get(role string) (*mspproto.MSPPrincipal, error) {
   138  	return &mspproto.MSPPrincipal{Principal: m.Principal}, nil
   139  }