From 5c66135cd4b52d65d24305de2b414f131660409a Mon Sep 17 00:00:00 2001 From: Igor German Date: Thu, 11 Jul 2019 13:58:51 +0300 Subject: [PATCH] Added tests for SOFTWARE attribute Added tests to confirm that SOFTWARE is added to requests/responses --- client.go | 2 +- client_test.go | 38 +++++++++++++++++++++++++++++ sender.go | 6 ++--- server.go | 2 +- server_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 4ff0b86d..540a11c3 100644 --- a/client.go +++ b/client.go @@ -43,7 +43,7 @@ func NewClient(config *ClientConfig) (*Client, error) { } if config.Sender == nil { - config.Sender = DefaultSender + config.Sender = DefaultBuildAndSend } c := &Client{ diff --git a/client_test.go b/client_test.go index 3f4fa101..be3f0b5c 100644 --- a/client_test.go +++ b/client_test.go @@ -4,6 +4,10 @@ import ( "net" "testing" + "github.com/pion/stun" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/pion/logging" ) @@ -51,6 +55,40 @@ func TestClient(t *testing.T) { } }) + t.Run("SendSTUNRequest adds SOFTWARE attribute to message", func(t *testing.T) { + const testSoftware = "CLIENT_SOFTWARE" + + cfg := &ClientConfig{ + ListeningAddress: "0.0.0.0:0", + LoggerFactory: loggerFactory, + Sender: func(conn net.PacketConn, addr net.Addr, attrs ...stun.Setter) error { + msg, err := stun.Build(attrs...) + if err != nil { + return errors.Wrap(err, "could not build message") + } + var software stun.Software + if err = software.GetFrom(msg); err != nil { + return errors.Wrap(err, "could not get SOFTWARE attribute") + } + + assert.Equal(t, testSoftware, software.String()) + + // just forward to the default sender. + return DefaultBuildAndSend(conn, addr, attrs...) + }, + } + software := stun.NewSoftware(testSoftware) + cfg.Software = &software + + c, err := NewClient(cfg) + if err != nil { + t.Fatal(err) + } + if _, err = c.SendSTUNRequest(net.IPv4(74, 125, 143, 127), 19302); err != nil { + t.Fatal(err) + } + }) + t.Run("Listen error", func(t *testing.T) { _, err := NewClient(&ClientConfig{ ListeningAddress: "255.255.255.256:65535", diff --git a/sender.go b/sender.go index e8635eb4..ceb06553 100644 --- a/sender.go +++ b/sender.go @@ -9,10 +9,8 @@ import ( // Sender is responsible for building a message and sending it to the given addr. type Sender func(conn net.PacketConn, addr net.Addr, attrs ...stun.Setter) error -// DefaultSender is a sender default implementation. -var DefaultSender Sender = buildAndSend - -func buildAndSend(conn net.PacketConn, dst net.Addr, attrs ...stun.Setter) error { +// DefaultBuildAndSend is a sender default implementation. +func DefaultBuildAndSend(conn net.PacketConn, dst net.Addr, attrs ...stun.Setter) error { msg, err := stun.Build(attrs...) if err != nil { return err diff --git a/server.go b/server.go index a13c97e5..1c0d9931 100644 --- a/server.go +++ b/server.go @@ -80,7 +80,7 @@ func NewServer(config *ServerConfig) *Server { } if config.Sender == nil { - config.Sender = DefaultSender + config.Sender = DefaultBuildAndSend } manager := allocation.NewManager(&allocation.ManagerConfig{ diff --git a/server_test.go b/server_test.go index e265f26f..b3561ae9 100644 --- a/server_test.go +++ b/server_test.go @@ -5,6 +5,9 @@ import ( "testing" "time" + "github.com/pion/stun" + "github.com/pkg/errors" + "github.com/gortc/turn" "github.com/pion/logging" "github.com/stretchr/testify/assert" @@ -136,4 +139,67 @@ func TestServer(t *testing.T) { err = server.Close() assert.NoError(t, err, "should succeed") }) + + t.Run("Adds SOFTWARE attribute to response", func(t *testing.T) { + const testSoftware = "SERVER_SOFTWARE" + cfg := &ServerConfig{ + AuthHandler: func(username string, srcAddr net.Addr) (password string, ok bool) { + if pw, ok := credMap[username]; ok { + return pw, true + } + return "", false + }, + Realm: "pion.ly", + LoggerFactory: loggerFactory, + Sender: func(conn net.PacketConn, addr net.Addr, attrs ...stun.Setter) error { + msg, err := stun.Build(attrs...) + if err != nil { + return errors.Wrap(err, "could not build message") + } + var software stun.Software + if err = software.GetFrom(msg); err != nil { + return errors.Wrap(err, "could not get SOFTWARE attribute") + } + + assert.Equal(t, testSoftware, software.String()) + // just forward to the default sender. + return DefaultBuildAndSend(conn, addr, attrs...) + }, + } + software := stun.NewSoftware(testSoftware) + cfg.Software = &software + + server := NewServer(cfg) + + err := server.AddListeningIPAddr("127.0.0.1") + assert.NoError(t, err, "should succeed") + + log.Debug("start listening...") + err = server.Start() + assert.NoError(t, err, "should succeed") + + // make sure the server is listening before running + // the client. + time.Sleep(100 * time.Microsecond) + + log.Debug("creating a client.") + client, err := NewClient(&ClientConfig{ + ListeningAddress: "0.0.0.0:0", + LoggerFactory: loggerFactory, + }) + if !assert.NoError(t, err, "should succeed") { + return + } + + log.Debug("sending a binding request.") + resp, err := client.SendSTUNRequest(net.IPv4(127, 0, 0, 1), 3478) + assert.NoError(t, err, "should succeed") + t.Logf("resp: %v", resp) + + log.Debug("now closing the server...") + + // Close server + err = server.Close() + assert.NoError(t, err, "should succeed") + }) }