checker-blacklist/checker/virustotal_test.go
Pierre-Olivier Mercier c437339bda Separate observation from evaluation in blacklist sources
Each source's Query() method previously set r.Listed and r.Severity,
embedding verdict logic inside the prober. Evaluation now lives in a
dedicated Evaluate(SourceResult) (bool, string) method per source,
keeping Query() as pure observation.

A package-level EvaluateResult() helper looks up the source by ID and
delegates to its Evaluate method; rules.go, report.go, types.go, and
provider.go all call this instead of reading pre-set r.Listed/r.Severity
values. An unknownSource sentinel handles results whose source is no
longer registered.
2026-05-15 18:04:17 +08:00

84 lines
2.7 KiB
Go

package checker
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
sdk "git.happydns.org/checker-sdk-go/checker"
)
func newVTServer(t *testing.T, status int, body string) (string, func()) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("x-apikey") == "" {
t.Errorf("missing x-apikey header")
}
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
return srv.URL + "/", srv.Close
}
func TestVTSource_NoKey(t *testing.T) {
s := &virusTotalSource{endpoint: virusTotalEndpoint}
r := s.Query(context.Background(), "example.com", "example.com", sdk.CheckerOptions{})[0]
if r.Enabled {
t.Errorf("expected disabled without API key, got %+v", r)
}
}
func TestVTSource_Listed(t *testing.T) {
body := `{"data":{"attributes":{
"reputation":-25,
"last_analysis_stats":{"harmless":50,"malicious":3,"suspicious":1,"undetected":40,"timeout":0},
"last_analysis_results":{
"E1":{"category":"malicious","result":"phishing","engine_name":"E1"},
"E2":{"category":"suspicious","result":"susp","engine_name":"E2"},
"E3":{"category":"harmless","result":"clean","engine_name":"E3"}
}
}}}`
endpoint, stop := newVTServer(t, http.StatusOK, body)
defer stop()
s := &virusTotalSource{endpoint: endpoint}
r := s.Query(context.Background(), "example.com", "example.com", sdk.CheckerOptions{"virustotal_api_key": "k"})[0]
if listed, severity := s.Evaluate(r); !listed || severity != SeverityCrit {
t.Errorf("expected Evaluate()=(true, crit), got (%v, %q)", listed, severity)
}
var d vtDetails
if err := json.Unmarshal(r.Details, &d); err != nil {
t.Fatalf("details decode: %v", err)
}
if d.Malicious != 3 || d.Suspicious != 1 || d.Reputation != -25 {
t.Errorf("counts wrong: %+v", d)
}
if len(d.Vendors) != 2 || d.Vendors[0].Category != "malicious" {
t.Errorf("vendor ordering wrong: %+v", d.Vendors)
}
html, err := s.RenderDetail(r)
if err != nil || !strings.Contains(string(html), "malicious") {
t.Errorf("RenderDetail html=%q err=%v", html, err)
}
}
func TestVTSource_NotFound(t *testing.T) {
endpoint, stop := newVTServer(t, http.StatusNotFound, `{"error":{"code":"NotFoundError"}}`)
defer stop()
s := &virusTotalSource{endpoint: endpoint}
r := s.Query(context.Background(), "example.com", "example.com", sdk.CheckerOptions{"virustotal_api_key": "k"})[0]
if r.Error != "" {
t.Errorf("404 should be quiet not-listed: %+v", r)
}
if listed, severity := s.Evaluate(r); listed || severity != "" {
t.Errorf("Evaluate() on clean result = (%v, %q), want (false, \"\")", listed, severity)
}
if !strings.Contains(r.Reference, "example.com") {
t.Errorf("reference URL missing: %+v", r)
}
}