added support for string type
[govpp.git] / vendor / github.com / google / gopacket / pcapgo / read_test.go
1 // Copyright 2014 Damjan Cvetko. All rights reserved.
2 //
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
5 // tree.
6 package pcapgo
7
8 import (
9         "bytes"
10         "testing"
11         "time"
12 )
13
14 // test header read
15 func TestCreatePcapReader(t *testing.T) {
16         test := []byte{
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,
20         }
21         buf := bytes.NewBuffer(test)
22         _, err := NewReader(buf)
23         if err != nil {
24                 t.Error(err)
25                 t.FailNow()
26         }
27 }
28
29 // test big endian file read
30 func TestCreatePcapReaderBigEndian(t *testing.T) {
31         test := []byte{
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,
35         }
36         buf := bytes.NewBuffer(test)
37         _, err := NewReader(buf)
38         if err != nil {
39                 t.Error(err)
40                 t.FailNow()
41         }
42 }
43
44 // test opening invalid data
45 func TestCreatePcapReaderFail(t *testing.T) {
46         test := []byte{
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,
50         }
51         buf := bytes.NewBuffer(test)
52         _, err := NewReader(buf)
53         if err == nil {
54                 t.Error("Should fail but did not")
55                 t.FailNow()
56         }
57 }
58
59 func TestPacket(t *testing.T) {
60         test := []byte{
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
67         }
68
69         buf := bytes.NewBuffer(test)
70         r, err := NewReader(buf)
71
72         data, ci, err := r.ReadPacketData()
73         if err != nil {
74                 t.Error(err)
75                 t.FailNow()
76         }
77         if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1000, time.UTC)) {
78                 t.Error("Invalid time read")
79                 t.FailNow()
80         }
81         if ci.CaptureLength != 4 || ci.Length != 8 {
82                 t.Error("Invalid CapLen or Len")
83         }
84         want := []byte{1, 2, 3, 4}
85         if !bytes.Equal(data, want) {
86                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data)
87         }
88 }
89
90 func TestPacketNano(t *testing.T) {
91         test := []byte{
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
98         }
99
100         buf := bytes.NewBuffer(test)
101         r, err := NewReader(buf)
102
103         data, ci, err := r.ReadPacketData()
104         if err != nil {
105                 t.Error(err)
106                 t.FailNow()
107         }
108         if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1, time.UTC)) {
109                 t.Error("Invalid time read")
110                 t.FailNow()
111         }
112         if ci.CaptureLength != 4 || ci.Length != 8 {
113                 t.Error("Invalid CapLen or Len")
114         }
115         want := []byte{1, 2, 3, 4}
116         if !bytes.Equal(data, want) {
117                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data)
118         }
119 }
120
121 func TestGzipPacket(t *testing.T) {
122         test := []byte{
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,
130         }
131         buf := bytes.NewBuffer(test)
132         r, err := NewReader(buf)
133         if err != nil {
134                 t.Error("Unexpected error returned:", err)
135                 t.FailNow()
136         }
137
138         data, ci, err := r.ReadPacketData()
139         if err != nil {
140                 t.Error(err)
141                 t.FailNow()
142         }
143         if !ci.Timestamp.Equal(time.Date(2014, 9, 18, 12, 13, 14, 1000, time.UTC)) {
144                 t.Error("Invalid time read")
145                 t.FailNow()
146         }
147         if ci.CaptureLength != 4 || ci.Length != 8 {
148                 t.Error("Invalid CapLen or Len")
149         }
150         want := []byte{1, 2, 3, 4}
151         if !bytes.Equal(data, want) {
152                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data)
153         }
154 }
155
156 func TestTruncatedGzipPacket(t *testing.T) {
157         test := []byte{
158                 0x1f, 0x8b, 0x08,
159         }
160         buf := bytes.NewBuffer(test)
161         _, err := NewReader(buf)
162         if err == nil {
163                 t.Error("Should fail but did not")
164                 t.FailNow()
165         }
166 }
167
168 func TestPacketBufferReuse(t *testing.T) {
169         test := []byte{
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
179         }
180
181         buf := bytes.NewBuffer(test)
182         r, err := NewReader(buf)
183
184         data1, _, err := r.ReadPacketData()
185         if err != nil {
186                 t.Error(err)
187                 t.FailNow()
188         }
189         if want := []byte{1, 2, 3, 4}; !bytes.Equal(data1, want) {
190                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data1)
191         }
192         data2, _, err := r.ReadPacketData()
193         if err != nil {
194                 t.Error(err)
195                 t.FailNow()
196         }
197         for i := range data1 {
198                 data1[i] = 0xff // modify data1 after getting data2, make sure we don't overlap buffers.
199         }
200         if want := []byte{1, 2, 3, 4}; !bytes.Equal(data2, want) {
201                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, data2)
202         }
203 }