-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathethernet_test.go
71 lines (61 loc) · 1.57 KB
/
ethernet_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
package proto
import (
"net"
"reflect"
"testing"
)
func TestEthernet(t *testing.T) {
b := []byte{
156, 78, 54, 89, 178, 84,
232, 222, 39, 187, 107, 170,
8, 0,
69, 32, 0, 71, 159, 133, 0, 0, 41, 17, 112, 72,
192, 168, 0, 1,
192, 168, 0, 103,
0, 53, 111, 47, 0, 51, 144, 128, 126, 47, 129, 128,
0, 1, 0, 1, 0, 0, 0, 0, 6, 109, 105, 115, 102, 114,
97, 2, 109, 101, 0, 0, 1, 0, 1, 192, 12, 0, 1, 0,
1, 0, 0, 84, 7, 0, 4, 199, 58, 162, 130,
}
frame, err := DecodeEthernet(b)
if err != nil {
t.Fatal(err)
}
if frame.Source.String() != "e8:de:27:bb:6b:aa" {
t.Error("Got the wrong source MAC:", frame.Source.String())
}
}
func TestEthernetEncode(t *testing.T) {
e := EthernetFrame{
Source: net.HardwareAddr{0x9c, 0x4e, 0x36, 0x59, 0xb2, 0x54},
Destination: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
VlanTag: 0,
EtherType: 0x0800,
Payload: []byte("foobarbaz"),
}
b := e.Bytes()
frame, err := DecodeEthernet(b)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(frame, e) {
t.Error("Encoded and decoded Ethernet frames not equal:", frame, e)
}
}
func BenchmarkEthernet(b *testing.B) {
buf := [...]byte{
156, 78, 54, 89, 178, 84,
232, 222, 39, 187, 107, 170,
8, 0,
69, 32, 0, 71, 159, 133, 0, 0, 41, 17, 112, 72,
192, 168, 0, 1,
192, 168, 0, 103,
0, 53, 111, 47, 0, 51, 144, 128, 126, 47, 129, 128,
0, 1, 0, 1, 0, 0, 0, 0, 6, 109, 105, 115, 102, 114,
97, 2, 109, 101, 0, 0, 1, 0, 1, 192, 12, 0, 1, 0,
1, 0, 0, 84, 7, 0, 4, 199, 58, 162, 130,
}
for i := 0; i < b.N; i++ {
DecodeEthernet(buf[:])
}
}