package api import ( "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" ) func TestAgentBinaryCandidates(t *testing.T) { dir := "/srv/usb" cases := []struct { platform string wantName string wantPath string }{ {"windows", "crypto-miner-agent.exe", filepath.Join(dir, "agent", "crypto-miner-agent.exe")}, {"mac", "crypto-miner-agent", filepath.Join(dir, "agent", "crypto-miner-agent-darwin")}, {"linux", "crypto-miner-agent", filepath.Join(dir, "agent", "crypto-miner-agent-linux")}, } for _, tc := range cases { candidates, dlName := agentBinaryCandidates(tc.platform, dir) if dlName != tc.wantName { t.Fatalf("platform=%s dlName=%q want %q", tc.platform, dlName, tc.wantName) } if len(candidates) == 0 || candidates[0] != tc.wantPath { t.Fatalf("platform=%s candidates=%v want first %q", tc.platform, candidates, tc.wantPath) } } } func TestFindAgentBinaryPrefersAgentSubdir(t *testing.T) { root := t.TempDir() agentDir := filepath.Join(root, "agent") if err := os.MkdirAll(agentDir, 0755); err != nil { t.Fatal(err) } nested := filepath.Join(agentDir, "crypto-miner-agent.exe") rootLevel := filepath.Join(root, "crypto-miner-agent.exe") if err := os.WriteFile(nested, []byte("nested-agent"), 0644); err != nil { t.Fatal(err) } if err := os.WriteFile(rootLevel, []byte("root-agent"), 0644); err != nil { t.Fatal(err) } got, dlName, ok := findAgentBinary("windows", root) if !ok || got != nested { t.Fatalf("findAgentBinary = %q ok=%v want nested %q", got, ok, nested) } if dlName != "crypto-miner-agent.exe" { t.Fatalf("dlName=%q", dlName) } } func TestFindAgentBinaryFallbackRootLevel(t *testing.T) { root := t.TempDir() bin := filepath.Join(root, "crypto-miner-agent-linux") if err := os.WriteFile(bin, []byte("linux-agent"), 0644); err != nil { t.Fatal(err) } got, dlName, ok := findAgentBinary("linux", root) if !ok || got != bin { t.Fatalf("findAgentBinary = %q ok=%v", got, ok) } if dlName != "crypto-miner-agent" { t.Fatalf("dlName=%q", dlName) } } func TestFindAgentBinaryMacGenericFallback(t *testing.T) { root := t.TempDir() agentDir := filepath.Join(root, "agent") if err := os.MkdirAll(agentDir, 0755); err != nil { t.Fatal(err) } bin := filepath.Join(agentDir, "crypto-miner-agent") if err := os.WriteFile(bin, []byte("generic-unix-agent"), 0644); err != nil { t.Fatal(err) } got, _, ok := findAgentBinary("mac", root) if !ok || got != bin { t.Fatalf("findAgentBinary = %q ok=%v", got, ok) } } func TestFindAgentBinaryMissing(t *testing.T) { _, _, ok := findAgentBinary("mac", t.TempDir()) if ok { t.Fatal("expected missing binary") } } func withAgentBinarySearchDir(t *testing.T, root string) { t.Helper() orig := agentBinarySearchDir agentBinarySearchDir = func() (string, error) { return root, nil } t.Cleanup(func() { agentBinarySearchDir = orig }) } func TestServeAgentBinaryDownloadWindows(t *testing.T) { root := t.TempDir() bin := filepath.Join(root, "agent", "crypto-miner-agent.exe") if err := os.MkdirAll(filepath.Dir(bin), 0755); err != nil { t.Fatal(err) } content := []byte("MZ-fake-windows-agent") if err := os.WriteFile(bin, content, 0644); err != nil { t.Fatal(err) } withAgentBinarySearchDir(t, root) req := httptest.NewRequest(http.MethodGet, "/api/download/agent-windows", nil) rec := httptest.NewRecorder() serveAgentBinary("windows")(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) } if rec.Body.String() != string(content) { t.Fatalf("body=%q", rec.Body.String()) } if !strings.Contains(rec.Header().Get("Content-Disposition"), "crypto-miner-agent.exe") { t.Fatalf("disposition=%q", rec.Header().Get("Content-Disposition")) } } func TestServeAgentBinaryDownloadMac(t *testing.T) { root := t.TempDir() bin := filepath.Join(root, "crypto-miner-agent-darwin") if err := os.WriteFile(bin, []byte("darwin-agent"), 0644); err != nil { t.Fatal(err) } withAgentBinarySearchDir(t, root) req := httptest.NewRequest(http.MethodGet, "/api/download/agent-mac", nil) rec := httptest.NewRecorder() serveAgentBinary("mac")(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status=%d", rec.Code) } if !strings.Contains(rec.Header().Get("Content-Disposition"), "crypto-miner-agent") { t.Fatalf("disposition=%q", rec.Header().Get("Content-Disposition")) } } func TestServeAgentBinaryNotFound(t *testing.T) { withAgentBinarySearchDir(t, t.TempDir()) req := httptest.NewRequest(http.MethodGet, "/api/download/agent-linux", nil) rec := httptest.NewRecorder() serveAgentBinary("linux")(rec, req) if rec.Code != http.StatusNotFound { t.Fatalf("status=%d want 404", rec.Code) } }