Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: unknown key. #1156

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading