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