Expand P2 test coverage: mining chain, spread lanes, path forge, WS/beacon, E2E onion, file handling
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled

This commit is contained in:
AetherForge
2026-06-07 04:58:55 -07:00
parent b2a7b1723f
commit 7b2d41cda8
118 changed files with 9938 additions and 223 deletions

View File

@@ -1,9 +1,11 @@
package client
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
@@ -70,6 +72,99 @@ func TestIsBlockedDeletePath(t *testing.T) {
}
}
func TestReadFileCommandRejectsOversize(t *testing.T) {
dir := t.TempDir()
big := filepath.Join(dir, "huge.bin")
if err := os.WriteFile(big, make([]byte, maxReadFileBytes+1), 0o644); err != nil {
t.Fatal(err)
}
c := newTestClient(t)
var gotOK bool
var gotMsg string
c.commandResultHook = func(_ string, success bool, message string) {
gotOK = success
gotMsg = message
}
c.handleCommand("read_file", 0, "", big, "", "")
if gotOK {
t.Fatal("oversize read_file should fail")
}
if !strings.Contains(gotMsg, "file too large") {
t.Fatalf("message=%q", gotMsg)
}
}
func TestReadFileCommandAcceptsWithinCap(t *testing.T) {
dir := t.TempDir()
small := filepath.Join(dir, "small.txt")
want := "config-value"
if err := os.WriteFile(small, []byte(want), 0o644); err != nil {
t.Fatal(err)
}
c := newTestClient(t)
var gotOK bool
var gotMsg string
c.commandResultHook = func(_ string, success bool, message string) {
gotOK = success
gotMsg = message
}
c.handleCommand("read_file", 0, "", small, "", "")
if !gotOK || gotMsg != want {
t.Fatalf("ok=%v msg=%q want %q", gotOK, gotMsg, want)
}
}
func TestAgentConfigFileUploadReadRoundTrip(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "agent-config.json")
payload := `{"server_url":"http://127.0.0.1:8989","worker_name":"test-node"}`
encoded := base64.StdEncoding.EncodeToString([]byte(payload))
c := newTestClient(t)
uploadDone := make(chan struct{})
c.commandResultHook = func(action string, success bool, message string) {
if action == "upload" {
if !success {
t.Fatalf("upload failed: %s", message)
}
close(uploadDone)
}
}
c.handleCommand("upload", 0, "", cfgPath, encoded, "")
<-uploadDone
readDone := make(chan struct{})
var readBody string
c.commandResultHook = func(action string, success bool, message string) {
if action == "read_file" {
if !success {
t.Fatalf("read_file failed: %s", message)
}
readBody = message
close(readDone)
}
}
c.handleCommand("read_file", 0, "", cfgPath, "", "")
<-readDone
if readBody != payload {
t.Fatalf("round-trip mismatch:\nwant %q\ngot %q", payload, readBody)
}
}
func TestReadFileCommandRejectsTraversal(t *testing.T) {
c := newTestClient(t)
var gotMsg string
c.commandResultHook = func(_ string, success bool, message string) {
if !success {
gotMsg = message
}
}
c.handleCommand("read_file", 0, "", "../../etc/passwd", "", "")
if !strings.Contains(gotMsg, "path traversal") {
t.Fatalf("message=%q", gotMsg)
}
}
func TestReadDirectoryEntriesCapsCount(t *testing.T) {
dir := t.TempDir()
for i := 0; i < maxListDirEntries+10; i++ {