aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/google/gopacket/pcapgo/write_test.go
blob: 5b87d6a5aa7cdbbaf6c766080dd1c5f40fcb034b (plain)
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
// 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)
		}
	}
}