github.com/gopacket/gopacket@v1.1.0/layers/geneve_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 var testPacketGeneve1 = []byte{ 17 0x00, 0x04, 0x00, 0x01, 0x00, 0x06, 0xfa, 0x16, 0x3e, 0x23, 0xd3, 0x42, 18 0x00, 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x86, 0x87, 0x39, 0x40, 0x00, 19 0x40, 0x11, 0x31, 0x35, 0xc0, 0xa8, 0x00, 0x53, 0xc0, 0xa8, 0x00, 0x55, 20 0x31, 0x57, 0x17, 0xc1, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x65, 0x58, 21 0x00, 0x00, 0x00, 0x00, 0xba, 0x09, 0x60, 0x5f, 0xa0, 0x91, 0xa2, 0xfe, 22 0x54, 0x48, 0x88, 0x51, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0x01, 0xf6, 23 0x40, 0x00, 0x40, 0x01, 0xb7, 0x5f, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8, 24 0x00, 0x02, 0x08, 0x00, 0x79, 0xdf, 0x0c, 0xfa, 0x63, 0xc4, 0x03, 0x0b, 25 0x50, 0x58, 0x00, 0x00, 0x00, 0x00, 0xee, 0x2b, 0x0d, 0x00, 0x00, 0x00, 26 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 27 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 28 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 29 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 30 } 31 32 var testPacketGeneve2 = []byte{ 33 0x12, 0xbe, 0x4e, 0xb6, 0xa7, 0xc7, 0x02, 0x88, 0x0a, 0x81, 0xbd, 0x6d, 34 0x08, 0x00, 0x45, 0x00, 0x00, 0x86, 0x20, 0xf2, 0x00, 0x00, 0x40, 0x11, 35 0x01, 0x52, 0xac, 0x10, 0x00, 0x01, 0xac, 0x10, 0x00, 0x02, 0x40, 0xa6, 36 0x17, 0xc1, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x65, 0x58, 0x00, 0x00, 37 0x0a, 0x00, 0xd2, 0x8c, 0xdb, 0x12, 0x53, 0xd5, 0x8e, 0xab, 0xa2, 0xa5, 38 0x02, 0xf7, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0x38, 0x1a, 0x40, 0x00, 39 0x40, 0x01, 0x81, 0x3b, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8, 0x00, 0x02, 40 0x08, 0x00, 0xdd, 0x9d, 0x7e, 0xde, 0x02, 0xc3, 0xcb, 0x07, 0x51, 0x58, 41 0x00, 0x00, 0x00, 0x00, 0xba, 0x8d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 42 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 43 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 44 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 45 0x34, 0x35, 0x36, 0x37, 46 } 47 48 var testPacketGeneve3 = []byte{ 49 0x00, 0x1b, 0x21, 0x3c, 0xac, 0x30, 0x00, 0x1b, 0x21, 0x3c, 0xab, 0x64, 0x08, 0x00, 0x45, 0x00, 50 0x00, 0x8e, 0xdf, 0xad, 0x40, 0x00, 0x40, 0x11, 0x32, 0xaf, 0x14, 0x00, 0x00, 0x01, 0x14, 0x00, 51 0x00, 0x02, 0x31, 0x4a, 0x17, 0xc1, 0x00, 0x7a, 0x00, 0x00, 0x02, 0x40, 0x65, 0x58, 0x00, 0x00, 52 0x0a, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0xfe, 0x71, 0xd8, 0x83, 0x72, 0x4f, 53 0xb6, 0x9e, 0xd2, 0x49, 0x51, 0x48, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0xbd, 0xa2, 0x40, 0x00, 54 0x40, 0x01, 0x41, 0x04, 0x1e, 0x00, 0x00, 0x01, 0x1e, 0x00, 0x00, 0x02, 0x08, 0x00, 0x2c, 0x54, 55 0x29, 0x52, 0x00, 0x17, 0xf1, 0xa2, 0xce, 0x54, 0x00, 0x00, 0x00, 0x00, 0x17, 0x78, 0x0c, 0x00, 56 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 57 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 58 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 59 } 60 61 func TestDecodeGeneve1(t *testing.T) { 62 p := gopacket.NewPacket(testPacketGeneve1, LinkTypeLinuxSLL, gopacket.Default) 63 if p.ErrorLayer() != nil { 64 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 65 } 66 checkLayers(p, []gopacket.LayerType{ 67 LayerTypeLinuxSLL, LayerTypeIPv4, LayerTypeUDP, LayerTypeGeneve, 68 LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload, 69 }, t) 70 if got, ok := p.Layer(LayerTypeGeneve).(*Geneve); ok { 71 want := &Geneve{ 72 BaseLayer: BaseLayer{ 73 Contents: testPacketGeneve1[44:52], 74 Payload: testPacketGeneve1[52:150], 75 }, 76 Version: 0x0, 77 OptionsLength: 0x0, 78 OAMPacket: false, 79 CriticalOption: false, 80 Protocol: EthernetTypeTransparentEthernetBridging, 81 VNI: 0x0, 82 } 83 if !reflect.DeepEqual(want, got) { 84 t.Errorf("Geneve layer mismatch, \nwant %#v\ngot %#v\n", want, got) 85 } 86 } 87 } 88 89 func TestDecodeGeneve2(t *testing.T) { 90 p := gopacket.NewPacket(testPacketGeneve2, LinkTypeEthernet, gopacket.Default) 91 if p.ErrorLayer() != nil { 92 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 93 } 94 checkLayers(p, []gopacket.LayerType{ 95 LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeGeneve, 96 LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload, 97 }, t) 98 if got, ok := p.Layer(LayerTypeGeneve).(*Geneve); ok { 99 want := &Geneve{ 100 BaseLayer: BaseLayer{ 101 Contents: testPacketGeneve2[42:50], 102 Payload: testPacketGeneve2[50:148], 103 }, 104 Version: 0x0, 105 OptionsLength: 0x0, 106 OAMPacket: false, 107 CriticalOption: false, 108 Protocol: EthernetTypeTransparentEthernetBridging, 109 VNI: 0xa, 110 } 111 if !reflect.DeepEqual(want, got) { 112 t.Errorf("Geneve layer mismatch, \nwant %#v\ngot %#v\n", want, got) 113 } 114 } 115 } 116 117 func TestDecodeGeneve3(t *testing.T) { 118 p := gopacket.NewPacket(testPacketGeneve3, LinkTypeEthernet, gopacket.Default) 119 if p.ErrorLayer() != nil { 120 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 121 } 122 checkLayers(p, []gopacket.LayerType{ 123 LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeGeneve, 124 LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload, 125 }, t) 126 if got, ok := p.Layer(LayerTypeGeneve).(*Geneve); ok { 127 want := &Geneve{ 128 BaseLayer: BaseLayer{ 129 Contents: testPacketGeneve3[42:58], 130 Payload: testPacketGeneve3[58:156], 131 }, 132 Version: 0x0, 133 OptionsLength: 0x8, 134 OAMPacket: false, 135 CriticalOption: true, 136 Protocol: EthernetTypeTransparentEthernetBridging, 137 VNI: 0xa, 138 Options: []*GeneveOption{ 139 { 140 Class: 0x0, 141 Type: 0x80, 142 Length: 8, 143 Data: []byte{0, 0, 0, 0xc}, 144 }, 145 }, 146 } 147 if !reflect.DeepEqual(want, got) { 148 t.Errorf("Geneve layer mismatch, \nwant %#v\ngot %#v\n", want, got) 149 } 150 } 151 } 152 153 func BenchmarkDecodeGeneve1(b *testing.B) { 154 for i := 0; i < b.N; i++ { 155 gopacket.NewPacket(testPacketGeneve1, LinkTypeEthernet, gopacket.NoCopy) 156 } 157 } 158 159 func TestGeneveSerializeToNoCrashWithWrongLengths(t *testing.T) { 160 gn := &Geneve{ 161 Version: 0x0, 162 OptionsLength: 0x0, 163 OAMPacket: false, 164 CriticalOption: true, 165 Protocol: EthernetTypeTransparentEthernetBridging, 166 VNI: 0xa, 167 Options: []*GeneveOption{ 168 { 169 Class: 0x0, 170 Type: 0x80, 171 Length: 0x0, 172 Data: []byte{0, 0, 0, 0, 0, 0, 0, 0xc}, 173 }, 174 { 175 Class: 0x0, 176 Type: 0x80, 177 Length: 0x0, 178 Data: []byte{0, 0, 0, 0xc}, 179 }, 180 }, 181 } 182 183 b := gopacket.NewSerializeBuffer() 184 gn.SerializeTo(b, gopacket.SerializeOptions{}) 185 } 186 187 func TestIsomorphicPacketGeneve(t *testing.T) { 188 gn := &Geneve{ 189 Version: 0x0, 190 OptionsLength: 0x14, 191 OAMPacket: false, 192 CriticalOption: true, 193 Protocol: EthernetTypeTransparentEthernetBridging, 194 VNI: 0xa, 195 Options: []*GeneveOption{ 196 { 197 Class: 0x0, 198 Type: 0x80, 199 Length: 12, 200 Data: []byte{0, 0, 0, 0, 0, 0, 0, 0xc}, 201 }, 202 { 203 Class: 0x0, 204 Type: 0x80, 205 Length: 8, 206 Data: []byte{0, 0, 0, 0xc}, 207 }, 208 }, 209 } 210 211 b := gopacket.NewSerializeBuffer() 212 gn.SerializeTo(b, gopacket.SerializeOptions{}) 213 214 p := gopacket.NewPacket(b.Bytes(), gopacket.DecodeFunc(decodeGeneve), gopacket.Default) 215 gnTranslated := p.Layer(LayerTypeGeneve).(*Geneve) 216 gnTranslated.BaseLayer = BaseLayer{} 217 218 if !reflect.DeepEqual(gn, gnTranslated) { 219 t.Errorf("Geneve isomorph mismatch, \nwant %#v\ngot %#v\n", gn, gnTranslated) 220 } 221 } 222 223 func TestIsomorphicPacketGeneveFixLengths(t *testing.T) { 224 gn := &Geneve{ 225 Version: 0x0, 226 OptionsLength: 0x14, 227 OAMPacket: false, 228 CriticalOption: true, 229 Protocol: EthernetTypeTransparentEthernetBridging, 230 VNI: 0xa, 231 Options: []*GeneveOption{ 232 { 233 Class: 0x0, 234 Type: 0x80, 235 Length: 12, 236 Data: []byte{0, 0, 0, 0, 0, 0, 0, 0xc}, 237 }, 238 { 239 Class: 0x0, 240 Type: 0x80, 241 Length: 8, 242 Data: []byte{0, 0, 0, 0xc}, 243 }, 244 }, 245 } 246 247 gnWrongLengths := &Geneve{ 248 Version: 0x0, 249 OptionsLength: 0x0, 250 OAMPacket: false, 251 CriticalOption: true, 252 Protocol: EthernetTypeTransparentEthernetBridging, 253 VNI: 0xa, 254 Options: []*GeneveOption{ 255 { 256 Class: 0x0, 257 Type: 0x80, 258 Length: 0x0, 259 Data: []byte{0, 0, 0, 0, 0, 0, 0, 0xc}, 260 }, 261 { 262 Class: 0x0, 263 Type: 0x80, 264 Length: 0x0, 265 Data: []byte{0, 0, 0, 0xc}, 266 }, 267 }, 268 } 269 270 b := gopacket.NewSerializeBuffer() 271 gnWrongLengths.SerializeTo(b, gopacket.SerializeOptions{FixLengths: true}) 272 273 p := gopacket.NewPacket(b.Bytes(), gopacket.DecodeFunc(decodeGeneve), gopacket.Default) 274 gnTranslated := p.Layer(LayerTypeGeneve).(*Geneve) 275 gnTranslated.BaseLayer = BaseLayer{} 276 277 if !reflect.DeepEqual(gn, gnTranslated) { 278 t.Errorf("Geneve isomorph mismatch, \nwant %#v\ngot %#v\n", gn, gnTranslated) 279 } 280 }