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

     1  package xray
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	// Opt into raw xray POC support for this test by registering the xray
     8  	// converter. Production code does not import convert, so it is not linked
     9  	// into normal builds of this package.
    10  	_ "github.com/chainreactors/neutron/convert"
    11  )
    12  
    13  // TestLoadRawXrayPOC verifies the engine auto-adapts a raw xray POC (rule +
    14  // expression schema) at load time — no pre-conversion step required.
    15  func TestLoadRawXrayPOC(t *testing.T) {
    16  	// A raw xray POC, expressed as the JSON map the engine ingests. It has
    17  	// `rules`/`expression` rather than neutron's `http`/`matchers`.
    18  	rawXray := []byte(`[{
    19  		"name": "fingerprint-test--08cms",
    20  		"transport": "http",
    21  		"detail": {"fingerprint": {"name": "08Cms", "cpe": "dingdian_network:08cms"}},
    22  		"rules": {
    23  			"r0": {"expression": "response.body_string.contains(\"08cms\")"}
    24  		},
    25  		"expression": "r0()"
    26  	}]`)
    27  
    28  	engine, err := NewXrayEngine(nil)
    29  	if err != nil {
    30  		t.Fatalf("new engine: %v", err)
    31  	}
    32  	if err := engine.LoadFromJSON(rawXray); err != nil {
    33  		t.Fatalf("LoadFromJSON raw xray: %v", err)
    34  	}
    35  	if engine.Len() != 1 {
    36  		t.Fatalf("expected 1 template loaded from raw xray poc, got %d", engine.Len())
    37  	}
    38  
    39  	resp := []byte("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html>powered by 08cms</html>")
    40  	frames := engine.WebMatch(resp)
    41  	if len(frames) == 0 {
    42  		t.Fatalf("expected raw xray poc to match, got no frameworks")
    43  	}
    44  	var found bool
    45  	for _, f := range frames {
    46  		if strings.EqualFold(f.Name, "08Cms") {
    47  			found = true
    48  		}
    49  	}
    50  	if !found {
    51  		t.Errorf("expected framework 08Cms, got %v", frames)
    52  	}
    53  }