From 13d71efa8f9cbd912409667645f3db501eec3cce Mon Sep 17 00:00:00 2001 From: Satwik Sai Prakash Sahoo Date: Wed, 22 Jan 2025 21:32:04 +0530 Subject: [PATCH 1/2] Add Alias `@pmf` for `@pmFromFile` --- internal/operators/pm_from_file.go | 3 +- internal/operators/pm_from_file_test.go | 62 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 internal/operators/pm_from_file_test.go diff --git a/internal/operators/pm_from_file.go b/internal/operators/pm_from_file.go index 035a448e..d15d48c4 100644 --- a/internal/operators/pm_from_file.go +++ b/internal/operators/pm_from_file.go @@ -51,5 +51,6 @@ func newPMFromFile(options plugintypes.OperatorOptions) (plugintypes.Operator, e } func init() { - Register("pmFromFile", newPMFromFile) + Register("pmFromFile", newPMFromFile) + Register("pmf", newPMFromFile) } diff --git a/internal/operators/pm_from_file_test.go b/internal/operators/pm_from_file_test.go new file mode 100644 index 00000000..1917eba1 --- /dev/null +++ b/internal/operators/pm_from_file_test.go @@ -0,0 +1,62 @@ +package operators + +import ( + "fmt" + "testing" + + "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" + "github.com/corazawaf/coraza/v3/internal/corazawaf" +) + +func TestPmFromFileAlias(t *testing.T) { + opts := plugintypes.OperatorOptions{ + Arguments: "testfile.txt", + Path: []string{"/mock/path"}, + Root: "/mock/root", + } + + mockFileContent := []byte("test_1\ntest_2\n") + loadFromFile = func(filepath string, paths []string, root string) ([]byte, error) { + if filepath == "testfile.txt" { + return mockFileContent, nil + } + return nil, fmt.Errorf("file not found") + } + + pmFromFile, err := newPMFromFile(opts) + if err != nil { + t.Fatalf("Failed to initialize @pmFromFile: %v", err) + } + + opts.Arguments = "testfile.txt" + pmfAlias, err := newPMFromFile(opts) + if err != nil { + t.Fatalf("Failed to initialize @pmf alias: %v", err) + } + + waf := corazawaf.NewWAF() + tx := waf.NewTransaction() + tx.Capture = true + + tests := []struct { + operator plugintypes.Operator + input string + expect bool + }{ + {pmFromFile, "test_1", true}, + {pmFromFile, "nonexistent", false}, + {pmfAlias, "test_2", true}, + {pmfAlias, "another_test", false}, + } + + for _, test := range tests { + if res := test.operator.Evaluate(tx, test.input); res != test.expect { + t.Errorf("Operator evaluation failed: input=%q, expected=%v, got=%v", test.input, test.expect, res) + } + } + + opts.Arguments = "invalidfile.txt" + if _, err := newPMFromFile(opts); err == nil { + t.Errorf("Expected failure for invalid file, but got no error") + } +} From e173f73980d21cba4962ceef92244cbc93a9a6e0 Mon Sep 17 00:00:00 2001 From: Satwik Sai Prakash Sahoo Date: Wed, 22 Jan 2025 22:45:28 +0530 Subject: [PATCH 2/2] Removed redundant tests for pmFromFile --- internal/operators/pm_from_file_test.go | 77 +++++++------------------ 1 file changed, 21 insertions(+), 56 deletions(-) diff --git a/internal/operators/pm_from_file_test.go b/internal/operators/pm_from_file_test.go index 1917eba1..2fa6849d 100644 --- a/internal/operators/pm_from_file_test.go +++ b/internal/operators/pm_from_file_test.go @@ -1,62 +1,27 @@ package operators - import ( - "fmt" - "testing" + "testing" - "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" - "github.com/corazawaf/coraza/v3/internal/corazawaf" + "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" ) - func TestPmFromFileAlias(t *testing.T) { - opts := plugintypes.OperatorOptions{ - Arguments: "testfile.txt", - Path: []string{"/mock/path"}, - Root: "/mock/root", - } - - mockFileContent := []byte("test_1\ntest_2\n") - loadFromFile = func(filepath string, paths []string, root string) ([]byte, error) { - if filepath == "testfile.txt" { - return mockFileContent, nil - } - return nil, fmt.Errorf("file not found") - } - - pmFromFile, err := newPMFromFile(opts) - if err != nil { - t.Fatalf("Failed to initialize @pmFromFile: %v", err) - } - - opts.Arguments = "testfile.txt" - pmfAlias, err := newPMFromFile(opts) - if err != nil { - t.Fatalf("Failed to initialize @pmf alias: %v", err) - } - - waf := corazawaf.NewWAF() - tx := waf.NewTransaction() - tx.Capture = true - - tests := []struct { - operator plugintypes.Operator - input string - expect bool - }{ - {pmFromFile, "test_1", true}, - {pmFromFile, "nonexistent", false}, - {pmfAlias, "test_2", true}, - {pmfAlias, "another_test", false}, - } - - for _, test := range tests { - if res := test.operator.Evaluate(tx, test.input); res != test.expect { - t.Errorf("Operator evaluation failed: input=%q, expected=%v, got=%v", test.input, test.expect, res) - } - } - - opts.Arguments = "invalidfile.txt" - if _, err := newPMFromFile(opts); err == nil { - t.Errorf("Expected failure for invalid file, but got no error") - } + opts := plugintypes.OperatorOptions{ + Arguments: "test_1", + Datasets: map[string][]string{ + "test_1": {"value1", "value2"}, + }, + } + pm, err := newPM(opts) + if err != nil { + t.Fatalf("Failed to initialize pm: %v", err) + } + pmFromFile, err := newPMFromFile(opts) + if err != nil { + t.Fatalf("Failed to initialize pmFromFile: %v", err) + } + input := "value1" + if pm.Evaluate(nil, input) != pmFromFile.Evaluate(nil, input) { + t.Errorf("pm and pmFromFile returned different results for input: %s", input) + } } +