204 lines
4.8 KiB
Go
204 lines
4.8 KiB
Go
package toolsrv
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestServer9P_RegisterAgent(t *testing.T) {
|
|
secret := []byte("test-secret-key-1234567890123456")
|
|
srv := NewServer9P(secret)
|
|
|
|
agentID := "agent-uuid-123"
|
|
cwd := "/home/user/project"
|
|
sig := ComputeRegistrationSig(secret, agentID, cwd)
|
|
|
|
// Register should succeed with valid signature
|
|
token, err := srv.RegisterAgent(agentID, cwd, sig)
|
|
if err != nil {
|
|
t.Fatalf("RegisterAgent failed: %v", err)
|
|
}
|
|
if token == "" {
|
|
t.Fatal("RegisterAgent returned empty token")
|
|
}
|
|
|
|
// Should be able to look up the agent
|
|
agent, ok := srv.GetAgent(token)
|
|
if !ok {
|
|
t.Fatal("GetAgent returned false")
|
|
}
|
|
if agent.ID != agentID {
|
|
t.Errorf("agent.ID = %q, want %q", agent.ID, agentID)
|
|
}
|
|
if agent.CWD != cwd {
|
|
t.Errorf("agent.CWD = %q, want %q", agent.CWD, cwd)
|
|
}
|
|
}
|
|
|
|
func TestServer9P_RegisterAgentBadSig(t *testing.T) {
|
|
secret := []byte("test-secret-key-1234567890123456")
|
|
srv := NewServer9P(secret)
|
|
|
|
agentID := "agent-uuid-123"
|
|
cwd := "/home/user/project"
|
|
badSig := "invalid-signature"
|
|
|
|
// Register should fail with invalid signature
|
|
_, err := srv.RegisterAgent(agentID, cwd, badSig)
|
|
if err == nil {
|
|
t.Fatal("RegisterAgent should fail with bad signature")
|
|
}
|
|
}
|
|
|
|
func TestServer9P_RegisterAgentWrongSecret(t *testing.T) {
|
|
secret := []byte("test-secret-key-1234567890123456")
|
|
wrongSecret := []byte("wrong-secret-key-abcdefghijklmnop")
|
|
srv := NewServer9P(secret)
|
|
|
|
agentID := "agent-uuid-123"
|
|
cwd := "/home/user/project"
|
|
// Sign with wrong secret
|
|
sig := ComputeRegistrationSig(wrongSecret, agentID, cwd)
|
|
|
|
// Register should fail
|
|
_, err := srv.RegisterAgent(agentID, cwd, sig)
|
|
if err == nil {
|
|
t.Fatal("RegisterAgent should fail with wrong secret")
|
|
}
|
|
}
|
|
|
|
func TestServer9P_GetAgentNotFound(t *testing.T) {
|
|
secret := []byte("test-secret-key-1234567890123456")
|
|
srv := NewServer9P(secret)
|
|
|
|
_, ok := srv.GetAgent("nonexistent-token")
|
|
if ok {
|
|
t.Fatal("GetAgent should return false for nonexistent token")
|
|
}
|
|
}
|
|
|
|
func TestServer9P_QidFor(t *testing.T) {
|
|
secret := []byte("test-secret-key-1234567890123456")
|
|
srv := NewServer9P(secret)
|
|
|
|
tests := []struct {
|
|
path string
|
|
wantDir bool
|
|
wantErr bool
|
|
}{
|
|
{"/", true, false},
|
|
{"/register", false, false},
|
|
{"/ctl", false, false},
|
|
{"/tools", false, false},
|
|
{"/info", false, false},
|
|
{"/proc", true, false},
|
|
{"/proc/new", false, false},
|
|
{"/proc/new.bg", false, false},
|
|
{"/proc/123", true, false},
|
|
{"/proc/123/out", false, false},
|
|
{"/proc/123/wait", false, false},
|
|
{"/proc/123/stat", false, false},
|
|
{"/proc/123/ctl", false, false},
|
|
{"/nonexistent", false, true},
|
|
{"/proc/123/nonexistent", false, true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
qt, _, err := srv.qidFor(tt.path)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("qidFor(%q) should error", tt.path)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Errorf("qidFor(%q) error: %v", tt.path, err)
|
|
continue
|
|
}
|
|
isDir := qt == 0x80 // QTDIR
|
|
if isDir != tt.wantDir {
|
|
t.Errorf("qidFor(%q) isDir = %v, want %v", tt.path, isDir, tt.wantDir)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseKV(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want map[string]string
|
|
}{
|
|
{
|
|
input: "key=value\n",
|
|
want: map[string]string{"key": "value"},
|
|
},
|
|
{
|
|
input: "a=1\nb=2\nc=3\n",
|
|
want: map[string]string{"a": "1", "b": "2", "c": "3"},
|
|
},
|
|
{
|
|
input: "path=/home/user/project\n",
|
|
want: map[string]string{"path": "/home/user/project"},
|
|
},
|
|
{
|
|
input: "multi=line\\none\\ntwo\n",
|
|
want: map[string]string{"multi": "line\none\ntwo"},
|
|
},
|
|
{
|
|
input: "empty=\n",
|
|
want: map[string]string{"empty": ""},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := parseKV(tt.input)
|
|
for k, v := range tt.want {
|
|
if got[k] != v {
|
|
t.Errorf("parseKV(%q)[%q] = %q, want %q", tt.input, k, got[k], v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestComputeRegistrationSig(t *testing.T) {
|
|
secret := []byte("test-secret")
|
|
|
|
sig1 := ComputeRegistrationSig(secret, "agent1", "/path1")
|
|
sig2 := ComputeRegistrationSig(secret, "agent1", "/path1")
|
|
sig3 := ComputeRegistrationSig(secret, "agent1", "/path2")
|
|
sig4 := ComputeRegistrationSig(secret, "agent2", "/path1")
|
|
|
|
// Same inputs should produce same signature
|
|
if sig1 != sig2 {
|
|
t.Error("same inputs should produce same signature")
|
|
}
|
|
|
|
// Different cwd should produce different signature
|
|
if sig1 == sig3 {
|
|
t.Error("different cwd should produce different signature")
|
|
}
|
|
|
|
// Different agent should produce different signature
|
|
if sig1 == sig4 {
|
|
t.Error("different agent should produce different signature")
|
|
}
|
|
}
|
|
|
|
func TestGenerateSecret(t *testing.T) {
|
|
s1, err := GenerateSecret()
|
|
if err != nil {
|
|
t.Fatalf("GenerateSecret failed: %v", err)
|
|
}
|
|
if len(s1) != 32 {
|
|
t.Errorf("secret length = %d, want 32", len(s1))
|
|
}
|
|
|
|
s2, err := GenerateSecret()
|
|
if err != nil {
|
|
t.Fatalf("GenerateSecret failed: %v", err)
|
|
}
|
|
|
|
// Should be different each time
|
|
if string(s1) == string(s2) {
|
|
t.Error("GenerateSecret should produce different values")
|
|
}
|
|
}
|