68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package peer
|
|
|
|
import (
|
|
"net/netip"
|
|
"sync"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
)
|
|
|
|
// fakeWGDevice records every call made to it. It is safe to read Calls after
|
|
// the event loop has processed the event under test (single-threaded loop
|
|
// means no extra synchronisation needed, but the mutex guards concurrent test
|
|
// helpers if needed).
|
|
type fakeWGDevice struct {
|
|
mu sync.Mutex
|
|
Calls []fakeCall
|
|
peers []wgtypes.Peer
|
|
}
|
|
|
|
type fakeCall struct {
|
|
Method string
|
|
PubKey wgtypes.Key
|
|
Endpoint netip.AddrPort
|
|
VPNiP netip.Addr
|
|
Network netip.Prefix
|
|
}
|
|
|
|
func (f *fakeWGDevice) record(c fakeCall) {
|
|
f.mu.Lock()
|
|
f.Calls = append(f.Calls, c)
|
|
f.mu.Unlock()
|
|
}
|
|
|
|
func (f *fakeWGDevice) Name() string { return "wg-test" }
|
|
|
|
func (f *fakeWGDevice) Peers() ([]wgtypes.Peer, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
out := make([]wgtypes.Peer, len(f.peers))
|
|
copy(out, f.peers)
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeWGDevice) AddDirect(pubKey wgtypes.Key, endpoint netip.AddrPort, vpnIP netip.Addr) error {
|
|
f.record(fakeCall{Method: "AddDirect", PubKey: pubKey, Endpoint: endpoint, VPNiP: vpnIP})
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWGDevice) SetRelay(pubKey wgtypes.Key, endpoint netip.AddrPort, network netip.Prefix) error {
|
|
f.record(fakeCall{Method: "SetRelay", PubKey: pubKey, Endpoint: endpoint, Network: network})
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWGDevice) AddProbe(pubKey wgtypes.Key, endpoint netip.AddrPort) error {
|
|
f.record(fakeCall{Method: "AddProbe", PubKey: pubKey, Endpoint: endpoint})
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWGDevice) Promote(pubKey wgtypes.Key, vpnIP netip.Addr) error {
|
|
f.record(fakeCall{Method: "Promote", PubKey: pubKey, VPNiP: vpnIP})
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWGDevice) RemovePeer(pubKey wgtypes.Key) error {
|
|
f.record(fakeCall{Method: "RemovePeer", PubKey: pubKey})
|
|
return nil
|
|
}
|