-
Notifications
You must be signed in to change notification settings - Fork 3
/
format_pcap_test.go
53 lines (47 loc) · 1.1 KB
/
format_pcap_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
package pktdump_test
import (
"log"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/google/gopacket"
"github.com/google/gopacket/pcapgo"
"github.com/x-way/pktdump"
)
func getTcpdumpOutput(filename string) string {
out, err := exec.Command("tcpdump", "-S", "-t", "-n", "-r", filename).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func getFormatOutput(filename string) string {
f, _ := os.Open(filename)
defer f.Close()
handle, err := pcapgo.NewReader(f)
if err != nil {
log.Fatal(err)
}
out := ""
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
out = out + pktdump.Format(packet) + "\n"
}
return out
}
func TestFormatPCAP(t *testing.T) {
files, err := os.ReadDir("test")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
if filepath.Ext(f.Name()) == ".pcap" {
expected := getTcpdumpOutput("test/" + f.Name())
got := getFormatOutput("test/" + f.Name())
if got != expected {
t.Errorf("pcap test failed for %s, got '%s', expected '%s'", f.Name(), got, expected)
}
}
}
}