1 // Copyright 2012 Google, Inc. 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
11 "github.com/google/gopacket"
12 "github.com/google/gopacket/layers"
13 "github.com/google/gopacket/pcapgo"
20 func TestPCAPGoWrite(t *testing.T) {
21 f, err := ioutil.TempFile("", "pcapgo")
25 data := []byte{0xab, 0xcd, 0xef, 0x01, 0x02, 0x03, 0x04}
26 ci := gopacket.CaptureInfo{
27 Timestamp: time.Unix(12345667, 1234567000),
29 CaptureLength: len(data),
33 w := pcapgo.NewWriter(f)
34 if err := w.WriteFileHeader(65536, layers.LinkTypeEthernet); err != nil {
37 if err := w.WritePacket(ci, data); err != nil {
41 h, err := OpenOffline(f.Name())
46 gotData, gotCI, err := h.ReadPacketData()
48 t.Fatal("could not read first packet:", err)
50 if !bytes.Equal(gotData, data) {
51 t.Errorf("byte mismatch:\nwant: %v\n got: %v", data, gotData)
53 if !reflect.DeepEqual(ci, gotCI) {
54 t.Errorf("CI mismatch:\nwant: %v\n got: %v", ci, gotCI)