From d44fab1cb06b7f6340ca4c0b989b434a21b1d9db Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 8 Aug 2014 17:34:29 -0600 Subject: [PATCH] Some pcap tests --- Makefile | 4 +- src/netarch/hd_test.go | 4 +- src/pcap/example.pcap | Bin 0 -> 27850 bytes src/pcap/pcap_test.go | 83 +++++++++++++++++++++++++++++++++++++++++ src/puniq/puniq.go | 41 ++++++++++++++++++++ 5 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 src/pcap/example.pcap create mode 100644 src/pcap/pcap_test.go create mode 100644 src/puniq/puniq.go diff --git a/Makefile b/Makefile index e2549f8..b65c825 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ TARGETS += netarch +TARGETS += puniq GOPATH = $(CURDIR) export GOPATH @@ -10,4 +11,5 @@ test: go test $(TARGETS) clean: - go clean + go clean $(TARGETS) + rm -f $(TARGETS) diff --git a/src/netarch/hd_test.go b/src/netarch/hd_test.go index 6bc3476..561686e 100644 --- a/src/netarch/hd_test.go +++ b/src/netarch/hd_test.go @@ -3,7 +3,7 @@ package netarch import ( ) -func ExampleStringDump() { +func ExampleDumpString() { DumpString("hello world, I can write!") // Output: // 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 } -func ExampleGapstrDump() { +func ExampleDumpGapstr() { g := new(Gapstr) g = g.AppendString("Hell") g = g.AppendGap(3) diff --git a/src/pcap/example.pcap b/src/pcap/example.pcap new file mode 100644 index 0000000000000000000000000000000000000000..931b43b3b878dc559b7423df26363bfe75c6a512 GIT binary patch literal 27850 zcmeHw3y@padEO-{>aq6Pq^EIdM;)Do#Arz^u)w}Zf{zdjU?0>zpj|8}$Fsy#Grk7^ ze09Qq&xUc98NAQ>?m^{uN*%g#?yCn4BRRLQtY%Xh z+{2%^-Z%wFeDzRrc<9@tWa8;gN@grj^0TP}?|l9X5Aw0!`SPLX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(KUCsPOBTZ#V{ z!us~+{+9^r-TA(-UK3c4_rm(%YU;q>{iCt35Y`W$`CgE)PCeTf){g|%CwgH$awT=( zLu%+x2y5=;^-mJkw?E$()?W#%kN3iQ^hSa8uL*1LS0DX#!us-a`-~NQ=Bw*SqWg@* z(2oVy2eyTE=tJtEuNmI&lu&l=bYu~cuoHxvIi6S#u9XJ|IKo@-n+>J_oUa~c96o%X zFylYyWX4Bt1T%gFzue&Ge)|c-_zIvr{p4F(>)QLv9xQ?Kp>3fYdj042f!}Z*b$W1s zus-oyZ|R3oUSK`i1uGyJ9Rx-nHH<$4l%GDf$H-$!eDzT7&Y_&J{m}Y za;IiMxv_}a>DLdjkbv3(O0bMT0!C$UfTOL12Mpw=<#k2=7<+&574x9{O0Acs-f!U7b-j`UMt=*hE5^h4`^A$#e{k=E z-{5!n^lwoNlUz!kn@Pr(5{YCoF`Jl4%p|9i(}`H(ToQlcF+ErY0w2vG9pR!q~rO-+_I`+xM*fmhnFUQ%`SL literal 0 HcmV?d00001 diff --git a/src/pcap/pcap_test.go b/src/pcap/pcap_test.go new file mode 100644 index 0000000..1602bcf --- /dev/null +++ b/src/pcap/pcap_test.go @@ -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) + } + } +} diff --git a/src/puniq/puniq.go b/src/puniq/puniq.go new file mode 100644 index 0000000..8ca8f1e --- /dev/null +++ b/src/puniq/puniq.go @@ -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 + } + } +}