netbird/relay/client/client_test.go

761 lines
18 KiB
Go
Raw Normal View History

2024-05-28 01:00:25 +02:00
package client
2024-05-17 17:43:28 +02:00
import (
2024-05-26 22:14:33 +02:00
"context"
2024-07-20 13:00:19 +02:00
"crypto/rand"
"fmt"
"net"
2024-05-17 17:43:28 +02:00
"os"
2024-07-20 13:00:19 +02:00
"sync"
2024-05-17 17:43:28 +02:00
"testing"
2024-05-28 01:00:25 +02:00
"time"
2024-05-17 17:43:28 +02:00
log "github.com/sirupsen/logrus"
2024-07-08 17:01:11 +02:00
"github.com/netbirdio/netbird/relay/auth"
"github.com/netbirdio/netbird/relay/auth/hmac"
"github.com/netbirdio/netbird/util"
2024-05-17 17:43:28 +02:00
"github.com/netbirdio/netbird/relay/server"
)
2024-07-08 17:01:11 +02:00
var (
av = &auth.AllowAllAuth{}
hmacTokenStore = &hmac.TokenStore{}
2024-07-08 21:56:15 +02:00
serverListenAddr = "127.0.0.1:1234"
serverURL = "rel://127.0.0.1:1234"
2024-07-08 17:01:11 +02:00
)
2024-05-17 17:43:28 +02:00
func TestMain(m *testing.M) {
2024-07-21 13:40:23 +02:00
_ = util.InitLog("error", "console")
2024-05-17 17:43:28 +02:00
code := m.Run()
os.Exit(code)
}
func TestClient(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-17 17:43:28 +02:00
go func() {
listenCfg := server.ListenerConfig{Address: serverListenAddr}
err := srv.Listen(listenCfg)
2024-05-17 17:43:28 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-17 17:43:28 +02:00
}
}()
defer func() {
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-08 22:13:31 +02:00
// wait for server to start
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
t.Log("alice connecting to server")
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, "alice")
2024-05-17 17:43:28 +02:00
err := clientAlice.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
defer clientAlice.Close()
t.Log("placeholder connecting to server")
2024-07-08 17:01:11 +02:00
clientPlaceHolder := NewClient(ctx, serverURL, hmacTokenStore, "clientPlaceHolder")
2024-05-17 17:43:28 +02:00
err = clientPlaceHolder.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
defer clientPlaceHolder.Close()
t.Log("Bob connecting to server")
2024-07-08 17:01:11 +02:00
clientBob := NewClient(ctx, serverURL, hmacTokenStore, "bob")
2024-05-17 17:43:28 +02:00
err = clientBob.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
defer clientBob.Close()
t.Log("Alice open connection to Bob")
2024-05-23 13:24:02 +02:00
connAliceToBob, err := clientAlice.OpenConn("bob")
2024-05-17 17:43:28 +02:00
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
t.Log("Bob open connection to Alice")
2024-05-23 13:24:02 +02:00
connBobToAlice, err := clientBob.OpenConn("alice")
2024-05-17 17:43:28 +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)
}
log.Debugf("alice sent message to bob")
buf := make([]byte, 65535)
n, err := connBobToAlice.Read(buf)
if err != nil {
t.Fatalf("failed to read from channel: %s", err)
}
log.Debugf("on new message from alice to bob")
if payload != string(buf[:n]) {
t.Fatalf("expected %s, got %s", payload, string(buf[:n]))
}
}
2024-07-20 13:00:19 +02:00
func TestDataTransfer(t *testing.T) {
2024-07-21 13:40:23 +02:00
dataSize := 1024 * 1024 * 10
2024-07-20 13:00:19 +02:00
2024-07-21 13:40:23 +02:00
testData, err := seedRandomData(dataSize)
2024-07-20 13:00:19 +02:00
if err != nil {
t.Fatalf("failed to seed random data: %s", err)
}
2024-07-21 13:40:23 +02:00
for _, peerPairs := range []int{1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100} {
t.Run(fmt.Sprintf("peerPairs-%d", peerPairs), func(t *testing.T) {
transfer(t, testData, peerPairs)
})
}
}
func transfer(t *testing.T, testData []byte, peerPairs int) {
t.Helper()
ctx := context.Background()
port := 35000 + peerPairs
serverAddress := fmt.Sprintf("127.0.0.1:%d", port)
serverConnURL := fmt.Sprintf("rel://%s", serverAddress)
srv := server.NewServer(serverConnURL, false, av)
2024-07-20 13:00:19 +02:00
errChan := make(chan error, 1)
go func() {
2024-07-21 13:40:23 +02:00
listenCfg := server.ListenerConfig{Address: serverAddress}
2024-07-20 13:00:19 +02:00
err := srv.Listen(listenCfg)
if err != nil {
errChan <- err
}
}()
defer func() {
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
// wait for server to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
clientsSender := make([]*Client, peerPairs)
for i := 0; i < cap(clientsSender); i++ {
2024-07-21 13:40:23 +02:00
c := NewClient(ctx, serverConnURL, hmacTokenStore, "sender-"+fmt.Sprint(i))
2024-07-20 13:00:19 +02:00
err := c.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
clientsSender[i] = c
}
clientsReceiver := make([]*Client, peerPairs)
2024-07-21 13:40:23 +02:00
for i := 0; i < cap(clientsReceiver); i++ {
c := NewClient(ctx, serverConnURL, hmacTokenStore, "receiver-"+fmt.Sprint(i))
2024-07-20 13:00:19 +02:00
err := c.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
clientsReceiver[i] = c
}
connsSender := make([]net.Conn, 0, peerPairs)
connsReceiver := make([]net.Conn, 0, peerPairs)
for i := 0; i < len(clientsSender); i++ {
conn, err := clientsSender[i].OpenConn("receiver-" + fmt.Sprint(i))
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
connsSender = append(connsSender, conn)
conn, err = clientsReceiver[i].OpenConn("sender-" + fmt.Sprint(i))
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
connsReceiver = append(connsReceiver, conn)
}
2024-07-21 13:40:23 +02:00
var transferDuration []time.Duration
2024-07-20 13:00:19 +02:00
wg := sync.WaitGroup{}
for i := 0; i < len(connsSender); i++ {
wg.Add(2)
2024-07-21 13:40:23 +02:00
start := time.Now()
2024-07-20 13:00:19 +02:00
go func(i int) {
pieceSize := 1024
testDataLen := len(testData)
for j := 0; j < testDataLen; j += pieceSize {
end := j + pieceSize
if end > testDataLen {
end = testDataLen
}
_, err := connsSender[i].Write(testData[j:end])
if err != nil {
t.Fatalf("failed to write to channel: %s", err)
}
}
wg.Done()
}(i)
2024-07-21 13:40:23 +02:00
go func(i int, start time.Time) {
buf := make([]byte, 8192)
rcv := 0
2024-07-20 13:00:19 +02:00
for receivedSize := 0; receivedSize < len(testData); {
2024-07-21 13:40:23 +02:00
2024-07-20 13:00:19 +02:00
n, err := connsReceiver[i].Read(buf)
if err != nil {
t.Fatalf("failed to read from channel: %s", err)
}
receivedSize += n
2024-07-21 13:40:23 +02:00
rcv += n
2024-07-20 13:00:19 +02:00
}
2024-07-21 13:40:23 +02:00
transferDuration = append(transferDuration, time.Since(start))
2024-07-20 13:00:19 +02:00
wg.Done()
2024-07-21 13:40:23 +02:00
}(i, start)
2024-07-20 13:00:19 +02:00
}
wg.Wait()
2024-07-21 13:40:23 +02:00
// calculate the megabytes per second from the average transferDuration against the dataSize
var totalDuration time.Duration
for _, d := range transferDuration {
totalDuration += d
}
avgDuration := totalDuration / time.Duration(len(transferDuration))
mbps := float64(len(testData)) / avgDuration.Seconds() / 1024 / 1024
t.Logf("average transfer duration: %s", avgDuration)
t.Logf("average transfer speed: %.2f MB/s", mbps)
2024-07-20 13:00:19 +02:00
for i := 0; i < len(connsSender); i++ {
err := connsSender[i].Close()
if err != nil {
t.Errorf("failed to close connection: %s", err)
}
err = connsReceiver[i].Close()
if err != nil {
t.Errorf("failed to close connection: %s", err)
}
}
}
func TestRegistration(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
}
}()
2024-07-08 22:13:31 +02:00
// wait for server to start
2024-07-09 09:44:23 +02:00
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, "alice")
err := clientAlice.Connect()
if err != nil {
2024-06-05 19:49:30 +02:00
_ = srv.Close()
t.Fatalf("failed to connect to server: %s", err)
}
2024-06-05 19:49:30 +02:00
err = clientAlice.Close()
if err != nil {
t.Errorf("failed to close conn: %s", err)
}
err = srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}
func TestRegistrationTimeout(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-06-05 19:49:30 +02:00
fakeUDPListener, err := net.ListenUDP("udp", &net.UDPAddr{
Port: 1234,
IP: net.ParseIP("0.0.0.0"),
})
if err != nil {
t.Fatalf("failed to bind UDP server: %s", err)
}
2024-06-05 19:49:30 +02:00
defer func(fakeUDPListener *net.UDPConn) {
_ = fakeUDPListener.Close()
}(fakeUDPListener)
2024-06-05 19:49:30 +02:00
fakeTCPListener, err := net.ListenTCP("tcp", &net.TCPAddr{
Port: 1234,
IP: net.ParseIP("0.0.0.0"),
})
if err != nil {
t.Fatalf("failed to bind TCP server: %s", err)
}
2024-06-05 19:49:30 +02:00
defer func(fakeTCPListener *net.TCPListener) {
_ = fakeTCPListener.Close()
}(fakeTCPListener)
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, "127.0.0.1:1234", hmacTokenStore, "alice")
err = clientAlice.Connect()
if err == nil {
t.Errorf("failed to connect to server: %s", err)
}
2024-06-05 19:49:30 +02:00
log.Debugf("%s", err)
err = clientAlice.Close()
if err != nil {
t.Errorf("failed to close conn: %s", err)
}
}
2024-05-17 17:43:28 +02:00
func TestEcho(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-05-23 13:24:02 +02:00
idAlice := "alice"
idBob := "bob"
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-17 17:43:28 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
2024-05-17 17:43:28 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-17 17:43:28 +02:00
}
}()
defer func() {
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, idAlice)
2024-05-17 17:43:28 +02:00
err := clientAlice.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
defer func() {
err := clientAlice.Close()
if err != nil {
t.Errorf("failed to close Alice client: %s", err)
}
}()
2024-07-08 17:01:11 +02:00
clientBob := NewClient(ctx, serverURL, hmacTokenStore, idBob)
2024-05-17 17:43:28 +02:00
err = clientBob.Connect()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
defer func() {
err := clientBob.Close()
if err != nil {
t.Errorf("failed to close Bob client: %s", err)
}
}()
2024-05-23 13:24:02 +02:00
connAliceToBob, err := clientAlice.OpenConn(idBob)
2024-05-17 17:43:28 +02:00
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
2024-05-23 13:24:02 +02:00
connBobToAlice, err := clientBob.OpenConn(idAlice)
2024-05-17 17:43:28 +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]))
}
}
func TestBindToUnavailabePeer(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-17 17:43:28 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
2024-05-17 17:43:28 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-17 17:43:28 +02:00
}
}()
defer func() {
log.Infof("closing server")
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, "alice")
2024-05-17 17:43:28 +02:00
err := clientAlice.Connect()
if err != nil {
t.Errorf("failed to connect to server: %s", err)
}
2024-05-23 13:24:02 +02:00
_, err = clientAlice.OpenConn("bob")
2024-05-17 17:43:28 +02:00
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
2024-06-05 19:49:30 +02:00
log.Infof("closing client")
err = clientAlice.Close()
if err != nil {
t.Errorf("failed to close client: %s", err)
}
2024-05-17 17:43:28 +02:00
}
func TestBindReconnect(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-17 17:43:28 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
2024-05-17 17:43:28 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-17 17:43:28 +02:00
}
}()
defer func() {
log.Infof("closing server")
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, "alice")
2024-05-17 17:43:28 +02:00
err := clientAlice.Connect()
if err != nil {
t.Errorf("failed to connect to server: %s", err)
}
2024-05-23 13:24:02 +02:00
_, err = clientAlice.OpenConn("bob")
2024-05-17 17:43:28 +02:00
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
2024-07-08 17:01:11 +02:00
clientBob := NewClient(ctx, serverURL, hmacTokenStore, "bob")
2024-05-17 17:43:28 +02:00
err = clientBob.Connect()
if err != nil {
t.Errorf("failed to connect to server: %s", err)
}
2024-05-23 13:24:02 +02:00
chBob, err := clientBob.OpenConn("alice")
2024-05-17 17:43:28 +02:00
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
2024-06-05 19:49:30 +02:00
log.Infof("closing client Alice")
2024-05-17 17:43:28 +02:00
err = clientAlice.Close()
if err != nil {
t.Errorf("failed to close client: %s", err)
}
2024-07-08 17:01:11 +02:00
clientAlice = NewClient(ctx, serverURL, hmacTokenStore, "alice")
2024-05-17 17:43:28 +02:00
err = clientAlice.Connect()
if err != nil {
t.Errorf("failed to connect to server: %s", err)
}
2024-05-23 13:24:02 +02:00
chAlice, err := clientAlice.OpenConn("bob")
2024-05-17 17:43:28 +02:00
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
testString := "hello alice, I am bob"
_, err = chBob.Write([]byte(testString))
if err != nil {
t.Errorf("failed to write to channel: %s", err)
}
buf := make([]byte, 65535)
n, err := chAlice.Read(buf)
if err != nil {
t.Errorf("failed to read from channel: %s", err)
}
if testString != string(buf[:n]) {
t.Errorf("expected %s, got %s", testString, string(buf[:n]))
}
log.Infof("closing client")
err = clientAlice.Close()
if err != nil {
t.Errorf("failed to close client: %s", err)
}
}
2024-05-27 10:25:08 +02:00
func TestCloseConn(t *testing.T) {
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-27 10:25:08 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
2024-05-27 10:25:08 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-27 10:25:08 +02:00
}
}()
defer func() {
log.Infof("closing server")
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-07-09 09:44:23 +02:00
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, "alice")
2024-05-27 10:25:08 +02:00
err := clientAlice.Connect()
if err != nil {
t.Errorf("failed to connect to server: %s", err)
}
conn, err := clientAlice.OpenConn("bob")
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
log.Infof("closing connection")
err = conn.Close()
if err != nil {
t.Errorf("failed to close connection: %s", err)
}
_, err = conn.Read(make([]byte, 1))
if err == nil {
t.Errorf("unexpected reading from closed connection")
}
_, err = conn.Write([]byte("hello"))
if err == nil {
t.Errorf("unexpected writing from closed connection")
}
}
2024-05-28 01:00:25 +02:00
2024-06-05 19:49:30 +02:00
func TestCloseRelayConn(t *testing.T) {
2024-05-28 01:00:25 +02:00
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-28 01:00:25 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv.Listen(srvCfg)
2024-05-28 01:00:25 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-28 01:00:25 +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
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-07-08 17:01:11 +02:00
clientAlice := NewClient(ctx, serverURL, hmacTokenStore, "alice")
2024-05-28 01:00:25 +02:00
err := clientAlice.Connect()
if err != nil {
2024-06-05 19:49:30 +02:00
t.Fatalf("failed to connect to server: %s", err)
2024-05-28 01:00:25 +02:00
}
conn, err := clientAlice.OpenConn("bob")
if err != nil {
t.Errorf("failed to bind channel: %s", err)
}
_ = clientAlice.relayConn.Close()
_, err = conn.Read(make([]byte, 1))
if err == nil {
t.Errorf("unexpected reading from closed connection")
}
_, err = clientAlice.OpenConn("bob")
2024-06-05 19:49:30 +02:00
if err == nil {
t.Errorf("unexpected opening connection to closed server")
2024-05-28 01:00:25 +02:00
}
}
2024-06-05 19:49:30 +02:00
func TestCloseByServer(t *testing.T) {
2024-05-28 01:00:25 +02:00
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv1 := server.NewServer(serverURL, false, av)
2024-07-09 09:44:23 +02:00
errChan := make(chan error, 1)
2024-05-28 01:00:25 +02:00
go func() {
2024-07-01 11:50:18 +02:00
err := srv1.Listen(srvCfg)
2024-05-28 01:00:25 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-28 01:00:25 +02:00
}
}()
2024-07-09 09:44:23 +02:00
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-06-05 19:49:30 +02:00
idAlice := "alice"
log.Debugf("connect by alice")
2024-07-08 17:01:11 +02:00
relayClient := NewClient(ctx, serverURL, hmacTokenStore, idAlice)
2024-06-05 19:49:30 +02:00
err := relayClient.Connect()
if err != nil {
log.Fatalf("failed to connect to server: %s", err)
}
disconnected := make(chan struct{})
relayClient.SetOnDisconnectListener(func() {
log.Infof("client disconnected")
close(disconnected)
})
err = srv1.Close()
if err != nil {
t.Fatalf("failed to close server: %s", err)
}
select {
case <-disconnected:
case <-time.After(3 * time.Second):
log.Fatalf("timeout waiting for client to disconnect")
}
_, err = relayClient.OpenConn("bob")
if err == nil {
t.Errorf("unexpected opening connection to closed server")
}
}
func TestCloseByClient(t *testing.T) {
ctx := context.Background()
2024-07-08 17:01:11 +02:00
srvCfg := server.ListenerConfig{Address: serverListenAddr}
srv := server.NewServer(serverURL, false, av)
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-05-28 01:00:25 +02:00
if err != nil {
2024-07-09 09:44:23 +02:00
errChan <- err
2024-05-28 01:00:25 +02:00
}
}()
2024-07-09 09:44:23 +02:00
// wait for servers to start
if err := waitForServerToStart(errChan); err != nil {
t.Fatalf("failed to start server: %s", err)
}
2024-07-08 22:13:31 +02:00
2024-06-05 19:49:30 +02:00
idAlice := "alice"
log.Debugf("connect by alice")
2024-07-08 17:01:11 +02:00
relayClient := NewClient(ctx, serverURL, hmacTokenStore, idAlice)
2024-06-05 19:49:30 +02:00
err := relayClient.Connect()
2024-05-28 01:00:25 +02:00
if err != nil {
2024-06-05 19:49:30 +02:00
log.Fatalf("failed to connect to server: %s", err)
2024-05-28 01:00:25 +02:00
}
2024-06-05 19:49:30 +02:00
err = relayClient.Close()
2024-05-28 01:00:25 +02:00
if err != nil {
2024-06-05 19:49:30 +02:00
t.Errorf("failed to close client: %s", err)
2024-05-28 01:00:25 +02:00
}
2024-06-05 19:49:30 +02:00
_, err = relayClient.OpenConn("bob")
2024-05-28 01:00:25 +02:00
if err == nil {
2024-06-05 19:49:30 +02:00
t.Errorf("unexpected opening connection to closed server")
2024-05-28 01:00:25 +02:00
}
2024-06-05 19:49:30 +02:00
err = srv.Close()
if err != nil {
t.Fatalf("failed to close server: %s", err)
2024-05-28 01:00:25 +02:00
}
}
2024-07-09 09:44:23 +02:00
func waitForServerToStart(errChan chan error) error {
select {
case err := <-errChan:
if err != nil {
return err
}
2024-07-09 10:09:09 +02:00
case <-time.After(300 * time.Millisecond):
2024-07-09 09:44:23 +02:00
return nil
}
return nil
}
2024-07-20 13:00:19 +02:00
2024-07-21 13:40:23 +02:00
func seedRandomData(size int) ([]byte, error) {
token := make([]byte, size)
2024-07-20 13:00:19 +02:00
_, err := rand.Read(token)
if err != nil {
return nil, err
}
return token, nil
}