github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/policies/inquire/compare_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package inquire
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/hechain20/hechain/common/policies"
    13  	"github.com/hechain20/hechain/protoutil"
    14  	"github.com/hyperledger/fabric-protos-go/msp"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestToPrincipalSet(t *testing.T) {
    19  	var cps ComparablePrincipalSet
    20  	cps = append(cps, NewComparablePrincipal(member("Org1MSP")))
    21  	cps = append(cps, NewComparablePrincipal(member("Org2MSP")))
    22  	expected := policies.PrincipalSet{member("Org1MSP"), member("Org2MSP")}
    23  	require.Equal(t, expected, cps.ToPrincipalSet())
    24  }
    25  
    26  func TestToPrincipalSets(t *testing.T) {
    27  	var cps, cps2 ComparablePrincipalSet
    28  
    29  	cps = append(cps, NewComparablePrincipal(member("Org1MSP")))
    30  	cps = append(cps, NewComparablePrincipal(member("Org2MSP")))
    31  
    32  	cps2 = append(cps2, NewComparablePrincipal(member("Org3MSP")))
    33  	cps2 = append(cps2, NewComparablePrincipal(member("Org4MSP")))
    34  
    35  	expected := policies.PrincipalSets{
    36  		{member("Org1MSP"), member("Org2MSP")},
    37  		{member("Org3MSP"), member("Org4MSP")},
    38  	}
    39  
    40  	require.Equal(t, expected, ComparablePrincipalSets{cps, cps2}.ToPrincipalSets())
    41  }
    42  
    43  func TestNewComparablePrincipal(t *testing.T) {
    44  	mspID := "Org1MSP"
    45  
    46  	t.Run("Nil input", func(t *testing.T) {
    47  		require.Nil(t, NewComparablePrincipal(nil))
    48  	})
    49  
    50  	t.Run("Identity", func(t *testing.T) {
    51  		expectedPrincipal := &ComparablePrincipal{
    52  			mspID:   mspID,
    53  			idBytes: []byte("identity"),
    54  			principal: &msp.MSPPrincipal{
    55  				PrincipalClassification: msp.MSPPrincipal_IDENTITY,
    56  				Principal:               protoutil.MarshalOrPanic(&msp.SerializedIdentity{IdBytes: []byte("identity"), Mspid: mspID}),
    57  			},
    58  		}
    59  
    60  		require.Equal(t, expectedPrincipal, NewComparablePrincipal(identity(mspID, []byte("identity"))))
    61  	})
    62  
    63  	t.Run("Invalid principal input", func(t *testing.T) {
    64  		member := member(mspID)
    65  		member.Principal = append(member.Principal, 0)
    66  		require.Nil(t, NewComparablePrincipal(member))
    67  
    68  		ou := ou(mspID)
    69  		ou.Principal = append(ou.Principal, 0)
    70  		require.Nil(t, NewComparablePrincipal(ou))
    71  	})
    72  
    73  	t.Run("Role", func(t *testing.T) {
    74  		expectedMember := &msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: mspID}
    75  		expectedPrincipal := &ComparablePrincipal{
    76  			role: expectedMember, mspID: mspID,
    77  			principal: &msp.MSPPrincipal{
    78  				PrincipalClassification: msp.MSPPrincipal_ROLE,
    79  				Principal:               protoutil.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: mspID}),
    80  			},
    81  		}
    82  		require.Equal(t, expectedPrincipal, NewComparablePrincipal(member(mspID)))
    83  	})
    84  
    85  	t.Run("OU", func(t *testing.T) {
    86  		expectedOURole := &msp.OrganizationUnit{OrganizationalUnitIdentifier: "ou", MspIdentifier: mspID}
    87  		expectedPrincipal := &ComparablePrincipal{
    88  			ou:    expectedOURole,
    89  			mspID: mspID,
    90  			principal: &msp.MSPPrincipal{
    91  				PrincipalClassification: msp.MSPPrincipal_ORGANIZATION_UNIT,
    92  				Principal:               protoutil.MarshalOrPanic(&msp.OrganizationUnit{OrganizationalUnitIdentifier: "ou", MspIdentifier: mspID}),
    93  			},
    94  		}
    95  		require.Equal(t, expectedPrincipal, NewComparablePrincipal(ou(mspID)))
    96  	})
    97  }
    98  
    99  func TestIsA(t *testing.T) {
   100  	id1 := NewComparablePrincipal(identity("Org1MSP", []byte("identity")))
   101  	id2 := NewComparablePrincipal(identity("Org2MSP", []byte("identity")))
   102  	id3 := NewComparablePrincipal(identity("Org1MSP", []byte("identity2")))
   103  	member1 := NewComparablePrincipal(member("Org1MSP"))
   104  	member2 := NewComparablePrincipal(member("Org2MSP"))
   105  	peer1 := NewComparablePrincipal(peer("Org1MSP"))
   106  	peer2 := NewComparablePrincipal(peer("Org2MSP"))
   107  	ou1 := NewComparablePrincipal(ou("Org1MSP"))
   108  	ou1ButDifferentIssuer := NewComparablePrincipal(ou("Org1MSP"))
   109  	ou1ButDifferentIssuer.ou.CertifiersIdentifier = []byte{1, 2, 3}
   110  	ou2 := NewComparablePrincipal(ou("Org2MSP"))
   111  
   112  	t.Run("Nil input", func(t *testing.T) {
   113  		require.False(t, member1.IsA(nil))
   114  	})
   115  
   116  	t.Run("Incorrect state", func(t *testing.T) {
   117  		require.False(t, (&ComparablePrincipal{}).IsA(member1))
   118  	})
   119  
   120  	t.Run("Same MSP ID", func(t *testing.T) {
   121  		require.True(t, member1.IsA(NewComparablePrincipal(member("Org1MSP"))))
   122  	})
   123  
   124  	t.Run("A peer is also a member", func(t *testing.T) {
   125  		require.True(t, peer1.IsA(member1))
   126  	})
   127  
   128  	t.Run("A member isn't a peer", func(t *testing.T) {
   129  		require.False(t, member1.IsA(peer1))
   130  	})
   131  
   132  	t.Run("Different MSP IDs", func(t *testing.T) {
   133  		require.False(t, member1.IsA(member2))
   134  		require.False(t, peer2.IsA(peer1))
   135  	})
   136  
   137  	t.Run("An OU member is also a member", func(t *testing.T) {
   138  		require.True(t, peer1.IsA(member1))
   139  	})
   140  
   141  	t.Run("A member isn't an OU member", func(t *testing.T) {
   142  		require.False(t, member1.IsA(peer1))
   143  	})
   144  
   145  	t.Run("Same OU", func(t *testing.T) {
   146  		require.True(t, ou1.IsA(NewComparablePrincipal(ou("Org1MSP"))))
   147  	})
   148  
   149  	t.Run("Different OU", func(t *testing.T) {
   150  		require.False(t, ou1.IsA(ou2))
   151  	})
   152  
   153  	t.Run("Same OU, different issuer", func(t *testing.T) {
   154  		require.False(t, ou1.IsA(ou1ButDifferentIssuer))
   155  	})
   156  
   157  	t.Run("OUs and Peers aren't the same", func(t *testing.T) {
   158  		require.False(t, ou1.IsA(peer1))
   159  	})
   160  	t.Run("Same identity", func(t *testing.T) {
   161  		require.True(t, id1.IsA(id1))
   162  	})
   163  	t.Run("Different identity bytes", func(t *testing.T) {
   164  		require.False(t, id1.IsA(id3))
   165  	})
   166  	t.Run("Different MSP ID", func(t *testing.T) {
   167  		require.False(t, id1.IsA(id2))
   168  	})
   169  }
   170  
   171  func TestIsFound(t *testing.T) {
   172  	member1 := NewComparablePrincipal(member("Org1MSP"))
   173  	member2 := NewComparablePrincipal(member("Org2MSP"))
   174  	peer1 := NewComparablePrincipal(peer("Org1MSP"))
   175  
   176  	require.True(t, member1.IsFound(member1, member2))
   177  	require.False(t, member1.IsFound())
   178  	require.False(t, member1.IsFound(member2, peer1))
   179  	require.True(t, peer1.IsFound(member1, member2))
   180  }
   181  
   182  func TestNewComparablePrincipalSet(t *testing.T) {
   183  	t.Run("Invalid principal", func(t *testing.T) {
   184  		idCP := NewComparablePrincipal(identity("Org1MSP", []byte("identity")))
   185  		principals := []*msp.MSPPrincipal{member("Org1MSP"), identity("Org1MSP", []byte("identity"))}
   186  		cps := NewComparablePrincipalSet(principals)
   187  		expected := ComparablePrincipalSet([]*ComparablePrincipal{member1, idCP})
   188  		require.Equal(t, expected, cps)
   189  	})
   190  
   191  	t.Run("Valid Principals", func(t *testing.T) {
   192  		member1 := NewComparablePrincipal(member("Org1MSP"))
   193  		peer2 := NewComparablePrincipal(peer("Org2MSP"))
   194  		principals := []*msp.MSPPrincipal{member("Org1MSP"), peer("Org2MSP")}
   195  		cps := NewComparablePrincipalSet(policies.PrincipalSet(principals))
   196  		expected := ComparablePrincipalSet([]*ComparablePrincipal{member1, peer2})
   197  		require.Equal(t, expected, cps)
   198  	})
   199  }
   200  
   201  func member(orgName string) *msp.MSPPrincipal {
   202  	return &msp.MSPPrincipal{
   203  		PrincipalClassification: msp.MSPPrincipal_ROLE,
   204  		Principal:               protoutil.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: orgName}),
   205  	}
   206  }
   207  
   208  func peer(orgName string) *msp.MSPPrincipal {
   209  	return &msp.MSPPrincipal{
   210  		PrincipalClassification: msp.MSPPrincipal_ROLE,
   211  		Principal:               protoutil.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_PEER, MspIdentifier: orgName}),
   212  	}
   213  }
   214  
   215  func ou(orgName string) *msp.MSPPrincipal {
   216  	return &msp.MSPPrincipal{
   217  		PrincipalClassification: msp.MSPPrincipal_ORGANIZATION_UNIT,
   218  		Principal:               protoutil.MarshalOrPanic(&msp.OrganizationUnit{OrganizationalUnitIdentifier: "ou", MspIdentifier: orgName}),
   219  	}
   220  }
   221  
   222  func identity(orgName string, idBytes []byte) *msp.MSPPrincipal {
   223  	return &msp.MSPPrincipal{
   224  		PrincipalClassification: msp.MSPPrincipal_IDENTITY,
   225  		Principal:               protoutil.MarshalOrPanic(&msp.SerializedIdentity{Mspid: orgName, IdBytes: idBytes}),
   226  	}
   227  }