github.com/ewagmig/fabric@v2.1.1+incompatible/common/policies/implicitmeta_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package policies 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/hyperledger/fabric/msp" 14 15 cb "github.com/hyperledger/fabric-protos-go/common" 16 "github.com/hyperledger/fabric/protoutil" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 const TestPolicyName = "TestPolicyName" 21 22 type acceptPolicy struct{} 23 24 func (ap acceptPolicy) EvaluateSignedData(signedData []*protoutil.SignedData) error { 25 return nil 26 } 27 28 func (ap acceptPolicy) EvaluateIdentities(identity []msp.Identity) error { 29 return nil 30 } 31 32 func TestImplicitMarshalError(t *testing.T) { 33 _, err := NewImplicitMetaPolicy([]byte("GARBAGE"), nil) 34 assert.Error(t, err, "Should have errored unmarshaling garbage") 35 } 36 37 func makeManagers(count, passing int) map[string]*ManagerImpl { 38 result := make(map[string]*ManagerImpl) 39 remaining := passing 40 for i := 0; i < count; i++ { 41 policyMap := make(map[string]Policy) 42 if remaining > 0 { 43 policyMap[TestPolicyName] = acceptPolicy{} 44 } 45 remaining-- 46 47 result[fmt.Sprintf("%d", i)] = &ManagerImpl{ 48 Policies: policyMap, 49 } 50 } 51 return result 52 } 53 54 // makePolicyTest creates an implicitMetaPolicy with a set of 55 func runPolicyTest(t *testing.T, rule cb.ImplicitMetaPolicy_Rule, managerCount int, passingCount int) error { 56 imp, err := NewImplicitMetaPolicy(protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{ 57 Rule: rule, 58 SubPolicy: TestPolicyName, 59 }), makeManagers(managerCount, passingCount)) 60 if err != nil { 61 panic(err) 62 } 63 64 errSD := imp.EvaluateSignedData(nil) 65 66 imp, err = NewImplicitMetaPolicy(protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{ 67 Rule: rule, 68 SubPolicy: TestPolicyName, 69 }), makeManagers(managerCount, passingCount)) 70 if err != nil { 71 panic(err) 72 } 73 74 errI := imp.EvaluateIdentities(nil) 75 76 assert.False(t, ((errI == nil && errSD != nil) || (errSD == nil && errI != nil))) 77 if errI != nil && errSD != nil { 78 assert.Equal(t, errI.Error(), errSD.Error()) 79 } 80 81 return errI 82 } 83 84 func TestImplicitMetaAny(t *testing.T) { 85 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ANY, 1, 1)) 86 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ANY, 10, 1)) 87 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ANY, 10, 8)) 88 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ANY, 0, 0)) 89 90 err := runPolicyTest(t, cb.ImplicitMetaPolicy_ANY, 10, 0) 91 assert.EqualError(t, err, "implicit policy evaluation failed - 0 sub-policies were satisfied, but this policy requires 1 of the 'TestPolicyName' sub-policies to be satisfied") 92 } 93 94 func TestImplicitMetaAll(t *testing.T) { 95 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ALL, 1, 1)) 96 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ALL, 10, 10)) 97 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_ALL, 0, 0)) 98 99 err := runPolicyTest(t, cb.ImplicitMetaPolicy_ALL, 10, 1) 100 assert.EqualError(t, err, "implicit policy evaluation failed - 1 sub-policies were satisfied, but this policy requires 10 of the 'TestPolicyName' sub-policies to be satisfied") 101 102 err = runPolicyTest(t, cb.ImplicitMetaPolicy_ALL, 10, 0) 103 assert.EqualError(t, err, "implicit policy evaluation failed - 0 sub-policies were satisfied, but this policy requires 10 of the 'TestPolicyName' sub-policies to be satisfied") 104 } 105 106 func TestImplicitMetaMajority(t *testing.T) { 107 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_MAJORITY, 1, 1)) 108 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_MAJORITY, 10, 6)) 109 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_MAJORITY, 3, 2)) 110 assert.NoError(t, runPolicyTest(t, cb.ImplicitMetaPolicy_MAJORITY, 0, 0)) 111 112 err := runPolicyTest(t, cb.ImplicitMetaPolicy_MAJORITY, 10, 5) 113 assert.EqualError(t, err, "implicit policy evaluation failed - 5 sub-policies were satisfied, but this policy requires 6 of the 'TestPolicyName' sub-policies to be satisfied") 114 115 err = runPolicyTest(t, cb.ImplicitMetaPolicy_MAJORITY, 10, 0) 116 assert.EqualError(t, err, "implicit policy evaluation failed - 0 sub-policies were satisfied, but this policy requires 6 of the 'TestPolicyName' sub-policies to be satisfied") 117 }