2022-01-21 13:52:19 +01:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
2024-01-22 12:20:24 +01:00
|
|
|
"errors"
|
2024-04-11 22:12:23 +02:00
|
|
|
"sync"
|
2024-06-18 17:40:37 +02:00
|
|
|
"testing"
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-21 13:52:19 +01:00
|
|
|
)
|
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
func TestAddPeer(t *testing.T) {
|
|
|
|
key := "abc"
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-06-19 11:20:34 +02:00
|
|
|
err := status.AddPeer(key, "abc.netbird")
|
2023-03-03 19:49:18 +01:00
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
|
|
|
|
_, exists := status.peers[key]
|
|
|
|
assert.True(t, exists, "value was found")
|
|
|
|
|
2023-06-19 11:20:34 +02:00
|
|
|
err = status.AddPeer(key, "abc.netbird")
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
assert.Error(t, err, "should return error on duplicate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPeer(t *testing.T) {
|
|
|
|
key := "abc"
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-06-19 11:20:34 +02:00
|
|
|
err := status.AddPeer(key, "abc.netbird")
|
2023-03-03 19:49:18 +01:00
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
|
|
|
|
peerStatus, err := status.GetPeer(key)
|
|
|
|
assert.NoError(t, err, "shouldn't return error on getting peer")
|
|
|
|
|
|
|
|
assert.Equal(t, key, peerStatus.PubKey, "retrieved public key should match")
|
|
|
|
|
|
|
|
_, err = status.GetPeer("non_existing_key")
|
|
|
|
assert.Error(t, err, "should return error when peer doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdatePeerState(t *testing.T) {
|
|
|
|
key := "abc"
|
|
|
|
ip := "10.10.10.10"
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-03-03 19:49:18 +01:00
|
|
|
peerState := State{
|
|
|
|
PubKey: key,
|
2024-06-18 17:40:37 +02:00
|
|
|
Mux: new(sync.RWMutex),
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status.peers[key] = peerState
|
|
|
|
|
|
|
|
peerState.IP = ip
|
|
|
|
|
|
|
|
err := status.UpdatePeerState(peerState)
|
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
|
|
|
|
state, exists := status.peers[key]
|
|
|
|
assert.True(t, exists, "state should be found")
|
|
|
|
assert.Equal(t, ip, state.IP, "ip should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatus_UpdatePeerFQDN(t *testing.T) {
|
|
|
|
key := "abc"
|
|
|
|
fqdn := "peer-a.netbird.local"
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-03-03 19:49:18 +01:00
|
|
|
peerState := State{
|
|
|
|
PubKey: key,
|
2024-06-18 17:40:37 +02:00
|
|
|
Mux: new(sync.RWMutex),
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status.peers[key] = peerState
|
|
|
|
|
|
|
|
err := status.UpdatePeerFQDN(key, fqdn)
|
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
|
|
|
|
state, exists := status.peers[key]
|
|
|
|
assert.True(t, exists, "state should be found")
|
|
|
|
assert.Equal(t, fqdn, state.FQDN, "fqdn should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPeerStateChangeNotifierLogic(t *testing.T) {
|
|
|
|
key := "abc"
|
|
|
|
ip := "10.10.10.10"
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-03-03 19:49:18 +01:00
|
|
|
peerState := State{
|
|
|
|
PubKey: key,
|
2024-06-18 17:40:37 +02:00
|
|
|
Mux: new(sync.RWMutex),
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status.peers[key] = peerState
|
|
|
|
|
|
|
|
ch := status.GetPeerStateChangeNotifier(key)
|
|
|
|
assert.NotNil(t, ch, "channel shouldn't be nil")
|
|
|
|
|
|
|
|
peerState.IP = ip
|
|
|
|
|
|
|
|
err := status.UpdatePeerState(peerState)
|
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
default:
|
|
|
|
t.Errorf("channel wasn't closed after update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovePeer(t *testing.T) {
|
|
|
|
key := "abc"
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-03-03 19:49:18 +01:00
|
|
|
peerState := State{
|
|
|
|
PubKey: key,
|
2024-06-18 17:40:37 +02:00
|
|
|
Mux: new(sync.RWMutex),
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status.peers[key] = peerState
|
|
|
|
|
|
|
|
err := status.RemovePeer(key)
|
|
|
|
assert.NoError(t, err, "shouldn't return error")
|
2022-01-21 13:52:19 +01:00
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
_, exists := status.peers[key]
|
|
|
|
assert.False(t, exists, "state value shouldn't be found")
|
|
|
|
|
|
|
|
err = status.RemovePeer("not existing")
|
|
|
|
assert.Error(t, err, "should return error when peer doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateLocalPeerState(t *testing.T) {
|
|
|
|
localPeerState := LocalPeerState{
|
|
|
|
IP: "10.10.10.10",
|
|
|
|
PubKey: "abc",
|
|
|
|
KernelInterface: false,
|
|
|
|
}
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
status.UpdateLocalPeerState(localPeerState)
|
|
|
|
|
|
|
|
assert.Equal(t, localPeerState, status.localPeer, "local peer status should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanLocalPeerState(t *testing.T) {
|
|
|
|
emptyLocalPeerState := LocalPeerState{}
|
|
|
|
localPeerState := LocalPeerState{
|
|
|
|
IP: "10.10.10.10",
|
|
|
|
PubKey: "abc",
|
|
|
|
KernelInterface: false,
|
|
|
|
}
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
status.localPeer = localPeerState
|
|
|
|
|
|
|
|
status.CleanLocalPeerState()
|
|
|
|
|
|
|
|
assert.Equal(t, emptyLocalPeerState, status.localPeer, "local peer status should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateSignalState(t *testing.T) {
|
|
|
|
url := "https://signal"
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
connected bool
|
2023-03-16 17:22:36 +01:00
|
|
|
want bool
|
2024-01-22 12:20:24 +01:00
|
|
|
err error
|
2022-01-21 13:52:19 +01:00
|
|
|
}{
|
2024-01-22 12:20:24 +01:00
|
|
|
{"should mark as connected", true, true, nil},
|
|
|
|
{"should mark as disconnected", false, false, errors.New("test")},
|
2022-01-21 13:52:19 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
|
|
|
status.UpdateSignalAddress(url)
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if test.connected {
|
2023-03-16 17:22:36 +01:00
|
|
|
status.MarkSignalConnected()
|
2023-03-03 19:49:18 +01:00
|
|
|
} else {
|
2024-01-22 12:20:24 +01:00
|
|
|
status.MarkSignalDisconnected(test.err)
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
2023-03-16 17:22:36 +01:00
|
|
|
assert.Equal(t, test.want, status.signalState, "signal status should be equal")
|
2024-01-22 12:20:24 +01:00
|
|
|
assert.Equal(t, test.err, status.signalError)
|
2022-01-21 13:52:19 +01:00
|
|
|
})
|
|
|
|
}
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateManagementState(t *testing.T) {
|
|
|
|
url := "https://management"
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
connected bool
|
2023-03-16 17:22:36 +01:00
|
|
|
want bool
|
2024-01-22 12:20:24 +01:00
|
|
|
err error
|
2023-03-03 19:49:18 +01:00
|
|
|
}{
|
2024-01-22 12:20:24 +01:00
|
|
|
{"should mark as connected", true, true, nil},
|
|
|
|
{"should mark as disconnected", false, false, errors.New("test")},
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder(url)
|
2023-03-03 19:49:18 +01:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if test.connected {
|
2023-03-16 17:22:36 +01:00
|
|
|
status.MarkManagementConnected()
|
2023-03-03 19:49:18 +01:00
|
|
|
} else {
|
2024-01-22 12:20:24 +01:00
|
|
|
status.MarkManagementDisconnected(test.err)
|
2023-03-03 19:49:18 +01:00
|
|
|
}
|
2023-03-16 17:22:36 +01:00
|
|
|
assert.Equal(t, test.want, status.managementState, "signalState status should be equal")
|
2024-01-22 12:20:24 +01:00
|
|
|
assert.Equal(t, test.err, status.managementError)
|
2023-03-03 19:49:18 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFullStatus(t *testing.T) {
|
|
|
|
key1 := "abc"
|
|
|
|
key2 := "def"
|
2023-03-16 17:22:36 +01:00
|
|
|
signalAddr := "https://signal"
|
2023-03-03 19:49:18 +01:00
|
|
|
managementState := ManagementState{
|
2023-03-16 17:22:36 +01:00
|
|
|
URL: "https://mgm",
|
2023-03-03 19:49:18 +01:00
|
|
|
Connected: true,
|
|
|
|
}
|
|
|
|
signalState := SignalState{
|
2023-03-16 17:22:36 +01:00
|
|
|
URL: signalAddr,
|
2023-03-03 19:49:18 +01:00
|
|
|
Connected: true,
|
|
|
|
}
|
|
|
|
peerState1 := State{
|
|
|
|
PubKey: key1,
|
|
|
|
}
|
|
|
|
|
|
|
|
peerState2 := State{
|
|
|
|
PubKey: key2,
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
status := NewRecorder("https://mgm")
|
|
|
|
status.UpdateSignalAddress(signalAddr)
|
2023-03-03 19:49:18 +01:00
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
status.managementState = managementState.Connected
|
|
|
|
status.signalState = signalState.Connected
|
2023-03-03 19:49:18 +01:00
|
|
|
status.peers[key1] = peerState1
|
|
|
|
status.peers[key2] = peerState2
|
|
|
|
|
|
|
|
fullStatus := status.GetFullStatus()
|
2022-01-21 13:52:19 +01:00
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
assert.Equal(t, managementState, fullStatus.ManagementState, "management status should be equal")
|
|
|
|
assert.Equal(t, signalState, fullStatus.SignalState, "signal status should be equal")
|
|
|
|
assert.ElementsMatch(t, []State{peerState1, peerState2}, fullStatus.Peers, "peers states should match")
|
2022-01-21 13:52:19 +01:00
|
|
|
}
|