netbird/relay/client/manager_test.go

157 lines
3.1 KiB
Go
Raw Normal View History

2024-05-29 16:40:26 +02:00
package client
import (
"context"
"testing"
2024-06-01 11:48:15 +02:00
log "github.com/sirupsen/logrus"
2024-05-29 16:40:26 +02:00
"github.com/netbirdio/netbird/relay/server"
)
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-05-29 16:40:26 +02:00
addr1 := "localhost:1234"
srv1 := server.NewServer()
go func() {
err := srv1.Listen(addr1)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv1.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
addr2 := "localhost:2234"
srv2 := server.NewServer()
go func() {
err := srv2.Listen(addr2)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv2.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-06-01 11:48:15 +02:00
idAlice := "alice"
log.Debugf("connect by alice")
2024-05-29 16:40:26 +02:00
clientAlice := NewManager(ctx, addr1, idAlice)
err := clientAlice.Serve()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
2024-06-01 11:48:15 +02:00
idBob := "bob"
log.Debugf("connect by bob")
2024-05-29 16:40:26 +02:00
clientBob := NewManager(ctx, addr2, idBob)
err = clientBob.Serve()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
bobsSrvAddr, err := clientBob.RelayAddress()
if err != nil {
t.Fatalf("failed to get relay address: %s", err)
}
connAliceToBob, err := clientAlice.OpenConn(bobsSrvAddr.String(), idBob)
2024-05-29 16:40:26 +02:00
if err != nil {
t.Fatalf("failed to bind channel: %s", err)
}
2024-06-01 11:48:15 +02:00
connBobToAlice, err := clientBob.OpenConn(bobsSrvAddr.String(), 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()
addr1 := "localhost:1234"
srv1 := server.NewServer()
go func() {
err := srv1.Listen(addr1)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv1.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
addr2 := "localhost:2234"
srv2 := server.NewServer()
go func() {
err := srv2.Listen(addr2)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv2.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
idAlice := "alice"
log.Debugf("connect by alice")
clientAlice := NewManager(ctx, addr1, idAlice)
err := clientAlice.Serve()
if err != nil {
t.Fatalf("failed to connect to server: %s", err)
}
conn, err := clientAlice.OpenConn(addr2, "anotherpeer")
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)
}
select {}
}