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