github.com/kaituanwang/hyperledger@v2.0.1+incompatible/common/policies/inquire/compare_test.go (about) 1 /* 2 Copyright IBM Corp. 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/hyperledger/fabric-protos-go/msp" 13 "github.com/hyperledger/fabric/common/policies" 14 "github.com/hyperledger/fabric/protoutil" 15 "github.com/stretchr/testify/assert" 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 assert.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 assert.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 assert.Nil(t, NewComparablePrincipal(nil)) 48 }) 49 50 t.Run("Invalid principal type", func(t *testing.T) { 51 assert.Nil(t, NewComparablePrincipal(identity(mspID))) 52 }) 53 54 t.Run("Invalid principal input", func(t *testing.T) { 55 member := member(mspID) 56 member.Principal = append(member.Principal, 0) 57 assert.Nil(t, NewComparablePrincipal(member)) 58 59 ou := ou(mspID) 60 ou.Principal = append(ou.Principal, 0) 61 assert.Nil(t, NewComparablePrincipal(ou)) 62 }) 63 64 t.Run("Role", func(t *testing.T) { 65 expectedMember := &msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: mspID} 66 expectedPrincipal := &ComparablePrincipal{ 67 role: expectedMember, mspID: mspID, 68 principal: &msp.MSPPrincipal{ 69 PrincipalClassification: msp.MSPPrincipal_ROLE, 70 Principal: protoutil.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: mspID}), 71 }, 72 } 73 assert.Equal(t, expectedPrincipal, NewComparablePrincipal(member(mspID))) 74 }) 75 76 t.Run("OU", func(t *testing.T) { 77 expectedOURole := &msp.OrganizationUnit{OrganizationalUnitIdentifier: "ou", MspIdentifier: mspID} 78 expectedPrincipal := &ComparablePrincipal{ 79 ou: expectedOURole, 80 mspID: mspID, 81 principal: &msp.MSPPrincipal{ 82 PrincipalClassification: msp.MSPPrincipal_ORGANIZATION_UNIT, 83 Principal: protoutil.MarshalOrPanic(&msp.OrganizationUnit{OrganizationalUnitIdentifier: "ou", MspIdentifier: mspID}), 84 }, 85 } 86 assert.Equal(t, expectedPrincipal, NewComparablePrincipal(ou(mspID))) 87 }) 88 } 89 90 func TestIsA(t *testing.T) { 91 member1 := NewComparablePrincipal(member("Org1MSP")) 92 member2 := NewComparablePrincipal(member("Org2MSP")) 93 peer1 := NewComparablePrincipal(peer("Org1MSP")) 94 peer2 := NewComparablePrincipal(peer("Org2MSP")) 95 ou1 := NewComparablePrincipal(ou("Org1MSP")) 96 ou1ButDifferentIssuer := NewComparablePrincipal(ou("Org1MSP")) 97 ou1ButDifferentIssuer.ou.CertifiersIdentifier = []byte{1, 2, 3} 98 ou2 := NewComparablePrincipal(ou("Org2MSP")) 99 100 t.Run("Nil input", func(t *testing.T) { 101 assert.False(t, member1.IsA(nil)) 102 }) 103 104 t.Run("Incorrect state", func(t *testing.T) { 105 assert.False(t, (&ComparablePrincipal{}).IsA(member1)) 106 }) 107 108 t.Run("Same MSP ID", func(t *testing.T) { 109 assert.True(t, member1.IsA(NewComparablePrincipal(member("Org1MSP")))) 110 }) 111 112 t.Run("A peer is also a member", func(t *testing.T) { 113 assert.True(t, peer1.IsA(member1)) 114 }) 115 116 t.Run("A member isn't a peer", func(t *testing.T) { 117 assert.False(t, member1.IsA(peer1)) 118 }) 119 120 t.Run("Different MSP IDs", func(t *testing.T) { 121 assert.False(t, member1.IsA(member2)) 122 assert.False(t, peer2.IsA(peer1)) 123 }) 124 125 t.Run("An OU member is also a member", func(t *testing.T) { 126 assert.True(t, peer1.IsA(member1)) 127 }) 128 129 t.Run("A member isn't an OU member", func(t *testing.T) { 130 assert.False(t, member1.IsA(peer1)) 131 }) 132 133 t.Run("Same OU", func(t *testing.T) { 134 assert.True(t, ou1.IsA(NewComparablePrincipal(ou("Org1MSP")))) 135 }) 136 137 t.Run("Different OU", func(t *testing.T) { 138 assert.False(t, ou1.IsA(ou2)) 139 }) 140 141 t.Run("Same OU, different issuer", func(t *testing.T) { 142 assert.False(t, ou1.IsA(ou1ButDifferentIssuer)) 143 }) 144 145 t.Run("OUs and Peers aren't the same", func(t *testing.T) { 146 assert.False(t, ou1.IsA(peer1)) 147 }) 148 } 149 150 func TestIsFound(t *testing.T) { 151 member1 := NewComparablePrincipal(member("Org1MSP")) 152 member2 := NewComparablePrincipal(member("Org2MSP")) 153 peer1 := NewComparablePrincipal(peer("Org1MSP")) 154 155 assert.True(t, member1.IsFound(member1, member2)) 156 assert.False(t, member1.IsFound()) 157 assert.False(t, member1.IsFound(member2, peer1)) 158 assert.True(t, peer1.IsFound(member1, member2)) 159 } 160 161 func TestNewComparablePrincipalSet(t *testing.T) { 162 t.Run("Invalid principal", func(t *testing.T) { 163 principals := []*msp.MSPPrincipal{member("Org1MSP"), identity("Org1MSP")} 164 assert.Nil(t, NewComparablePrincipalSet(policies.PrincipalSet(principals))) 165 }) 166 167 t.Run("Valid Principals", func(t *testing.T) { 168 member1 := NewComparablePrincipal(member("Org1MSP")) 169 peer2 := NewComparablePrincipal(peer("Org2MSP")) 170 principals := []*msp.MSPPrincipal{member("Org1MSP"), peer("Org2MSP")} 171 cps := NewComparablePrincipalSet(policies.PrincipalSet(principals)) 172 expected := ComparablePrincipalSet([]*ComparablePrincipal{member1, peer2}) 173 assert.Equal(t, expected, cps) 174 }) 175 } 176 177 func member(orgName string) *msp.MSPPrincipal { 178 return &msp.MSPPrincipal{ 179 PrincipalClassification: msp.MSPPrincipal_ROLE, 180 Principal: protoutil.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: orgName})} 181 } 182 183 func peer(orgName string) *msp.MSPPrincipal { 184 return &msp.MSPPrincipal{ 185 PrincipalClassification: msp.MSPPrincipal_ROLE, 186 Principal: protoutil.MarshalOrPanic(&msp.MSPRole{Role: msp.MSPRole_PEER, MspIdentifier: orgName})} 187 } 188 189 func ou(orgName string) *msp.MSPPrincipal { 190 return &msp.MSPPrincipal{ 191 PrincipalClassification: msp.MSPPrincipal_ORGANIZATION_UNIT, 192 Principal: protoutil.MarshalOrPanic(&msp.OrganizationUnit{OrganizationalUnitIdentifier: "ou", MspIdentifier: orgName})} 193 } 194 195 func identity(orgName string) *msp.MSPPrincipal { 196 return &msp.MSPPrincipal{ 197 PrincipalClassification: msp.MSPPrincipal_IDENTITY, 198 Principal: protoutil.MarshalOrPanic(&msp.SerializedIdentity{Mspid: orgName, IdBytes: []byte("identity")}), 199 } 200 }