-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayer_ip6_test.go
104 lines (91 loc) · 2.47 KB
/
layer_ip6_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
package packet
import (
"syscall"
"testing"
"golang.org/x/net/ipv6"
)
func TestIP6MarshalBinary(t *testing.T) {
const hopLimit = 1
ip6 := EncodeIP6(nil, hopLimit, ip6LLAHost, ip6LLA4)
if len(ip6) != 40 {
t.Fatal("invalid ip6 len", ip6)
}
// validate first with google ipv6
header, err := ipv6.ParseHeader(ip6)
if err != nil {
t.Fatal("failed to parse ip6 header: ", err)
}
if header.Version != 0x06 {
t.Fatalf("invalid version %v %+v", header.Version, header)
}
if header.HopLimit != 1 {
t.Fatal("invalid hop limit ", header.HopLimit)
}
if header.NextHeader != 59 {
t.Fatal("invalid next header ", header.NextHeader)
}
if !header.Src.Equal(ip6LLAHost.AsSlice()) {
t.Fatal("invalid src ip ", header.Src)
}
if !header.Dst.Equal(ip6LLA4.AsSlice()) {
t.Fatal("invalid dst ip ", header.Src)
}
// validate with our ipv6
frame := IP6(ip6)
if err := frame.IsValid(); err != nil {
t.Fatalf("invalid frame %+v error %v", frame, err)
}
if frame.Version() != 0x06 {
t.Fatalf("invalid version %v %+v", frame.Version(), frame)
}
if frame.HopLimit() != 1 {
t.Fatal("invalid hop limit ", frame.HopLimit())
}
if frame.NextHeader() != 59 {
t.Fatal("invalid next header ", frame.NextHeader())
}
if frame.Src() != ip6LLAHost {
t.Fatal("invalid src ip ", frame.Src())
}
if frame.Dst() != ip6LLA4 {
t.Fatal("invalid dst ip ", frame.Dst())
}
}
func TestIP6Payload(t *testing.T) {
var err error
mypayload := []byte{0xf, 0xb, 0xa}
buf := make([]byte, EthMaxSize) // allocate in the stack
ether := EncodeEther(buf, syscall.ETH_P_IPV6, routerMAC, mac2)
if len(ether) != 14 {
t.Fatal("invalid ether len", len(ether))
}
ip6 := EncodeIP6(ether.Payload(), 1, ip6LLAHost, ip6LLA2)
if len(ip6) != 40 {
t.Fatal("invalid ip6 len", len(ip6))
}
ip6, err = ip6.AppendPayload(mypayload, 25) // 25 as a test id
if err != nil {
t.Fatal("error ip6 append ", err)
}
if len(ip6) != 43 {
t.Fatal("invalid ip6 len 2", ip6)
}
ether, _ = ether.AppendPayload(ip6)
if len(ether) != 60 { // 40+14+len(mypayload)
t.Fatal("invalid packet len ", len(ether))
}
// validate first with google ipv6
header, err := ipv6.ParseHeader(ether.Payload())
if err != nil {
t.Fatal("failed to parse ip6 header: ", err)
}
if header.Version != 0x06 {
t.Fatalf("invalid version %v %+v", header.Version, header)
}
if !header.Src.Equal(ip6LLAHost.AsSlice()) {
t.Fatal("invalid src ip ", header.Src)
}
if !header.Dst.Equal(ip6LLA2.AsSlice()) {
t.Fatal("invalid dst ip ", header.Src)
}
}