56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package peer
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"vppn/m"
|
|
)
|
|
|
|
func TestNetworkState_RoundTrip(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "network.json")
|
|
|
|
state := m.NetworkState{Peers: []m.Peer{
|
|
{
|
|
PeerIP: 1,
|
|
Version: 7,
|
|
Name: "hub",
|
|
Addr4: []byte{10, 11, 12, 1},
|
|
Port: 51820,
|
|
Relay: true,
|
|
WGPubKey: make([]byte, 32),
|
|
SignPubKey: make([]byte, 32),
|
|
},
|
|
{
|
|
PeerIP: 10,
|
|
Version: 3,
|
|
Name: "laptop",
|
|
Addr4: []byte{10, 11, 12, 10},
|
|
Port: 51820,
|
|
WGPubKey: []byte("0123456789abcdef0123456789abcdef"),
|
|
SignPubKey: []byte("fedcba9876543210fedcba9876543210"),
|
|
},
|
|
}}
|
|
|
|
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")
|
|
}
|
|
}
|