|
1 | 1 | package openflow15 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "antrea.io/libOpenflow/protocol" |
| 11 | + "antrea.io/libOpenflow/util" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + xID = uint32(0x123456) |
7 | 16 | ) |
8 | 17 |
|
9 | | -func Test_PacketIn2UnMarshal(t *testing.T) { |
| 18 | +func Test_PacketIn2Unmarshal(t *testing.T) { |
10 | 19 | msgBytes := []byte{0, 0, 0, 50, 1, 0, 94, 20, 50, 173, 34, 101, 235, 44, 251, 123, 8, 0, 70, 192, 0, 32, 0, 0, 64, 0, 1, 2, 15, 169, 192, 168, 0, 5, 225, 20, 50, 173, 148, 4, 0, 0, 18, 0, 218, 61, 225, 20, 50, 173, 0, 0, 0, 0, 0, 0, 0, 3, 0, 5, 33, 0, 0, 0, 0, 4, 0, 16, 0, 0, 0, 0, 0, 3, 5, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0, 6, 0, 32, 128, 0, 0, 4, 0, 0, 0, 6, 128, 1, 1, 16, 0, 0, 0, 3, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 7, 0, 5, 3, 0, 0, 0} |
11 | 20 | pktIn2 := new(PacketIn2) |
12 | 21 | err := pktIn2.UnmarshalBinary(msgBytes) |
13 | 22 | assert.NoError(t, err) |
14 | 23 | } |
| 24 | + |
| 25 | +func TestPacketIn2MarshalUnmarshal(t *testing.T) { |
| 26 | + for _, tt := range []struct { |
| 27 | + name string |
| 28 | + packetLength uint16 |
| 29 | + expectedMarshalledBytes int |
| 30 | + expectedMessageLength uint16 |
| 31 | + }{ |
| 32 | + {"SmallPacket", 64, 96, 96}, |
| 33 | + {"MediumPacket", 1500, 1528, 1528}, |
| 34 | + {"LargePacketOverflow", 65530, 65560, 24}, |
| 35 | + } { |
| 36 | + t.Run(tt.name, func(t *testing.T) { |
| 37 | + ethPacket := generateEthernetPacket(tt.packetLength) |
| 38 | + pktIn2PacketProp := &PacketIn2PropPacket{ |
| 39 | + PropHeader: &PropHeader{ |
| 40 | + Type: uint16(NXPINT_PACKET), |
| 41 | + }, |
| 42 | + Packet: ethPacket, |
| 43 | + } |
| 44 | + pktIn2TableProp := &PacketIn2PropTableID{ |
| 45 | + PropHeader: &PropHeader{ |
| 46 | + Type: uint16(NXPINT_TABLE_ID), |
| 47 | + }, |
| 48 | + TableID: 1, |
| 49 | + } |
| 50 | + pktIn2Msg := NewPacketIn2([]Property{pktIn2PacketProp, pktIn2TableProp}) |
| 51 | + pktIn2Msg.Header.Xid = xID |
| 52 | + messageBytes, err := pktIn2Msg.MarshalBinary() |
| 53 | + require.NoError(t, err) |
| 54 | + assert.Len(t, messageBytes, tt.expectedMarshalledBytes) |
| 55 | + |
| 56 | + decodedMsg, err := Parse(messageBytes) |
| 57 | + require.NoError(t, err) |
| 58 | + decodedVendorHeader, ok := decodedMsg.(*VendorHeader) |
| 59 | + require.True(t, ok) |
| 60 | + assert.Equal(t, tt.expectedMessageLength, decodedVendorHeader.Header.Length) |
| 61 | + assert.Equal(t, uint32(NxExperimenterID), decodedVendorHeader.Vendor) |
| 62 | + assert.Equal(t, uint32(Type_PacketIn2), decodedVendorHeader.ExperimenterType) |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestPacketOutMarshalUnmarshal(t *testing.T) { |
| 68 | + for _, tt := range []struct { |
| 69 | + name string |
| 70 | + packetLength uint16 |
| 71 | + expectedMarshalledBytes int |
| 72 | + expectedMessageLength uint16 |
| 73 | + }{ |
| 74 | + {"SmallPacket", 64, 112, 112}, |
| 75 | + {"MediumPacket", 1500, 1548, 1548}, |
| 76 | + {"LargePacketOverflow", 65530, 65578, 42}, |
| 77 | + } { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + ethPacket := generateEthernetPacket(tt.packetLength) |
| 80 | + packetBytes, err := ethPacket.MarshalBinary() |
| 81 | + require.NoError(t, err) |
| 82 | + packetOut := NewPacketOut() |
| 83 | + packetOut.Header.Xid = xID |
| 84 | + packetOut.Data = ðPacket |
| 85 | + packetOut.Match = *NewMatch() |
| 86 | + packetOut.Match.AddField(*NewInPortField(1)) |
| 87 | + packetOut.AddAction(NewActionOutput(30)) |
| 88 | + messageBytes, err := packetOut.MarshalBinary() |
| 89 | + require.NoError(t, err) |
| 90 | + assert.Len(t, messageBytes, tt.expectedMarshalledBytes) |
| 91 | + |
| 92 | + decodedMsg, err := Parse(messageBytes) |
| 93 | + require.NoError(t, err) |
| 94 | + decodedPacketOut, ok := decodedMsg.(*PacketOut) |
| 95 | + require.True(t, ok) |
| 96 | + assert.Equal(t, tt.expectedMessageLength, decodedPacketOut.Header.Length) |
| 97 | + decodedPkt, ok := decodedPacketOut.Data.(*util.Buffer) |
| 98 | + require.True(t, ok) |
| 99 | + assert.Equal(t, packetBytes, decodedPkt.Bytes()) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func generateEthernetPacket(packetLength uint16) protocol.Ethernet { |
| 105 | + ipPacketLength := packetLength - 14 |
| 106 | + udpPacketLength := ipPacketLength - 20 |
| 107 | + udpPayloadLength := udpPacketLength - 8 |
| 108 | + return protocol.Ethernet{ |
| 109 | + HWDst: []byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}, |
| 110 | + HWSrc: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}, |
| 111 | + Ethertype: 0x0800, |
| 112 | + Data: &protocol.IPv4{ |
| 113 | + Version: 0x45, |
| 114 | + IHL: 0x00, |
| 115 | + Length: ipPacketLength, |
| 116 | + NWDst: net.ParseIP("192.168.0.1").To4(), |
| 117 | + NWSrc: net.ParseIP("192.168.0.2").To4(), |
| 118 | + Protocol: protocol.Type_UDP, |
| 119 | + Data: &protocol.UDP{ |
| 120 | + PortSrc: 1234, |
| 121 | + PortDst: 5678, |
| 122 | + Length: udpPacketLength, |
| 123 | + Checksum: 0, |
| 124 | + Data: make([]byte, udpPayloadLength), |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
0 commit comments