|
1 | 1 | package operators
|
2 |
| - |
3 | 2 | import (
|
4 |
| - "fmt" |
5 |
| - "testing" |
| 3 | + "testing" |
6 | 4 |
|
7 |
| - "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" |
8 |
| - "github.com/corazawaf/coraza/v3/internal/corazawaf" |
| 5 | + "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" |
9 | 6 | )
|
10 |
| - |
11 | 7 | 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 |
| - } |
| 8 | + opts := plugintypes.OperatorOptions{ |
| 9 | + Arguments: "test_1", |
| 10 | + Datasets: map[string][]string{ |
| 11 | + "test_1": {"value1", "value2"}, |
| 12 | + }, |
| 13 | + } |
| 14 | + pm, err := newPM(opts) |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("Failed to initialize pm: %v", err) |
| 17 | + } |
| 18 | + pmFromFile, err := newPMFromFile(opts) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to initialize pmFromFile: %v", err) |
| 21 | + } |
| 22 | + input := "value1" |
| 23 | + if pm.Evaluate(nil, input) != pmFromFile.Evaluate(nil, input) { |
| 24 | + t.Errorf("pm and pmFromFile returned different results for input: %s", input) |
| 25 | + } |
62 | 26 | }
|
| 27 | + |
0 commit comments