package catalog import "testing" func TestCond(t *testing.T) { env := Env{Answers: map[string]any{"a": "x", "n": 3, "l": []any{"p", "q"}, "b": true, "e": ""}, Mode: LevelGuided} cases := []struct { c Cond want bool }{ {Cond{}, true}, {Cond{"a": "x"}, true}, {Cond{"a": "y"}, false}, {Cond{"a": []any{"y", "x"}}, true}, {Cond{"l": "q"}, true}, {Cond{"l": "z"}, false}, {Cond{"a": map[string]any{"not": "x"}}, false}, {Cond{"n": map[string]any{"gte": 3, "lt": 4}}, true}, {Cond{"l": map[string]any{"count_gte": 2}}, true}, {Cond{"e": map[string]any{"set": false}}, true}, {Cond{"missing": map[string]any{"set": true}}, false}, {Cond{"b": true}, true}, {Cond{"b": "true"}, true}, {Cond{"any": []any{map[string]any{"a": "y"}, map[string]any{"n": 3}}}, true}, {Cond{"all": []any{map[string]any{"a": "x"}, map[string]any{"n": 4}}}, false}, {Cond{"not": map[string]any{"a": "x"}}, false}, {Cond{"$mode": "guided"}, true}, } for i, c := range cases { got, err := c.c.Eval(env) if err != nil || got != c.want { t.Errorf("%d %v: got %v %v", i, c.c, got, err) } } if _, err := (Cond{"a": map[string]any{"regex": "x"}}).Eval(env); err == nil { t.Error("unknown operator accepted") } deep := Cond{"a": "x"} for i := 0; i < MaxCondDepth+2; i++ { deep = Cond{"not": map[string]any(deep)} } if _, err := deep.Eval(env); err == nil { t.Error("unbounded nesting accepted") } if r := (Cond{"any": []any{map[string]any{"x": 1}}, "y": 2, "$mode": "q"}).Refs(); len(r) != 2 || r[0] != "x" || r[1] != "y" { t.Errorf("refs %v", r) } } func TestSafeRelPath(t *testing.T) { for _, ok := range []string{"a/b.yaml", "yamase/yamase.yaml"} { if !SafeRelPath(ok) { t.Errorf("%s rejected", ok) } } for _, bad := range []string{"", "/etc/passwd", "../x", "a/../../x", "a/./b", "a//b", ".hidden/x", `a\b`, "c:/x"} { if SafeRelPath(bad) { t.Errorf("%q accepted", bad) } } }