-
Notifications
You must be signed in to change notification settings - Fork 1
/
handshake_test.go
63 lines (48 loc) · 1.05 KB
/
handshake_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
package tcprouter
import (
"bytes"
"net"
"sync"
"testing"
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/require"
)
func TestHandshakeEncodeDecode(t *testing.T) {
h := Handshake{
MagicNr: MagicNr,
Secret: []byte("hello world"),
}
b := bytes.Buffer{}
err := h.Write(&b)
require.NoError(t, err)
h2 := &Handshake{}
err = h2.Read(&b)
require.NoError(t, err)
assert.Equal(t, h.MagicNr, h2.MagicNr)
assert.Equal(t, h.Secret, h2.Secret)
}
func TestHandshake(t *testing.T) {
hc := Handshake{
MagicNr: MagicNr,
Secret: []byte("hello world"),
}
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
conn, err := l.Accept()
require.NoError(t, err)
hs := Handshake{}
err = hs.Read(conn)
require.NoError(t, err)
assert.Equal(t, hs.MagicNr, hc.MagicNr)
assert.Equal(t, hs.Secret, hc.Secret)
}()
conn, err := net.Dial("tcp", l.Addr().String())
require.NoError(t, err)
err = hc.Write(conn)
require.NoError(t, err)
wg.Wait()
}