Debian Codegraph @

actions/policy_test.go

raw
1 /*
2 * policy_test.go - tests for creating and modifying policies
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 package actions
21
22 import (
23 "testing"
24
25 "github.com/pkg/errors"
26 )
27
28 // Makes a protector and policy
29 func makeBoth() (*Protector, *Policy, error) {
30 protector, err := CreateProtector(testContext, testProtectorName, goodCallback, nil)
31 if err != nil {
32 return nil, nil, err
33 }
34 policy, err := CreatePolicy(testContext, protector)
35 if err != nil {
36 cleanupProtector(protector)
37 return nil, nil, err
38 }
39 return protector, policy, nil
40 }
41
42 func cleanupProtector(protector *Protector) {
43 protector.Lock()
44 protector.Destroy()
45 }
46
47 func cleanupPolicy(policy *Policy) {
48 policy.Lock()
49 policy.Destroy()
50 }
51
52 // Tests that we can make a policy/protector pair
53 func TestCreatePolicy(t *testing.T) {
54 pro, pol, err := makeBoth()
55 if err != nil {
56 t.Error(err)
57 }
58 cleanupPolicy(pol)
59 cleanupProtector(pro)
60 }
61
62 // Tests that we can add another protector to the policy
63 func TestPolicyGoodAddProtector(t *testing.T) {
64 pro1, pol, err := makeBoth()
65 defer cleanupProtector(pro1)
66 defer cleanupPolicy(pol)
67 if err != nil {
68 t.Fatal(err)
69 }
70
71 pro2, err := CreateProtector(testContext, testProtectorName2, goodCallback, nil)
72 if err != nil {
73 t.Fatal(err)
74 }
75 defer cleanupProtector(pro2)
76
77 err = pol.AddProtector(pro2)
78 if err != nil {
79 t.Error(err)
80 }
81 }
82
83 // Tests that we cannot add a protector to a policy twice
84 func TestPolicyBadAddProtector(t *testing.T) {
85 pro, pol, err := makeBoth()
86 defer cleanupProtector(pro)
87 defer cleanupPolicy(pol)
88 if err != nil {
89 t.Fatal(err)
90 }
91
92 if pol.AddProtector(pro) == nil {
93 t.Error("we should not be able to add the same protector twice")
94 }
95 }
96
97 // Tests that we can remove a protector we added
98 func TestPolicyGoodRemoveProtector(t *testing.T) {
99 pro1, pol, err := makeBoth()
100 defer cleanupProtector(pro1)
101 defer cleanupPolicy(pol)
102 if err != nil {
103 t.Fatal(err)
104 }
105
106 pro2, err := CreateProtector(testContext, testProtectorName2, goodCallback, nil)
107 if err != nil {
108 t.Fatal(err)
109 }
110 defer cleanupProtector(pro2)
111
112 err = pol.AddProtector(pro2)
113 if err != nil {
114 t.Fatal(err)
115 }
116
117 err = pol.RemoveProtector(pro1.Descriptor())
118 if err != nil {
119 t.Error(err)
120 }
121 }
122
123 // Tests various bad ways to remove protectors
124 func TestPolicyBadRemoveProtector(t *testing.T) {
125 pro1, pol, err := makeBoth()
126 defer cleanupProtector(pro1)
127 defer cleanupPolicy(pol)
128 if err != nil {
129 t.Fatal(err)
130 }
131
132 pro2, err := CreateProtector(testContext, testProtectorName2, goodCallback, nil)
133 if err != nil {
134 t.Fatal(err)
135 }
136 defer cleanupProtector(pro2)
137
138 if pol.RemoveProtector(pro2.Descriptor()) == nil {
139 t.Error("we should not be able to remove a protector we did not add")
140 }
141
142 if pol.RemoveProtector(pro1.Descriptor()) == nil {
143 t.Error("we should not be able to remove all the protectors from a policy")
144 }
145 }
146
147 // Tests that policy can be unlocked with a callback.
148 func TestPolicyUnlockWithCallback(t *testing.T) {
149 // Our optionFunc just selects the first protector
150 optionFn := func(policyDescriptor string, options []*ProtectorOption) (int, error) {
151 return 0, nil
152 }
153
154 pro1, pol, err := makeBoth()
155 defer cleanupProtector(pro1)
156 defer cleanupPolicy(pol)
157 if err != nil {
158 t.Fatal(err)
159 }
160
161 if err := pol.Lock(); err != nil {
162 t.Fatal(err)
163 }
164 if err := pol.Unlock(optionFn, goodCallback); err != nil {
165 t.Error(err)
166 }
167 if err := pol.Lock(); err != nil {
168 t.Error(err)
169 }
170 }
171
172 // Tests that policy can be unlock with an unlocked protector.
173 func TestPolicyUnlockWithProtector(t *testing.T) {
174 pro1, pol, err := makeBoth()
175 defer cleanupProtector(pro1)
176 defer cleanupPolicy(pol)
177 if err != nil {
178 t.Fatal(err)
179 }
180
181 if err := pol.Lock(); err != nil {
182 t.Fatal(err)
183 }
184 if err := pol.UnlockWithProtector(pro1); err != nil {
185 t.Error(err)
186 }
187 if err := pol.Lock(); err != nil {
188 t.Error(err)
189 }
190 }
191
192 // Tests that locked protectors cannot unlock a policy.
193 func TestPolicyUnlockWithLockedProtector(t *testing.T) {
194 pro1, pol, err := makeBoth()
195 defer cleanupProtector(pro1)
196 defer cleanupPolicy(pol)
197 if err != nil {
198 t.Fatal(err)
199 }
200
201 if err := pol.Lock(); err != nil {
202 t.Fatal(err)
203 }
204 if err := pro1.Lock(); err != nil {
205 t.Fatal(err)
206 }
207
208 if err := pol.UnlockWithProtector(pro1); errors.Cause(err) != ErrLocked {
209 t.Errorf("Expected a cause of %v got %v", ErrLocked, err)
210 if err == nil {
211 pol.Lock()
212 }
213 }
214 }