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

     1  // Copyright 2014, 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 layers
     8  
     9  import (
    10  	_ "fmt"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/gopacket/gopacket"
    15  )
    16  
    17  // Generator python layers/test_creator.py --link_type USB --name USB dongle.pcap
    18  // http://wiki.wireshark.org/SampleCaptures#Sample_Captures
    19  
    20  // testPacketUSB0 is the packet:
    21  //
    22  //	02:41:04.689546 INTERRUPT COMPLETE to 2:1:1
    23  //		0x0000:  0038 4a3b 0088 ffff 4301 8101 0200 2d00  .8J;....C.....-.
    24  //		0x0010:  c0d3 5b50 0000 0000 8a85 0a00 0000 0000  ..[P............
    25  //		0x0020:  0100 0000 0100 0000 0000 0000 0000 0000  ................
    26  //		0x0030:  8000 0000 0000 0000 0002 0000 0000 0000  ................
    27  //		0x0040:  04                                       .
    28  var testPacketUSB0 = []byte{
    29  	0x00, 0x38, 0x4a, 0x3b, 0x00, 0x88, 0xff, 0xff, 0x43, 0x01, 0x81, 0x01, 0x02, 0x00, 0x2d, 0x00,
    30  	0xc0, 0xd3, 0x5b, 0x50, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x85, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
    31  	0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    32  	0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    33  	0x04,
    34  }
    35  
    36  func TestPacketUSB0(t *testing.T) {
    37  	p := gopacket.NewPacket(testPacketUSB0, LinkTypeLinuxUSB, gopacket.Default)
    38  	if p.ErrorLayer() != nil {
    39  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    40  	}
    41  	checkLayers(p, []gopacket.LayerType{LayerTypeUSB, LayerTypeUSBInterrupt}, t)
    42  
    43  	if got, ok := p.Layer(LayerTypeUSB).(*USB); ok {
    44  		want := &USB{
    45  			BaseLayer: BaseLayer{
    46  				Contents: []uint8{0x0, 0x38, 0x4a, 0x3b, 0x0, 0x88, 0xff, 0xff, 0x43, 0x1, 0x81, 0x1, 0x2, 0x0, 0x2d, 0x0, 0xc0, 0xd3, 0x5b, 0x50, 0x0, 0x0, 0x0, 0x0, 0x8a, 0x85, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0},
    47  				Payload:  []uint8{0x4},
    48  			},
    49  			ID:             0xffff88003b4a3800,
    50  			EventType:      USBEventTypeComplete,
    51  			TransferType:   USBTransportTypeInterrupt,
    52  			Direction:      0x1,
    53  			EndpointNumber: 0x1,
    54  			DeviceAddress:  0x1,
    55  			BusID:          0x2,
    56  			TimestampSec:   1348195264,
    57  			TimestampUsec:  689546,
    58  			Setup:          false,
    59  			Data:           true,
    60  			Status:         0,
    61  			UrbLength:      0x1,
    62  			UrbDataLength:  0x1,
    63  		}
    64  
    65  		if !reflect.DeepEqual(got, want) {
    66  			t.Errorf("USB packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
    67  		}
    68  	}
    69  
    70  }
    71  func BenchmarkDecodePacketUSB0(b *testing.B) {
    72  	for i := 0; i < b.N; i++ {
    73  		gopacket.NewPacket(testPacketUSB0, LinkTypeLinuxUSB, gopacket.NoCopy)
    74  	}
    75  }