netbird/relay/client/manager_test.go

433 lines
9.7 KiB
Go
Raw Normal View History

2024-05-29 16:40:26 +02:00
package client
import (
"context"
"testing"
"time"
2024-05-29 16:40:26 +02:00
2024-06-01 11:48:15 +02:00
log "github.com/sirupsen/logrus"
2024-07-24 16:26:26 +02:00
"go.opentelemetry.io/otel"
2024-06-01 11:48:15 +02:00
2024-05-29 16:40:26 +02:00
"github.com/netbirdio/netbird/relay/server"
)
2024-07-29 21:39:17 +02:00
func TestEmptyURL(t *testing.T) {
mgr := NewManager(context.Background(), "", "alice")
err := mgr.Serve()
if err == nil {
t.Errorf("expected error, got nil")
}
}
2024-06-01 11:48:15 +02:00
func TestForeignConn(t *testing.T) {
2024-05-29 16:40:26 +02:00
ctx := context.Background()
2024-06-01 11:48:15 +02:00
2024-07-02 11:57:17 +02:00
srvCfg1 := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:1234",
}
2024-07-24 16:26:26 +02:00
srv1, err := server.NewServer(otel.Meter(""), srvCfg1.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-29 16:40:26 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv1.Listen(srvCfg1)
2024-05-29 16:40:26 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-29 16:40:26 +02:00
}
}()
defer func() {
err := srv1.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-02 11:57:17 +02:00
srvCfg2 := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:2234",
}
2024-07-24 16:26:26 +02:00
srv2, err := server.NewServer(otel.Meter(""), srvCfg2.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan2 := make(chan error, 1)
2024-05-29 16:40:26 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv2.Listen(srvCfg2)
2024-05-29 16:40:26 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan2 <- err
2024-05-29 16:40:26 +02:00
}
}()
defer func() {
err := srv2.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan2); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-06-01 11:48:15 +02:00
idAlice := "alice"
log.Debugf("connect by alice")
2024-06-05 19:49:30 +02:00
mCtx, cancel := context.WithCancel(ctx)
defer cancel()
2024-07-08 21:18:19 +02:00
clientAlice := NewManager(mCtx, toURL(srvCfg1), idAlice)
2024-07-24 16:26:26 +02:00
err = clientAlice.Serve()
2024-07-09 09:44:23 +02:00
if err != nil {
t.Fatalf("failed to serve manager: %s", err)
}
2024-05-29 16:40:26 +02:00
2024-06-01 11:48:15 +02:00
idBob := "bob"
log.Debugf("connect by bob")
2024-07-08 21:18:19 +02:00
clientBob := NewManager(mCtx, toURL(srvCfg2), idBob)
2024-07-09 09:44:23 +02:00
err = clientBob.Serve()
if err != nil {
t.Fatalf("failed to serve manager: %s", err)
}
2024-07-02 11:57:17 +02:00
bobsSrvAddr, err := clientBob.RelayInstanceAddress()
2024-05-29 16:40:26 +02:00
if err != nil {
t.Fatalf("failed to get relay address: %s", err)
}
connAliceToBob, err := clientAlice.OpenConn(bobsSrvAddr, idBob)
2024-05-29 16:40:26 +02:00
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
connBobToAlice, err := clientBob.OpenConn(bobsSrvAddr, idAlice)
2024-05-29 16:40:26 +02:00
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
payload := "hello bob, I am alice"
_, err = connAliceToBob.Write([]byte(payload))
if err != nil {
t.Fatalf("failed to write to channel: %s", err)
}
buf := make([]byte, 65535)
n, err := connBobToAlice.Read(buf)
if err != nil {
t.Fatalf("failed to read from channel: %s", err)
}
_, err = connBobToAlice.Write(buf[:n])
if err != nil {
t.Fatalf("failed to write to channel: %s", err)
}
n, err = connAliceToBob.Read(buf)
if err != nil {
t.Fatalf("failed to read from channel: %s", err)
}
if payload != string(buf[:n]) {
t.Fatalf("expected %s, got %s", payload, string(buf[:n]))
}
}
2024-06-01 11:48:15 +02:00
func TestForeginConnClose(t *testing.T) {
ctx := context.Background()
2024-07-02 11:57:17 +02:00
srvCfg1 := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:1234",
}
2024-07-24 16:26:26 +02:00
srv1, err := server.NewServer(otel.Meter(""), srvCfg1.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-06-01 11:48:15 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv1.Listen(srvCfg1)
2024-06-01 11:48:15 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-06-01 11:48:15 +02:00
}
}()
defer func() {
err := srv1.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-02 11:57:17 +02:00
srvCfg2 := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:2234",
}
2024-07-24 16:26:26 +02:00
srv2, err := server.NewServer(otel.Meter(""), srvCfg2.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan2 := make(chan error, 1)
2024-06-01 11:48:15 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv2.Listen(srvCfg2)
2024-06-01 11:48:15 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan2 <- err
2024-06-01 11:48:15 +02:00
}
}()
defer func() {
err := srv2.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan2); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-06-01 11:48:15 +02:00
idAlice := "alice"
log.Debugf("connect by alice")
2024-06-05 19:49:30 +02:00
mCtx, cancel := context.WithCancel(ctx)
defer cancel()
2024-07-08 21:18:19 +02:00
mgr := NewManager(mCtx, toURL(srvCfg1), idAlice)
2024-07-24 16:26:26 +02:00
err = mgr.Serve()
2024-07-08 23:14:09 +02:00
if err != nil {
t.Fatalf("failed to serve manager: %s", err)
}
conn, err := mgr.OpenConn(toURL(srvCfg2), "anotherpeer")
2024-06-01 11:48:15 +02:00
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
err = conn.Close()
if err != nil {
t.Fatalf("failed to close connection: %s", err)
}
}
func TestForeginAutoClose(t *testing.T) {
ctx := context.Background()
2024-06-05 19:49:30 +02:00
relayCleanupInterval = 1 * time.Second
2024-07-02 11:57:17 +02:00
srvCfg1 := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:1234",
}
2024-07-24 16:26:26 +02:00
srv1, err := server.NewServer(otel.Meter(""), srvCfg1.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
go func() {
2024-06-05 19:49:30 +02:00
t.Log("binding server 1.")
2024-07-01 11:50:18 +02:00
err := srv1.Listen(srvCfg1)
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
}
}()
defer func() {
2024-06-05 19:49:30 +02:00
t.Logf("closing server 1.")
err := srv1.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
2024-06-05 19:49:30 +02:00
t.Logf("server 1. closed")
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-02 11:57:17 +02:00
srvCfg2 := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:2234",
}
2024-07-24 16:26:26 +02:00
srv2, err := server.NewServer(otel.Meter(""), srvCfg2.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan2 := make(chan error, 1)
go func() {
2024-06-05 19:49:30 +02:00
t.Log("binding server 2.")
2024-07-01 11:50:18 +02:00
err := srv2.Listen(srvCfg2)
if err != nil {
2024-07-09 09:44:23 +02:00
errChan2 <- err
}
}()
defer func() {
2024-06-05 19:49:30 +02:00
t.Logf("closing server 2.")
err := srv2.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
2024-06-05 19:49:30 +02:00
t.Logf("server 2 closed.")
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan2); err != nil {
t.Fatalf("failed to start server: %s", err)
}
idAlice := "alice"
2024-06-05 19:49:30 +02:00
t.Log("connect to server 1.")
mCtx, cancel := context.WithCancel(ctx)
defer cancel()
2024-07-08 21:18:19 +02:00
mgr := NewManager(mCtx, toURL(srvCfg1), idAlice)
2024-07-24 16:26:26 +02:00
err = mgr.Serve()
2024-07-08 23:14:09 +02:00
if err != nil {
t.Fatalf("failed to serve manager: %s", err)
}
2024-06-05 19:49:30 +02:00
t.Log("open connection to another peer")
conn, err := mgr.OpenConn(toURL(srvCfg2), "anotherpeer")
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
2024-06-05 19:49:30 +02:00
t.Log("close conn")
err = conn.Close()
if err != nil {
t.Fatalf("failed to close connection: %s", err)
}
2024-06-05 19:49:30 +02:00
t.Logf("waiting for relay cleanup: %s", relayCleanupInterval+1*time.Second)
time.Sleep(relayCleanupInterval + 1*time.Second)
if len(mgr.relayClients) != 0 {
t.Errorf("expected 0, got %d", len(mgr.relayClients))
}
2024-06-05 19:49:30 +02:00
t.Logf("closing manager")
}
func TestAutoReconnect(t *testing.T) {
ctx := context.Background()
reconnectingTimeout = 2 * time.Second
2024-07-02 11:57:17 +02:00
srvCfg := server.ListenerConfig{
2024-07-01 11:50:18 +02:00
Address: "localhost:1234",
}
2024-07-24 16:26:26 +02:00
srv, err := server.NewServer(otel.Meter(""), srvCfg.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-06-05 19:49:30 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
2024-06-05 19:49:30 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-06-05 19:49:30 +02:00
}
}()
defer func() {
err := srv.Close()
if err != nil {
log.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-06-05 19:49:30 +02:00
mCtx, cancel := context.WithCancel(ctx)
defer cancel()
2024-07-08 21:18:19 +02:00
clientAlice := NewManager(mCtx, toURL(srvCfg), "alice")
2024-07-24 16:26:26 +02:00
err = clientAlice.Serve()
2024-07-09 09:44:23 +02:00
if err != nil {
t.Fatalf("failed to serve manager: %s", err)
}
2024-07-02 11:57:17 +02:00
ra, err := clientAlice.RelayInstanceAddress()
2024-06-05 19:49:30 +02:00
if err != nil {
t.Errorf("failed to get relay address: %s", err)
}
conn, err := clientAlice.OpenConn(ra, "bob")
2024-06-05 19:49:30 +02:00
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
t.Log("closing client relay connection")
// todo figure out moc server
_ = clientAlice.relayClient.relayConn.Close()
t.Log("start test reading")
_, err = conn.Read(make([]byte, 1))
if err == nil {
t.Errorf("unexpected reading from closed connection")
}
log.Infof("waiting for reconnection")
time.Sleep(reconnectingTimeout + 1*time.Second)
log.Infof("reopent the connection")
_, err = clientAlice.OpenConn(ra, "bob")
2024-06-05 19:49:30 +02:00
if err != nil {
t.Errorf("failed to open channel: %s", err)
}
}
2024-07-08 21:18:19 +02:00
func TestNotifierDoubleAdd(t *testing.T) {
ctx := context.Background()
srvCfg1 := server.ListenerConfig{
Address: "localhost:1234",
}
srv1, err := server.NewServer(otel.Meter(""), srvCfg1.Address, false, av)
if err != nil {
t.Fatalf("failed to create server: %s", err)
}
errChan := make(chan error, 1)
go func() {
err := srv1.Listen(srvCfg1)
if err != nil {
errChan <- err
}
}()
defer func() {
err := srv1.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
idAlice := "alice"
log.Debugf("connect by alice")
mCtx, cancel := context.WithCancel(ctx)
defer cancel()
clientAlice := NewManager(mCtx, toURL(srvCfg1), idAlice)
err = clientAlice.Serve()
if err != nil {
t.Fatalf("failed to serve manager: %s", err)
}
conn1, err := clientAlice.OpenConn(clientAlice.ServerURL(), "idBob")
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
fnCloseListener := OnServerCloseListener(func() {
log.Infof("close listener")
})
err = clientAlice.AddCloseListener(clientAlice.ServerURL(), fnCloseListener)
if err != nil {
t.Fatalf("failed to add close listener: %s", err)
}
err = clientAlice.AddCloseListener(clientAlice.ServerURL(), fnCloseListener)
if err != nil {
t.Fatalf("failed to add close listener: %s", err)
}
err = conn1.Close()
if err != nil {
t.Errorf("failed to close connection: %s", err)
}
}
2024-07-08 21:18:19 +02:00
func toURL(address server.ListenerConfig) string {
return "rel://" + address.Address
}