netbird/management/server/account_test.go

212 lines
4.5 KiB
Go
Raw Normal View History

2021-08-20 15:18:29 +02:00
package server
import (
2021-08-20 15:44:18 +02:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2021-08-20 15:18:29 +02:00
"net"
"testing"
)
func TestAccountManager_AddAccount(t *testing.T) {
2021-08-20 15:44:18 +02:00
manager, err := createManager(t)
2021-08-20 15:18:29 +02:00
if err != nil {
t.Fatal(err)
2021-08-20 15:44:18 +02:00
return
2021-08-20 15:18:29 +02:00
}
expectedId := "test_account"
expectedPeersSize := 0
expectedSetupKeysSize := 1
expectedNetwork := net.IPNet{
IP: net.IP{100, 64, 0, 0},
Mask: net.IPMask{255, 192, 0, 0},
}
account, err := manager.AddAccount(expectedId)
if err != nil {
t.Fatal(err)
}
if account.Id != expectedId {
2021-08-20 22:33:43 +02:00
t.Errorf("expected account to have Id = %s, got %s", expectedId, account.Id)
2021-08-20 15:18:29 +02:00
}
if len(account.Peers) != expectedPeersSize {
t.Errorf("expected account to have len(Peers) = %v, got %v", expectedPeersSize, len(account.Peers))
}
if len(account.SetupKeys) != expectedSetupKeysSize {
t.Errorf("expected account to have len(SetupKeys) = %v, got %v", expectedSetupKeysSize, len(account.SetupKeys))
}
if account.Network.Net.String() != expectedNetwork.String() {
t.Errorf("expected account to have Network = %v, got %v", expectedNetwork.String(), account.Network.Net.String())
}
2021-08-20 15:44:18 +02:00
}
func TestAccountManager_GetOrCreateAccount(t *testing.T) {
manager, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
expectedId := "test_account"
//make sure account doesn't exist
account, err := manager.GetAccount(expectedId)
if err != nil {
errStatus, ok := status.FromError(err)
if !(ok && errStatus.Code() == codes.NotFound) {
t.Fatal(err)
}
}
if account != nil {
t.Fatal("expecting empty account")
}
account, err = manager.GetOrCreateAccount(expectedId)
if err != nil {
t.Fatal(err)
}
if account.Id != expectedId {
t.Fatalf("expected to create an account, got wrong account")
}
account, err = manager.GetOrCreateAccount(expectedId)
if err != nil {
t.Errorf("expected to get existing account after creation, failed")
}
2021-08-20 15:51:29 +02:00
if account.Id != expectedId {
t.Fatalf("expected to create an account, got wrong account")
}
2021-08-20 15:44:18 +02:00
}
func TestAccountManager_AccountExists(t *testing.T) {
manager, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
expectedId := "test_account"
_, err = manager.AddAccount(expectedId)
if err != nil {
t.Fatal(err)
}
exists, err := manager.AccountExists(expectedId)
if err != nil {
t.Fatal(err)
}
if !*exists {
t.Errorf("expected account to exist after creation, got false")
}
}
func TestAccountManager_GetAccount(t *testing.T) {
manager, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
expectedId := "test_account"
account, err := manager.AddAccount(expectedId)
if err != nil {
t.Fatal(err)
}
//AddAccount has been already tested so we can assume it is correct and compare results
getAccount, err := manager.GetAccount(expectedId)
if err != nil {
t.Fatal(err)
return
}
if account.Id != getAccount.Id {
2021-08-20 22:33:43 +02:00
t.Errorf("expected account.Id %s, got %s", account.Id, getAccount.Id)
2021-08-20 15:44:18 +02:00
}
for _, peer := range account.Peers {
if _, ok := getAccount.Peers[peer.Key]; !ok {
t.Errorf("expected account to have peer %s, not found", peer.Key)
}
}
for _, key := range account.SetupKeys {
if _, ok := getAccount.SetupKeys[key.Key]; !ok {
t.Errorf("expected account to have setup key %s, not found", key.Key)
}
}
2021-08-20 15:18:29 +02:00
}
func TestAccountManager_AddPeer(t *testing.T) {
2021-08-20 15:44:18 +02:00
manager, err := createManager(t)
if err != nil {
t.Fatal(err)
return
}
2021-08-20 15:18:29 +02:00
2021-08-20 15:44:18 +02:00
account, err := manager.AddAccount("test_account")
2021-08-20 15:18:29 +02:00
if err != nil {
t.Fatal(err)
}
2021-08-20 15:44:18 +02:00
var setupKey *SetupKey
for _, key := range account.SetupKeys {
setupKey = key
}
if setupKey == nil {
t.Errorf("expecting account to have a default setup key")
return
}
2021-08-20 15:18:29 +02:00
2021-08-20 15:44:18 +02:00
key, err := wgtypes.GenerateKey()
2021-08-20 15:18:29 +02:00
if err != nil {
t.Fatal(err)
2021-08-20 15:44:18 +02:00
return
}
expectedPeerKey := key.PublicKey().String()
expectedPeerIP := "100.64.0.1"
peer, err := manager.AddPeer(setupKey.Key, expectedPeerKey)
if err != nil {
t.Errorf("expecting peer to be added, got failure %v", err)
return
}
if peer.Key != expectedPeerKey {
t.Errorf("expecting just added peer to have key = %s, got %s", expectedPeerKey, peer.Key)
}
if peer.Key != expectedPeerKey {
t.Errorf("expecting just added peer to have IP = %s, got %s", expectedPeerIP, peer.IP.String())
2021-08-20 15:18:29 +02:00
}
2021-08-20 15:44:18 +02:00
}
func createManager(t *testing.T) (*AccountManager, error) {
store, err := createStore(t)
if err != nil {
return nil, err
}
return NewManager(store), nil
2021-08-20 15:18:29 +02:00
}
func createStore(t *testing.T) (Store, error) {
dataDir := t.TempDir()
store, err := NewStore(dataDir)
if err != nil {
return nil, err
}
return store, nil
}