aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/google/gopacket/pcapgo/read_test.go
blob: 87bf7abc25341d5810cd7b39eb7c8b6fc7cf0a72 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2014 Damjan Cvetko. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package pcapgo

import (
	"bytes"
	"testing"
	"time"
)

// test header read
func TestCreatePcapReader(t *testing.T) {
	test := []byte{
		0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
	}
	buf := bytes.NewBuffer(test)
	_, err := NewReader(buf)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
}

// test big endian file read
func TestCreatePcapReaderBigEndian(t *testing.T) {
	test := []byte{
		0xa1, 0xb2, 0xc3, 0xd4, 0x00, 0x02, 0x00, 0x04,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
	}
	buf := bytes.NewBuffer(test)
	_, err := NewReader(buf)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
}

// test opening invalid data
func TestCreatePcapReaderFail(t *testing.T) {
	test := []byte{
		0xd0, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
	}
	buf := bytes.NewBuffer(test)
	_, err := NewReader(buf)
	if err == nil {
		t.Error("Should fail but did not")
		t.FailNow()
	}
}

func TestPacket(t *testing.T) {
	test := []byte{
		0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, // magic, maj, min
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tz, sigfigs
		0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // snaplen, linkType
		0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
		0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
		0x01, 0x02, 0x03, 0x04, // data
	}

	buf := bytes.NewBuffer(test)
	r, err := NewReader(buf)

	data, ci, err := r.ReadPacketData()
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1000, time.UTC)) {
		t.Error("Invalid time read")
		t.FailNow()
	}
	if ci.CaptureLength != 4 || ci.Length != 8 {
		t.Error("Invalid CapLen or Len")
	}
	want := []byte{1, 2, 3, 4}
	if !bytes.Equal(data, want) {
		t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data)
	}
}

func TestPacketNano(t *testing.T) {
	test := []byte{
		0x4d, 0x3c, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, // magic, maj, min
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tz, sigfigs
		0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // snaplen, linkType
		0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
		0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
		0x01, 0x02, 0x03, 0x04, // data
	}

	buf := bytes.NewBuffer(test)
	r, err := NewReader(buf)

	data, ci, err := r.ReadPacketData()
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1, time.UTC)) {
		t.Error("Invalid time read")
		t.FailNow()
	}
	if ci.CaptureLength != 4 || ci.Length != 8 {
		t.Error("Invalid CapLen or Len")
	}
	want := []byte{1, 2, 3, 4}
	if !bytes.Equal(data, want) {
		t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data)
	}
}

func TestGzipPacket(t *testing.T) {
	test := []byte{
		0x1f, 0x8b, 0x08, 0x08, 0x92, 0x4d, 0x81, 0x57,
		0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0xbb,
		0x72, 0x78, 0xd3, 0x42, 0x26, 0x06, 0x16, 0x06,
		0x18, 0xf8, 0xff, 0x9f, 0x81, 0x81, 0x11, 0x48,
		0x47, 0x9d, 0x91, 0x0a, 0x01, 0xd1, 0x20, 0x19,
		0x0e, 0x20, 0x66, 0x64, 0x62, 0x66, 0x01, 0x00,
		0xe4, 0x76, 0x9b, 0x75, 0x2c, 0x00, 0x00, 0x00,
	}
	buf := bytes.NewBuffer(test)
	r, err := NewReader(buf)
	if err != nil {
		t.Error("Unexpected error returned:", err)
		t.FailNow()
	}

	data, ci, err := r.ReadPacketData()
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1000, time.UTC)) {
		t.Error("Invalid time read")
		t.FailNow()
	}
	if ci.CaptureLength != 4 || ci.Length != 8 {
		t.Error("Invalid CapLen or Len")
	}
	want := []byte{1, 2, 3, 4}
	if !bytes.Equal(data, want) {
		t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data)
	}
}

func TestTruncatedGzipPacket(t *testing.T) {
	test := []byte{
		0x1f, 0x8b, 0x08,
	}
	buf := bytes.NewBuffer(test)
	_, err := NewReader(buf)
	if err == nil {
		t.Error("Should fail but did not")
		t.FailNow()
	}
}

func TestPacketBufferReuse(t *testing.T) {
	test := []byte{
		0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, // magic, maj, min
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tz, sigfigs
		0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // snaplen, linkType
		0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
		0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
		0x01, 0x02, 0x03, 0x04, // data
		0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
		0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
		0x01, 0x02, 0x03, 0x04, // data
	}

	buf := bytes.NewBuffer(test)
	r, err := NewReader(buf)

	data1, _, err := r.ReadPacketData()
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	if want := []byte{1, 2, 3, 4}; !bytes.Equal(data1, want) {
		t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data1)
	}
	data2, _, err := r.ReadPacketData()
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	for i := range data1 {
		data1[i] = 0xff // modify data1 after getting data2, make sure we don't overlap buffers.
	}
	if want := []byte{1, 2, 3, 4}; !bytes.Equal(data2, want) {
		t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data2)
	}
}