package main import ( kademlia "gitlab.lrz.de/netintum/teaching/p2psec_projects_2024/DHT-6/pkg" "io" "testing" "time" ) func TestPingP2PServer(t *testing.T) { ip := "127.0.0.1" port := uint64(8080) config := &kademlia.Config{ P2PAddress: ip, P2PPort: port, KeyFile: "./certs/p2pkey.pem", CertFile: "./certs/p2pcert.pem", } dht := kademlia.NewDHT(config) go initializeP2PServer(config, dht) // Wait for server to start time.Sleep(1 * time.Second) conn, err := kademlia.ConnectToNode(dht.SelfNode) if err != nil { t.Fatalf("Testing P2PServer: Failed to connect to server: %s", err) } defer conn.Close() ping := kademlia.SerialisePingPacket(&kademlia.P2PPingPacket{ SourceIP: dht.SelfNode.IPAddress, SourcePort: uint16(dht.SelfNode.Port), Certificate: dht.SelfNode.Certificate, }) deadline := time.Now().Add(1 * time.Second) conn.SetDeadline(deadline) conn.Write(ping) reply := make([]byte, 4) io.ReadFull(conn, reply) if !kademlia.PacketsEqual(reply, kademlia.SerialisePongPacket(&kademlia.P2PPongPacket{})) { t.Fatalf("Testing P2PServer: Failed, invalid Ping response") } }