-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcapnggo_test.go
60 lines (56 loc) · 1.32 KB
/
pcapnggo_test.go
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
// Copyright 2018 The GoPacket Authors. 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 pcap
import (
"bytes"
"io/ioutil"
"reflect"
"testing"
"time"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/gopacket/gopacket/pcapgo"
)
func TestPCAPGoNgWrite(t *testing.T) {
f, err := ioutil.TempFile("", "pcapnggo")
if err != nil {
t.Fatal(err)
}
data := []byte{0xab, 0xcd, 0xef, 0x01, 0x02, 0x03, 0x04}
ci := gopacket.CaptureInfo{
Timestamp: time.Unix(12345667, 1234567123),
Length: 700,
CaptureLength: len(data),
}
func() {
defer f.Close()
w, err := pcapgo.NewNgWriter(f, layers.LinkTypeEthernet)
if err != nil {
t.Fatal(err)
}
if err := w.WritePacket(ci, data); err != nil {
t.Fatal(err)
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
}()
h, err := OpenOffline(f.Name())
if err != nil {
t.Fatal(err)
}
defer h.Close()
gotData, gotCI, err := h.ReadPacketData()
if err != nil {
t.Fatal("could not read first packet:", err)
}
if !bytes.Equal(gotData, data) {
t.Errorf("byte mismatch:\nwant: %v\n got: %v", data, gotData)
}
if !reflect.DeepEqual(ci, gotCI) {
t.Errorf("CI mismatch:\nwant: %v\n got: %v", ci, gotCI)
}
}