-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconn_test.go
74 lines (65 loc) · 1.47 KB
/
conn_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
package ike
import (
"bytes"
"net"
"runtime"
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestOsxV6(t *testing.T) {
if !(runtime.GOOS == "darwin") {
t.Skip()
}
if checkV4onX(":80") {
t.Error("1")
}
if !checkV4onX("0.0.0.0:80") {
t.Error("2")
}
if !checkV4onX("localhost:80") {
t.Error("3")
}
if checkV4onX("[::1]:80") {
t.Error("4")
}
}
type mockConn struct {
ch chan []byte
}
func (m *mockConn) ReadPacket() (b []byte, remoteAddr, localAddr net.Addr, err error) {
return <-m.ch, &net.UDPAddr{}, &net.UDPAddr{}, nil
}
func (m *mockConn) WritePacket(reply []byte, remoteAddr net.Addr) error {
copy := append([]byte{}, reply...)
m.ch <- copy
return nil
}
func (m *mockConn) Inner() net.Conn { return nil }
func (m *mockConn) Close() error {
close(m.ch)
return nil
}
func testConn() *mockConn {
return &mockConn{ch: make(chan []byte, 2)}
}
func testCfg() *Config {
cfg := testConfig()
cfg.LocalID = pskTestID
cfg.PeerID = pskTestID
return cfg
}
var zeroAddr = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
func TestReadFragment(t *testing.T) {
conn := testConn()
sess, _ := NewInitiator(testCfg(), zeroAddr, zeroAddr, conn, &SessionCallback{}, logger)
msg, _ := InitFromSession(sess).Encode(nil, false, sess.Logger)
conn.WritePacket(msg[:40], nil)
conn.WritePacket(msg[40:], nil)
m2, _ := ReadMessage(conn, sess.Logger)
msg2, _ := m2.Encode(sess.tkm, false, sess.Logger)
if !bytes.Equal(msg, msg2) {
spew.Dump(msg)
spew.Dump(msg2)
t.Fail()
}
}