-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtunnel_test.go
122 lines (112 loc) · 3.04 KB
/
tunnel_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
116
117
118
119
120
121
122
package l2tp
import (
"bytes"
"net"
"reflect"
"testing"
)
// Tests can ONLY be run on Little Endian architectures.
func TestTunnelMessageMarshalBinary(t *testing.T) {
tests := []struct {
name string
m Message
b []byte
err error
}{
{
name: "defaulting verion to 3",
m: &TunnelMessage{
EncapType: Encap(L2TP_ENCAPTYPE_IP),
},
b: []byte{
0x06, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
0x05, 0x00, 0x07, 0x00, 0x03, 0x00, 0x00, 0x00,
},
},
{
name: "all attr session message",
m: &TunnelMessage{
EncapType: Encap(L2TP_ENCAPTYPE_IP),
ProtoVersion: Version(3),
ConnId: ID(98302),
PeerConnId: ID(2391),
IpSaddr: IP(net.ParseIP("192.168.3.3")),
IpDaddr: IP(net.ParseIP("10.34.2.19")),
UdpSport: Port(45934),
UdpDport: Port(34291),
Ip6Saddr: IP(net.ParseIP("::1")),
Ip6Daddr: IP(net.ParseIP("::2")),
},
b: []byte{
0x06, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
0x05, 0x00, 0x07, 0x00, 0x03, 0x00, 0x00, 0x00,
0x08, 0x00, 0x09, 0x00, 0xfe, 0x7f, 0x01, 0x00,
0x08, 0x00, 0x0a, 0x00, 0x57, 0x09, 0x00, 0x00,
0x08, 0x00, 0x18, 0x00, 0xc0, 0xa8, 0x03, 0x03,
0x14, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0x0a, 0x22, 0x02, 0x13, 0x06, 0x00, 0x1a, 0x00,
0x6e, 0xb3, 0x00, 0x00, 0x06, 0x00, 0x1b, 0x00,
0xf3, 0x85, 0x00, 0x00, 0x14, 0x00, 0x1f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x14, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, err := tt.m.MarshalBinary()
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
if err != nil {
return
}
if want, got := tt.b, b; !bytes.Equal(want, got) {
t.Fatalf("unexpected Message bytes:\n- want: [%# x]\n- got: [%# x]", want, got)
}
})
}
}
func TestTunnelMessageUnmarshalBinary(t *testing.T) {
tests := []struct {
name string
b []byte
m Message
err error
}{
{
name: "data",
b: []byte{
0x06, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
0x05, 0x00, 0x07, 0x00, 0x03, 0x00, 0x00, 0x00,
0x08, 0x00, 0x09, 0x00, 0xfe, 0x7f, 0x01, 0x00,
0x08, 0x00, 0x0a, 0x00, 0x57, 0x09, 0x00, 0x00,
},
m: &TunnelMessage{
EncapType: Encap(L2TP_ENCAPTYPE_IP),
ProtoVersion: Version(3),
ConnId: ID(98302),
PeerConnId: ID(2391),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &TunnelMessage{}
err := (m).UnmarshalBinary(tt.b)
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
if err != nil {
return
}
if want, got := tt.m, m; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected Message:\n- want: %#v\n- got: %#v", want, got)
}
})
}
}