]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
unittests: more realistic packet from UTHBuildPacketReal
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 6 Feb 2025 12:35:42 +0000 (13:35 +0100)
committerVictor Julien <victor@inliniac.net>
Sat, 19 Apr 2025 16:20:02 +0000 (18:20 +0200)
So that its contents can be reused when translating unit tests
to SV tests

src/util-unittest-helper.c

index e6331a189f04ac7e3ba7e309e7bca1dfc5086b2c..c1fa775255219ce73910dbdc4414224c02685a6b 100644 (file)
@@ -294,33 +294,39 @@ Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len,
     ip4h->s_ip_src.s_addr = p->src.addr_data32[0];
     ip4h->s_ip_dst.s_addr = p->dst.addr_data32[0];
     ip4h->ip_proto = ipproto;
-    ip4h->ip_verhl = sizeof(IPV4Hdr);
+    ip4h->ip_verhl = 0x40 | (sizeof(IPV4Hdr) / 4);
     p->proto = ipproto;
 
     int hdr_offset = sizeof(IPV4Hdr);
     switch (ipproto) {
         case IPPROTO_UDP: {
-            UDPHdr *udph = PacketSetUDP(p, (GET_PKT_DATA(p) + sizeof(IPV4Hdr)));
+            UDPHdr *udph = PacketSetUDP(p, (GET_PKT_DATA(p) + hdr_offset));
             if (udph == NULL)
                 goto error;
 
-            udph->uh_sport = sport;
-            udph->uh_dport = dport;
+            udph->uh_sport = htons(sport);
+            udph->uh_dport = htons(dport);
+            udph->uh_len = htons(payload_len + sizeof(UDPHdr));
+            ip4h->ip_len = htons(payload_len + sizeof(IPV4Hdr) + sizeof(UDPHdr));
             hdr_offset += sizeof(UDPHdr);
             break;
         }
         case IPPROTO_TCP: {
-            TCPHdr *tcph = PacketSetTCP(p, GET_PKT_DATA(p) + sizeof(IPV4Hdr));
+            TCPHdr *tcph = PacketSetTCP(p, GET_PKT_DATA(p) + hdr_offset);
             if (tcph == NULL)
                 goto error;
 
             tcph->th_sport = htons(sport);
             tcph->th_dport = htons(dport);
+            tcph->th_offx2 = (sizeof(TCPHdr) / 4) << 4;
+            tcph->th_win = 0x4444; // non-zero window
+            tcph->th_flags = TH_ACK;
+            ip4h->ip_len = htons(payload_len + sizeof(IPV4Hdr) + sizeof(TCPHdr));
             hdr_offset += sizeof(TCPHdr);
             break;
         }
         case IPPROTO_ICMP: {
-            ICMPV4Hdr *icmpv4h = PacketSetICMPv4(p, (GET_PKT_DATA(p) + sizeof(IPV4Hdr)));
+            ICMPV4Hdr *icmpv4h = PacketSetICMPv4(p, (GET_PKT_DATA(p) + hdr_offset));
             if (icmpv4h == NULL)
                 goto error;
 
@@ -939,9 +945,9 @@ static int CheckUTHTestPacket(Packet *p, uint8_t ipproto)
             const UDPHdr *udph = PacketGetUDP(p);
             if (udph == NULL)
                 return 0;
-            if (udph->uh_sport != sport)
+            if (SCNtohs(udph->uh_sport) != sport)
                 return 0;
-            if (udph->uh_dport != dport)
+            if (SCNtohs(udph->uh_dport) != dport)
                 return 0;
         break;
         }