113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package pnyx
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"net"
|
|
"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)
|
|
client_id := ID[PeerID](client_pubkey)
|
|
|
|
server_pubkey, server_key, err := ed25519.GenerateKey(rand.Reader)
|
|
fatalErr(t, err)
|
|
server_id := ID[PeerID](server_pubkey)
|
|
|
|
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)
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
func TestSessionConnect(t *testing.T) {
|
|
secret := make([]byte, 32)
|
|
test_addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")
|
|
fatalErr(t, err)
|
|
|
|
session_connect := NewSessionConnect(test_addr, secret)
|
|
parsed_addr, err := ParseSessionConnect(session_connect[COMMAND_LENGTH + ID_LENGTH:], secret)
|
|
fatalErr(t, err)
|
|
|
|
if parsed_addr.String() != test_addr.String() {
|
|
t.Fatalf("Parsed address(%s) does not match test address(%s)", parsed_addr, test_addr)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|