| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package policy |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/google/go-cmp/cmp" |
| 22 |
"go.yaml.in/yaml/v3" |
| 23 |
) |
| 24 |
|
| 25 |
func TestParse(t *testing.T) { |
| 26 |
for _, tst := range policyTests { |
| 27 |
srcFile := readPolicy(t, fmt.Sprintf("testdata/%s/policy.yaml", tst.name)) |
| 28 |
parser, err := NewParser(tst.parseOpts...) |
| 29 |
if err != nil { |
| 30 |
t.Fatalf("NewParser() failed: %v", err) |
| 31 |
} |
| 32 |
p, iss := parser.Parse(srcFile) |
| 33 |
if iss.Err() != nil { |
| 34 |
t.Fatalf("parser.Parse() failed: %v", iss.Err()) |
| 35 |
} |
| 36 |
if p.Name().Value != tst.name { |
| 37 |
t.Errorf("policy name is %v, wanted %q", p.name, tst.name) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestParseError(t *testing.T) { |
| 43 |
tests := []struct { |
| 44 |
txt string |
| 45 |
err string |
| 46 |
}{ |
| 47 |
{ |
| 48 |
txt: ` |
| 49 |
name: |
| 50 |
illegal: yaml-type`, |
| 51 |
err: `ERROR: <input>:3:3: got yaml node type tag:yaml.org,2002:map, wanted type(s) [tag:yaml.org,2002:str !txt] |
| 52 |
| illegal: yaml-type |
| 53 |
| ..^`, |
| 54 |
}, |
| 55 |
{ |
| 56 |
txt: ` |
| 57 |
rule: |
| 58 |
custom: yaml-type`, |
| 59 |
err: `ERROR: <input>:3:3: unsupported rule tag: custom |
| 60 |
| custom: yaml-type |
| 61 |
| ..^`, |
| 62 |
}, |
| 63 |
{ |
| 64 |
txt: ` |
| 65 |
inputs: |
| 66 |
- name: a |
| 67 |
- name: b`, |
| 68 |
err: `ERROR: <input>:2:1: unsupported policy tag: inputs |
| 69 |
| inputs: |
| 70 |
| ^`, |
| 71 |
}, |
| 72 |
{ |
| 73 |
txt: ` |
| 74 |
rule: |
| 75 |
variables: |
| 76 |
- name: "true" |
| 77 |
alt_name: "bool_true"`, |
| 78 |
err: `ERROR: <input>:5:7: unsupported variable tag: alt_name |
| 79 |
| alt_name: "bool_true" |
| 80 |
| ......^`, |
| 81 |
}, |
| 82 |
{ |
| 83 |
txt: ` |
| 84 |
rule: |
| 85 |
match: |
| 86 |
- name: "true" |
| 87 |
alt_name: "bool_true"`, |
| 88 |
err: `ERROR: <input>:4:7: unsupported match tag: name |
| 89 |
| - name: "true" |
| 90 |
| ......^ |
| 91 |
ERROR: <input>:4:7: match does not specify a rule or output |
| 92 |
| - name: "true" |
| 93 |
| ......^ |
| 94 |
ERROR: <input>:5:7: unsupported match tag: alt_name |
| 95 |
| alt_name: "bool_true" |
| 96 |
| ......^`, |
| 97 |
}, |
| 98 |
{ |
| 99 |
txt: ` |
| 100 |
- rule: |
| 101 |
id: a`, |
| 102 |
err: `ERROR: <input>:2:1: got yaml node type tag:yaml.org,2002:seq, wanted type(s) [tag:yaml.org,2002:map] |
| 103 |
| - rule: |
| 104 |
| ^`, |
| 105 |
}, |
| 106 |
{ |
| 107 |
txt: ` |
| 108 |
rule: |
| 109 |
match: |
| 110 |
- condition: "true" |
| 111 |
output: "world" |
| 112 |
rule: |
| 113 |
match: |
| 114 |
- output: "hello"`, |
| 115 |
err: `ERROR: <input>:6:7: only the rule or the output may be set |
| 116 |
| rule: |
| 117 |
| ......^`, |
| 118 |
}, |
| 119 |
{ |
| 120 |
txt: ` |
| 121 |
rule: |
| 122 |
match: |
| 123 |
- condition: "true" |
| 124 |
rule: |
| 125 |
match: |
| 126 |
- output: "hello" |
| 127 |
output: "world"`, |
| 128 |
err: `ERROR: <input>:8:7: only the rule or the output may be set |
| 129 |
| output: "world" |
| 130 |
| ......^`, |
| 131 |
}, |
| 132 |
{ |
| 133 |
txt: ` |
| 134 |
rule: |
| 135 |
match: |
| 136 |
- condition: "true" |
| 137 |
explanation: "hi" |
| 138 |
rule: |
| 139 |
match: |
| 140 |
- output: "hello"`, |
| 141 |
err: `ERROR: <input>:6:7: explanation can only be set on output match cases, not nested rules |
| 142 |
| rule: |
| 143 |
| ......^`, |
| 144 |
}, |
| 145 |
{ |
| 146 |
txt: ` |
| 147 |
rule: |
| 148 |
match: |
| 149 |
- condition: "true" |
| 150 |
rule: |
| 151 |
match: |
| 152 |
- output: "hello" |
| 153 |
explanation: "hi"`, |
| 154 |
err: `ERROR: <input>:8:7: explanation can only be set on output match cases, not nested rules |
| 155 |
| explanation: "hi" |
| 156 |
| ......^`, |
| 157 |
}, |
| 158 |
{ |
| 159 |
txt: ` |
| 160 |
imports: |
| 161 |
- first`, |
| 162 |
err: `ERROR: <input>:3:5: got yaml node type tag:yaml.org,2002:str, wanted type(s) [tag:yaml.org,2002:map] |
| 163 |
| - first |
| 164 |
| ....^`, |
| 165 |
}, |
| 166 |
{ |
| 167 |
txt: ` |
| 168 |
imports: |
| 169 |
first: name`, |
| 170 |
err: `ERROR: <input>:3:3: got yaml node type tag:yaml.org,2002:map, wanted type(s) [tag:yaml.org,2002:seq] |
| 171 |
| first: name |
| 172 |
| ..^`, |
| 173 |
}, |
| 174 |
{ |
| 175 |
txt: ` |
| 176 |
rule: |
| 177 |
- variables: name`, |
| 178 |
err: `ERROR: <input>:3:3: got yaml node type tag:yaml.org,2002:seq, wanted type(s) [tag:yaml.org,2002:map] |
| 179 |
| - variables: name |
| 180 |
| ..^`, |
| 181 |
}, |
| 182 |
{ |
| 183 |
txt: ` |
| 184 |
rule: |
| 185 |
variables: name`, |
| 186 |
err: `ERROR: <input>:3:14: got yaml node type tag:yaml.org,2002:str, wanted type(s) [tag:yaml.org,2002:seq] |
| 187 |
| variables: name |
| 188 |
| .............^`, |
| 189 |
}, |
| 190 |
{ |
| 191 |
txt: ` |
| 192 |
rule: |
| 193 |
variables: |
| 194 |
- name`, |
| 195 |
err: `ERROR: <input>:4:7: got yaml node type tag:yaml.org,2002:str, wanted type(s) [tag:yaml.org,2002:map] |
| 196 |
| - name |
| 197 |
| ......^`, |
| 198 |
}, |
| 199 |
{ |
| 200 |
txt: ` |
| 201 |
rule: |
| 202 |
match: |
| 203 |
name: value`, |
| 204 |
err: `ERROR: <input>:4:5: got yaml node type tag:yaml.org,2002:map, wanted type(s) [tag:yaml.org,2002:seq] |
| 205 |
| name: value |
| 206 |
| ....^`, |
| 207 |
}, |
| 208 |
{ |
| 209 |
txt: ` |
| 210 |
rule: |
| 211 |
match: |
| 212 |
- name`, |
| 213 |
err: `ERROR: <input>:4:7: got yaml node type tag:yaml.org,2002:str, wanted type(s) [tag:yaml.org,2002:map] |
| 214 |
| - name |
| 215 |
| ......^`, |
| 216 |
}, |
| 217 |
} |
| 218 |
|
| 219 |
for _, tst := range tests { |
| 220 |
parser, err := NewParser() |
| 221 |
if err != nil { |
| 222 |
t.Fatalf("NewParser() failed: %v", err) |
| 223 |
} |
| 224 |
_, iss := parser.Parse(StringSource(tst.txt, "<input>")) |
| 225 |
if iss.Err() == nil { |
| 226 |
t.Fatalf("parser.Parse(%q) did not error, wanted %s", tst.txt, tst.err) |
| 227 |
} |
| 228 |
|
| 229 |
if iss.Err().Error() != tst.err { |
| 230 |
t.Errorf("parser.Parse(%q) got error %v, wanted error %s", tst.txt, iss.Err(), tst.err) |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func TestGetExplanationOutputPolicy(t *testing.T) { |
| 236 |
tst := ` |
| 237 |
rule: |
| 238 |
match: |
| 239 |
- condition: "false" |
| 240 |
rule: |
| 241 |
match: |
| 242 |
- condition: "1 > 2" |
| 243 |
output: "false" |
| 244 |
explanation: "'bad_inner'" |
| 245 |
- output: "true" |
| 246 |
explanation: "'good_inner'" |
| 247 |
- output: "true" |
| 248 |
explanation: "'good_outer'" |
| 249 |
` |
| 250 |
|
| 251 |
parser, err := NewParser() |
| 252 |
if err != nil { |
| 253 |
t.Fatalf("NewParser() failed: %v", err) |
| 254 |
} |
| 255 |
policy, iss := parser.Parse(StringSource(tst, "<input>")) |
| 256 |
if iss != nil { |
| 257 |
t.Fatalf("Parse() failed: %v", err) |
| 258 |
} |
| 259 |
|
| 260 |
explanationPolicy := policy.GetExplanationOutputPolicy() |
| 261 |
|
| 262 |
want := "'bad_inner'" |
| 263 |
got := explanationPolicy.Rule().Matches()[0].rule.Matches()[0].output.Value |
| 264 |
if got != want { |
| 265 |
t.Errorf("First inner output = %v, wanted %v", got, want) |
| 266 |
} |
| 267 |
|
| 268 |
want = "1 > 2" |
| 269 |
got = explanationPolicy.Rule().Matches()[0].rule.Matches()[0].condition.Value |
| 270 |
if got != want { |
| 271 |
t.Errorf("First inner condition = %v, wanted %v", got, want) |
| 272 |
} |
| 273 |
|
| 274 |
want = "'good_inner'" |
| 275 |
got = explanationPolicy.Rule().Matches()[0].rule.Matches()[1].output.Value |
| 276 |
if got != want { |
| 277 |
t.Errorf("Second inner output = %v, wanted %v", got, want) |
| 278 |
} |
| 279 |
|
| 280 |
want = "'good_outer'" |
| 281 |
got = explanationPolicy.Rule().Matches()[1].output.Value |
| 282 |
if got != want { |
| 283 |
t.Errorf("Second outer output = %v, wanted %v", got, want) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
type testTagHandler struct { |
| 288 |
defaultTagVisitor |
| 289 |
|
| 290 |
description string |
| 291 |
} |
| 292 |
|
| 293 |
func (t *testTagHandler) PolicyTag(ctx ParserContext, id int64, tagName string, node *yaml.Node, p *Policy) { |
| 294 |
if tagName == "description" { |
| 295 |
t.description = node.Value |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
func TestDescriptionTag(t *testing.T) { |
| 300 |
tst := `name: "test" |
| 301 |
description: |-2 |
| 302 |
A test description. |
| 303 |
rule: |
| 304 |
match: |
| 305 |
- condition: "true" |
| 306 |
output: "true" |
| 307 |
|
| 308 |
` |
| 309 |
|
| 310 |
parser, err := NewParser() |
| 311 |
if err != nil { |
| 312 |
t.Fatalf("NewParser() failed: %v", err) |
| 313 |
} |
| 314 |
handler := &testTagHandler{} |
| 315 |
parser.TagVisitor = handler |
| 316 |
policy, iss := parser.Parse(StringSource(tst, "<input>")) |
| 317 |
if iss != nil { |
| 318 |
t.Fatalf("Parse() failed: %v", iss.Err()) |
| 319 |
} |
| 320 |
|
| 321 |
if dx := cmp.Diff(" A test description.", handler.description); dx != "" { |
| 322 |
t.Errorf("handler.description (+got, -want): %s", dx) |
| 323 |
} |
| 324 |
|
| 325 |
if dx := cmp.Diff(" A test description.", policy.Description().Value); dx != "" { |
| 326 |
t.Errorf("policy.Description() (+got, -want): %s", dx) |
| 327 |
} |
| 328 |
} |