pnyx/packet_test.go

69 lines
1.8 KiB
Go

2024-04-03 18:52:04 -06:00
package pnyx
import (
"crypto/ed25519"
"crypto/rand"
"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)
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)
test_addr := "test_addr"
session_connect := NewSessionConnect(test_addr, secret)
parsed_session_id, parsed_addr, err := ParseSessionConnect(session_connect, secret)
fatalErr(t, err)
if parsed_addr != test_addr {
t.Fatalf("Parsed address(%s) does not match test address(%s)", parsed_addr, test_addr)
}
if parsed_session_id != SessionKeyID(secret) {
t.Fatalf("Parsed session ID %s does not match original %s", parsed_session_id, SessionKeyID(secret))
}
}