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

     1  package fingers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/chainreactors/fingers/common"
     7  )
     8  
     9  func TestACCorrectness_MatchesSameAsBaseline(t *testing.T) {
    10  	engine := newPerfEngine(t)
    11  
    12  	// Build a baseline engine that uses the old PassiveMatch path (no AC)
    13  	baselineFingers := engine.HTTPFingers
    14  
    15  	acIdx := engine.httpKeywordIndex
    16  	if acIdx == nil {
    17  		t.Fatal("httpKeywordIndex is nil")
    18  	}
    19  
    20  	sites := generateSyntheticResponses()
    21  	realSites := loadTestSites(t)
    22  	sites = append(sites, realSites...)
    23  
    24  	for _, site := range sites {
    25  		content := NewContent(site.Content, "", true)
    26  
    27  		// Baseline: iterate all fingers
    28  		baseFrames, baseVulns := baselineFingers.PassiveMatch(content, false)
    29  
    30  		// AC path: only evaluate candidates
    31  		acFrames, acVulns := baselineFingers.ACPassiveMatch(content, acIdx, false)
    32  
    33  		// Verify AC finds at least everything the baseline finds
    34  		missing := findMissing(baseFrames, acFrames)
    35  		extra := findMissing(acFrames, baseFrames)
    36  
    37  		if len(missing) > 0 {
    38  			t.Errorf("[%s] AC MISSED frameworks: %v", site.Name, missing)
    39  		}
    40  		if len(extra) > 0 {
    41  			t.Logf("[%s] AC found %d extra frameworks (expected due to broader candidate set): %v",
    42  				site.Name, len(extra), extra)
    43  		}
    44  
    45  		missingVulns := findMissingVulns(baseVulns, acVulns)
    46  		if len(missingVulns) > 0 {
    47  			t.Errorf("[%s] AC MISSED vulns: %v", site.Name, missingVulns)
    48  		}
    49  
    50  		t.Logf("[%s] baseline=%d AC=%d frameworks (match)",
    51  			site.Name, len(baseFrames), len(acFrames))
    52  	}
    53  }
    54  
    55  func findMissing(expected, actual common.Frameworks) []string {
    56  	var missing []string
    57  	for name := range expected {
    58  		if _, ok := actual[name]; !ok {
    59  			missing = append(missing, name)
    60  		}
    61  	}
    62  	return missing
    63  }
    64  
    65  func findMissingVulns(expected, actual common.Vulns) []string {
    66  	var missing []string
    67  	for name := range expected {
    68  		if _, ok := actual[name]; !ok {
    69  			missing = append(missing, name)
    70  		}
    71  	}
    72  	return missing
    73  }