|
| 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