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) session_open, client_ecdh, err := NewSessionOpen(client_key) fatalErr(t, err) client_pubkey_parsed, client_ecdh_parsed, err := ParseSessionOpen(session_open[COMMAND_LENGTH:]) 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") } } 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) } }