Skip to content

Commit 13d71ef

Browse files
committed
Add Alias @pmf for @pmFromFile
1 parent 62e8e79 commit 13d71ef

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Diff for: internal/operators/pm_from_file.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ func newPMFromFile(options plugintypes.OperatorOptions) (plugintypes.Operator, e
5151
}
5252

5353
func init() {
54-
Register("pmFromFile", newPMFromFile)
54+
Register("pmFromFile", newPMFromFile)
55+
Register("pmf", newPMFromFile)
5556
}

Diff for: internal/operators/pm_from_file_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package operators
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
8+
"github.com/corazawaf/coraza/v3/internal/corazawaf"
9+
)
10+
11+
func TestPmFromFileAlias(t *testing.T) {
12+
opts := plugintypes.OperatorOptions{
13+
Arguments: "testfile.txt",
14+
Path: []string{"/mock/path"},
15+
Root: "/mock/root",
16+
}
17+
18+
mockFileContent := []byte("test_1\ntest_2\n")
19+
loadFromFile = func(filepath string, paths []string, root string) ([]byte, error) {
20+
if filepath == "testfile.txt" {
21+
return mockFileContent, nil
22+
}
23+
return nil, fmt.Errorf("file not found")
24+
}
25+
26+
pmFromFile, err := newPMFromFile(opts)
27+
if err != nil {
28+
t.Fatalf("Failed to initialize @pmFromFile: %v", err)
29+
}
30+
31+
opts.Arguments = "testfile.txt"
32+
pmfAlias, err := newPMFromFile(opts)
33+
if err != nil {
34+
t.Fatalf("Failed to initialize @pmf alias: %v", err)
35+
}
36+
37+
waf := corazawaf.NewWAF()
38+
tx := waf.NewTransaction()
39+
tx.Capture = true
40+
41+
tests := []struct {
42+
operator plugintypes.Operator
43+
input string
44+
expect bool
45+
}{
46+
{pmFromFile, "test_1", true},
47+
{pmFromFile, "nonexistent", false},
48+
{pmfAlias, "test_2", true},
49+
{pmfAlias, "another_test", false},
50+
}
51+
52+
for _, test := range tests {
53+
if res := test.operator.Evaluate(tx, test.input); res != test.expect {
54+
t.Errorf("Operator evaluation failed: input=%q, expected=%v, got=%v", test.input, test.expect, res)
55+
}
56+
}
57+
58+
opts.Arguments = "invalidfile.txt"
59+
if _, err := newPMFromFile(opts); err == nil {
60+
t.Errorf("Expected failure for invalid file, but got no error")
61+
}
62+
}

0 commit comments

Comments
 (0)