github.com/gopacket/gopacket@v1.1.0/layers/tcp_test.go (about) 1 // Copyright 2016, 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 "reflect" 11 "testing" 12 13 "github.com/gopacket/gopacket" 14 ) 15 16 func TestTCPOptionKindString(t *testing.T) { 17 testData := []struct { 18 o *TCPOption 19 s string 20 }{ 21 {&TCPOption{ 22 OptionType: TCPOptionKindNop, 23 OptionLength: 1, 24 }, 25 "TCPOption(NOP:)"}, 26 {&TCPOption{ 27 OptionType: TCPOptionKindMSS, 28 OptionLength: 4, 29 OptionData: []byte{0x12, 0x34}, 30 }, 31 "TCPOption(MSS:4660 0x1234)"}, 32 {&TCPOption{ 33 OptionType: TCPOptionKindTimestamps, 34 OptionLength: 10, 35 OptionData: []byte{0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01}, 36 }, 37 "TCPOption(Timestamps:2/1 0x0000000200000001)"}} 38 39 for _, tc := range testData { 40 if s := tc.o.String(); s != tc.s { 41 t.Errorf("expected %#v string to be %s, got %s", tc.o, tc.s, s) 42 } 43 } 44 } 45 46 func TestTCPSerializePadding(t *testing.T) { 47 tcp := &TCP{} 48 tcp.Options = append(tcp.Options, TCPOption{ 49 OptionType: TCPOptionKindNop, 50 OptionLength: 1, 51 }) 52 buf := gopacket.NewSerializeBuffer() 53 opts := gopacket.SerializeOptions{FixLengths: true} 54 err := gopacket.SerializeLayers(buf, opts, tcp) 55 if err != nil { 56 t.Fatal(err) 57 } 58 if len(buf.Bytes())%4 != 0 { 59 t.Errorf("TCP data of len %d not padding to 32 bit boundary", len(buf.Bytes())) 60 } 61 } 62 63 // testPacketTCPOptionDecode is the packet: 64 // 65 // 16:17:26.239051 IP 192.168.0.1.12345 > 192.168.0.2.54321: Flags [S], seq 3735928559:3735928563, win 0, options [mss 8192,eol], length 4 66 // 0x0000: 0000 0000 0001 0000 0000 0001 0800 4500 ..............E. 67 // 0x0010: 0034 0000 0000 8006 b970 c0a8 0001 c0a8 .4.......p...... 68 // 0x0020: 0002 3039 d431 dead beef 0000 0000 7002 ..09.1........p. 69 // 0x0030: 0000 829c 0000 0204 2000 0000 0000 5465 ..............Te 70 // 0x0040: 7374 st 71 var testPacketTCPOptionDecode = []byte{ 72 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x45, 0x00, 73 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x80, 0x06, 0xb9, 0x70, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8, 74 0x00, 0x02, 0x30, 0x39, 0xd4, 0x31, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x70, 0x02, 75 0x00, 0x00, 0x82, 0x9c, 0x00, 0x00, 0x02, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x65, 76 0x73, 0x74, 77 } 78 79 func TestPacketTCPOptionDecode(t *testing.T) { 80 p := gopacket.NewPacket(testPacketTCPOptionDecode, LinkTypeEthernet, gopacket.Default) 81 if p.ErrorLayer() != nil { 82 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 83 } 84 tcp := p.Layer(LayerTypeTCP).(*TCP) 85 if tcp == nil { 86 t.Error("Expected TCP layer, but got none") 87 } 88 89 expected := []TCPOption{ 90 { 91 OptionType: TCPOptionKindMSS, 92 OptionLength: 4, 93 OptionData: []byte{32, 00}, 94 }, 95 { 96 OptionType: TCPOptionKindEndList, 97 OptionLength: 1, 98 }, 99 } 100 101 if !reflect.DeepEqual(expected, tcp.Options) { 102 t.Errorf("expected options to be %#v, but got %#v", expected, tcp.Options) 103 } 104 }