69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
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")
|
|
}
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|