Skip to content

Commit

Permalink
tests: unknown key.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Sep 27, 2024
1 parent c2d37d5 commit e05509f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
20 changes: 19 additions & 1 deletion experimental/plugins/macro/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ func TestCompile(t *testing.T) {
}
})

t.Run("unknown key", func(t *testing.T) {
m := &macro{}

err := m.compile("%{tx.missing_key}")
if err != nil {
t.Fatalf("unexpected error")
}

if want, have := 1, len(m.tokens); want != have {
t.Fatalf("unexpected number of tokens: want %d, have %d", want, have)
}

expectedMacro := macroToken{"tx.missing_key", variables.TX, "missing_key"}
if want, have := m.tokens[0], expectedMacro; want != have {
t.Errorf("unexpected token: wanted %v, got %v", want, have)
}
})

t.Run("valid macro", func(t *testing.T) {
m := &macro{}
err := m.compile("%{tx.count}")
Expand Down Expand Up @@ -123,7 +141,7 @@ func TestCompile(t *testing.T) {
}

func TestExpand(t *testing.T) {
t.Run("no expansion", func(t *testing.T) {
t.Run("unknown variable", func(t *testing.T) {
m := &macro{
tokens: []macroToken{
{"text", variables.Unknown, ""},
Expand Down
17 changes: 11 additions & 6 deletions internal/actions/setvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,29 @@ func TestSetvarEvaluate(t *testing.T) {
a := setvar()
metadata := &md{}
if err := a.Init(metadata, tt.init); err != nil {
t.Error("unexpected error during setvar init")
t.Fatal("unexpected error during setvar init")
}

waf := corazawaf.NewWAF()
waf.Logger = logger

tx := waf.NewTransaction()
a.Evaluate(metadata, tx)

if tt.expectInvalidSyntaxError {
t.Log(logsBuf.String())
if logsBuf.Len() == 0 {
t.Fatal("expected error")
t.Fatal("expected logs")
}

if !strings.Contains(logsBuf.String(), invalidSyntaxAtoiError) {
t.Errorf("expected error containing %q, got %q", invalidSyntaxAtoiError, logsBuf.String())
t.Errorf("expected error log containing %q, got %q", invalidSyntaxAtoiError, logsBuf.String())
}

if !strings.Contains(logsBuf.String(), warningKeyNotFoundInCollection) {
t.Errorf("expected error containing %q, got %q", warningKeyNotFoundInCollection, logsBuf.String())
t.Errorf("expected error log containing %q, got %q", warningKeyNotFoundInCollection, logsBuf.String())
}
}
if logsBuf.Len() != 0 && !tt.expectInvalidSyntaxError {
} else if logsBuf.Len() != 0 {
t.Fatalf("unexpected error: %s", logsBuf.String())
}

Expand Down

0 comments on commit e05509f

Please sign in to comment.