forked from Lukasa/gopcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_test.go
110 lines (104 loc) · 3.72 KB
/
transport_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
package gopcap
import (
"bytes"
"testing"
)
func TestTCPGood(t *testing.T) {
// Define some data.
data := []byte{
0x0B, 0x20, 0x1A, 0x0B, 0x4D, 0xC8, 0x4E, 0xED, 0x54, 0xF1, 0x10, 0x72, 0x80, 0x18, 0x1F, 0x4B, 0x6D, 0x2E, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0A, 0x00, 0xD8,
0xEA, 0x48, 0x82, 0xE4, 0xDA, 0xB0, 0x49, 0x53, 0x4F, 0x4E, 0x20, 0x54, 0x68, 0x75, 0x6E, 0x66, 0x69, 0x73, 0x63, 0x68, 0x20, 0x53, 0x6D, 0x69, 0x6C, 0x65,
0x79, 0x20, 0x53, 0x6D, 0x69, 0x6C, 0x65, 0x79, 0x47, 0x0A,
}
optBytes := []byte{0x01, 0x01, 0x08, 0x0a, 0x00, 0xd8, 0xea, 0x48, 0x82, 0xe4, 0xda, 0xb0}
pkt := new(TCPSegment)
err := pkt.FromBytes(data)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if pkt.SourcePort != uint16(2848) {
t.Errorf("Unexpected source port: expected %v, got %v", 2848, pkt.SourcePort)
}
if pkt.DestinationPort != uint16(6667) {
t.Errorf("Unexpected destination port: expected %v, got %v", 6667, pkt.DestinationPort)
}
if pkt.SequenceNumber != uint32(1304973037) {
t.Errorf("Unexpected sequence number: expected %v, got %v", 1304973037, pkt.SequenceNumber)
}
if pkt.AckNumber != uint32(1425084530) {
t.Errorf("Unexpected ack number: expected %v, got %v", 1425084530, pkt.AckNumber)
}
if pkt.HeaderSize != uint8(8) {
t.Errorf("Unexpected header size: expected %v, got %v", 8, pkt.HeaderSize)
}
if pkt.NS {
t.Errorf("Expected NS flag not to be set and it was.")
}
if pkt.CWR {
t.Errorf("Expected CWR flag not to be set and it was.")
}
if pkt.ECE {
t.Errorf("Expected ECE flag not to be set and it was.")
}
if pkt.URG {
t.Errorf("Expected URG flag not to be set and it was.")
}
if !pkt.ACK {
t.Errorf("Expected ACK flag to be set and it wasn't.")
}
if !pkt.PSH {
t.Errorf("Expected PSH flag to be set and it wasn't.")
}
if pkt.RST {
t.Errorf("Expected RST flag not to be set and it was.")
}
if pkt.SYN {
t.Errorf("Expected SYN flag not to be set and it was.")
}
if pkt.FIN {
t.Errorf("Expected FIN flag not to be set and it was.")
}
if pkt.WindowSize != uint16(8011) {
t.Errorf("Unexpected window size: expected %v, got %v", 8011, pkt.WindowSize)
}
if pkt.Checksum != 0x6d2e {
t.Errorf("Unexpected checksum: expected %v, got %v", 0x6d2e, pkt.Checksum)
}
if pkt.UrgentOffset != uint16(0) {
t.Errorf("Unexpected urgent offset: expected %v, got %v", 0, pkt.UrgentOffset)
}
if bytes.Compare(pkt.OptionData, optBytes) != 0 {
t.Errorf("Unexpected option data: expected %v, got %v", optBytes, pkt.OptionData)
}
if len(pkt.TransportData()) != 30 {
t.Errorf("Unexpected length of transport data: expected %v, got %v", 30, len(pkt.TransportData()))
}
}
func TestUDPGood(t *testing.T) {
// Define the data for a UDP datagram.
data := []byte{
0x08, 0x50, 0x00, 0x35, 0x00, 0x32, 0x83, 0x97, 0x31, 0x1f, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x32, 0x01, 0x31,
0x03, 0x31, 0x36, 0x38, 0x03, 0x31, 0x39, 0x32, 0x07, 0x69, 0x6e, 0x2d, 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61, 0x00, 0x00, 0x0c,
0x00, 0x01,
}
dgram := new(UDPDatagram)
err := dgram.FromBytes(data)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if dgram.SourcePort != uint16(2128) {
t.Errorf("Unexpected source port: expected %v, got %v.", 2128, dgram.SourcePort)
}
if dgram.DestinationPort != uint16(53) {
t.Errorf("Unexpected destination port: expected %v, got %v.", 53, dgram.DestinationPort)
}
if dgram.Length != uint16(50) {
t.Errorf("Unexpected length: expected %v, got %v.", 50, dgram.Length)
}
if dgram.Checksum != uint16(33687) {
t.Errorf("Unexpected checksum: expected %v, got %v.", 33687, dgram.Checksum)
}
if len(dgram.TransportData()) != 42 {
t.Errorf("Unexpected length of contained data: expected %v, got %v", 42, len(dgram.TransportData()))
}
}