-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_test.go
More file actions
67 lines (63 loc) · 1.92 KB
/
Copy pathpacket_test.go
File metadata and controls
67 lines (63 loc) · 1.92 KB
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
package main
import (
"encoding/binary"
"math/rand"
"net/netip"
"testing"
)
func TestBuildIPv4UDPHeader(t *testing.T) {
header, err := buildPacketHeader(PacketSpec{
Internal: netip.MustParseAddr("10.0.0.42"),
Peer: netip.MustParseAddr("198.18.0.1"),
Direction: DirectionIncoming,
Protocol: ProtocolUDP,
FrameLength: 900,
}, rand.New(rand.NewSource(1)))
if err != nil {
t.Fatal(err)
}
if got := binary.BigEndian.Uint16(header[12:14]); got != 0x0800 {
t.Fatalf("EtherType=%#x, expected IPv4", got)
}
if header[23] != 17 {
t.Fatalf("IPv4 protocol=%d, expected UDP", header[23])
}
if got := netip.AddrFrom4([4]byte{header[26], header[27], header[28], header[29]}); got.String() != "198.18.0.1" {
t.Fatalf("source=%s", got)
}
if got := netip.AddrFrom4([4]byte{header[30], header[31], header[32], header[33]}); got.String() != "10.0.0.42" {
t.Fatalf("destination=%s", got)
}
}
func TestBuildIPv6TCPHeader(t *testing.T) {
header, err := buildPacketHeader(PacketSpec{
Internal: netip.MustParseAddr("2001:db8:10::42"),
Peer: netip.MustParseAddr("2001:db8:ffff::1"),
Direction: DirectionIncoming,
Protocol: ProtocolTCP,
FrameLength: 900,
TCPFlags: "syn",
}, rand.New(rand.NewSource(1)))
if err != nil {
t.Fatal(err)
}
if got := binary.BigEndian.Uint16(header[12:14]); got != 0x86dd {
t.Fatalf("EtherType=%#x, expected IPv6", got)
}
if header[20] != 6 {
t.Fatalf("IPv6 next header=%d, expected TCP", header[20])
}
if header[67] != 0x02 {
t.Fatalf("TCP flags=%#x, expected SYN", header[67])
}
}
func TestCisternSourceIDWorkaround(t *testing.T) {
typeValue, indexValue := cisternSourceIDWorkaround(2)
// This is the expression used by pinned upstream code.
encoded := uint32(typeValue) | indexValue<<24
var wire [4]byte
binary.BigEndian.PutUint32(wire[:], encoded)
if wire != [4]byte{0, 0, 0, 2} {
t.Fatalf("workaround encoded %v, expected ifIndex source ID", wire)
}
}