github.com/chainreactors/fingers@v1.2.1/xray/compile_test.go (about)

     1  package xray
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/chainreactors/neutron/operators"
     7  	"github.com/chainreactors/neutron/templates"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  func TestCompileNginxTemplate(t *testing.T) {
    12  	yamlStr := `
    13  id: f5-nginx
    14  info:
    15    name: Nginx
    16    severity: info
    17  http:
    18  - method: GET
    19    path:
    20    - '{{BaseURL}}/'
    21    matchers:
    22    - type: dsl
    23      dsl:
    24      - icontains(server, "nginx")
    25    - type: word
    26      words:
    27      - Welcome to nginx!
    28      - <center>nginx
    29  `
    30  	tmpl := &templates.Template{}
    31  	if err := yaml.Unmarshal([]byte(yamlStr), tmpl); err != nil {
    32  		t.Fatalf("unmarshal: %v", err)
    33  	}
    34  
    35  	err := tmpl.Compile(nil)
    36  	t.Logf("Compile error: %v", err)
    37  
    38  	for i, req := range tmpl.GetRequests() {
    39  		t.Logf("req[%d] CompiledOperators: %v", i, req.CompiledOperators != nil)
    40  		if req.CompiledOperators != nil {
    41  			for j, m := range req.CompiledOperators.Matchers {
    42  				t.Logf("  matcher[%d] type=%s dsl=%v words=%v", j, m.Type, m.DSL, m.Words)
    43  			}
    44  		}
    45  
    46  		// Try manual compile
    47  		t.Logf("req[%d] manual compile...", i)
    48  		compileErr := (&req.Operators).Compile()
    49  		t.Logf("req[%d] manual compile error: %v", i, compileErr)
    50  		if compileErr == nil {
    51  			req.CompiledOperators = &req.Operators
    52  			for j, m := range req.CompiledOperators.Matchers {
    53  				t.Logf("  matcher[%d] type=%s dsl=%v words=%v matcherType=%v", j, m.Type, m.DSL, m.Words, m.GetType())
    54  			}
    55  		}
    56  	}
    57  
    58  	// Also test direct matcher compile
    59  	t.Log("\n--- Direct DSL compile ---")
    60  	m := &operators.Matcher{Type: "dsl", DSL: []string{`icontains(server, "nginx")`}}
    61  	err = m.CompileMatchers()
    62  	t.Logf("DSL compile error: %v", err)
    63  }