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

     1  // Copyright 2017 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  
     8  package layers
     9  
    10  import (
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/gopacket/gopacket"
    15  )
    16  
    17  // testGTPPacket is the packet:
    18  //0000  00 0c 29 e3 c6 4d 00 0c  29 da d1 de 08 00 45 00   ..)..M.. ).....E.
    19  //0010  00 7c 00 00 40 00 40 11  67 bb c0 a8 28 b2 c0 a8   .|..@.@. g...(...
    20  //0020  28 b3 08 68 08 68 00 68  c1 c4 32 ff 00 58 00 00   (..h.h.h ..2..X..
    21  //0030  00 01 26 7b 00 00 45 00  00 54 06 76 00 00 40 01   ..&{..E. .T.v..@.
    22  //0040  98 2f c0 a8 28 b2 ca 0b  28 9e 00 00 39 e9 00 00   ./..(... (...9...
    23  //0050  28 7d 06 11 20 4b 7f 3a  0d 00 08 09 0a 0b 0c 0d   (}.. K.: ........
    24  //0060  0e 0f 10 11 12 13 14 15  16 17 18 19 1a 1b 1c 1d   ........ ........
    25  //0070  1e 1f 20 21 22 23 24 25  26 27 28 29 2a 2b 2c 2d   .. !"#$% &'()*+,-
    26  //0080  2e 2f 30 31 32 33 34 35  36 37                     ./012345 67
    27  
    28  var testGTPPacket = []byte{
    29  	0x00, 0x0c, 0x29, 0xe3, 0xc6, 0x4d, 0x00, 0x0c,
    30  	0x29, 0xda, 0xd1, 0xde, 0x08, 0x00, 0x45, 0x00,
    31  	0x00, 0x7c, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11,
    32  	0x67, 0xbb, 0xc0, 0xa8, 0x28, 0xb2, 0xc0, 0xa8,
    33  	0x28, 0xb3, 0x08, 0x68, 0x08, 0x68, 0x00, 0x68,
    34  	0xc1, 0xc4, 0x32, 0xff, 0x00, 0x58, 0x00, 0x00,
    35  	0x00, 0x01, 0x26, 0x7b, 0x00, 0x00, 0x45, 0x00,
    36  	0x00, 0x54, 0x06, 0x76, 0x00, 0x00, 0x40, 0x01,
    37  	0x98, 0x2f, 0xc0, 0xa8, 0x28, 0xb2, 0xca, 0x0b,
    38  	0x28, 0x9e, 0x00, 0x00, 0x39, 0xe9, 0x00, 0x00,
    39  	0x28, 0x7d, 0x06, 0x11, 0x20, 0x4b, 0x7f, 0x3a,
    40  	0x0d, 0x00, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
    41  	0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
    42  	0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
    43  	0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
    44  	0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
    45  	0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
    46  	0x36, 0x37,
    47  }
    48  
    49  func TestGTPPacket(t *testing.T) {
    50  	p := gopacket.NewPacket(testGTPPacket, LayerTypeEthernet, gopacket.Default)
    51  	if p.ErrorLayer() != nil {
    52  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    53  	}
    54  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeGTPv1U, LayerTypeIPv4,
    55  		LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
    56  	if got, ok := p.Layer(LayerTypeGTPv1U).(*GTPv1U); ok {
    57  		want := &GTPv1U{
    58  			Version:             1,
    59  			ProtocolType:        1,
    60  			Reserved:            0,
    61  			ExtensionHeaderFlag: false,
    62  			SequenceNumberFlag:  true,
    63  			NPDUFlag:            false,
    64  			MessageType:         255,
    65  			MessageLength:       88,
    66  			TEID:                1,
    67  			SequenceNumber:      9851,
    68  		}
    69  		want.BaseLayer = BaseLayer{testGTPPacket[42:54], testGTPPacket[54:]}
    70  		if !reflect.DeepEqual(got, want) {
    71  			t.Errorf("GTP packet mismatch:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
    72  
    73  		}
    74  		buf := gopacket.NewSerializeBuffer()
    75  		opts := gopacket.SerializeOptions{}
    76  		err := got.SerializeTo(buf, opts)
    77  		if err != nil {
    78  			t.Error(err)
    79  		}
    80  		if !reflect.DeepEqual(got.Contents, buf.Bytes()) {
    81  			t.Errorf("GTP packet serialization failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n", buf.Bytes(), got.Contents)
    82  		}
    83  	} else {
    84  		t.Error("Incorrect gtp packet")
    85  	}
    86  }
    87  
    88  // testGTPPacketWithEH is the packet
    89  //000000 00 0c 29 e3 c6 4d 00 0c 29 da d1 de 08 00 45 00 ..)..M..).....E.
    90  //000010 00 80 00 00 40 00 40 11 67 bb c0 a8 28 b2 c0 a8 ....@.@.g...(...
    91  //000020 28 b3 08 68 08 68 00 6c c1 95 36 ff 00 58 00 10 (..h.h.l..6..X..
    92  //000030 06 57 00 05 00 c0 01 09 04 00 45 00 00 54 06 a5 .W........E..T..
    93  //000040 00 00 40 01 98 00 c0 a8 28 b2 ca 0b 28 9e 00 00 ..@.....(...(...
    94  //000050 e3 b6 00 00 28 ac 35 11 20 4b a6 3d 0d 00 08 09 ....(.5. K.=....
    95  //000060 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 ................
    96  //000070 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 ...... !"#$%&'()
    97  //000080 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37
    98  
    99  var testGTPPacketWithEH = []byte{
   100  	0x00, 0x0c, 0x29, 0xe3, 0xc6, 0x4d, 0x00, 0x0c,
   101  	0x29, 0xda, 0xd1, 0xde, 0x08, 0x00, 0x45, 0x00,
   102  	0x00, 0x80, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11,
   103  	0x67, 0xbb, 0xc0, 0xa8, 0x28, 0xb2, 0xc0, 0xa8,
   104  	0x28, 0xb3, 0x08, 0x68, 0x08, 0x68, 0x00, 0x6c,
   105  	0xc1, 0x95, 0x36, 0xff, 0x00, 0x58, 0x00, 0x10,
   106  	0x06, 0x57, 0x00, 0x05, 0x00, 0xc0, 0x01, 0x09,
   107  	0x04, 0x00, 0x45, 0x00, 0x00, 0x54, 0x06, 0xa5,
   108  	0x00, 0x00, 0x40, 0x01, 0x98, 0x00, 0xc0, 0xa8,
   109  	0x28, 0xb2, 0xca, 0x0b, 0x28, 0x9e, 0x00, 0x00,
   110  	0xe3, 0xb6, 0x00, 0x00, 0x28, 0xac, 0x35, 0x11,
   111  	0x20, 0x4b, 0xa6, 0x3d, 0x0d, 0x00, 0x08, 0x09,
   112  	0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
   113  	0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
   114  	0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
   115  	0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
   116  	0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
   117  	0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
   118  }
   119  
   120  func TestGTPPacketWithEH(t *testing.T) {
   121  	p := gopacket.NewPacket(testGTPPacketWithEH, LayerTypeEthernet, gopacket.Default)
   122  	if p.ErrorLayer() != nil {
   123  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
   124  	}
   125  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeGTPv1U, LayerTypeIPv4,
   126  		LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
   127  	if got, ok := p.Layer(LayerTypeGTPv1U).(*GTPv1U); ok {
   128  		want := &GTPv1U{
   129  			Version:             1,
   130  			ProtocolType:        1,
   131  			Reserved:            0,
   132  			ExtensionHeaderFlag: true,
   133  			SequenceNumberFlag:  true,
   134  			NPDUFlag:            false,
   135  			MessageType:         255,
   136  			MessageLength:       88,
   137  			TEID:                1050199,
   138  			SequenceNumber:      5,
   139  			GTPExtensionHeaders: []GTPExtensionHeader{GTPExtensionHeader{Type: uint8(192), Content: []byte{0x9, 0x4}}},
   140  		}
   141  		want.BaseLayer = BaseLayer{testGTPPacketWithEH[42:58], testGTPPacketWithEH[58:]}
   142  		if !reflect.DeepEqual(got, want) {
   143  			t.Errorf("GTP packet mismatch:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
   144  
   145  		}
   146  		buf := gopacket.NewSerializeBuffer()
   147  		opts := gopacket.SerializeOptions{}
   148  		err := got.SerializeTo(buf, opts)
   149  		if err != nil {
   150  			t.Error(err)
   151  		}
   152  		if !reflect.DeepEqual(got.Contents, buf.Bytes()) {
   153  			t.Errorf("GTP packet serialization failed:\ngot  :\n%#v\n\nbuf :\n%#v\n\n", got.Contents, buf.Bytes())
   154  		}
   155  	} else {
   156  		t.Errorf("Invalid GTP packet")
   157  	}
   158  
   159  }