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)
|
|
|
|
|
|
|
|
session_open, client_ecdh, err := NewSessionOpen(client_key)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2024-04-06 16:38:14 -06:00
|
|
|
client_pubkey_parsed, client_ecdh_parsed, err := ParseSessionOpen(session_open[COMMAND_LENGTH:])
|
2024-04-03 18:52:04 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
if slices.Compare(client_pubkey, client_pubkey_parsed) != 0 {
|
|
|
|
t.Fatalf("Client Pubkey %x does not match parsed %x", client_pubkey, client_pubkey_parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if slices.Compare(client_ecdh.Public().(ed25519.PublicKey), client_ecdh_parsed) != 0 {
|
|
|
|
t.Fatalf("Client Pubkey %x does not match parsed %x", client_pubkey, client_pubkey_parsed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
session_connect := NewSessionConnect(test_addr, secret)
|
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)
|
|
|
|
}
|
|
|
|
}
|