11package openflow15
22
33import (
4+ "net"
45 "testing"
56
67 "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 )
716)
817
918func Test_PacketIn2UnMarshal (t * testing.T ) {
@@ -12,3 +21,108 @@ func Test_PacketIn2UnMarshal(t *testing.T) {
1221 err := pktIn2 .UnmarshalBinary (msgBytes )
1322 assert .NoError (t , err )
1423}
24+
25+ func TestPacketIn2UnMarshalWithLargePacket (t * testing.T ) {
26+ for _ , tt := range []struct {
27+ name string
28+ packetLength uint16
29+ bytesSize int
30+ messageLength uint16
31+ }{
32+ {"SmallPacket" , 64 , 96 , 96 },
33+ {"MediumPacket" , 1500 , 1528 , 1528 },
34+ {"LargePacket" , 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 .Equal (t , tt .bytesSize , len (messageBytes ))
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 .messageLength , 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+ bytesSize int
72+ messageLength uint16
73+ }{
74+ {"SmallPacket" , 64 , 112 , 112 },
75+ {"MediumPacket" , 1500 , 1548 , 1548 },
76+ {"LargePacket" , 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 = & ethPacket
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 .Equal (t , tt .bytesSize , len (messageBytes ))
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 .messageLength , 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