/
client_test.go
115 lines (97 loc) · 2.71 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package turn
import (
"net"
"testing"
"github.com/pion/stun"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/pion/logging"
)
func TestClient(t *testing.T) {
loggerFactory := logging.NewDefaultLoggerFactory()
t.Run("SendSTUNRequest Parallel", func(t *testing.T) {
c, err := NewClient(&ClientConfig{
ListeningAddress: "0.0.0.0:0",
LoggerFactory: loggerFactory,
})
if err != nil {
t.Fatal(err)
}
// simple channel fo go routine start signaling
started := make(chan struct{})
finished := make(chan struct{})
var err1 error
var resp1 interface{}
// stun1.l.google.com:19302, more at https://gist.github.com/zziuni/3741933#file-stuns-L5
go func() {
close(started)
resp1, err1 = c.SendSTUNRequest(net.IPv4(74, 125, 143, 127), 19302)
close(finished)
}()
// block until go routine is started to make two almost parallel requests
<-started
resp2, err2 := c.SendSTUNRequest(net.IPv4(74, 125, 143, 127), 19302)
if err2 != nil {
t.Fatal(err)
} else {
t.Log(resp2)
}
<-finished
if err1 != nil {
t.Fatal(err)
} else {
t.Log(resp1)
}
})
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",
LoggerFactory: loggerFactory,
})
if err == nil {
t.Fatal("listening on 255.255.255.256:65535 should fail")
}
})
/*
// Unable to perform this test atm because there is no timeout and the test may run infinitely
t.Run("SendSTUNRequest timeout", func(t *testing.T) {
c, err := NewClient("0.0.0.0:0")
if err != nil {
t.Fatal(err)
}
_, err = c.SendSTUNRequest(net.IPv4(255, 255, 255, 255), 65535)
if err == nil {
t.Fatal("request to 255.255.255.255:65535 should fail")
}
})
*/
}