-
Notifications
You must be signed in to change notification settings - Fork 4
/
socket_test.go
78 lines (72 loc) · 2 KB
/
socket_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
package MoldUDP
import (
"github.com/kjx98/golib/nettypes"
"net"
"testing"
"unsafe"
)
func TestHeaderSize(t *testing.T) {
if n := unsafe.Sizeof(ipHeader{}); n != 20 {
t.Errorf("error ipHeader size: %d should be 20", n)
}
if n := unsafe.Sizeof(udpHeader{}); n != 8 {
t.Errorf("error udpHeader size: %d should be 8", n)
}
}
func TestBuildUDP(t *testing.T) {
buff := make([]byte, 128)
srcBuf := []byte("test only, should be fine")
n := len(srcBuf)
srcIP := net.IPv4(192, 168, 0, 1).To4()
dstIP := net.IPv4(239, 192, 168, 1).To4()
buildIP(buff, n, srcIP, dstIP)
ip := nettypes.IPv4_P(buff)
ckSum := ip.CalculateChecksum()
buff[10] = byte(ckSum >> 8)
buff[11] = byte(ckSum & 0xff)
if ip.PacketCorrupt() {
t.Errorf("IP checksum dismatch: %x expect %x", ip.Checksum(),
ip.CalculateChecksum())
}
buildUDP(buff[20:], 5858, n)
copy(buff[28:], srcBuf)
nn := n + 28
t.Log("dump IP frame:", ip.String(uint16(nn), 0))
}
func TestBuildRawUDP(t *testing.T) {
buff := make([]byte, 128)
srcBuf := []byte("test only, should be fine")
n := len(srcBuf)
srcIP := net.IPv4(192, 168, 0, 1).To4()
dstIP := net.IPv4(239, 192, 168, 1).To4()
buildRawUDP(buff, n, 5858, srcIP, dstIP)
ip := nettypes.IPv4_P(buff[14:])
if ip.PacketCorrupt() {
t.Errorf("IP checksum dismatch: %x expect %x", ip.Checksum(),
ip.CalculateChecksum())
}
copy(buff[14+28:], srcBuf)
nn := n + 28 + 14
f := nettypes.Frame(buff[:nn])
t.Log("dump Ether frame:", f.String(uint16(nn), 0))
}
func BenchmarkBuildIP(b *testing.B) {
buff := [128]byte{}
srcBuf := []byte("test only, should be fine")
n := len(srcBuf)
srcIP := net.IPv4(192, 168, 0, 1).To4()
dstIP := net.IPv4(239, 192, 168, 1).To4()
for i := 0; i < b.N; i++ {
buildIP(buff[:], n, srcIP, dstIP)
}
}
func BenchmarkBuildIPv4(b *testing.B) {
buff := [128]byte{}
srcBuf := []byte("test only, should be fine")
n := len(srcBuf)
srcIP := net.IPv4(192, 168, 0, 1).To4()
dstIP := net.IPv4(239, 192, 168, 1).To4()
for i := 0; i < b.N; i++ {
buildIPv4(buff[:], n, srcIP, dstIP)
}
}