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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright 2012 Google, Inc. 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 (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"testing"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
)
func TestPcapNonexistentFile(t *testing.T) {
handle, err := OpenOffline("/path/to/nonexistent/file")
if err == nil {
t.Error("No error returned for nonexistent file open")
} else {
t.Logf("Error returned for nonexistent file: %v", err)
}
if handle != nil {
t.Error("Non-nil handle returned for nonexistent file open")
}
}
func TestPcapFileRead(t *testing.T) {
invalidData := []byte{
0xAB, 0xAD, 0x1D, 0xEA,
}
invalidPcap, err := ioutil.TempFile("", "invalid.pcap")
if err != nil {
t.Fatal(err)
}
invalidPcap.Close() // if the file is still open later, the invalid test fails with permission denied on windows
defer os.Remove(invalidPcap.Name())
err = ioutil.WriteFile(invalidPcap.Name(), invalidData, 0644)
if err != nil {
t.Fatal(err)
}
for _, file := range []struct {
filename string
num int
expectedLayers []gopacket.LayerType
err string
}{
{filename: "test_loopback.pcap",
num: 24,
expectedLayers: []gopacket.LayerType{
layers.LayerTypeLoopback,
layers.LayerTypeIPv6,
layers.LayerTypeTCP,
},
},
{filename: "test_ethernet.pcap",
num: 10,
expectedLayers: []gopacket.LayerType{
layers.LayerTypeEthernet,
layers.LayerTypeIPv4,
layers.LayerTypeTCP,
},
},
{filename: "test_dns.pcap",
num: 10,
expectedLayers: []gopacket.LayerType{
layers.LayerTypeEthernet,
layers.LayerTypeIPv4,
layers.LayerTypeUDP,
layers.LayerTypeDNS,
},
},
{filename: invalidPcap.Name(),
num: 0,
err: "unknown file format",
},
} {
t.Logf("\n\n\n\nProcessing file %s\n\n\n\n", file.filename)
packets := []gopacket.Packet{}
if handle, err := OpenOffline(file.filename); err != nil {
if file.err != "" {
if err.Error() != file.err {
t.Errorf("expected message %q; got %q", file.err, err.Error())
}
} else {
t.Fatal(err)
}
} else {
if file.err != "" {
t.Fatalf("Expected error, got none")
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
packets = append(packets, packet)
}
}
if len(packets) != file.num {
t.Fatal("Incorrect number of packets, want", file.num, "got", len(packets))
}
for i, p := range packets {
t.Log(p.Dump())
for _, layertype := range file.expectedLayers {
if p.Layer(layertype) == nil {
t.Fatal("Packet", i, "has no layer type\n%s", layertype, p.Dump())
}
}
}
}
}
func TestBPF(t *testing.T) {
handle, err := OpenOffline("test_ethernet.pcap")
if err != nil {
t.Fatal(err)
}
for _, expected := range []struct {
expr string
Error bool
Result bool
}{
{"foobar", true, false},
{"tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)", false, true},
{"tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-ack", false, true},
{"udp", false, false},
{string([]byte("udp")), false, false}, // test for #664
} {
data, ci, err := handle.ReadPacketData()
if err != nil {
t.Fatal(err)
}
t.Log("Testing filter", expected.expr)
if bpf, err := handle.NewBPF(expected.expr); err != nil {
if !expected.Error {
t.Error(err, "while compiling filter was unexpected")
}
} else if expected.Error {
t.Error("expected error but didn't see one")
} else if matches := bpf.Matches(ci, data); matches != expected.Result {
t.Error("Filter result was", matches, "but should be", expected.Result)
}
}
}
func TestBPFInstruction(t *testing.T) {
handle, err := OpenOffline("test_ethernet.pcap")
if err != nil {
t.Fatal(err)
}
cntr := 0
oversizedBpfInstructionBuffer := [MaxBpfInstructions + 1]BPFInstruction{}
for _, expected := range []struct {
Filter string
BpfInstruction []BPFInstruction
Error bool
Result bool
}{
// {"foobar", true, false},
{"foobar", []BPFInstruction{}, true, false},
// tcpdump -dd 'tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)'
{"tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)",
[]BPFInstruction{
{0x28, 0, 0, 0x0000000c},
{0x15, 0, 9, 0x00000800},
{0x30, 0, 0, 0x00000017},
{0x15, 0, 7, 0x00000006},
{0x28, 0, 0, 0x00000014},
{0x45, 5, 0, 0x00001fff},
{0xb1, 0, 0, 0x0000000e},
{0x50, 0, 0, 0x0000001b},
{0x54, 0, 0, 0x00000012},
{0x15, 0, 1, 0x00000012},
{0x6, 0, 0, 0x0000ffff},
{0x6, 0, 0, 0x00000000},
}, false, true},
// tcpdump -dd 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-ack'
{"tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-ack",
[]BPFInstruction{
{0x28, 0, 0, 0x0000000c},
{0x15, 0, 9, 0x00000800},
{0x30, 0, 0, 0x00000017},
{0x15, 0, 7, 0x00000006},
{0x28, 0, 0, 0x00000014},
{0x45, 5, 0, 0x00001fff},
{0xb1, 0, 0, 0x0000000e},
{0x50, 0, 0, 0x0000001b},
{0x54, 0, 0, 0x00000012},
{0x15, 0, 1, 0x00000010},
{0x6, 0, 0, 0x0000ffff},
{0x6, 0, 0, 0x00000000},
}, false, true},
// tcpdump -dd '(ip or ip6) and udp'
{"(ip or ip6) and udp",
[]BPFInstruction{
{0x28, 0, 0, 0x0000000c},
{0x15, 0, 2, 0x00000800},
{0x30, 0, 0, 0x00000017},
{0x15, 6, 7, 0x00000011},
{0x15, 0, 6, 0x000086dd},
{0x30, 0, 0, 0x00000014},
{0x15, 3, 0, 0x00000011},
{0x15, 0, 3, 0x0000002c},
{0x30, 0, 0, 0x00000036},
{0x15, 0, 1, 0x00000011},
{0x6, 0, 0, 0x0000ffff},
{0x6, 0, 0, 0x00000000},
}, false, false},
{"", oversizedBpfInstructionBuffer[:], true, false},
} {
cntr++
data, ci, err := handle.ReadPacketData()
if err != nil {
t.Fatal(err)
}
t.Log("Testing BpfInstruction filter", cntr)
if bpf, err := handle.NewBPFInstructionFilter(expected.BpfInstruction); err != nil {
if !expected.Error {
t.Error(err, "while compiling filter was unexpected")
}
} else if expected.Error {
t.Error("expected error but didn't see one")
} else if matches := bpf.Matches(ci, data); matches != expected.Result {
t.Error("Filter result was", matches, "but should be", expected.Result)
}
if expected.Filter != "" {
t.Log("Testing dead bpf filter", cntr)
if bpf, err := CompileBPFFilter(layers.LinkTypeEthernet, 65535, expected.Filter); err != nil {
if !expected.Error {
t.Error(err, "while compiling filter was unexpected")
}
} else if expected.Error {
t.Error("expected error but didn't see one")
} else {
if len(bpf) != len(expected.BpfInstruction) {
t.Logf("expected %d instructions, got %d", len(expected.BpfInstruction), len(bpf))
}
for i := 0; i < len(bpf); i++ {
if bpf[i] != expected.BpfInstruction[i] {
t.Logf("expected instruction %d = %x, got %x", i, expected.BpfInstruction[i], bpf[i])
}
}
}
}
}
}
func ExampleBPF() {
handle, err := OpenOffline("test_ethernet.pcap")
if err != nil {
log.Fatal(err)
}
synack, err := handle.NewBPF("tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)")
if err != nil {
log.Fatal(err)
}
syn, err := handle.NewBPF("tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn")
if err != nil {
log.Fatal(err)
}
for {
data, ci, err := handle.ReadPacketData()
switch {
case err == io.EOF:
return
case err != nil:
log.Fatal(err)
case synack.Matches(ci, data):
fmt.Println("SYN/ACK packet")
case syn.Matches(ci, data):
fmt.Println("SYN packet")
default:
fmt.Println("SYN flag not set")
}
}
// Output:
// SYN packet
// SYN/ACK packet
// SYN flag not set
// SYN flag not set
// SYN flag not set
// SYN flag not set
// SYN flag not set
// SYN flag not set
// SYN flag not set
// SYN flag not set
}
|