2024-04-03 18:52:04 -06:00
|
|
|
package pnyx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
2024-04-06 16:38:14 -06:00
|
|
|
"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:35:01 -06:00
|
|
|
if client_so[0] != byte(SESSION_OPEN) {
|
|
|
|
t.Fatalf("Session open command byte mismatch(%x != %x)", client_so[0], SESSION_OPEN)
|
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
|
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)
|
2024-04-06 16:38:14 -06:00
|
|
|
test_addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")
|
|
|
|
fatalErr(t, err)
|
2024-04-03 19:31:58 -06:00
|
|
|
|
2024-04-07 13:35:01 -06:00
|
|
|
session_id := ID[SessionID](secret)
|
2024-04-03 19:31:58 -06:00
|
|
|
session_connect := NewSessionConnect(test_addr, secret)
|
2024-04-07 13:35:01 -06:00
|
|
|
if session_connect[0] != byte(SESSION_CONNECT) {
|
|
|
|
t.Fatalf("Session open command byte mismatch(%x != %x)", session_connect[0], SESSION_CONNECT)
|
|
|
|
} else if slices.Compare(session_connect[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH], session_id[:]) != 0 {
|
|
|
|
t.Fatal("Session open ID mismatch")
|
|
|
|
}
|
|
|
|
|
2024-04-06 16:38:14 -06:00
|
|
|
parsed_addr, err := ParseSessionConnect(session_connect[COMMAND_LENGTH + ID_LENGTH:], secret)
|
2024-04-03 19:31:58 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-04-06 16:38:14 -06:00
|
|
|
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")
|
2024-04-07 13:35:01 -06:00
|
|
|
session_data, err := NewSessionData(&server_session, message)
|
2024-04-07 13:27:28 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-04-07 13:35:01 -06:00
|
|
|
if session_data[0] != byte(SESSION_DATA) {
|
|
|
|
t.Fatalf("Session data command byte mismatch(%x != %x)", session_data[0], SESSION_DATA)
|
|
|
|
} else if slices.Compare(session_data[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH], server_session.ID[:]) != 0 {
|
|
|
|
t.Fatal("Session data ID mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed_message, err := ParseSessionData(&client_session, session_data[COMMAND_LENGTH+ID_LENGTH:])
|
2024-04-07 13:27:28 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
if slices.Compare(message, parsed_message) != 0 {
|
|
|
|
t.Fatalf("Parsed message(%s) != test message(%s)", parsed_message, message)
|
|
|
|
}
|
|
|
|
}
|
2024-04-07 13:29:17 -06:00
|
|
|
|
|
|
|
func TestSessionClose(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)
|
|
|
|
|
|
|
|
session_close := NewSessionClose(&client_session)
|
2024-04-07 13:35:01 -06:00
|
|
|
|
|
|
|
if session_close[0] != byte(SESSION_CLOSE) {
|
|
|
|
t.Fatalf("Session close command byte mismatch(%x != %x)", session_close[0], SESSION_CLOSE)
|
|
|
|
} else if slices.Compare(session_close[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH], server_session.ID[:]) != 0 {
|
|
|
|
t.Fatal("Session close ID mismatch")
|
|
|
|
}
|
|
|
|
|
2024-04-07 13:29:17 -06:00
|
|
|
fatalErr(t, ParseSessionClose(&server_session, session_close[COMMAND_LENGTH+ID_LENGTH:]))
|
|
|
|
}
|