1 // Copyright 2014 Damjan Cvetko. All rights reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
15 func TestCreatePcapReader(t *testing.T) {
17 0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
18 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
21 buf := bytes.NewBuffer(test)
22 _, err := NewReader(buf)
29 // test big endian file read
30 func TestCreatePcapReaderBigEndian(t *testing.T) {
32 0xa1, 0xb2, 0xc3, 0xd4, 0x00, 0x02, 0x00, 0x04,
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
36 buf := bytes.NewBuffer(test)
37 _, err := NewReader(buf)
44 // test opening invalid data
45 func TestCreatePcapReaderFail(t *testing.T) {
47 0xd0, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
51 buf := bytes.NewBuffer(test)
52 _, err := NewReader(buf)
54 t.Error("Should fail but did not")
59 func TestPacket(t *testing.T) {
61 0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, // magic, maj, min
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tz, sigfigs
63 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // snaplen, linkType
64 0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
65 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
66 0x01, 0x02, 0x03, 0x04, // data
69 buf := bytes.NewBuffer(test)
70 r, err := NewReader(buf)
72 data, ci, err := r.ReadPacketData()
77 if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1000, time.UTC)) {
78 t.Error("Invalid time read")
81 if ci.CaptureLength != 4 || ci.Length != 8 {
82 t.Error("Invalid CapLen or Len")
84 want := []byte{1, 2, 3, 4}
85 if !bytes.Equal(data, want) {
86 t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, data)
90 func TestPacketNano(t *testing.T) {
92 0x4d, 0x3c, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, // magic, maj, min
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tz, sigfigs
94 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // snaplen, linkType
95 0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
96 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
97 0x01, 0x02, 0x03, 0x04, // data
100 buf := bytes.NewBuffer(test)
101 r, err := NewReader(buf)
103 data, ci, err := r.ReadPacketData()
108 if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1, time.UTC)) {
109 t.Error("Invalid time read")
112 if ci.CaptureLength != 4 || ci.Length != 8 {
113 t.Error("Invalid CapLen or Len")
115 want := []byte{1, 2, 3, 4}
116 if !bytes.Equal(data, want) {
117 t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, data)
121 func TestGzipPacket(t *testing.T) {
123 0x1f, 0x8b, 0x08, 0x08, 0x92, 0x4d, 0x81, 0x57,
124 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x00, 0xbb,
125 0x72, 0x78, 0xd3, 0x42, 0x26, 0x06, 0x16, 0x06,
126 0x18, 0xf8, 0xff, 0x9f, 0x81, 0x81, 0x11, 0x48,
127 0x47, 0x9d, 0x91, 0x0a, 0x01, 0xd1, 0x20, 0x19,
128 0x0e, 0x20, 0x66, 0x64, 0x62, 0x66, 0x01, 0x00,
129 0xe4, 0x76, 0x9b, 0x75, 0x2c, 0x00, 0x00, 0x00,
131 buf := bytes.NewBuffer(test)
132 r, err := NewReader(buf)
134 t.Error("Unexpected error returned:", err)
138 data, ci, err := r.ReadPacketData()
143 if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1000, time.UTC)) {
144 t.Error("Invalid time read")
147 if ci.CaptureLength != 4 || ci.Length != 8 {
148 t.Error("Invalid CapLen or Len")
150 want := []byte{1, 2, 3, 4}
151 if !bytes.Equal(data, want) {
152 t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, data)
156 func TestTruncatedGzipPacket(t *testing.T) {
160 buf := bytes.NewBuffer(test)
161 _, err := NewReader(buf)
163 t.Error("Should fail but did not")
168 func TestPacketBufferReuse(t *testing.T) {
170 0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, // magic, maj, min
171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // tz, sigfigs
172 0xff, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // snaplen, linkType
173 0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
174 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
175 0x01, 0x02, 0x03, 0x04, // data
176 0x5A, 0xCC, 0x1A, 0x54, 0x01, 0x00, 0x00, 0x00, // sec, usec
177 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // cap len, full len
178 0x01, 0x02, 0x03, 0x04, // data
181 buf := bytes.NewBuffer(test)
182 r, err := NewReader(buf)
184 data1, _, err := r.ReadPacketData()
189 if want := []byte{1, 2, 3, 4}; !bytes.Equal(data1, want) {
190 t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, data1)
192 data2, _, err := r.ReadPacketData()
197 for i := range data1 {
198 data1[i] = 0xff // modify data1 after getting data2, make sure we don't overlap buffers.
200 if want := []byte{1, 2, 3, 4}; !bytes.Equal(data2, want) {
201 t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, data2)