Skip to content

Commit

Permalink
Start tests against pkg/service
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Der committed Dec 13, 2023
1 parent 3a3aa14 commit 85d1d47
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
17 changes: 17 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"fmt"
"net"
"os"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -133,3 +134,19 @@ func (c *Config) GetLoggerFields() logrus.Fields {

return fields
}

func GetLocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", nil
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}

return "", fmt.Errorf("No local IP found")
}
39 changes: 39 additions & 0 deletions pkg/sip/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sip

import (
"fmt"
"math/rand"
"testing"

"github.com/livekit/mediatransportutil/pkg/rtcconfig"
"github.com/livekit/sip/pkg/config"
"github.com/stretchr/testify/require"
)

const (
testPortSIPMin = 30000
testPortSIPMax = 30050

testPortRTPMin = 30100
testPortRTPMax = 30150
)

// Test service E2E that
func TestService(t *testing.T) {
sipPort := rand.Intn(testPortSIPMax-testPortSIPMin) + testPortSIPMin

s, err := NewService(&config.Config{
SIPPort: sipPort,
RTPPort: rtcconfig.PortRange{Start: testPortRTPMin, End: testPortRTPMax},
})
require.NoError(t, err)
require.NotNil(t, s)

s.SetAuthHandler(func(_, _, _, _ string) (string, string, error) {
return "", "", fmt.Errorf("Auth Failure")
})

require.NoError(t, s.Start())

s.Stop()
}
27 changes: 6 additions & 21 deletions test/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
"github.com/icholy/digest"
"github.com/livekit/sip/pkg/config"
"github.com/livekit/sip/pkg/media/ulaw"
"github.com/pion/rtp"
"github.com/pion/sdp/v2"
Expand Down Expand Up @@ -113,22 +114,6 @@ func startMediaListener() *net.UDPConn {
return conn
}

func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}

panic("No IP Found")
}

func getResponse(tx sip.ClientTransaction) *sip.Response {
select {
case <-tx.Done():
Expand All @@ -153,13 +138,13 @@ func createOffer(port int) ([]byte, error) {
SessionVersion: sessionId,
NetworkType: "IN",
AddressType: "IP4",
UnicastAddress: getLocalIP(),
UnicastAddress: config.GetLocalIP(),

Check failure on line 141 in test/client/main.go

View workflow job for this annotation

GitHub Actions / test

multiple-value config.GetLocalIP() (value of type (string, error)) in single-value context
},
SessionName: "LiveKit",
ConnectionInformation: &sdp.ConnectionInformation{
NetworkType: "IN",
AddressType: "IP4",
Address: &sdp.Address{Address: getLocalIP()},
Address: &sdp.Address{Address: config.GetLocalIP()},

Check failure on line 147 in test/client/main.go

View workflow job for this annotation

GitHub Actions / test

multiple-value config.GetLocalIP() (value of type (string, error)) in single-value context
},
TimeDescriptions: []sdp.TimeDescription{
sdp.TimeDescription{
Expand Down Expand Up @@ -261,7 +246,7 @@ func attemptInvite(sipClient *sipgo.Client, offer []byte, authorizationHeaderVal
inviteRequest.SetDestination(*sipServer)
inviteRequest.SetBody(offer)
inviteRequest.AppendHeader(sip.NewHeader("Content-Type", "application/sdp"))
inviteRequest.AppendHeader(sip.NewHeader("Contact", fmt.Sprintf("<sip:livekit@%s:5060>", getLocalIP())))
inviteRequest.AppendHeader(sip.NewHeader("Contact", fmt.Sprintf("<sip:livekit@%s:5060>", config.GetLocalIP())))

Check failure on line 249 in test/client/main.go

View workflow job for this annotation

GitHub Actions / test

multiple-value config.GetLocalIP() (value of type (string, error)) in single-value context
inviteRequest.AppendHeader(sip.NewHeader("Allow", "INVITE, ACK, CANCEL, BYE, NOTIFY, REFER, MESSAGE, OPTIONS, INFO, SUBSCRIBE"))

if authorizationHeaderValue != "" {
Expand All @@ -281,7 +266,7 @@ func main() {
flag.Parse()

if *sipServer == "" {
*sipServer = getLocalIP() + ":5060"
*sipServer = config.GetLocalIP() + ":5060"

Check failure on line 269 in test/client/main.go

View workflow job for this annotation

GitHub Actions / test

multiple-value config.GetLocalIP() (value of type (string, error)) in single-value context
}

mediaConn := startMediaListener()
Expand All @@ -297,7 +282,7 @@ func main() {
panic(err)
}

sipClient, err := sipgo.NewClient(ua, sipgo.WithClientHostname(getLocalIP()))
sipClient, err := sipgo.NewClient(ua, sipgo.WithClientHostname(config.GetLocalIP()))

Check failure on line 285 in test/client/main.go

View workflow job for this annotation

GitHub Actions / test

too many arguments in call to sipgo.WithClientHostname
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 85d1d47

Please sign in to comment.