Skip to content

Commit 5a11f32

Browse files
Additional encoder cost tests
1 parent 7b55cc0 commit 5a11f32

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

ext/encoders_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ func TestEncodersCosts(t *testing.T) {
197197
actualCost: 3,
198198
version: 1,
199199
},
200+
{
201+
name: "encode_empty_bytes_v1_literal",
202+
expr: "base64.encode(b'') == ''",
203+
estimatedCost: checker.FixedCostEstimate(1),
204+
actualCost: 1,
205+
version: 1,
206+
},
207+
{
208+
name: "decode_empty_string_v1_literal",
209+
expr: "base64.decode('') == b''",
210+
estimatedCost: checker.FixedCostEstimate(1),
211+
actualCost: 1,
212+
version: 1,
213+
},
214+
{
215+
name: "encode_non_utf8_bytes_v1_literal",
216+
expr: "base64.encode(b'\xff\xfe\xfd') != '////'",
217+
estimatedCost: checker.FixedCostEstimate(3),
218+
actualCost: 3,
219+
version: 1,
220+
},
200221
}
201222
for _, tc := range tests {
202223
t.Run(tc.name, func(t *testing.T) {
@@ -219,3 +240,28 @@ func TestEncodersCosts(t *testing.T) {
219240
})
220241
}
221242
}
243+
244+
func TestDecodeNonBase64Error(t *testing.T) {
245+
env := testEncodersCostsEnv(t, 1)
246+
pAst, iss := env.Parse("base64.decode('abc-') == b''")
247+
if iss.Err() != nil {
248+
t.Fatalf("env.Parse() failed: %v", iss.Err())
249+
}
250+
cAst, iss := env.Check(pAst)
251+
if iss.Err() != nil {
252+
t.Fatalf("env.Check() failed: %v", iss.Err())
253+
}
254+
testCheckCost(t, env, cAst, nil, checker.FixedCostEstimate(2))
255+
prgOpts := []cel.ProgramOption{}
256+
if cAst.IsChecked() {
257+
prgOpts = append(prgOpts, cel.CostTracking(nil))
258+
}
259+
prg, err := env.Program(cAst, prgOpts...)
260+
if err != nil {
261+
t.Fatalf("env.Program() failed: %v", err)
262+
}
263+
_, _, err = prg.Eval(cel.NoVars())
264+
if err == nil {
265+
t.Fatal("expected eval error for non-base64 string decoding, got nil")
266+
}
267+
}

0 commit comments

Comments
 (0)