2021-12-27 13:17:15 +01:00
package server
import (
2022-03-26 12:08:54 +01:00
"github.com/netbirdio/netbird/util"
2022-11-07 12:10:56 +01:00
"github.com/stretchr/testify/assert"
2022-03-01 15:22:18 +01:00
"github.com/stretchr/testify/require"
2021-12-27 13:17:15 +01:00
"net"
"path/filepath"
"testing"
"time"
)
2022-11-07 12:10:56 +01:00
type accounts struct {
Accounts map [ string ] * Account
}
2021-12-27 13:17:15 +01:00
func TestNewStore ( t * testing . T ) {
store := newStore ( t )
if store . Accounts == nil || len ( store . Accounts ) != 0 {
t . Errorf ( "expected to create a new empty Accounts map when creating a new FileStore" )
}
2022-11-07 12:10:56 +01:00
if store . SetupKeyID2AccountID == nil || len ( store . SetupKeyID2AccountID ) != 0 {
t . Errorf ( "expected to create a new empty SetupKeyID2AccountID map when creating a new FileStore" )
2021-12-27 13:17:15 +01:00
}
2022-11-07 12:10:56 +01:00
if store . PeerKeyID2AccountID == nil || len ( store . PeerKeyID2AccountID ) != 0 {
t . Errorf ( "expected to create a new empty PeerKeyID2AccountID map when creating a new FileStore" )
2021-12-27 13:17:15 +01:00
}
2022-11-07 12:10:56 +01:00
if store . UserID2AccountID == nil || len ( store . UserID2AccountID ) != 0 {
t . Errorf ( "expected to create a new empty UserID2AccountID map when creating a new FileStore" )
2021-12-27 13:17:15 +01:00
}
}
func TestSaveAccount ( t * testing . T ) {
store := newStore ( t )
2022-06-20 18:20:43 +02:00
account := newAccountWithId ( "account_id" , "testuser" , "" )
2021-12-27 13:17:15 +01:00
setupKey := GenerateDefaultSetupKey ( )
account . SetupKeys [ setupKey . Key ] = setupKey
account . Peers [ "testpeer" ] = & Peer {
Key : "peerkey" ,
SetupKey : "peerkeysetupkey" ,
IP : net . IP { 127 , 0 , 0 , 1 } ,
Meta : PeerSystemMeta { } ,
Name : "peer name" ,
Status : & PeerStatus { Connected : true , LastSeen : time . Now ( ) } ,
}
// SaveAccount should trigger persist
err := store . SaveAccount ( account )
if err != nil {
return
}
if store . Accounts [ account . Id ] == nil {
t . Errorf ( "expecting Account to be stored after SaveAccount()" )
}
2022-11-07 12:10:56 +01:00
if store . PeerKeyID2AccountID [ "peerkey" ] == "" {
t . Errorf ( "expecting PeerKeyID2AccountID index updated after SaveAccount()" )
2021-12-27 13:17:15 +01:00
}
2022-11-07 12:10:56 +01:00
if store . UserID2AccountID [ "testuser" ] == "" {
t . Errorf ( "expecting UserID2AccountID index updated after SaveAccount()" )
2021-12-27 13:17:15 +01:00
}
2022-11-07 12:10:56 +01:00
if store . SetupKeyID2AccountID [ setupKey . Key ] == "" {
t . Errorf ( "expecting SetupKeyID2AccountID index updated after SaveAccount()" )
2021-12-27 13:17:15 +01:00
}
}
func TestStore ( t * testing . T ) {
store := newStore ( t )
2022-06-20 18:20:43 +02:00
account := newAccountWithId ( "account_id" , "testuser" , "" )
2021-12-27 13:17:15 +01:00
account . Peers [ "testpeer" ] = & Peer {
Key : "peerkey" ,
SetupKey : "peerkeysetupkey" ,
IP : net . IP { 127 , 0 , 0 , 1 } ,
Meta : PeerSystemMeta { } ,
Name : "peer name" ,
Status : & PeerStatus { Connected : true , LastSeen : time . Now ( ) } ,
}
// SaveAccount should trigger persist
err := store . SaveAccount ( account )
if err != nil {
return
}
2022-11-08 10:46:12 +01:00
restored , err := NewFileStore ( store . storeFile )
2021-12-27 13:17:15 +01:00
if err != nil {
return
}
restoredAccount := restored . Accounts [ account . Id ]
if restoredAccount == nil {
t . Errorf ( "failed to restore a FileStore file - missing Account %s" , account . Id )
}
if restoredAccount != nil && restoredAccount . Peers [ "testpeer" ] == nil {
t . Errorf ( "failed to restore a FileStore file - missing Peer testpeer" )
}
if restoredAccount != nil && restoredAccount . CreatedBy != "testuser" {
t . Errorf ( "failed to restore a FileStore file - missing Account CreatedBy" )
}
if restoredAccount != nil && restoredAccount . Users [ "testuser" ] == nil {
t . Errorf ( "failed to restore a FileStore file - missing User testuser" )
}
if restoredAccount != nil && restoredAccount . Network == nil {
t . Errorf ( "failed to restore a FileStore file - missing Network" )
}
}
func TestRestore ( t * testing . T ) {
storeDir := t . TempDir ( )
err := util . CopyFileContents ( "testdata/store.json" , filepath . Join ( storeDir , "store.json" ) )
if err != nil {
t . Fatal ( err )
}
2022-11-08 10:46:12 +01:00
store , err := NewFileStore ( storeDir )
2021-12-27 13:17:15 +01:00
if err != nil {
return
}
account := store . Accounts [ "bf1c8084-ba50-4ce7-9439-34653001fc3b" ]
2022-03-01 15:22:18 +01:00
require . NotNil ( t , account , "failed to restore a FileStore file - missing account bf1c8084-ba50-4ce7-9439-34653001fc3b" )
2021-12-27 13:17:15 +01:00
2022-03-01 15:22:18 +01:00
require . NotNil ( t , account . Users [ "edafee4e-63fb-11ec-90d6-0242ac120003" ] , "failed to restore a FileStore file - missing Account User edafee4e-63fb-11ec-90d6-0242ac120003" )
2021-12-27 13:17:15 +01:00
2022-03-01 15:22:18 +01:00
require . NotNil ( t , account . Users [ "f4f6d672-63fb-11ec-90d6-0242ac120003" ] , "failed to restore a FileStore file - missing Account User f4f6d672-63fb-11ec-90d6-0242ac120003" )
2021-12-27 13:17:15 +01:00
2022-03-01 15:22:18 +01:00
require . NotNil ( t , account . Network , "failed to restore a FileStore file - missing Account Network" )
require . NotNil ( t , account . SetupKeys [ "A2C8E62B-38F5-4553-B31E-DD66C696CEBB" ] , "failed to restore a FileStore file - missing Account SetupKey A2C8E62B-38F5-4553-B31E-DD66C696CEBB" )
2022-11-07 12:10:56 +01:00
require . Len ( t , store . UserID2AccountID , 2 , "failed to restore a FileStore wrong UserID2AccountID mapping length" )
2022-03-01 15:22:18 +01:00
2022-11-07 12:10:56 +01:00
require . Len ( t , store . SetupKeyID2AccountID , 1 , "failed to restore a FileStore wrong SetupKeyID2AccountID mapping length" )
2022-03-01 15:22:18 +01:00
2022-11-07 12:10:56 +01:00
require . Len ( t , store . PrivateDomain2AccountID , 1 , "failed to restore a FileStore wrong PrivateDomain2AccountID mapping length" )
2022-03-01 15:22:18 +01:00
}
2021-12-27 13:17:15 +01:00
2022-03-01 15:22:18 +01:00
func TestGetAccountByPrivateDomain ( t * testing . T ) {
storeDir := t . TempDir ( )
err := util . CopyFileContents ( "testdata/store.json" , filepath . Join ( storeDir , "store.json" ) )
if err != nil {
t . Fatal ( err )
2021-12-27 13:17:15 +01:00
}
2022-11-08 10:46:12 +01:00
store , err := NewFileStore ( storeDir )
2022-03-01 15:22:18 +01:00
if err != nil {
return
2021-12-27 13:17:15 +01:00
}
2022-03-01 15:22:18 +01:00
existingDomain := "test.com"
account , err := store . GetAccountByPrivateDomain ( existingDomain )
require . NoError ( t , err , "should found account" )
require . Equal ( t , existingDomain , account . Domain , "domains should match" )
_ , err = store . GetAccountByPrivateDomain ( "missing-domain.com" )
require . Error ( t , err , "should return error on domain lookup" )
2021-12-27 13:17:15 +01:00
}
2022-11-07 12:10:56 +01:00
func TestFileStore_GetAccount ( t * testing . T ) {
storeDir := t . TempDir ( )
storeFile := filepath . Join ( storeDir , "store.json" )
err := util . CopyFileContents ( "testdata/store.json" , storeFile )
if err != nil {
t . Fatal ( err )
}
accounts := & accounts { }
_ , err = util . ReadJson ( storeFile , accounts )
if err != nil {
t . Fatal ( err )
}
2022-11-08 10:46:12 +01:00
store , err := NewFileStore ( storeDir )
2022-11-07 12:10:56 +01:00
if err != nil {
t . Fatal ( err )
}
expected := accounts . Accounts [ "bf1c8084-ba50-4ce7-9439-34653001fc3b" ]
if expected == nil {
t . Fatalf ( "expected account doesn't exist" )
}
account , err := store . GetAccount ( expected . Id )
if err != nil {
t . Fatal ( err )
}
assert . Equal ( t , expected . IsDomainPrimaryAccount , account . IsDomainPrimaryAccount )
assert . Equal ( t , expected . DomainCategory , account . DomainCategory )
assert . Equal ( t , expected . Domain , account . Domain )
assert . Equal ( t , expected . CreatedBy , account . CreatedBy )
assert . Equal ( t , expected . Network . Id , account . Network . Id )
assert . Len ( t , account . Peers , len ( expected . Peers ) )
assert . Len ( t , account . Users , len ( expected . Users ) )
assert . Len ( t , account . SetupKeys , len ( expected . SetupKeys ) )
assert . Len ( t , account . Rules , len ( expected . Rules ) )
assert . Len ( t , account . Routes , len ( expected . Routes ) )
assert . Len ( t , account . NameServerGroups , len ( expected . NameServerGroups ) )
}
2022-11-08 10:46:12 +01:00
func TestFileStore_SavePeerStatus ( t * testing . T ) {
storeDir := t . TempDir ( )
err := util . CopyFileContents ( "testdata/store.json" , filepath . Join ( storeDir , "store.json" ) )
if err != nil {
t . Fatal ( err )
}
store , err := NewFileStore ( storeDir )
if err != nil {
return
}
account , err := store . getAccount ( "bf1c8084-ba50-4ce7-9439-34653001fc3b" )
if err != nil {
t . Fatal ( err )
}
// save status of non-existing peer
newStatus := PeerStatus { Connected : true , LastSeen : time . Now ( ) }
err = store . SavePeerStatus ( account . Id , "non-existing-peer" , newStatus )
assert . Error ( t , err )
// save new status of existing peer
account . Peers [ "testpeer" ] = & Peer {
Key : "peerkey" ,
SetupKey : "peerkeysetupkey" ,
IP : net . IP { 127 , 0 , 0 , 1 } ,
Meta : PeerSystemMeta { } ,
Name : "peer name" ,
Status : & PeerStatus { Connected : false , LastSeen : time . Now ( ) } ,
}
err = store . SaveAccount ( account )
if err != nil {
t . Fatal ( err )
}
err = store . SavePeerStatus ( account . Id , "testpeer" , newStatus )
if err != nil {
t . Fatal ( err )
}
account , err = store . getAccount ( account . Id )
if err != nil {
t . Fatal ( err )
}
actual := account . Peers [ "testpeer" ] . Status
assert . Equal ( t , newStatus , * actual )
}
2021-12-27 13:17:15 +01:00
func newStore ( t * testing . T ) * FileStore {
2022-11-08 10:46:12 +01:00
store , err := NewFileStore ( t . TempDir ( ) )
2021-12-27 13:17:15 +01:00
if err != nil {
t . Errorf ( "failed creating a new store" )
}
return store
}