MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestHMACSpecificAlgorithmOptions

Function TestHMACSpecificAlgorithmOptions

ext/security/hmac/hmac_test.go:377–440  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

375}
376
377func TestHMACSpecificAlgorithmOptions(t *testing.T) {
378 env, err := cel.NewEnv(
379 hmaclib.Library(hmaclib.Algorithm(crypto.SHA256)),
380 cel.Variable("msgStr", cel.StringType),
381 cel.Variable("secretStr", cel.StringType),
382 )
383 if err != nil {
384 t.Fatalf("cel.NewEnv failed: %v", err)
385 }
386
387 vars := map[string]any{
388 "msgStr": "msg",
389 "secretStr": "key",
390 }
391
392 tests := []struct {
393 name string
394 expr string
395 mode string // "compile_error" or "eval_error" or "success"
396 }{
397 {
398 name: "sha256_success",
399 expr: `hmac.compute(msgStr, secretStr, hmac.SHA256)`,
400 mode: "success",
401 },
402 {
403 name: "unregistered_constant_sha512",
404 expr: `hmac.compute(msgStr, secretStr, hmac.SHA512)`,
405 mode: "compile_error",
406 },
407 {
408 name: "unregistered_literal_sha512",
409 expr: `hmac.compute(msgStr, secretStr, 'SHA512')`,
410 mode: "eval_error",
411 },
412 }
413
414 for _, tc := range tests {
415 t.Run(tc.name, func(t *testing.T) {
416 ast, issues := env.Compile(tc.expr)
417 if tc.mode == "compile_error" {
418 if issues == nil || issues.Err() == nil {
419 t.Errorf("expected compile error for %q, got nil", tc.expr)
420 }
421 return
422 }
423 if issues != nil && issues.Err() != nil {
424 t.Fatalf("Compile(%q) failed unexpectedly: %v", tc.expr, issues.Err())
425 }
426 prg, err := env.Program(ast)
427 if err != nil {
428 t.Fatalf("Program(%q) failed: %v", tc.expr, err)
429 }
430 _, _, evalErr := prg.Eval(vars)
431 if tc.mode == "eval_error" {
432 if evalErr == nil {
433 t.Errorf("expected eval error for %q, got nil", tc.expr)
434 }

Callers

nothing calls this directly

Calls 6

CompileMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
VariableFunction · 0.92
ErrMethod · 0.80
EvalMethod · 0.65

Tested by

no test coverage detected