github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/cauthdsl/policy_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cauthdsl 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/hyperledger/fabric/common/policies" 24 cb "github.com/hyperledger/fabric/protos/common" 25 26 "github.com/golang/protobuf/proto" 27 ) 28 29 var acceptAllPolicy *cb.Policy 30 var rejectAllPolicy *cb.Policy 31 32 func init() { 33 acceptAllPolicy = makePolicySource(true) 34 rejectAllPolicy = makePolicySource(false) 35 } 36 37 // The proto utils has become a dumping ground of cyclic imports, it's easier to define this locally 38 func marshalOrPanic(msg proto.Message) []byte { 39 data, err := proto.Marshal(msg) 40 if err != nil { 41 panic(fmt.Errorf("Error marshaling messages: %s, %s", msg, err)) 42 } 43 return data 44 } 45 46 func makePolicySource(policyResult bool) *cb.Policy { 47 var policyData *cb.SignaturePolicyEnvelope 48 if policyResult { 49 policyData = AcceptAllPolicy 50 } else { 51 policyData = RejectAllPolicy 52 } 53 return &cb.Policy{ 54 Type: int32(cb.Policy_SIGNATURE), 55 Policy: marshalOrPanic(policyData), 56 } 57 } 58 59 func addPolicy(manager policies.Proposer, id string, policy *cb.Policy) { 60 manager.BeginPolicyProposals(id, nil) 61 _, err := manager.ProposePolicy(id, id, &cb.ConfigPolicy{ 62 Policy: policy, 63 }) 64 if err != nil { 65 panic(err) 66 } 67 manager.CommitProposals(id) 68 } 69 70 func providerMap() map[int32]policies.Provider { 71 r := make(map[int32]policies.Provider) 72 r[int32(cb.Policy_SIGNATURE)] = NewPolicyProvider(&mockDeserializer{}) 73 return r 74 } 75 76 func TestAccept(t *testing.T) { 77 policyID := "policyID" 78 m := policies.NewManagerImpl("test", providerMap()) 79 addPolicy(m, policyID, acceptAllPolicy) 80 policy, ok := m.GetPolicy(policyID) 81 if !ok { 82 t.Error("Should have found policy which was just added, but did not") 83 } 84 err := policy.Evaluate([]*cb.SignedData{}) 85 if err != nil { 86 t.Fatalf("Should not have errored evaluating an acceptAll policy: %s", err) 87 } 88 } 89 90 func TestReject(t *testing.T) { 91 policyID := "policyID" 92 m := policies.NewManagerImpl("test", providerMap()) 93 addPolicy(m, policyID, rejectAllPolicy) 94 policy, ok := m.GetPolicy(policyID) 95 if !ok { 96 t.Error("Should have found policy which was just added, but did not") 97 } 98 err := policy.Evaluate([]*cb.SignedData{}) 99 if err == nil { 100 t.Fatal("Should have errored evaluating the rejectAll policy") 101 } 102 } 103 104 func TestRejectOnUnknown(t *testing.T) { 105 m := policies.NewManagerImpl("test", providerMap()) 106 policy, ok := m.GetPolicy("FakePolicyID") 107 if ok { 108 t.Error("Should not have found policy which was never added, but did") 109 } 110 err := policy.Evaluate([]*cb.SignedData{}) 111 if err == nil { 112 t.Fatal("Should have errored evaluating the default policy") 113 } 114 }