github.com/true-sqn/fabric@v2.1.1+incompatible/core/common/privdata/membershipinfo_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/msp"
    15  	"github.com/hyperledger/fabric/protoutil"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestMembershipInfoProvider(t *testing.T) {
    20  	mspID := "peer0"
    21  	peerSelfSignedData := protoutil.SignedData{
    22  		Identity:  []byte("peer0"),
    23  		Signature: []byte{1, 2, 3},
    24  		Data:      []byte{4, 5, 6},
    25  	}
    26  
    27  	identityDeserializer := func(chainID string) msp.IdentityDeserializer {
    28  		return &mockDeserializer{}
    29  	}
    30  
    31  	// verify membership provider returns true
    32  	membershipProvider := NewMembershipInfoProvider(mspID, peerSelfSignedData, identityDeserializer)
    33  	res, err := membershipProvider.AmMemberOf("test1", getAccessPolicy([]string{"peer0", "peer1"}))
    34  	assert.True(t, res)
    35  	assert.Nil(t, err)
    36  
    37  	// verify membership provider returns false
    38  	res, err = membershipProvider.AmMemberOf("test1", getAccessPolicy([]string{"peer2", "peer3"}))
    39  	assert.False(t, res)
    40  	assert.Nil(t, err)
    41  
    42  	// verify membership provider returns false and nil when collection policy config is nil
    43  	res, err = membershipProvider.AmMemberOf("test1", nil)
    44  	assert.False(t, res)
    45  	assert.Nil(t, err)
    46  
    47  	// verify membership provider returns false and nil when collection policy config is invalid
    48  	res, err = membershipProvider.AmMemberOf("test1", getBadAccessPolicy([]string{"signer0"}, 1))
    49  	assert.False(t, res)
    50  	assert.Nil(t, err)
    51  }
    52  
    53  func getAccessPolicy(signers []string) *peer.CollectionPolicyConfig {
    54  	var data [][]byte
    55  	for _, signer := range signers {
    56  		data = append(data, []byte(signer))
    57  	}
    58  	policyEnvelope := policydsl.Envelope(policydsl.Or(policydsl.SignedBy(0), policydsl.SignedBy(1)), data)
    59  	return createCollectionPolicyConfig(policyEnvelope)
    60  }
    61  
    62  func getBadAccessPolicy(signers []string, badIndex int32) *peer.CollectionPolicyConfig {
    63  	var data [][]byte
    64  	for _, signer := range signers {
    65  		data = append(data, []byte(signer))
    66  	}
    67  	// use a out of range index to trigger error
    68  	policyEnvelope := policydsl.Envelope(policydsl.Or(policydsl.SignedBy(0), policydsl.SignedBy(badIndex)), data)
    69  	return createCollectionPolicyConfig(policyEnvelope)
    70  }