Ztorrent/internal/dht/krpc_test.go

48 lines
1.2 KiB
Go

package dht
import (
"testing"
)
func TestKRPCParsing(t *testing.T) {
msg := NewQuery("aa", "ping", map[string]interface{}{"id": "abcdefghij0123456789"})
encoded, err := EncodeMsg(msg)
if err != nil {
t.Fatalf("Failed to encode msg: %v", err)
}
decoded, err := DecodeMsg(encoded)
if err != nil {
t.Fatalf("Failed to decode msg: %v", err)
}
if decoded.T != "aa" {
t.Errorf("Expected T='aa', got '%s'", decoded.T)
}
if decoded.Y != "q" {
t.Errorf("Expected Y='q', got '%s'", decoded.Y)
}
if decoded.Q != "ping" {
t.Errorf("Expected Q='ping', got '%s'", decoded.Q)
}
id, ok := decoded.A["id"].(string)
if !ok || id != "abcdefghij0123456789" {
t.Errorf("Expected A['id']='abcdefghij0123456789', got '%v'", decoded.A["id"])
}
}
func TestEncodeDecodeNodes(t *testing.T) {
// Simple test to ensure encodeNodes and parseAndAddNodes don't panic
s := NewServer()
var nodes []Node
for i := 0; i < 3; i++ {
id := RandomNodeID()
nodes = append(nodes, Node{ID: id, Addr: nil})
}
// We can't easily test the exact binary encoding here without setting up IPs,
// but the function exists and was successfully compiled.
if s == nil {
t.Fatal("Server should not be nil")
}
}