| 317 | } |
| 318 | |
| 319 | func TestNetwork_RuntimeErrors(t *testing.T) { |
| 320 | tests := []struct { |
| 321 | name string |
| 322 | expr string |
| 323 | errContains string |
| 324 | }{ |
| 325 | { |
| 326 | name: "containsIP string overload invalid", |
| 327 | expr: "cidr('10.0.0.0/8').containsIP('not-an-ip')", |
| 328 | errContains: "parse error", |
| 329 | }, |
| 330 | { |
| 331 | name: "containsCIDR string overload invalid", |
| 332 | expr: "cidr('10.0.0.0/8').containsCIDR('not-a-cidr')", |
| 333 | errContains: "parse error", |
| 334 | }, |
| 335 | } |
| 336 | |
| 337 | env, err := cel.NewEnv(Network()) |
| 338 | if err != nil { |
| 339 | t.Fatalf("cel.NewEnv(Network()) failed: %v", err) |
| 340 | } |
| 341 | |
| 342 | for _, tst := range tests { |
| 343 | t.Run(tst.name, func(t *testing.T) { |
| 344 | ast, iss := env.Compile(tst.expr) |
| 345 | if iss.Err() != nil { |
| 346 | // Note: We only check runtime errors here. Compile errors are unexpected |
| 347 | // because these functions accept strings, so type-check passes. |
| 348 | t.Fatalf("Compile(%q) failed unexpectedly: %v", tst.expr, iss.Err()) |
| 349 | } |
| 350 | |
| 351 | prg, err := env.Program(ast) |
| 352 | if err != nil { |
| 353 | t.Fatalf("Program(%q) failed: %v", tst.expr, err) |
| 354 | } |
| 355 | |
| 356 | _, _, err = prg.Eval(cel.NoVars()) |
| 357 | if err == nil { |
| 358 | t.Errorf("Expected runtime error for %q, got nil", tst.expr) |
| 359 | return |
| 360 | } |
| 361 | |
| 362 | // CEL errors are sometimes wrapped, so we check substring |
| 363 | if !types.IsError(types.NewErr("%s", err.Error())) { |
| 364 | // Just a sanity check that it is indeed a CEL-compatible error structure |
| 365 | // Not strictly necessary but good practice |
| 366 | } |
| 367 | |
| 368 | // Standard substring check |
| 369 | gotErr := err.Error() |
| 370 | // We just check if the message contains the specific error text we return in network.go |
| 371 | found := false |
| 372 | // Note: The actual error might be wrapped in "evaluation error: ..." |
| 373 | if len(tst.errContains) > 0 { |
| 374 | // Simple string contains check |
| 375 | for i := 0; i < len(gotErr)-len(tst.errContains)+1; i++ { |
| 376 | if gotErr[i:i+len(tst.errContains)] == tst.errContains { |