From 3f1edad4e6ba0a7876750aea55507fae14d8badf Mon Sep 17 00:00:00 2001 From: Milan Lenco Date: Wed, 11 Oct 2017 16:40:58 +0200 Subject: ODPM 266: Go-libmemif + 2 examples. Change-Id: Icdb9b9eb2314eff6c96afe7996fcf2728291de4a Signed-off-by: Milan Lenco --- .../google/gopacket/pcapgo/write_test.go | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 vendor/github.com/google/gopacket/pcapgo/write_test.go (limited to 'vendor/github.com/google/gopacket/pcapgo/write_test.go') diff --git a/vendor/github.com/google/gopacket/pcapgo/write_test.go b/vendor/github.com/google/gopacket/pcapgo/write_test.go new file mode 100644 index 0000000..5b87d6a --- /dev/null +++ b/vendor/github.com/google/gopacket/pcapgo/write_test.go @@ -0,0 +1,71 @@ +// 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 pcapgo + +import ( + "bytes" + "github.com/google/gopacket" + "testing" + "time" +) + +func TestWriteHeader(t *testing.T) { + var buf bytes.Buffer + w := NewWriter(&buf) + w.WriteFileHeader(0x1234, 0x56) + want := []byte{ + 0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x34, 0x12, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00, + } + if got := buf.Bytes(); !bytes.Equal(got, want) { + t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, got) + } +} + +func TestWritePacket(t *testing.T) { + ci := gopacket.CaptureInfo{ + Timestamp: time.Unix(0x01020304, 0xAA*1000), + Length: 0xABCD, + CaptureLength: 10, + } + data := []byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0} + var buf bytes.Buffer + w := NewWriter(&buf) + w.WritePacket(ci, data) + want := []byte{ + 0x04, 0x03, 0x02, 0x01, 0xAA, 0x00, 0x00, 0x00, + 0x0A, 0x00, 0x00, 0x00, 0xCD, 0xAB, 0x00, 0x00, + 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, + } + if got := buf.Bytes(); !bytes.Equal(got, want) { + t.Errorf("buf mismatch:\nwant: %+v\ngot: %+v", want, got) + } +} + +func TestCaptureInfoErrors(t *testing.T) { + data := []byte{1, 2, 3, 4} + ts := time.Unix(0, 0) + for _, test := range []gopacket.CaptureInfo{ + gopacket.CaptureInfo{ + Timestamp: ts, + Length: 5, + CaptureLength: 5, + }, + gopacket.CaptureInfo{ + Timestamp: ts, + Length: 3, + CaptureLength: 4, + }, + } { + var buf bytes.Buffer + w := NewWriter(&buf) + if err := w.WritePacket(test, data); err == nil { + t.Errorf("CaptureInfo %+v should have error", test) + } + } +} -- cgit 1.2.3-korg