2021-08-29 17:48:31 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2021-08-29 17:48:31 +02:00
|
|
|
"testing"
|
2022-08-27 12:57:03 +02:00
|
|
|
"time"
|
2023-11-16 18:21:52 +01:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2024-07-18 12:50:44 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-29 17:48:31 +02:00
|
|
|
)
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
// var peersUpdater *PeersUpdateManager
|
2021-08-29 17:48:31 +02:00
|
|
|
|
|
|
|
func TestCreateChannel(t *testing.T) {
|
|
|
|
peer := "test-create"
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdater := NewPeersUpdateManager(nil)
|
2024-07-03 11:33:02 +02:00
|
|
|
defer peersUpdater.CloseChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = peersUpdater.CreateChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; !ok {
|
|
|
|
t.Error("Error creating the channel")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendUpdate(t *testing.T) {
|
|
|
|
peer := "test-sendupdate"
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdater := NewPeersUpdateManager(nil)
|
2022-08-27 12:57:03 +02:00
|
|
|
update1 := &UpdateMessage{Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
|
|
|
Serial: 0,
|
|
|
|
},
|
|
|
|
}}
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = peersUpdater.CreateChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; !ok {
|
|
|
|
t.Error("Error creating the channel")
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.SendUpdate(context.Background(), peer, update1)
|
2021-08-29 17:48:31 +02:00
|
|
|
select {
|
|
|
|
case <-peersUpdater.peerChannels[peer]:
|
|
|
|
default:
|
|
|
|
t.Error("Update wasn't send")
|
|
|
|
}
|
2022-08-27 12:57:03 +02:00
|
|
|
|
|
|
|
for range [channelBufferSize]int{} {
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.SendUpdate(context.Background(), peer, update1)
|
2022-08-27 12:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update2 := &UpdateMessage{Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
|
|
|
Serial: 10,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.SendUpdate(context.Background(), peer, update2)
|
2022-08-27 12:57:03 +02:00
|
|
|
timeout := time.After(5 * time.Second)
|
|
|
|
for range [channelBufferSize]int{} {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Error("timed out reading previously sent updates")
|
|
|
|
case updateReader := <-peersUpdater.peerChannels[peer]:
|
|
|
|
if updateReader.Update.NetworkMap.Serial == update2.Update.NetworkMap.Serial {
|
|
|
|
t.Error("got the update that shouldn't have been sent")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseChannel(t *testing.T) {
|
|
|
|
peer := "test-close"
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdater := NewPeersUpdateManager(nil)
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = peersUpdater.CreateChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; !ok {
|
|
|
|
t.Error("Error creating the channel")
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
peersUpdater.CloseChannel(context.Background(), peer)
|
2021-08-29 17:48:31 +02:00
|
|
|
if _, ok := peersUpdater.peerChannels[peer]; ok {
|
|
|
|
t.Error("Error closing the channel")
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 12:50:44 +02:00
|
|
|
|
|
|
|
func TestHandlePeerMessageUpdate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
peerID string
|
|
|
|
existingUpdate *UpdateMessage
|
|
|
|
newUpdate *UpdateMessage
|
|
|
|
expectedResult bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "update message with turn credentials update",
|
|
|
|
peerID: "peer",
|
|
|
|
newUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
WiretrusteeConfig: &proto.WiretrusteeConfig{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedResult: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update message for peer without existing update",
|
|
|
|
peerID: "peer1",
|
|
|
|
newUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 1},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 2}},
|
|
|
|
},
|
|
|
|
expectedResult: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update message with no changes in update",
|
|
|
|
peerID: "peer2",
|
|
|
|
existingUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 1},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 1}},
|
|
|
|
Checks: []*posture.Checks{},
|
|
|
|
},
|
|
|
|
newUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 1},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 1}},
|
|
|
|
Checks: []*posture.Checks{},
|
|
|
|
},
|
|
|
|
expectedResult: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update message with changes in checks",
|
|
|
|
peerID: "peer3",
|
|
|
|
existingUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 1},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 1}},
|
|
|
|
Checks: []*posture.Checks{},
|
|
|
|
},
|
|
|
|
newUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 2},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 2}},
|
|
|
|
Checks: []*posture.Checks{{ID: "check1"}},
|
|
|
|
},
|
|
|
|
expectedResult: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update message with lower serial number",
|
|
|
|
peerID: "peer4",
|
|
|
|
existingUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 2},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 2}},
|
|
|
|
},
|
|
|
|
newUpdate: &UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
|
|
|
NetworkMap: &proto.NetworkMap{Serial: 1},
|
|
|
|
},
|
|
|
|
NetworkMap: &NetworkMap{Network: &Network{Serial: 1}},
|
|
|
|
},
|
|
|
|
expectedResult: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := NewPeersUpdateManager(nil)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if tt.existingUpdate != nil {
|
|
|
|
p.peerUpdateMessage[tt.peerID] = tt.existingUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
result := p.handlePeerMessageUpdate(ctx, tt.peerID, tt.newUpdate)
|
|
|
|
assert.Equal(t, tt.expectedResult, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|