Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
154 lines
4.3 KiB
Go
154 lines
4.3 KiB
Go
package deploy
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"crypto-miner-agent/config"
|
|
)
|
|
|
|
func TestVerifyDeployPlanSignatureAgent(t *testing.T) {
|
|
plan := DeployPlanBody{
|
|
JoinLane: "winrm",
|
|
Action: "winrm",
|
|
Script: "# noop",
|
|
}
|
|
payload, _ := json.Marshal(plan)
|
|
mac := hmac.New(sha256.New, []byte("fleet-test"))
|
|
mac.Write(payload)
|
|
sig := hex.EncodeToString(mac.Sum(nil))
|
|
if !VerifyDeployPlanSignature(plan, sig, "fleet-test") {
|
|
t.Fatal("expected valid signature")
|
|
}
|
|
}
|
|
|
|
func TestRunDiscoverAndJoinFakeServices(t *testing.T) {
|
|
cfg := config.RuntimeConfig{
|
|
BuiltinConfig: config.BuiltinConfig{
|
|
FleetSecret: "fleet-test",
|
|
WorkerName: "test-worker",
|
|
ServerURL: "http://127.0.0.1:8989",
|
|
},
|
|
}
|
|
|
|
fetch := func(services []DeployServiceFinding, uncPath string) (DeployPlanResponse, error) {
|
|
if len(services) == 0 {
|
|
t.Fatal("expected services")
|
|
}
|
|
if services[0].Name != "CCMEXEC" {
|
|
t.Fatalf("service=%q", services[0].Name)
|
|
}
|
|
plan := DeployPlanBody{
|
|
JoinLane: "gpo",
|
|
Action: "gpo",
|
|
Script: "$env:AETHER_DEFER_MINING='1'",
|
|
}
|
|
payload, _ := json.Marshal(plan)
|
|
mac := hmac.New(sha256.New, []byte(cfg.FleetSecret))
|
|
mac.Write(payload)
|
|
return DeployPlanResponse{
|
|
OK: true,
|
|
JoinLane: "gpo",
|
|
Plan: plan,
|
|
Signature: hex.EncodeToString(mac.Sum(nil)),
|
|
}, nil
|
|
}
|
|
|
|
// Inject fake discovery via ParseServiceDiscoverJSON path
|
|
oldDiscover := runServiceDiscoverFn
|
|
runServiceDiscoverFn = func(maxLANHosts int) string {
|
|
return `{"probed_at":"2026-06-06T12:00:00Z","local":{"host":"10.0.0.1","subnet":"10.0.0","services":[{"service_name":"CCMEXEC","status":"running","join_lane_candidate":"gpo","source":"local_service"}]}}`
|
|
}
|
|
defer func() { runServiceDiscoverFn = oldDiscover }()
|
|
|
|
lane, detail, err := RunDiscoverAndJoin(cfg, 8, fetch)
|
|
if err != nil {
|
|
// gpo script execution may fail on non-windows — still expect lane selection + signature pass
|
|
if lane != "gpo" {
|
|
t.Fatalf("lane=%q err=%v", lane, err)
|
|
}
|
|
return
|
|
}
|
|
if lane != "gpo" {
|
|
t.Fatalf("lane=%q detail=%q", lane, detail)
|
|
}
|
|
}
|
|
|
|
func TestRunDiscoverAndJoinDOPeerPlan(t *testing.T) {
|
|
cfg := config.RuntimeConfig{
|
|
BuiltinConfig: config.BuiltinConfig{
|
|
FleetSecret: "fleet-test",
|
|
WorkerName: "test-worker",
|
|
ServerURL: "http://127.0.0.1:8989",
|
|
},
|
|
}
|
|
|
|
payload := []byte("do-peer-signed-plan")
|
|
sum := sha256.Sum256(payload)
|
|
hash := hex.EncodeToString(sum[:])
|
|
|
|
oldBits := doPeerDownloadBITSFn
|
|
doPeerDownloadBITSFn = func(url, dest string) error {
|
|
return os.WriteFile(dest, payload, 0o644)
|
|
}
|
|
defer func() { doPeerDownloadBITSFn = oldBits }()
|
|
|
|
fetch := func(services []DeployServiceFinding, uncPath string) (DeployPlanResponse, error) {
|
|
plan := DeployPlanBody{
|
|
JoinLane: "do_peer",
|
|
Action: "do_peer",
|
|
PeerGroup: "af-peer-lab",
|
|
Manifest: &StagingManifest{
|
|
Method: "bits",
|
|
Chunks: []StagingChunk{{URL: "http://127.0.0.1/chunk", File: "peer-0.bin"}},
|
|
SHA256: hash,
|
|
Dest: "do-peer-test-worker.exe",
|
|
Launch: "exe",
|
|
DeferMining: true,
|
|
SpreadInstall: true,
|
|
},
|
|
}
|
|
payloadJSON, _ := json.Marshal(plan)
|
|
mac := hmac.New(sha256.New, []byte(cfg.FleetSecret))
|
|
mac.Write(payloadJSON)
|
|
return DeployPlanResponse{
|
|
OK: true,
|
|
JoinLane: "do_peer",
|
|
Plan: plan,
|
|
Signature: hex.EncodeToString(mac.Sum(nil)),
|
|
}, nil
|
|
}
|
|
|
|
oldDiscover := runServiceDiscoverFn
|
|
runServiceDiscoverFn = func(maxLANHosts int) string {
|
|
return `{"probed_at":"2026-06-06T12:00:00Z","local":{"host":"10.0.0.1","subnet":"10.0.0","services":[{"service_name":"DoSvc","status":"running","join_lane_candidate":"do_peer","source":"local_service"}]}}`
|
|
}
|
|
defer func() { runServiceDiscoverFn = oldDiscover }()
|
|
|
|
lane, detail, err := RunDiscoverAndJoin(cfg, 8, fetch)
|
|
if lane != "do_peer" {
|
|
t.Fatalf("lane=%q err=%v", lane, err)
|
|
}
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "Windows-only") || strings.Contains(err.Error(), "launch") {
|
|
return
|
|
}
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if detail == "" {
|
|
t.Fatal("expected success detail")
|
|
}
|
|
}
|
|
|
|
func TestExecuteDeployPlanDOPeerRequiresManifest(t *testing.T) {
|
|
_, err := ExecuteDeployPlan(config.RuntimeConfig{}, DeployPlanBody{JoinLane: "do_peer", Action: "do_peer"})
|
|
if err == nil || !strings.Contains(err.Error(), "requires staging manifest") {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
}
|