Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Dashboard ambient layer, comrade presence, Mission Deck and War Room, Emberwake supply chain, spread/docs publishing, fleet policy and modules API, CI docker mining, and refreshed USB pack.
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package client
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"crypto-miner-agent/config"
|
|
)
|
|
|
|
func signTestModule(m ModuleManifest, secret string) ModuleManifest {
|
|
m.Signature = ""
|
|
payload, _ := json.Marshal(m)
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
mac.Write(payload)
|
|
m.Signature = hex.EncodeToString(mac.Sum(nil))
|
|
return m
|
|
}
|
|
|
|
func TestParseFleetPolicyUpdate(t *testing.T) {
|
|
raw := json.RawMessage(`{"mining_mode":"scheduled","schedule_start":"22:00","schedule_end":"06:00","max_cpu_usage_pct":70}`)
|
|
p, err := parseFleetPolicyUpdate(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.MiningMode != "scheduled" || p.ScheduleStart != "22:00" || p.MaxCPUUsagePct != 70 {
|
|
t.Fatalf("unexpected policy: %+v", p)
|
|
}
|
|
}
|
|
|
|
func TestParseFleetPolicyUpdateWithPushID(t *testing.T) {
|
|
raw := json.RawMessage(`{"push_id":"pol-abc","mining_mode":"idle","max_cpu_usage_pct":40}`)
|
|
p, err := parseFleetPolicyUpdate(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.PushID != "pol-abc" || p.MiningMode != "idle" || p.MaxCPUUsagePct != 40 {
|
|
t.Fatalf("unexpected policy: %+v", p)
|
|
}
|
|
}
|
|
|
|
func TestApplyFleetPolicyUpdate(t *testing.T) {
|
|
cfg := config.RuntimeConfig{BuiltinConfig: config.BuiltinConfig{MiningMode: "always", MaxCPUUsage: 80}}
|
|
applyFleetPolicyUpdate(&cfg, FleetPolicyUpdate{
|
|
MiningMode: "idle",
|
|
MaxCPUUsagePct: 55,
|
|
PoolHost: "pool.example",
|
|
PoolPort: 4444,
|
|
})
|
|
if cfg.MiningMode != "idle" || cfg.MaxCPUUsage != 55 || cfg.PoolHost != "pool.example" || cfg.PoolPort != 4444 {
|
|
t.Fatalf("cfg not updated: %+v", cfg.BuiltinConfig)
|
|
}
|
|
}
|
|
|
|
func TestApplyModuleFeatures(t *testing.T) {
|
|
cfg := config.RuntimeConfig{BuiltinConfig: config.BuiltinConfig{}}
|
|
applyModuleFeatures(&cfg, map[string]interface{}{
|
|
"remote_aggressive": true,
|
|
"auto_spread": true,
|
|
"gpu_enabled": true,
|
|
})
|
|
if !cfg.RemoteAggressive || !cfg.AutoSpread || !cfg.GPUEnabled {
|
|
t.Fatalf("features not applied: %+v", cfg.BuiltinConfig)
|
|
}
|
|
}
|
|
|
|
func TestVerifyModuleSignature(t *testing.T) {
|
|
secret := "test-fleet-secret"
|
|
m := signTestModule(ModuleManifest{
|
|
Name: "crucible_ops",
|
|
Version: "1",
|
|
Features: map[string]interface{}{"remote_aggressive": true},
|
|
}, secret)
|
|
if !verifyModuleSignature(m, secret) {
|
|
t.Fatal("expected valid signature")
|
|
}
|
|
if verifyModuleSignature(m, "wrong") {
|
|
t.Fatal("expected invalid signature with wrong secret")
|
|
}
|
|
m.Features["auto_spread"] = true
|
|
if verifyModuleSignature(m, secret) {
|
|
t.Fatal("expected invalid signature after tamper")
|
|
}
|
|
}
|