pnyx/packet_test.go

113 lines
3.1 KiB
Go

2024-04-03 18:52:04 -06:00
package pnyx
import (
"crypto/ed25519"
"crypto/rand"
"net"
2024-04-03 18:52:04 -06:00
"slices"
"testing"
)
func fatalErr(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
func TestSessionOpen(t *testing.T) {
client_pubkey, client_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
2024-04-07 13:27:28 -06:00
client_id := ID[PeerID](client_pubkey)
2024-04-03 18:52:04 -06:00
2024-04-07 13:27:28 -06:00
server_pubkey, server_key, err := ed25519.GenerateKey(rand.Reader)
2024-04-03 18:52:04 -06:00
fatalErr(t, err)
2024-04-07 13:27:28 -06:00
server_id := ID[PeerID](server_pubkey)
2024-04-03 18:52:04 -06:00
2024-04-07 13:27:28 -06:00
client_so, client_ecdh, err := NewSessionOpen(client_key)
2024-04-03 18:52:04 -06:00
fatalErr(t, err)
2024-04-07 13:27:28 -06:00
server_so, server_ecdh, err := NewSessionOpen(server_key)
fatalErr(t, err)
server_session, err := ParseSessionOpen(server_ecdh, client_so[COMMAND_LENGTH:])
fatalErr(t, err)
client_session, err := ParseSessionOpen(client_ecdh, server_so[COMMAND_LENGTH:])
fatalErr(t, err)
if client_id != server_session.Peer {
t.Fatalf("Server session(%s) has wrong peer ID(%s)", server_session.Peer, client_id)
}
if server_id != client_session.Peer {
t.Fatalf("Client session(%s) has wrong peer ID(%s)", server_session.Peer, client_id)
2024-04-03 18:52:04 -06:00
}
2024-04-07 13:27:28 -06:00
if slices.Compare(server_session.secret, client_session.secret) != 0 {
t.Fatalf("Client secret(%x) and server secret(%x) do not match", client_session.secret, server_session.secret)
2024-04-03 18:52:04 -06:00
}
}
func TestECDH(t *testing.T) {
client_public, client_private, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
server_public, server_private, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
server_secret, err := ECDH(client_public, server_private)
fatalErr(t, err)
client_secret, err := ECDH(server_public, client_private)
fatalErr(t, err)
if slices.Compare(server_secret, client_secret) != 0 {
t.Fatalf("Server and Client secrets do not match")
}
}
2024-04-03 19:31:58 -06:00
func TestSessionConnect(t *testing.T) {
secret := make([]byte, 32)
test_addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")
fatalErr(t, err)
2024-04-03 19:31:58 -06:00
session_connect := NewSessionConnect(test_addr, secret)
parsed_addr, err := ParseSessionConnect(session_connect[COMMAND_LENGTH + ID_LENGTH:], secret)
2024-04-03 19:31:58 -06:00
fatalErr(t, err)
if parsed_addr.String() != test_addr.String() {
2024-04-03 19:31:58 -06:00
t.Fatalf("Parsed address(%s) does not match test address(%s)", parsed_addr, test_addr)
}
}
2024-04-07 13:27:28 -06:00
func TestSessionData(t *testing.T) {
_, client_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
_, server_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
client_so, client_ecdh, err := NewSessionOpen(client_key)
fatalErr(t, err)
server_so, server_ecdh, err := NewSessionOpen(server_key)
fatalErr(t, err)
server_session, err := ParseSessionOpen(server_ecdh, client_so[COMMAND_LENGTH:])
fatalErr(t, err)
client_session, err := ParseSessionOpen(client_ecdh, server_so[COMMAND_LENGTH:])
fatalErr(t, err)
message := []byte("hello")
server_hello, err := NewSessionData(&server_session, message)
fatalErr(t, err)
parsed_message, err := ParseSessionData(&client_session, server_hello[COMMAND_LENGTH+ID_LENGTH:])
fatalErr(t, err)
if slices.Compare(message, parsed_message) != 0 {
t.Fatalf("Parsed message(%s) != test message(%s)", parsed_message, message)
}
}