-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathwitness_policy_test.go
More file actions
183 lines (170 loc) · 5.79 KB
/
Copy pathwitness_policy_test.go
File metadata and controls
183 lines (170 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2025 The Tessera authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tessera
import (
"strings"
"testing"
)
func TestNewWitnessGroupFromPolicy(t *testing.T) {
for _, test := range []struct {
name string
policy string
}{
{
name: "tidy",
policy: `
witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
group g1 all w1 w2
quorum g1
`,
}, {
name: "whitespace and comments",
policy: `
# comment
witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/ #comment
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
#comment
group g1 all w1 w2
quorum g1
`,
},
} {
t.Run(test.name, func(t *testing.T) {
wg, err := NewWitnessGroupFromPolicy([]byte(test.policy))
if err != nil {
t.Fatalf("NewWitnessGroupFromPolicy() failed: %v", err)
}
if wg.N != 2 {
t.Errorf("Expected top-level group to have N=2, got %d", wg.N)
}
if len(wg.Components) != 2 {
t.Fatalf("Expected top-level group to have 2 components, got %d", len(wg.Components))
}
})
}
}
func TestNewWitnessGroupFromPolicy_GroupN(t *testing.T) {
testCases := []struct {
desc string
policy string
wantN int
}{
{
desc: "group numerical",
policy: `
witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
witness w3 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
witness w4 remora.n621.de+da77ade7+BOvN63jn/bLvkieywe8R6UYAtVtNbZpXh34x7onlmtw2 https://example.com/remora
group g1 2 w1 w2 w3 w4
quorum g1
`,
wantN: 2,
},
{
desc: "group all",
policy: `
witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
witness w3 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
group g1 all w1 w2 w3
quorum g1
`,
wantN: 3,
},
{
desc: "group any",
policy: `
witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
witness w3 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
group g1 any w1
quorum g1
`,
wantN: 1,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
wg, err := NewWitnessGroupFromPolicy([]byte(tc.policy))
if err != nil {
t.Fatalf("NewWitnessGroupFromPolicy() failed: %v", err)
}
if wg.N != tc.wantN {
t.Errorf("wg.N = %d, want %d", wg.N, tc.wantN)
}
})
}
}
func TestNewWitnessGroupFromPolicy_Errors(t *testing.T) {
testCases := []struct {
desc string
policy string
errStr string
}{
{
desc: "no quorum",
policy: "witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/",
errStr: "policy file must define a quorum",
},
{
desc: "unknown quorum component",
policy: "quorum unknown",
errStr: "quorum component \"unknown\" not found",
},
{
desc: "duplicate component name",
policy: "witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/\nwitness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/\nquorum w1",
errStr: "duplicate component name",
},
{
desc: "negative threshold",
policy: `witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
witness w3 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://example.com/witness/
group g1 -1 w1
quorum g1`,
errStr: "invalid threshold",
},
{
desc: "witness name is keyword",
policy: `witness all sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/`,
errStr: "invalid witness name",
},
{
desc: "witness name is keyword",
policy: `group none 1 witness`,
errStr: "invalid group name",
}, {
desc: "witness URL has multiple signer names",
policy: `
witness w1 sigsum.org+e4ade967+AZuUY6B08pW3QVHu8uvsrxWPcAv9nykap2Nb4oxCee+r https://sigsum.org/witness/
witness w2 example.com+3753d3de+AebBhMcghIUoavZpjuDofa4sW6fYHyVn7gvwDBfvkvuM https://sigsum.org/witness/
group g1 1 w1 w2
quorum g1`,
errStr: "has multiple witness signer names assigned",
}}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
_, err := NewWitnessGroupFromPolicy([]byte(tc.policy))
if err == nil {
t.Fatal("Expected error, got nil")
}
if !strings.Contains(err.Error(), tc.errStr) {
t.Errorf("Expected error string to contain %q, got %q", tc.errStr, err.Error())
}
})
}
}