netbird/relay/test/client_test.go

405 lines
8.5 KiB
Go
Raw Normal View History

2024-05-17 17:43:28 +02:00
package test
import (
2024-05-26 22:14:33 +02:00
"context"
"net"
2024-05-17 17:43:28 +02:00
"os"
"testing"
log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/util"
2024-05-17 17:43:28 +02:00
"github.com/netbirdio/netbird/relay/client"
"github.com/netbirdio/netbird/relay/server"
)
func TestMain(m *testing.M) {
_ = util.InitLog("trace", "console")
code := m.Run()
os.Exit(code)
}
func TestClient(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-05-17 17:43:28 +02:00
addr := "localhost:1234"
srv := server.NewServer()
go func() {
err := srv.Listen(addr)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-05-26 22:14:33 +02:00
clientAlice := client.NewClient(ctx, addr, "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()
2024-05-26 22:14:33 +02:00
clientPlaceHolder := client.NewClient(ctx, addr, "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()
2024-05-26 22:14:33 +02:00
clientBob := client.NewClient(ctx, addr, "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()
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)
}
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]))
}
}
func TestRegistration(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
addr := "localhost:1234"
srv := server.NewServer()
go func() {
err := srv.Listen(addr)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-05-26 22:14:33 +02:00
clientAlice := client.NewClient(ctx, addr, "alice")
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 conn: %s", err)
}
}()
}
func TestRegistrationTimeout(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
udpListener, 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)
}
defer udpListener.Close()
tcpListener, 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)
}
defer tcpListener.Close()
2024-05-26 22:14:33 +02:00
clientAlice := client.NewClient(ctx, "127.0.0.1:1234", "alice")
err = clientAlice.Connect()
if err == nil {
t.Errorf("failed to connect to server: %s", err)
}
defer func() {
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-05-17 17:43:28 +02:00
addr := "localhost:1234"
srv := server.NewServer()
go func() {
err := srv.Listen(addr)
if err != nil {
t.Fatalf("failed to bind server: %s", err)
}
}()
defer func() {
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-05-26 22:14:33 +02:00
clientAlice := client.NewClient(ctx, addr, 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-05-26 22:14:33 +02:00
clientBob := client.NewClient(ctx, addr, 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-05-17 17:43:28 +02:00
addr := "localhost:1234"
srv := server.NewServer()
go func() {
err := srv.Listen(addr)
if err != nil {
t.Fatalf("failed to bind server: %s", 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-05-26 22:14:33 +02:00
clientAlice := client.NewClient(ctx, addr, "alice")
2024-05-17 17:43:28 +02:00
err := clientAlice.Connect()
if err != nil {
t.Errorf("failed to connect to server: %s", err)
}
defer func() {
log.Infof("closing client")
err := clientAlice.Close()
if err != nil {
t.Errorf("failed to close client: %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)
}
}
func TestBindReconnect(t *testing.T) {
2024-05-26 22:14:33 +02:00
ctx := context.Background()
2024-05-17 17:43:28 +02:00
addr := "localhost:1234"
srv := server.NewServer()
go func() {
err := srv.Listen(addr)
if err != nil {
t.Errorf("failed to bind server: %s", err)
}
}()
defer func() {
log.Infof("closing server")
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
2024-05-26 22:14:33 +02:00
clientAlice := client.NewClient(ctx, addr, "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-05-26 22:14:33 +02:00
clientBob := client.NewClient(ctx, addr, "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)
}
log.Infof("closing client")
err = clientAlice.Close()
if err != nil {
t.Errorf("failed to close client: %s", err)
}
2024-05-26 22:14:33 +02:00
clientAlice = client.NewClient(ctx, addr, "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()
addr := "localhost:1234"
srv := server.NewServer()
go func() {
err := srv.Listen(addr)
if err != nil {
t.Errorf("failed to bind server: %s", err)
}
}()
defer func() {
log.Infof("closing server")
err := srv.Close()
if err != nil {
t.Errorf("failed to close server: %s", err)
}
}()
clientAlice := client.NewClient(ctx, addr, "alice")
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")
}
}