| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package model
- import (
- "reflect"
- "testing"
- )
- func TestClientToRecordRoundTripWireGuard(t *testing.T) {
- c := &Client{
- Email: "[email protected]",
- Enable: true,
- PrivateKey: "cGVlci1wcml2YXRlLWtleS1iYXNlNjQtMzJieXRlcw==",
- PublicKey: "cGVlci1wdWJsaWMta2V5LWJhc2U2NC0zMmJ5dGVzISE=",
- AllowedIPs: []string{"10.0.0.2/32", "fd00::2/128"},
- PreSharedKey: "cHNrLWJhc2U2NC0zMmJ5dGVzLXBsYWNlaG9sZGVyISE=",
- KeepAlive: 25,
- }
- rec := c.ToRecord()
- if rec.AllowedIPs != "10.0.0.2/32,fd00::2/128" {
- t.Fatalf("AllowedIPs CSV = %q, want %q", rec.AllowedIPs, "10.0.0.2/32,fd00::2/128")
- }
- got := rec.ToClient()
- for _, f := range []struct {
- name string
- a, b any
- }{
- {"PrivateKey", c.PrivateKey, got.PrivateKey},
- {"PublicKey", c.PublicKey, got.PublicKey},
- {"PreSharedKey", c.PreSharedKey, got.PreSharedKey},
- {"KeepAlive", c.KeepAlive, got.KeepAlive},
- } {
- if f.a != f.b {
- t.Errorf("%s round-trip = %v, want %v", f.name, f.b, f.a)
- }
- }
- if !reflect.DeepEqual(got.AllowedIPs, c.AllowedIPs) {
- t.Errorf("AllowedIPs round-trip = %v, want %v", got.AllowedIPs, c.AllowedIPs)
- }
- }
- func TestClientRecordEmptyAllowedIPs(t *testing.T) {
- rec := &ClientRecord{Email: "[email protected]", AllowedIPs: ""}
- if got := rec.ToClient().AllowedIPs; got != nil {
- t.Fatalf("empty CSV → AllowedIPs = %v, want nil", got)
- }
- rec.AllowedIPs = " 10.0.0.5/32 , ,"
- if got := rec.ToClient().AllowedIPs; !reflect.DeepEqual(got, []string{"10.0.0.5/32"}) {
- t.Fatalf("trimmed CSV → AllowedIPs = %v, want [10.0.0.5/32]", got)
- }
- }
- func TestMergeClientRecordWireGuardKeysPreserved(t *testing.T) {
- existing := &ClientRecord{
- Email: "[email protected]",
- PrivateKey: "existing-private",
- PublicKey: "existing-public",
- AllowedIPs: "10.0.0.7/32",
- UpdatedAt: 100,
- }
- incomingEmpty := &ClientRecord{Email: "[email protected]", UpdatedAt: 200}
- MergeClientRecord(existing, incomingEmpty)
- if existing.PrivateKey != "existing-private" || existing.PublicKey != "existing-public" {
- t.Fatalf("empty incoming wiped keys: priv=%q pub=%q", existing.PrivateKey, existing.PublicKey)
- }
- if existing.AllowedIPs != "10.0.0.7/32" {
- t.Fatalf("empty incoming wiped allowedIPs: %q", existing.AllowedIPs)
- }
- incomingNewer := &ClientRecord{
- Email: "[email protected]",
- AllowedIPs: "10.0.0.8/32",
- UpdatedAt: 300,
- }
- MergeClientRecord(existing, incomingNewer)
- if existing.AllowedIPs != "10.0.0.8/32" {
- t.Fatalf("newer allowedIPs not applied: %q", existing.AllowedIPs)
- }
- }
|