| 1 |
package policy |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/google/cel-go/cel" |
| 8 |
"github.com/google/cel-go/common/ast" |
| 9 |
"github.com/google/cel-go/common/debug" |
| 10 |
"github.com/google/cel-go/ext" |
| 11 |
) |
| 12 |
|
| 13 |
func TestCompose_SourceInfo(t *testing.T) { |
| 14 |
policyYAML := `name: test_policy |
| 15 |
rule: |
| 16 |
match: |
| 17 |
- condition: "2 == 1" |
| 18 |
output: "'hi'" |
| 19 |
- output: "'hello' + ' world'" |
| 20 |
` |
| 21 |
src := StringSource(policyYAML, "test_policy.yaml") |
| 22 |
parser, err := NewParser() |
| 23 |
if err != nil { |
| 24 |
t.Fatalf("NewParser() failed: %v", err) |
| 25 |
} |
| 26 |
policy, iss := parser.Parse(src) |
| 27 |
if iss.Err() != nil { |
| 28 |
t.Fatalf("parser.Parse() failed: %v", iss.Err()) |
| 29 |
} |
| 30 |
|
| 31 |
env, err := cel.NewEnv(cel.OptionalTypes(), ext.Bindings()) |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("cel.NewEnv() failed: %v", err) |
| 34 |
} |
| 35 |
compiledRule, iss := CompileRule(env, policy) |
| 36 |
if iss.Err() != nil { |
| 37 |
t.Fatalf("CompileRule() failed: %v", iss.Err()) |
| 38 |
} |
| 39 |
composer, err := NewRuleComposer(env) |
| 40 |
if err != nil { |
| 41 |
t.Fatalf("NewRuleComposer() failed: %v", err) |
| 42 |
} |
| 43 |
compAST, iss := composer.Compose(compiledRule) |
| 44 |
if iss.Err() != nil { |
| 45 |
t.Fatalf("composer.Compose() failed: %v", iss.Err()) |
| 46 |
} |
| 47 |
|
| 48 |
si := compAST.SourceInfo() |
| 49 |
if si.Location != "test_policy.yaml" { |
| 50 |
t.Errorf("SourceInfo.Location got %q, wanted test_policy.yaml", si.Location) |
| 51 |
} |
| 52 |
verifySourceInfoTransfer(t, compiledRule, compAST) |
| 53 |
} |
| 54 |
|
| 55 |
func TestCompose_Unnest(t *testing.T) { |
| 56 |
policyYAML := `name: unnest |
| 57 |
rule: |
| 58 |
match: |
| 59 |
- condition: "2 == 1" |
| 60 |
output: "'hi'" |
| 61 |
- output: "'hello'" |
| 62 |
` |
| 63 |
src := StringSource(policyYAML, "unnest.yaml") |
| 64 |
parser, err := NewParser() |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("NewParser() failed: %v", err) |
| 67 |
} |
| 68 |
policy, iss := parser.Parse(src) |
| 69 |
if iss.Err() != nil { |
| 70 |
t.Fatalf("parser.Parse() failed: %v", iss.Err()) |
| 71 |
} |
| 72 |
|
| 73 |
env, err := cel.NewEnv(cel.OptionalTypes(), ext.Bindings()) |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("cel.NewEnv() failed: %v", err) |
| 76 |
} |
| 77 |
compiledRule, iss := CompileRule(env, policy) |
| 78 |
if iss.Err() != nil { |
| 79 |
t.Fatalf("CompileRule() failed: %v", iss.Err()) |
| 80 |
} |
| 81 |
|
| 82 |
composer, err := NewRuleComposer(env, ExpressionUnnestHeight(1)) |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("NewRuleComposer() failed: %v", err) |
| 85 |
} |
| 86 |
compAST, iss := composer.Compose(compiledRule) |
| 87 |
if iss.Err() != nil { |
| 88 |
t.Fatalf("composer.Compose() failed: %v", iss.Err()) |
| 89 |
} |
| 90 |
|
| 91 |
verifySourceInfoTransfer(t, compiledRule, compAST) |
| 92 |
if t.Failed() { |
| 93 |
t.Logf("composed AST: %s", debug.ToDebugStringWithIDs(compAST.NativeRep().Expr())) |
| 94 |
t.Logf("SourceInfo: %v", compAST.NativeRep().SourceInfo().OffsetRanges()) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func verifySourceInfoTransfer(t *testing.T, compiledRule *CompiledRule, composed *cel.Ast) { |
| 100 |
t.Helper() |
| 101 |
dstRanges := make(map[ast.OffsetRange]ast.Expr) |
| 102 |
check := func(a *cel.Ast) { |
| 103 |
ast.PostOrderVisit(a.NativeRep().Expr(), &transferChecker{ |
| 104 |
t: t, |
| 105 |
srcInfo: a.NativeRep().SourceInfo(), |
| 106 |
dstInfo: composed.NativeRep().SourceInfo(), |
| 107 |
ranges: &dstRanges}) |
| 108 |
} |
| 109 |
ast.PostOrderVisit(composed.NativeRep().Expr(), &collectRanges{sourceInfo: composed.NativeRep().SourceInfo(), ranges: &dstRanges}) |
| 110 |
for _, match := range compiledRule.matches { |
| 111 |
check(match.cond) |
| 112 |
check(match.output.expr) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
type collectRanges struct { |
| 117 |
sourceInfo *ast.SourceInfo |
| 118 |
ranges *map[ast.OffsetRange]ast.Expr |
| 119 |
} |
| 120 |
|
| 121 |
func (r *collectRanges) VisitExpr(e ast.Expr) { |
| 122 |
if or, found := r.sourceInfo.GetOffsetRange(e.ID()); found { |
| 123 |
(*r.ranges)[or] = e |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func (r *collectRanges) VisitEntryExpr(ast.EntryExpr) { |
| 128 |
} |
| 129 |
|
| 130 |
type transferChecker struct { |
| 131 |
t *testing.T |
| 132 |
srcInfo *ast.SourceInfo |
| 133 |
dstInfo *ast.SourceInfo |
| 134 |
ranges *map[ast.OffsetRange]ast.Expr |
| 135 |
} |
| 136 |
|
| 137 |
func (c *transferChecker) VisitExpr(srcExpr ast.Expr) { |
| 138 |
if srcRange, haveSrc := c.srcInfo.GetOffsetRange(srcExpr.ID()); haveSrc { |
| 139 |
if srcRange.Start == 0 { |
| 140 |
|
| 141 |
return |
| 142 |
} |
| 143 |
dstExpr, haveDst := (*c.ranges)[srcRange] |
| 144 |
if !haveDst { |
| 145 |
c.t.Errorf("composed node not found for rule node: %s", debug.ToDebugString(srcExpr)) |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if dstExpr.Kind() == ast.IdentKind && strings.HasPrefix(dstExpr.AsIdent(), "@index") { |
| 151 |
|
| 152 |
return |
| 153 |
} |
| 154 |
dstStr, err := cel.ExprToString(dstExpr, c.dstInfo) |
| 155 |
if err != nil { |
| 156 |
c.t.Errorf("failed to convert dstExpr") |
| 157 |
return |
| 158 |
} |
| 159 |
srcStr, err := cel.ExprToString(srcExpr, c.srcInfo) |
| 160 |
if err != nil { |
| 161 |
c.t.Errorf("failed to convert srcExpr") |
| 162 |
return |
| 163 |
} |
| 164 |
if srcStr != dstStr { |
| 165 |
c.t.Errorf("mismatched nodes, rule: %s composed: %s", srcStr, dstStr) |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func (c *transferChecker) VisitEntryExpr(ast.EntryExpr) { |
| 171 |
} |