Some pcap tests

This commit is contained in:
Neale Pickett 2014-08-08 17:34:29 -06:00
parent 4ce4a8f188
commit d44fab1cb0
5 changed files with 129 additions and 3 deletions

View File

@ -1,4 +1,5 @@
TARGETS += netarch TARGETS += netarch
TARGETS += puniq
GOPATH = $(CURDIR) GOPATH = $(CURDIR)
export GOPATH export GOPATH
@ -10,4 +11,5 @@ test:
go test $(TARGETS) go test $(TARGETS)
clean: clean:
go clean go clean $(TARGETS)
rm -f $(TARGETS)

View File

@ -3,7 +3,7 @@ package netarch
import ( import (
) )
func ExampleStringDump() { func ExampleDumpString() {
DumpString("hello world, I can write!") DumpString("hello world, I can write!")
// Output: // Output:
// 00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 2c 20 49 20 63 ┆hello world, I c┆ // 00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 2c 20 49 20 63 ┆hello world, I c┆
@ -11,7 +11,7 @@ func ExampleStringDump() {
// 00000019 // 00000019
} }
func ExampleGapstrDump() { func ExampleDumpGapstr() {
g := new(Gapstr) g := new(Gapstr)
g = g.AppendString("Hell") g = g.AppendString("Hell")
g = g.AppendGap(3) g = g.AppendGap(3)

BIN
src/pcap/example.pcap Normal file

Binary file not shown.

83
src/pcap/pcap_test.go Normal file
View File

@ -0,0 +1,83 @@
package pcap
import (
"bytes"
"io"
"math/rand"
"os"
"testing"
"time"
)
func randomString(prng *rand.Rand, length int) string {
var b bytes.Buffer
for i := 0; i < length; i += 1 {
b.WriteByte(byte(prng.Int() % 256))
}
return b.String()
}
// Just read in a file, shouldn't be any errors.
func TestReader(t *testing.T) {
f, err := os.Open("example.pcap")
if err != nil {
t.Fatal(err)
}
defer f.Close()
pin, err := NewReader(f)
if err != nil {
t.Fatal(err)
}
for {
frame, err := pin.Read()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if len(frame.Payload) > 2000 {
t.Error("Unusually large payload")
}
}
}
func TestReadWrite(t *testing.T) {
prng := rand.New(rand.NewSource(time.Now().Unix()))
frames := make([]Frame, 20)
outf := new(bytes.Buffer)
pout, err := NewWriter(outf)
if err != nil {
t.Fatal(err)
}
for _, f := range frames {
f.Header.Sec = prng.Uint32()
f.Header.Usec = prng.Uint32()
f.Header.Caplen = prng.Uint32()
f.Header.Framelen = prng.Uint32()
f.Payload = randomString(prng, prng.Intn(400))
pout.WriteFrame(f)
}
pin, err := NewReader(outf)
if err != nil {
t.Fatal(err)
}
for _, f := range frames {
ff, err := pin.Read()
if err != nil {
t.Fatal(err)
}
if *ff != f {
t.Fatal(ff, f)
}
}
}

41
src/puniq/puniq.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"fmt"
"os"
"pcap"
)
func main() {
r, err := pcap.NewReader(os.Stdin)
if err != nil {
panic(err)
}
w, err := pcap.NewWriter(os.Stdout)
if err != nil {
panic(err)
}
fmt.Println(r.Header)
var lastframe *pcap.Frame = nil
for {
frame, err := r.Read()
if err != nil {
panic(err)
}
if frame == nil {
break
}
if (lastframe != frame) {
err = w.WriteFrame(*frame)
if err != nil {
panic(err)
}
lastframe = frame
}
}
}