-
Notifications
You must be signed in to change notification settings - Fork 18
/
tun_router_test.go
46 lines (34 loc) · 882 Bytes
/
tun_router_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
package meshboi
import (
"net"
"reflect"
"testing"
"golang.org/x/net/ipv4"
"inet.af/netaddr"
)
func TestRouter(t *testing.T) {
store := NewPeerConnStore()
tunClient, tunServer := net.Pipe()
tr := NewTunRouter(tunClient, store)
go tr.Run()
defer tr.Stop()
peerClient, peerServer := net.Pipe()
peer := NewPeerConn(netaddr.MustParseIP("192.168.4.3"), netaddr.MustParseIPPort("192.152.12.2:2222"), peerClient, tunClient)
go peer.readLoop()
go peer.sendLoop()
store.Add(&peer)
hdr := ipv4.Header{
Src: net.ParseIP("192.168.4.2"),
Dst: net.ParseIP("192.168.4.3"),
Len: 20,
Version: 4,
}
hdrBytes, _ := hdr.Marshal()
msg := append(hdrBytes[:], []byte("hello")...)
tunServer.Write(msg)
readBytes := make([]byte, 1000)
n, _ := peerServer.Read(readBytes)
if !reflect.DeepEqual(readBytes[:n], msg) {
t.Errorf("Messages not equal")
}
}