|
| 1 | +// Copyright 2025 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 | + "encoding/hex" |
| 11 | + "net" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/google/gopacket" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGueDecoding(t *testing.T) { |
| 18 | + // This is a packet generated by the Linux kernel GUE implementation, captured |
| 19 | + // by pcap. It includes: |
| 20 | + // - Ethernet |
| 21 | + // - IPv4 |
| 22 | + // - UDP |
| 23 | + // - AGueVar0 (port 666) |
| 24 | + // - IPv4 |
| 25 | + // - ICMP (ping) |
| 26 | + // TODO: build a test packet using port 666 (0x029a) |
| 27 | + ph := `02427b2522f502420ae0d90608004500007451ea4000ff119d050ae0d9060afa9da88c0f029a00608cfa000400004500005459f240004001e2cd0ae0d9060afd0f0608000a7e005f000cea811f59000000005daa080000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627` |
| 28 | + pr, err := hex.DecodeString(ph) |
| 29 | + if err != nil { |
| 30 | + t.Errorf("Error decoding hex packet: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + p := gopacket.NewPacket(pr, LayerTypeEthernet, gopacket.Default) |
| 34 | + // require.Nil(t, p.ErrorLayer()) |
| 35 | + if p.ErrorLayer() != nil { |
| 36 | + t.Errorf("AGUEVar0 layer is nil") |
| 37 | + } |
| 38 | + t.Logf("%v", p) |
| 39 | + |
| 40 | + gue := p.Layer(LayerTypeAGUEVar0).(AGUEVar0) |
| 41 | + // require.NotNil(t, gue) |
| 42 | + if uint8(0) != gue.Version { |
| 43 | + t.Errorf("gue.Version is not 0") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +var testIPv4OverAGUEVar0 = []gopacket.SerializableLayer{ |
| 48 | + &Ethernet{ |
| 49 | + SrcMAC: net.HardwareAddr{142, 122, 18, 195, 169, 113}, |
| 50 | + DstMAC: net.HardwareAddr{58, 86, 107, 105, 89, 94}, |
| 51 | + EthernetType: EthernetTypeIPv4, |
| 52 | + }, |
| 53 | + &IPv4{ |
| 54 | + Version: 4, |
| 55 | + SrcIP: net.IP{192, 168, 1, 1}, |
| 56 | + DstIP: net.IP{192, 168, 1, 2}, |
| 57 | + Protocol: IPProtocolUDP, |
| 58 | + Flags: IPv4DontFragment, |
| 59 | + TTL: 64, |
| 60 | + Id: 33852, |
| 61 | + IHL: 5, |
| 62 | + }, |
| 63 | + &UDP{ |
| 64 | + SrcPort: 8, |
| 65 | + DstPort: 666, |
| 66 | + }, |
| 67 | + &AGUEVar0{ |
| 68 | + Protocol: IPProtocolIPv4, |
| 69 | + }, |
| 70 | + &IPv4{ |
| 71 | + Version: 4, |
| 72 | + SrcIP: net.IP{172, 16, 1, 1}, |
| 73 | + DstIP: net.IP{172, 16, 2, 1}, |
| 74 | + Protocol: IPProtocolICMPv4, |
| 75 | + Flags: IPv4DontFragment, |
| 76 | + TTL: 64, |
| 77 | + IHL: 5, |
| 78 | + Id: 1160, |
| 79 | + }, |
| 80 | + &ICMPv4{ |
| 81 | + TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0), |
| 82 | + Id: 4724, |
| 83 | + Seq: 1, |
| 84 | + }, |
| 85 | + gopacket.Payload{ |
| 86 | + 0xc8, 0x92, 0xa3, 0x54, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 87 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 88 | + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 89 | + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +func TestIPv4OverAGUEVar0Encode(t *testing.T) { |
| 94 | + b := gopacket.NewSerializeBuffer() |
| 95 | + opts := gopacket.SerializeOptions{ |
| 96 | + ComputeChecksums: false, // if desired, see gre_test:setNetworkLayer() |
| 97 | + FixLengths: true, |
| 98 | + } |
| 99 | + if err := gopacket.SerializeLayers(b, opts, testIPv4OverAGUEVar0...); err != nil { |
| 100 | + t.Errorf("Unable to serialize: %v", err) |
| 101 | + } |
| 102 | + p := gopacket.NewPacket(b.Bytes(), LinkTypeEthernet, gopacket.Default) |
| 103 | + if p.ErrorLayer() != nil { |
| 104 | + t.Error("Failed to decode packet:", p.ErrorLayer().Error()) |
| 105 | + } |
| 106 | + checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeAGUEVar0, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) |
| 107 | + |
| 108 | + // We don't have a corresponding sample packet capture, but if we did, the verify would look like this: |
| 109 | + // if got, want := b.Bytes(), testPacketAGUEVar0``; !reflect.DeepEqual(want, got) { |
| 110 | + // t.Errorf("Encoding mismatch, \nwant: %v\ngot %v\n", want, got) |
| 111 | + // } |
| 112 | +} |
0 commit comments