github.com/gopacket/gopacket@v1.1.0/packet_test.go (about)

     1  // Copyright 2012 Google, Inc. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  
     7  package gopacket
     8  
     9  import (
    10  	"io"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  type embedded struct {
    16  	A, B int
    17  }
    18  
    19  type embedding struct {
    20  	embedded
    21  	C, D int
    22  }
    23  
    24  func TestDumpEmbedded(t *testing.T) {
    25  	e := embedding{embedded: embedded{A: 1, B: 2}, C: 3, D: 4}
    26  	if got, want := layerString(reflect.ValueOf(e), false, false), "{A=1 B=2 C=3 D=4}"; got != want {
    27  		t.Errorf("embedded dump mismatch:\n   got: %v\n  want: %v", got, want)
    28  	}
    29  }
    30  
    31  type singlePacketSource [1][]byte
    32  
    33  func (s *singlePacketSource) ReadPacketData() ([]byte, CaptureInfo, error) {
    34  	if (*s)[0] == nil {
    35  		return nil, CaptureInfo{}, io.EOF
    36  	}
    37  	out := (*s)[0]
    38  	(*s)[0] = nil
    39  	return out, CaptureInfo{}, nil
    40  }
    41  
    42  func TestConcatPacketSources(t *testing.T) {
    43  	sourceA := &singlePacketSource{[]byte{1}}
    44  	sourceB := &singlePacketSource{[]byte{2}}
    45  	sourceC := &singlePacketSource{[]byte{3}}
    46  	concat := ConcatFinitePacketDataSources(sourceA, sourceB, sourceC)
    47  	a, _, err := concat.ReadPacketData()
    48  	if err != nil || len(a) != 1 || a[0] != 1 {
    49  		t.Errorf("expected [1], got %v/%v", a, err)
    50  	}
    51  	b, _, err := concat.ReadPacketData()
    52  	if err != nil || len(b) != 1 || b[0] != 2 {
    53  		t.Errorf("expected [2], got %v/%v", b, err)
    54  	}
    55  	c, _, err := concat.ReadPacketData()
    56  	if err != nil || len(c) != 1 || c[0] != 3 {
    57  		t.Errorf("expected [3], got %v/%v", c, err)
    58  	}
    59  	if _, _, err := concat.ReadPacketData(); err != io.EOF {
    60  		t.Errorf("expected io.EOF, got %v", err)
    61  	}
    62  }