package client import ( "encoding/json" "testing" "crypto-miner-agent/deploy" ) func TestHandleReconRelayScanCommand(t *testing.T) { deploy.SetProbePortsHook(func(host string, ports []int) []int { if host == "10.1.2.3" { return []int{22, 445} } return nil }) t.Cleanup(func() { deploy.SetProbePortsHook(nil) }) deploy.SetRelayUDPGuessHook(func(host string, ports []int) []deploy.RelayUDPHint { return []deploy.RelayUDPHint{{Port: 51820, Open: true, Service: "wireguard"}} }) t.Cleanup(func() { deploy.SetRelayUDPGuessHook(nil) }) var gotAction string var gotSuccess bool var gotMsg string c := &AgentClient{} c.commandResultHook = func(action string, success bool, message string) { gotAction, gotSuccess, gotMsg = action, success, message } if !c.handleReconCommand("recon_relay_scan", "10.1.2.3", "true") { t.Fatal("expected handled") } if gotAction != "recon_relay_scan" || !gotSuccess { t.Fatalf("action=%s success=%v", gotAction, gotSuccess) } var parsed map[string]interface{} if err := json.Unmarshal([]byte(gotMsg), &parsed); err != nil { t.Fatal(err) } ports, _ := parsed["open_ports"].([]interface{}) if len(ports) != 2 { t.Fatalf("ports=%v", ports) } } func TestHandleReconRelayScanMissingHost(t *testing.T) { c := &AgentClient{} c.commandResultHook = func(action string, success bool, _ string) { if action != "recon_relay_scan" || success { t.Fatalf("action=%s success=%v", action, success) } } if !c.handleReconCommand("recon_relay_scan", "", "") { t.Fatal("expected handled") } }