summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/google/gopacket/packet_test.go
blob: bd0544888b8e5a3b7a3b898ddb44cf5e93ef9c08 (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
// 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 gopacket

import (
	"io"
	"reflect"
	"testing"
)

type embedded struct {
	A, B int
}

type embedding struct {
	embedded
	C, D int
}

func TestDumpEmbedded(t *testing.T) {
	e := embedding{embedded: embedded{A: 1, B: 2}, C: 3, D: 4}
	if got, want := layerString(reflect.ValueOf(e), false, false), "{A=1 B=2 C=3 D=4}"; got != want {
		t.Errorf("embedded dump mismatch:\n   got: %v\n  want: %v", got, want)
	}
}

type singlePacketSource [1][]byte

func (s *singlePacketSource) ReadPacketData() ([]byte, CaptureInfo, error) {
	if (*s)[0] == nil {
		return nil, CaptureInfo{}, io.EOF
	}
	out := (*s)[0]
	(*s)[0] = nil
	return out, CaptureInfo{}, nil
}

func TestConcatPacketSources(t *testing.T) {
	sourceA := &singlePacketSource{[]byte{1}}
	sourceB := &singlePacketSource{[]byte{2}}
	sourceC := &singlePacketSource{[]byte{3}}
	concat := ConcatFinitePacketDataSources(sourceA, sourceB, sourceC)
	a, _, err := concat.ReadPacketData()
	if err != nil || len(a) != 1 || a[0] != 1 {
		t.Errorf("expected [1], got %v/%v", a, err)
	}
	b, _, err := concat.ReadPacketData()
	if err != nil || len(b) != 1 || b[0] != 2 {
		t.Errorf("expected [2], got %v/%v", b, err)
	}
	c, _, err := concat.ReadPacketData()
	if err != nil || len(c) != 1 || c[0] != 3 {
		t.Errorf("expected [3], got %v/%v", c, err)
	}
	if _, _, err := concat.ReadPacketData(); err != io.EOF {
		t.Errorf("expected io.EOF, got %v", err)
	}
}