57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package peer
|
|
|
|
import (
|
|
"net/netip"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"vppn/m"
|
|
)
|
|
|
|
func TestNetworkState_RoundTrip(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "network.json")
|
|
|
|
var sign1 [32]byte
|
|
copy(sign1[:], []byte("0123456789abcdef0123456789abcdef"))
|
|
|
|
state := m.NetworkState{Peers: []m.Peer{
|
|
{
|
|
PeerIP: 1,
|
|
Name: "hub",
|
|
Addr4: netip.MustParseAddr("10.11.12.1"),
|
|
Port: 51820,
|
|
Relay: true,
|
|
WGPubKey: mustKey(t),
|
|
SignPubKey: sign1,
|
|
},
|
|
{
|
|
PeerIP: 10,
|
|
Name: "laptop",
|
|
Addr4: netip.MustParseAddr("10.11.12.10"),
|
|
Port: 51820,
|
|
WGPubKey: mustKey(t),
|
|
},
|
|
}}
|
|
|
|
if err := saveNetworkState(path, state); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err := loadNetworkState(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, state) {
|
|
t.Errorf("round-trip mismatch:\n got: %+v\nwant: %+v", got.Peers[1], state.Peers[1])
|
|
}
|
|
}
|
|
|
|
func TestNetworkState_LoadMissing(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "does-not-exist.json")
|
|
if _, err := loadNetworkState(path); err == nil {
|
|
t.Fatal("expected error loading missing cache, got nil")
|
|
}
|
|
}
|