github.com/renegr87/renegr87@v2.1.1+incompatible/core/common/privdata/store_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package privdata
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/hyperledger/fabric-protos-go/peer"
    13  	"github.com/hyperledger/fabric/common/policydsl"
    14  	"github.com/hyperledger/fabric/core/common/privdata/mock"
    15  	"github.com/hyperledger/fabric/core/ledger"
    16  	"github.com/hyperledger/fabric/protoutil"
    17  	"github.com/pkg/errors"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  // local interfaces to avoid import cycles
    22  //go:generate counterfeiter -o mock/query_executor_factory.go -fake-name QueryExecutorFactory . queryExecutorFactory
    23  //go:generate counterfeiter -o mock/chaincode_info_provider.go -fake-name ChaincodeInfoProvider . chaincodeInfoProvider
    24  //go:generate counterfeiter -o mock/identity_deserializer_factory.go -fake-name IdentityDeserializerFactory . identityDeserializerFactory
    25  //go:generate counterfeiter -o mock/query_executor.go -fake-name QueryExecutor . queryExecutor
    26  
    27  type queryExecutorFactory interface{ QueryExecutorFactory }
    28  type chaincodeInfoProvider interface{ ChaincodeInfoProvider }
    29  type identityDeserializerFactory interface{ IdentityDeserializerFactory }
    30  type queryExecutor interface{ ledger.QueryExecutor }
    31  
    32  func TestNewSimpleCollectionStore(t *testing.T) {
    33  	mockQueryExecutorFactory := &mock.QueryExecutorFactory{}
    34  	mockCCInfoProvider := &mock.ChaincodeInfoProvider{}
    35  
    36  	cs := NewSimpleCollectionStore(mockQueryExecutorFactory, mockCCInfoProvider)
    37  	assert.NotNil(t, cs)
    38  	assert.Exactly(t, mockQueryExecutorFactory, cs.qeFactory)
    39  	assert.Exactly(t, mockCCInfoProvider, cs.ccInfoProvider)
    40  	assert.NotNil(t, cs.idDeserializerFactory)
    41  }
    42  
    43  func TestCollectionStore(t *testing.T) {
    44  	mockQueryExecutorFactory := &mock.QueryExecutorFactory{}
    45  	mockCCInfoProvider := &mock.ChaincodeInfoProvider{}
    46  	mockCCInfoProvider.AllCollectionsConfigPkgReturns(nil, errors.New("Chaincode [non-existing-chaincode] does not exist"))
    47  	mockIDDeserializerFactory := &mock.IdentityDeserializerFactory{}
    48  	mockIDDeserializerFactory.GetIdentityDeserializerReturns(&mockDeserializer{})
    49  
    50  	cs := &SimpleCollectionStore{
    51  		qeFactory:             mockQueryExecutorFactory,
    52  		ccInfoProvider:        mockCCInfoProvider,
    53  		idDeserializerFactory: mockIDDeserializerFactory,
    54  	}
    55  
    56  	mockQueryExecutorFactory.NewQueryExecutorReturns(nil, errors.New("new-query-executor-failed"))
    57  	_, err := cs.RetrieveCollection(CollectionCriteria{})
    58  	assert.Contains(t, err.Error(), "could not retrieve query executor for collection criteria")
    59  
    60  	mockQueryExecutorFactory.NewQueryExecutorReturns(&mock.QueryExecutor{}, nil)
    61  	_, err = cs.retrieveCollectionConfigPackage(CollectionCriteria{Namespace: "non-existing-chaincode"}, nil)
    62  	assert.EqualError(t, err, "Chaincode [non-existing-chaincode] does not exist")
    63  
    64  	_, err = cs.RetrieveCollection(CollectionCriteria{})
    65  	assert.Contains(t, err.Error(), "could not be found")
    66  
    67  	ccr := CollectionCriteria{Channel: "ch", Namespace: "cc", Collection: "mycollection"}
    68  	mockCCInfoProvider.CollectionInfoReturns(nil, errors.New("collection-info-error"))
    69  	_, err = cs.RetrieveCollection(ccr)
    70  	assert.EqualError(t, err, "collection-info-error")
    71  
    72  	scc := &peer.StaticCollectionConfig{Name: "mycollection"}
    73  	mockCCInfoProvider.CollectionInfoReturns(scc, nil)
    74  	_, err = cs.RetrieveCollection(ccr)
    75  	assert.Contains(t, err.Error(), "error setting up collection for collection criteria")
    76  
    77  	var signers = [][]byte{[]byte("signer0"), []byte("signer1")}
    78  	policyEnvelope := policydsl.Envelope(policydsl.Or(policydsl.SignedBy(0), policydsl.SignedBy(1)), signers)
    79  	accessPolicy := createCollectionPolicyConfig(policyEnvelope)
    80  
    81  	scc = &peer.StaticCollectionConfig{
    82  		Name:             "mycollection",
    83  		MemberOrgsPolicy: accessPolicy,
    84  		MemberOnlyRead:   false,
    85  		MemberOnlyWrite:  false,
    86  	}
    87  
    88  	mockCCInfoProvider.CollectionInfoReturns(scc, nil)
    89  	c, err := cs.RetrieveCollection(ccr)
    90  	assert.NoError(t, err)
    91  	assert.NotNil(t, c)
    92  
    93  	ca, err := cs.RetrieveCollectionAccessPolicy(ccr)
    94  	assert.NoError(t, err)
    95  	assert.NotNil(t, ca)
    96  
    97  	scc = &peer.StaticCollectionConfig{
    98  		Name:             "mycollection",
    99  		MemberOrgsPolicy: accessPolicy,
   100  		MemberOnlyRead:   true,
   101  		MemberOnlyWrite:  true,
   102  	}
   103  	cc := &peer.CollectionConfig{
   104  		Payload: &peer.CollectionConfig_StaticCollectionConfig{StaticCollectionConfig: scc},
   105  	}
   106  	ccp := &peer.CollectionConfigPackage{Config: []*peer.CollectionConfig{cc}}
   107  
   108  	mockCCInfoProvider.CollectionInfoReturns(scc, nil)
   109  	mockCCInfoProvider.ChaincodeInfoReturns(
   110  		&ledger.DeployedChaincodeInfo{ExplicitCollectionConfigPkg: ccp},
   111  		nil,
   112  	)
   113  	mockCCInfoProvider.AllCollectionsConfigPkgReturns(ccp, nil)
   114  
   115  	ccc, err := cs.RetrieveCollectionConfigPackage(ccr)
   116  	assert.NoError(t, err)
   117  	assert.NotNil(t, ccc)
   118  
   119  	signedProp, _ := protoutil.MockSignedEndorserProposalOrPanic("A", &peer.ChaincodeSpec{}, []byte("signer0"), []byte("msg1"))
   120  	readP, writeP, err := cs.RetrieveReadWritePermission(ccr, signedProp, &mock.QueryExecutor{})
   121  	assert.NoError(t, err)
   122  	assert.True(t, readP)
   123  	assert.True(t, writeP)
   124  
   125  	// only signer0 and signer1 are the members
   126  	signedProp, _ = protoutil.MockSignedEndorserProposalOrPanic("A", &peer.ChaincodeSpec{}, []byte("signer2"), []byte("msg1"))
   127  	readP, writeP, err = cs.RetrieveReadWritePermission(ccr, signedProp, &mock.QueryExecutor{})
   128  	assert.NoError(t, err)
   129  	assert.False(t, readP)
   130  	assert.False(t, writeP)
   131  
   132  	scc = &peer.StaticCollectionConfig{
   133  		Name:             "mycollection",
   134  		MemberOrgsPolicy: accessPolicy,
   135  		MemberOnlyRead:   false,
   136  		MemberOnlyWrite:  false,
   137  	}
   138  	mockCCInfoProvider.CollectionInfoReturns(scc, nil)
   139  
   140  	// only signer0 and signer1 are the members
   141  	signedProp, _ = protoutil.MockSignedEndorserProposalOrPanic("A", &peer.ChaincodeSpec{}, []byte("signer2"), []byte("msg1"))
   142  	readP, writeP, err = cs.RetrieveReadWritePermission(ccr, signedProp, &mock.QueryExecutor{})
   143  	assert.NoError(t, err)
   144  	assert.True(t, readP)
   145  	assert.True(t, writeP)
   146  }