added support for string type
[govpp.git] / vendor / github.com / google / gopacket / pcapgo / write_test.go
1 // Copyright 2012 Google, Inc. 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
7 package pcapgo
8
9 import (
10         "bytes"
11         "github.com/google/gopacket"
12         "testing"
13         "time"
14 )
15
16 func TestWriteHeader(t *testing.T) {
17         var buf bytes.Buffer
18         w := NewWriter(&buf)
19         w.WriteFileHeader(0x1234, 0x56)
20         want := []byte{
21                 0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
22                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23                 0x34, 0x12, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
24         }
25         if got := buf.Bytes(); !bytes.Equal(got, want) {
26                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, got)
27         }
28 }
29
30 func TestWritePacket(t *testing.T) {
31         ci := gopacket.CaptureInfo{
32                 Timestamp:     time.Unix(0x01020304, 0xAA*1000),
33                 Length:        0xABCD,
34                 CaptureLength: 10,
35         }
36         data := []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
37         var buf bytes.Buffer
38         w := NewWriter(&buf)
39         w.WritePacket(ci, data)
40         want := []byte{
41                 0x04, 0x03, 0x02, 0x01, 0xAA, 0x00, 0x00, 0x00,
42                 0x0A, 0x00, 0x00, 0x00, 0xCD, 0xAB, 0x00, 0x00,
43                 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
44         }
45         if got := buf.Bytes(); !bytes.Equal(got, want) {
46                 t.Errorf("buf mismatch:\nwant: %+v\ngot:  %+v", want, got)
47         }
48 }
49
50 func TestCaptureInfoErrors(t *testing.T) {
51         data := []byte{1, 2, 3, 4}
52         ts := time.Unix(0, 0)
53         for _, test := range []gopacket.CaptureInfo{
54                 gopacket.CaptureInfo{
55                         Timestamp:     ts,
56                         Length:        5,
57                         CaptureLength: 5,
58                 },
59                 gopacket.CaptureInfo{
60                         Timestamp:     ts,
61                         Length:        3,
62                         CaptureLength: 4,
63                 },
64         } {
65                 var buf bytes.Buffer
66                 w := NewWriter(&buf)
67                 if err := w.WritePacket(test, data); err == nil {
68                         t.Errorf("CaptureInfo %+v should have error", test)
69                 }
70         }
71 }