Skip to content

Commit

Permalink
Added tests for SOFTWARE attribute
Browse files Browse the repository at this point in the history
Added tests to confirm that SOFTWARE is added to
requests/responses
  • Loading branch information
Igor German authored and Sean-Der committed Jul 11, 2019
1 parent 03d0802 commit 5c66135
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewClient(config *ClientConfig) (*Client, error) {
}

if config.Sender == nil {
config.Sender = DefaultSender
config.Sender = DefaultBuildAndSend
}

c := &Client{
Expand Down
38 changes: 38 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"net"
"testing"

"github.com/pion/stun"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/pion/logging"
)

Expand Down Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewServer(config *ServerConfig) *Server {
}

if config.Sender == nil {
config.Sender = DefaultSender
config.Sender = DefaultBuildAndSend
}

manager := allocation.NewManager(&allocation.ManagerConfig{
Expand Down
66 changes: 66 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
})
}

0 comments on commit 5c66135

Please sign in to comment.