135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
package toolsrv
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestServer9P_AuthenticateFirst(t *testing.T) {
|
|
srv := NewServer9P()
|
|
|
|
secret := "test-secret-0123456789abcdef"
|
|
|
|
// First auth sets the secret and returns a token
|
|
token, err := srv.Authenticate(secret)
|
|
if err != nil {
|
|
t.Fatalf("Authenticate failed: %v", err)
|
|
}
|
|
if token == "" {
|
|
t.Fatal("Authenticate returned empty token")
|
|
}
|
|
|
|
// Token() should return the same value
|
|
if srv.Token() != token {
|
|
t.Errorf("Token() = %q, want %q", srv.Token(), token)
|
|
}
|
|
}
|
|
|
|
func TestServer9P_AuthenticateSameSecret(t *testing.T) {
|
|
srv := NewServer9P()
|
|
|
|
secret := "test-secret-0123456789abcdef"
|
|
|
|
// First auth
|
|
token1, err := srv.Authenticate(secret)
|
|
if err != nil {
|
|
t.Fatalf("First Authenticate failed: %v", err)
|
|
}
|
|
|
|
// Second auth with same secret should succeed and return same token
|
|
token2, err := srv.Authenticate(secret)
|
|
if err != nil {
|
|
t.Fatalf("Second Authenticate failed: %v", err)
|
|
}
|
|
|
|
if token1 != token2 {
|
|
t.Errorf("Token changed: %q -> %q", token1, token2)
|
|
}
|
|
}
|
|
|
|
func TestServer9P_AuthenticateWrongSecret(t *testing.T) {
|
|
srv := NewServer9P()
|
|
|
|
secret := "test-secret-0123456789abcdef"
|
|
wrongSecret := "wrong-secret-fedcba9876543210"
|
|
|
|
// First auth establishes the secret
|
|
_, err := srv.Authenticate(secret)
|
|
if err != nil {
|
|
t.Fatalf("First Authenticate failed: %v", err)
|
|
}
|
|
|
|
// Second auth with wrong secret should fail
|
|
_, err = srv.Authenticate(wrongSecret)
|
|
if err == nil {
|
|
t.Fatal("Authenticate should fail with wrong secret")
|
|
}
|
|
}
|
|
|
|
func TestServer9P_TokenBeforeAuth(t *testing.T) {
|
|
srv := NewServer9P()
|
|
|
|
// Token should be empty before authentication
|
|
if srv.Token() != "" {
|
|
t.Errorf("Token() = %q before auth, want empty", srv.Token())
|
|
}
|
|
}
|
|
|
|
func TestServer9P_BuildTree(t *testing.T) {
|
|
srv := NewServer9P()
|
|
|
|
tree := srv.BuildTree()
|
|
if tree == nil {
|
|
t.Fatal("BuildTree returned nil")
|
|
}
|
|
}
|
|
|
|
func TestServer9P_HostInfo(t *testing.T) {
|
|
srv := NewServer9P()
|
|
|
|
info := srv.HostInfo()
|
|
if info == "" {
|
|
t.Fatal("HostInfo returned empty string")
|
|
}
|
|
|
|
// Should contain platform info
|
|
if !contains(info, "platform=") {
|
|
t.Error("HostInfo missing platform=")
|
|
}
|
|
if !contains(info, "arch=") {
|
|
t.Error("HostInfo missing arch=")
|
|
}
|
|
}
|
|
|
|
func TestGenerateSecret(t *testing.T) {
|
|
s1, err := GenerateSecret()
|
|
if err != nil {
|
|
t.Fatalf("GenerateSecret failed: %v", err)
|
|
}
|
|
if s1 == "" {
|
|
t.Fatal("GenerateSecret returned empty string")
|
|
}
|
|
// 32 bytes -> 64 hex chars
|
|
if len(s1) != 64 {
|
|
t.Errorf("GenerateSecret returned %d chars, want 64", len(s1))
|
|
}
|
|
|
|
// Two calls should return different secrets
|
|
s2, _ := GenerateSecret()
|
|
if s1 == s2 {
|
|
t.Error("GenerateSecret returned same secret twice")
|
|
}
|
|
}
|
|
|
|
func contains(s, substr string) bool {
|
|
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsAt(s, substr))
|
|
}
|
|
|
|
func containsAt(s, substr string) bool {
|
|
for i := 0; i <= len(s)-len(substr); i++ {
|
|
if s[i:i+len(substr)] == substr {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|