-
Notifications
You must be signed in to change notification settings - Fork 107
/
packet_test.go
183 lines (175 loc) · 4.07 KB
/
packet_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"net"
"reflect"
"testing"
)
var (
testICMPPing = IPPacket([]byte{0x45, 0x00, 0x00, 0x54, 0x0e, 0xe1, 0x40, 0x00, 0x40, 0x01, 0xa4, 0x65, 0xc0, 0xa8, 0x03, 0x0f, 0xc0, 0xa8, 0x03, 0x03, 0x08, 0x00, 0xad, 0xf2, 0x6a, 0x8c, 0x00, 0x08, 0x4c, 0xee, 0xfc, 0x58, 0xab, 0x2e, 0x00, 0x00, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37})
testICMPPong = IPPacket([]byte{0x45, 0x00, 0x00, 0x54, 0xed, 0x4e, 0x00, 0x00, 0x40, 0x01, 0x05, 0xf8, 0xc0, 0xa8, 0x03, 0x03, 0xc0, 0xa8, 0x03, 0x0f, 0x00, 0x00, 0xb5, 0xf2, 0x6a, 0x8c, 0x00, 0x08, 0x4c, 0xee, 0xfc, 0x58, 0xab, 0x2e, 0x00, 0x00, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37})
)
func TestIPPacket_IPver(t *testing.T) {
tests := []struct {
name string
p IPPacket
want int
}{
{
name: "ping",
p: testICMPPing,
want: 4,
},
{
name: "pong",
p: testICMPPong,
want: 4,
},
{
name: "ipv6",
p: IPPacket([]byte{6 << 4, 0}),
want: 6,
},
{
name: "invalid",
p: IPPacket([]byte{3 << 4, 0}),
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.IPver(); got != tt.want {
t.Errorf("IPPacket.IPver() = %v, want %v", got, tt.want)
}
})
}
}
func TestIPPacket_Dst(t *testing.T) {
tests := []struct {
name string
p IPPacket
want [4]byte
}{
{
name: "ping",
p: testICMPPing,
want: [4]byte{192, 168, 3, 3},
},
{
name: "pong",
p: testICMPPong,
want: [4]byte{192, 168, 3, 15},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.Dst(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("IPPacket.Dst() = %v, want %v", got, tt.want)
}
})
}
}
func TestIPPacket_DstV4(t *testing.T) {
tests := []struct {
name string
p IPPacket
want net.IP
}{
{
name: "ping",
p: testICMPPing,
want: net.ParseIP("192.168.3.3"),
},
{
name: "pong",
p: testICMPPong,
want: net.ParseIP("192.168.3.15"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.DstV4(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("IPPacket.DstV4() = %v, want %v", got, tt.want)
}
})
}
}
func TestIPPacket_Src(t *testing.T) {
tests := []struct {
name string
p IPPacket
want [4]byte
}{
{
name: "ping",
p: testICMPPing,
want: [4]byte{192, 168, 3, 15},
},
{
name: "pong",
p: testICMPPong,
want: [4]byte{192, 168, 3, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.Src(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("IPPacket.Src() = %v, want %v", got, tt.want)
}
})
}
}
func TestIPPacket_IsMulticast(t *testing.T) {
tests := []struct {
name string
p IPPacket
want bool
}{
{
name: "230.0.0.1",
p: IPPacket([]byte{4 << 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 230, 0, 0, 1, 0}),
want: true,
},
{
name: "ping",
p: testICMPPing,
want: false,
},
{
name: "pong",
p: testICMPPong,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.IsMulticast(); got != tt.want {
t.Errorf("IPPacket.IsMulticast() = %v, want %v", got, tt.want)
}
})
}
}
func TestIPPacket_GetSize(t *testing.T) {
tests := []struct {
name string
p IPPacket
want int
}{
{
name: "ping",
p: testICMPPing,
want: 84,
},
{
name: "pong",
p: testICMPPong,
want: 84,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.GetSize(); got != tt.want {
t.Errorf("IPPacket.GetSize() = %v, want %v", got, tt.want)
}
})
}
}