suricata
util-unittest-helper.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22  *
23  * This file provide a set of helper functions for reducing the complexity
24  * when constructing unittests
25  */
26 
27 #include "suricata-common.h"
28 
29 #include "decode.h"
30 
31 #include "flow-private.h"
32 #include "flow-util.h"
33 #include "flow-spare-pool.h"
34 
35 #include "detect.h"
36 #include "detect-parse.h"
37 #include "detect-engine.h"
38 #include "detect-engine-alert.h"
39 #include "detect-engine-sigorder.h"
40 #include "detect-engine-build.h"
41 
42 #include "stream-tcp.h"
43 #include "stream-tcp-private.h"
44 
45 #include "util-debug.h"
46 #include "util-time.h"
47 #include "util-error.h"
48 #include "util-unittest.h"
49 #include "util-unittest-helper.h"
50 
51 #if defined(UNITTESTS) || defined(FUZZ)
52 Flow *TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
53 {
54  struct in_addr in;
55 
56  Flow *f = SCMalloc(sizeof(Flow));
57  if (unlikely(f == NULL)) {
58  printf("FlowAlloc failed\n");
59  ;
60  return NULL;
61  }
62  memset(f, 0x00, sizeof(Flow));
63 
64  FLOW_INITIALIZE(f);
65 
66  if (family == AF_INET) {
67  f->flags |= FLOW_IPV4;
68  } else if (family == AF_INET6) {
69  f->flags |= FLOW_IPV6;
70  }
71 
72  if (src != NULL) {
73  if (family == AF_INET) {
74  if (inet_pton(AF_INET, src, &in) != 1) {
75  printf("invalid address %s\n", src);
76  SCFree(f);
77  return NULL;
78  }
79  f->src.addr_data32[0] = in.s_addr;
80  } else {
81  BUG_ON(1);
82  }
83  }
84  if (dst != NULL) {
85  if (family == AF_INET) {
86  if (inet_pton(AF_INET, dst, &in) != 1) {
87  printf("invalid address %s\n", dst);
88  SCFree(f);
89  return NULL;
90  }
91  f->dst.addr_data32[0] = in.s_addr;
92  } else {
93  BUG_ON(1);
94  }
95  }
96 
97  f->sp = sp;
98  f->dp = dp;
99 
100  return f;
101 }
102 /** \brief writes the contents of a buffer into a file */
103 int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
104 {
105  if (remove(name) != 0) {
106  if (errno != ENOENT) {
107  printf("failed remove, errno=%d\n", errno);
108  return -1;
109  }
110  }
111  FILE *fd = fopen(name, "wb");
112  if (fd == NULL) {
113  printf("failed open, errno=%d\n", errno);
114  return -2;
115  }
116  if (fwrite (data, 1, size, fd) != size) {
117  fclose(fd);
118  return -3;
119  }
120  fclose(fd);
121  return 0;
122 }
123 
124 /**
125  * \brief UTHBuildPacketReal is a function that create tcp/udp packets for unittests
126  * specifying ip and port sources and destinations
127  *
128  * \param payload pointer to the payload buffer
129  * \param payload_len pointer to the length of the payload
130  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
131  * \param src pointer to a string containing the ip source
132  * \param dst pointer to a string containing the ip destination
133  * \param sport pointer to a string containing the port source
134  * \param dport pointer to a string containing the port destination
135  *
136  * \retval Packet pointer to the built in packet
137  */
138 Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len,
139  uint8_t ipproto, const char *src, const char *dst,
140  uint16_t sport, uint16_t dport)
141 {
142  struct in_addr in;
143 
145  if (unlikely(p == NULL))
146  return NULL;
147 
148  p->ts = TimeGet();
149 
150  p->src.family = AF_INET;
151  p->dst.family = AF_INET;
152  p->payload = payload;
154  p->proto = ipproto;
155 
156  if (inet_pton(AF_INET, src, &in) != 1)
157  goto error;
158  p->src.addr_data32[0] = in.s_addr;
159  if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto == IPPROTO_SCTP)
160  p->sp = sport;
161 
162  if (inet_pton(AF_INET, dst, &in) != 1)
163  goto error;
164  p->dst.addr_data32[0] = in.s_addr;
165  if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto == IPPROTO_SCTP)
166  p->dp = dport;
167 
168  IPV4Hdr *ip4h = PacketSetIPV4(p, GET_PKT_DATA(p));
169  if (ip4h == NULL)
170  goto error;
171 
172  ip4h->s_ip_src.s_addr = p->src.addr_data32[0];
173  ip4h->s_ip_dst.s_addr = p->dst.addr_data32[0];
174  ip4h->ip_proto = ipproto;
175  ip4h->ip_verhl = 0x40 | (sizeof(IPV4Hdr) / 4);
176  p->proto = ipproto;
177 
178  int hdr_offset = sizeof(IPV4Hdr);
179  switch (ipproto) {
180  case IPPROTO_UDP: {
181  UDPHdr *udph = PacketSetUDP(p, (GET_PKT_DATA(p) + hdr_offset));
182  if (udph == NULL)
183  goto error;
184 
185  udph->uh_sport = htons(sport);
186  udph->uh_dport = htons(dport);
187  udph->uh_len = htons(payload_len + sizeof(UDPHdr));
188  ip4h->ip_len = htons(payload_len + sizeof(IPV4Hdr) + sizeof(UDPHdr));
189  hdr_offset += sizeof(UDPHdr);
190  break;
191  }
192  case IPPROTO_TCP: {
193  TCPHdr *tcph = PacketSetTCP(p, GET_PKT_DATA(p) + hdr_offset);
194  if (tcph == NULL)
195  goto error;
196 
197  tcph->th_sport = htons(sport);
198  tcph->th_dport = htons(dport);
199  tcph->th_offx2 = (sizeof(TCPHdr) / 4) << 4;
200  tcph->th_win = 0x4444; // non-zero window
201  tcph->th_flags = TH_ACK;
202  ip4h->ip_len = htons(payload_len + sizeof(IPV4Hdr) + sizeof(TCPHdr));
203  hdr_offset += sizeof(TCPHdr);
204  break;
205  }
206  case IPPROTO_ICMP: {
207  ICMPV4Hdr *icmpv4h = PacketSetICMPv4(p, (GET_PKT_DATA(p) + hdr_offset));
208  if (icmpv4h == NULL)
209  goto error;
210 
211  hdr_offset += sizeof(ICMPV4Hdr);
212  break;
213  }
214  default:
215  break;
216  /* TODO: Add more protocols */
217  }
218 
219  if (payload && payload_len) {
220  PacketCopyDataOffset(p, hdr_offset, payload, payload_len);
221  }
222  SET_PKT_LEN(p, hdr_offset + payload_len);
223  p->payload = GET_PKT_DATA(p)+hdr_offset;
225 
226  return p;
227 
228 error:
229  SCFree(p);
230  return NULL;
231 }
232 
233 /**
234  * \brief UTHBuildPacket is a wrapper that build packets with default ip
235  * and port fields
236  *
237  * \param payload pointer to the payload buffer
238  * \param payload_len pointer to the length of the payload
239  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
240  *
241  * \retval Packet pointer to the built in packet
242  */
243 Packet *UTHBuildPacket(uint8_t *payload, uint16_t payload_len,
244  uint8_t ipproto)
245 {
246  return UTHBuildPacketReal(payload, payload_len, ipproto,
247  "192.168.1.5", "192.168.1.1",
248  41424, 80);
249 }
250 
251 #endif
252 #ifdef UNITTESTS
254 {
255  PacketSetIPV4(p, (uint8_t *)ip4h);
256 }
257 
259 {
260  PacketSetIPV6(p, (uint8_t *)ip6h);
261 }
262 
264 {
265  PacketSetTCP(p, (uint8_t *)tcph);
266 }
267 
268 /**
269  * \brief return the uint32_t for a ipv4 address string
270  *
271  * \param str Valid ipaddress in string form (e.g. 1.2.3.4)
272  *
273  * \retval uint the uin32_t representation
274  */
275 uint32_t UTHSetIPv4Address(const char *str)
276 {
277  struct in_addr in;
278  if (inet_pton(AF_INET, str, &in) != 1) {
279  printf("invalid IPv6 address %s\n", str);
280  exit(EXIT_FAILURE);
281  }
282  return (uint32_t)in.s_addr;
283 }
284 
285 /**
286  * \brief UTHBuildPacketReal is a function that create tcp/udp packets for unittests
287  * specifying ip and port sources and destinations (IPV6)
288  *
289  * \param payload pointer to the payload buffer
290  * \param payload_len pointer to the length of the payload
291  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
292  * \param src pointer to a string containing the ip source
293  * \param dst pointer to a string containing the ip destination
294  * \param sport pointer to a string containing the port source
295  * \param dport pointer to a string containing the port destination
296  *
297  * \retval Packet pointer to the built in packet
298  */
299 Packet *UTHBuildPacketIPV6Real(uint8_t *payload, uint16_t payload_len, uint8_t ipproto,
300  const char *src, const char *dst, uint16_t sport, uint16_t dport)
301 {
302  uint32_t in[4];
303  TCPHdr *tcph = NULL;
304 
306  if (unlikely(p == NULL))
307  return NULL;
308 
309  p->ts = TimeGet();
310 
311  p->src.family = AF_INET6;
312  p->dst.family = AF_INET6;
313  p->payload = payload;
315  p->proto = ipproto;
316 
317  IPV6Hdr *ip6h = SCCalloc(1, sizeof(IPV6Hdr));
318  if (ip6h == NULL)
319  goto error;
320  ip6h->s_ip6_nxt = ipproto;
321  ip6h->s_ip6_plen = htons(payload_len + sizeof(TCPHdr));
322  UTHSetIPV6Hdr(p, ip6h);
323 
324  if (inet_pton(AF_INET6, src, &in) != 1)
325  goto error;
326  p->src.addr_data32[0] = in[0];
327  p->src.addr_data32[1] = in[1];
328  p->src.addr_data32[2] = in[2];
329  p->src.addr_data32[3] = in[3];
330  p->sp = sport;
331  ip6h->s_ip6_src[0] = in[0];
332  ip6h->s_ip6_src[1] = in[1];
333  ip6h->s_ip6_src[2] = in[2];
334  ip6h->s_ip6_src[3] = in[3];
335 
336  if (inet_pton(AF_INET6, dst, &in) != 1)
337  goto error;
338  p->dst.addr_data32[0] = in[0];
339  p->dst.addr_data32[1] = in[1];
340  p->dst.addr_data32[2] = in[2];
341  p->dst.addr_data32[3] = in[3];
342  p->dp = dport;
343  ip6h->s_ip6_dst[0] = in[0];
344  ip6h->s_ip6_dst[1] = in[1];
345  ip6h->s_ip6_dst[2] = in[2];
346  ip6h->s_ip6_dst[3] = in[3];
347 
348  tcph = SCMalloc(sizeof(TCPHdr));
349  if (tcph == NULL)
350  goto error;
351  memset(tcph, 0, sizeof(TCPHdr));
352  tcph->th_sport = htons(sport);
353  tcph->th_dport = htons(dport);
354  UTHSetTCPHdr(p, tcph);
355 
356  SET_PKT_LEN(p, sizeof(IPV6Hdr) + sizeof(TCPHdr) + payload_len);
357  return p;
358 
359 error:
360  if (p != NULL) {
361  if (ip6h != NULL) {
362  SCFree(ip6h);
363  }
364  if (tcph != NULL) {
365  SCFree(tcph);
366  }
367  SCFree(p);
368  }
369  return NULL;
370 }
371 
372 /**
373  * \brief UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes
374  *
375  * \param raw_eth pointer to the rawbytes containing an ethernet packet
376  * (and any other headers inside)
377  * \param pktsize pointer to the length of the payload
378  *
379  * \retval Packet pointer to the built in packet; NULL if something fail
380  */
381 Packet *UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize)
382 {
386  if (unlikely(p == NULL))
387  return NULL;
388  memset(&dtv, 0, sizeof(DecodeThreadVars));
389  memset(&th_v, 0, sizeof(th_v));
390 
391  DecodeEthernet(&th_v, &dtv, p, raw_eth, pktsize);
392  return p;
393 }
394 
395 /**
396  * \brief UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs
397  * and defaulting ports
398  *
399  * \param payload pointer to the payload buffer
400  * \param payload_len pointer to the length of the payload
401  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
402  *
403  * \retval Packet pointer to the built in packet
404  */
405 Packet *UTHBuildPacketSrcDst(uint8_t *payload, uint16_t payload_len,
406  uint8_t ipproto, const char *src, const char *dst)
407 {
408  return UTHBuildPacketReal(payload, payload_len, ipproto,
409  src, dst,
410  41424, 80);
411 }
412 
413 /**
414  * \brief UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs
415  * and defaulting ports (IPV6)
416  *
417  * \param payload pointer to the payload buffer
418  * \param payload_len pointer to the length of the payload
419  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
420  *
421  * \retval Packet pointer to the built in packet
422  */
423 Packet *UTHBuildPacketIPV6SrcDst(uint8_t *payload, uint16_t payload_len,
424  uint8_t ipproto, const char *src, const char *dst)
425 {
426  return UTHBuildPacketIPV6Real(payload, payload_len, ipproto,
427  src, dst,
428  41424, 80);
429 }
430 
431 /**
432  * \brief UTHBuildPacketSrcDstPorts is a wrapper that build packets specifying
433  * src and dst ports and defaulting IPs
434  *
435  * \param payload pointer to the payload buffer
436  * \param payload_len pointer to the length of the payload
437  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
438  *
439  * \retval Packet pointer to the built in packet
440  */
441 Packet *UTHBuildPacketSrcDstPorts(uint8_t *payload, uint16_t payload_len,
442  uint8_t ipproto, uint16_t sport, uint16_t dport)
443 {
444  return UTHBuildPacketReal(payload, payload_len, ipproto,
445  "192.168.1.5", "192.168.1.1",
446  sport, dport);
447 }
448 
449 /**
450  * \brief UTHFreePackets: function to release the allocated data
451  * from UTHBuildPacket and the packet itself
452  *
453  * \param p pointer to the Packet
454  */
455 void UTHFreePackets(Packet **p, int numpkts)
456 {
457  if (p == NULL)
458  return;
459 
460  int i = 0;
461  for (; i < numpkts; i++) {
462  UTHFreePacket(p[i]);
463  }
464 }
465 
466 /**
467  * \brief UTHFreePacket: function to release the allocated data
468  * from UTHBuildPacket and the packet itself
469  *
470  * \param p pointer to the Packet
471  */
473 {
474  if (p == NULL)
475  return;
476  /* for IPv6 UTHBuildPacketIPV6Real allocs both IPv6 hdr and TCP hdr */
477  if (p->l3.type == PACKET_L3_IPV6) {
478  SCFree(p->l3.hdrs.ip6h);
479  p->l3.hdrs.ip6h = NULL;
480  if (p->l4.type == PACKET_L4_TCP) {
481  SCFree(p->l4.hdrs.tcph);
482  p->l4.hdrs.tcph = NULL;
483  }
484  }
485  PacketFree(p);
486 }
487 
489 {
490  if (p && f) {
491  p->flow = f;
492  p->flags |= PKT_HAS_FLOW;
493  }
494 }
495 
496 Flow *UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
497 {
498  return TestHelperBuildFlow(family, src, dst, sp, dp);
499 }
500 
501 void UTHFreeFlow(Flow *flow)
502 {
503  if (flow != NULL) {
504  FLOW_DESTROY(flow);
505  SCFree(flow);//FlowFree(flow);
506  }
507 }
508 
509 int UTHAddStreamToFlow(Flow *f, int direction,
510  uint8_t *data, uint32_t data_len)
511 {
512  FAIL_IF_NULL(f);
513  FAIL_IF_NOT(f->proto == IPPROTO_TCP);
515  TcpSession *ssn = f->protoctx;
516 
517  StreamingBufferSegment seg;
518  TcpStream *stream = direction == 0 ? &ssn->client : &ssn->server;
519  int r = StreamingBufferAppend(&stream->sb, &stream_config.sbcnf, &seg, data, data_len);
520  FAIL_IF_NOT(r == 0);
521  stream->last_ack += data_len;
522  return 1;
523 }
524 
526  uint32_t ts_isn,
527  uint32_t tc_isn)
528 {
529  FAIL_IF_NULL(f);
530 
531  TcpSession *ssn = SCCalloc(1, sizeof(*ssn));
532  FAIL_IF_NULL(ssn);
533 
535  ssn->client.sb = x;
536  ssn->server.sb = x;
537 
538  ssn->client.isn = ts_isn;
539  ssn->server.isn = tc_isn;
540 
541  f->protoctx = ssn;
542  return 1;
543 }
544 
546 {
547  FAIL_IF_NULL(f);
548  FAIL_IF_NOT(f->proto == IPPROTO_TCP);
549  TcpSession *ssn = f->protoctx;
550  FAIL_IF_NULL(ssn);
552  SCFree(ssn);
553  f->protoctx = NULL;
554  return 1;
555 }
556 
557 /**
558  * \brief UTHGenericTest: function that perform a generic check taking care of
559  * as maximum common unittest elements as possible.
560  * It will create a detection engine, append an array
561  * of signatures an check the expected results for each
562  * of them, it check matches for an array of packets
563  *
564  * \param pkt pointer to the array of packets
565  * \param numpkts number of packets to match
566  * \param sigs array of char* pointing to signatures to load
567  * \param numsigs number of signatures to load and check
568  * \param results pointer to arrays of numbers, each of them foreach packet
569  * to check if sids matches that packet as expected with
570  * that number of times or not. The size of results should be
571  * numpkts * numsigs * sizeof(uint16_t *)
572  *
573  * Example:
574  * result[1][3] would mean the number of times the pkt[1]
575  * match the sid[3]
576  *
577  * \retval int 1 if the match of all the sids is the specified has the
578  * specified results; 0 if not
579  */
580 int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
581 {
582 
583  int result = 0;
584  if (pkt == NULL || sigs == NULL || numpkts == 0
585  || sids == NULL || results == NULL || numsigs == 0) {
586  SCLogError("Arguments invalid, that the pointer/arrays are not NULL, and the number of "
587  "signatures and packets is > 0");
588  goto end;
589  }
591  if (de_ctx == NULL) {
592  goto end;
593  }
594  de_ctx->flags |= DE_QUIET;
595 
596  if (UTHAppendSigs(de_ctx, sigs, numsigs) == 0)
597  goto cleanup;
598 
599  result = UTHMatchPacketsWithResults(de_ctx, pkt, numpkts, sids, results, numsigs);
600 
601 cleanup:
603 end:
604  return result;
605 }
606 
607 /**
608  * \brief UTHCheckPacketMatches: function to check if a packet match some sids
609  *
610  *
611  * \param p pointer to the Packet
612  * \param sigs array of char* pointing to signatures to load
613  * \param numsigs number of signatures to load from the array
614  * \param results pointer to an array of numbers to check if sids matches
615  * that number of times or not.
616  *
617  * \retval int 1 if the match of all the sids is the specified has the
618  * specified results; 0 if not
619  */
620 int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[], uint32_t results[], int numsigs)
621 {
622  if (p == NULL || sids == NULL) {
623  SCLogError("Arguments invalid, check if the "
624  "packet is NULL, and if the array contain sids is set");
625  return 0;
626  }
627 
628  int i = 0;
629  int res = 1;
630  for (; i < numsigs; i++) {
631  uint32_t r = PacketAlertCheck(p, sids[i]);
632  if (r != results[i]) {
633  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, and not %" PRIu32 " as expected",
634  sids[i], r, results[i]);
635  res = 0;
636  } else {
637  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, as expected", sids[i], r);
638  }
639  }
640  return res;
641 }
642 
643 /**
644  * \brief UTHAppendSigs: Add sigs to the detection_engine checking for errors
645  *
646  * \param de_ctx pointer to the DetectEngineCtx used
647  * \param sigs array of char* pointing to signatures to load
648  * \param numsigs number of signatures to load from the array
649  * (size of the array)
650  *
651  * \retval int 0 if we have errors; 1 if all the signatures loaded successfully
652  */
653 int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
654 {
655  BUG_ON(de_ctx == NULL);
656  BUG_ON(numsigs <= 0);
657  BUG_ON(sigs == NULL);
658 
659  for (int i = 0; i < numsigs; i++) {
660  if (sigs[i] == NULL) {
661  SCLogError("Check the signature"
662  " at position %d",
663  i);
664  return 0;
665  }
666  Signature *s = DetectEngineAppendSig(de_ctx, sigs[i]);
667  if (s == NULL) {
668  SCLogError("Check the signature at"
669  " position %d (%s)",
670  i, sigs[i]);
671  return 0;
672  }
673  }
674  return 1;
675 }
676 
677 /**
678  * \test UTHMatchPacketsWithResults Match a packet or a array of packets against sigs
679  * of a de_ctx, checking that each signature matches X times for certain packets
680  *
681  * \param de_ctx pointer with the signatures loaded
682  * \param p pointer to the array of packets
683  * \param num_packets number of packets in the array
684  *
685  * \retval return 1 if all goes well
686  * \retval return 0 if something fail
687  */
688 int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
689 {
690  BUG_ON(de_ctx == NULL);
691  BUG_ON(p == NULL);
692 
693  int result = 0;
696  DetectEngineThreadCtx *det_ctx = NULL;
697  memset(&dtv, 0, sizeof(DecodeThreadVars));
698  memset(&th_v, 0, sizeof(th_v));
699 
702  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
703 
704  for (int i = 0; i < num_packets; i++) {
705  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
706  if (UTHCheckPacketMatchResults(p[i], sids, &results[(i * numsigs)], numsigs) == 0)
707  goto cleanup;
708  }
709 
710  result = 1;
711 cleanup:
712  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
714  return result;
715 }
716 
717 /**
718  * \test UTHMatchPackets Match a packet or a array of packets against sigs
719  * of a de_ctx, but note that the return value doesn't mean that we have a
720  * match, we have to check it later with PacketAlertCheck()
721  *
722  * \param de_ctx pointer with the signatures loaded
723  * \param p pointer to the array of packets
724  * \param num_packets number of packets in the array
725  *
726  * \retval return 1 if all goes well
727  * \retval return 0 if something fail
728  */
729 int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
730 {
731  BUG_ON(de_ctx == NULL);
732  BUG_ON(p == NULL);
733  int result = 1;
736  DetectEngineThreadCtx *det_ctx = NULL;
737  memset(&dtv, 0, sizeof(DecodeThreadVars));
738  memset(&th_v, 0, sizeof(th_v));
741  if (SCSigOrderSignatures(de_ctx) != 0) {
742  result = 0;
743  }
746  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
747 
748  for (int i = 0; i < num_packets; i++)
749  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
750 
751  /* Here we don't check if the packet matched or not, because
752  * the de_ctx can have multiple signatures, and some of them may match
753  * and others may not. That check will be outside
754  */
755  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
757  return result;
758 }
759 
760 /**
761  * \test Test if a packet match a signature given as string and a mpm_type
762  * Hint: Useful for unittests with only one packet and one signature
763  *
764  * \param sig pointer to the string signature to test
765  * \param sid sid number of the signature
766  *
767  * \retval return 1 if match
768  * \retval return 0 if not
769  */
770 int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
771 {
772  SCEnter();
773 
774  int result = 0;
775 
778  DetectEngineThreadCtx *det_ctx = NULL;
779 
780  memset(&dtv, 0, sizeof(DecodeThreadVars));
781  memset(&th_v, 0, sizeof(th_v));
783 
784  if (mpm_type == MPM_AC) {
785  SCConfSet("mpm-algo", "ac");
786 #ifdef BUILD_HYPERSCAN
787  } else if (mpm_type == MPM_HS) {
788  SCConfSet("mpm-algo", "hs");
789 #endif
790  } else {
791  BUG_ON("unsupported MPM type");
792  }
793 
795  if (de_ctx == NULL) {
796  printf("de_ctx == NULL: ");
797  goto end;
798  }
799  de_ctx->flags |= DE_QUIET;
800 
802  if (s == NULL) {
803  printf("signature == NULL: ");
804  goto end;
805  }
806 
808  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
809 
810  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
811  if (PacketAlertCheck(p, s->id) != 1) {
812  printf("signature didn't alert: ");
813  goto end;
814  }
815 
816  result = 1;
817 end:
818  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
821  SCConfSet("mpm-algo", "auto");
822  SCReturnInt(result);
823 }
824 
825 /**
826  * \test Test if a packet match a signature given as string
827  * Hint: Useful for unittests with only one packet and one signature
828  *
829  * \param sig pointer to the string signature to test
830  * \param sid sid number of the signature
831  *
832  * \retval return 1 if match
833  * \retval return 0 if not
834  */
835 int UTHPacketMatchSig(Packet *p, const char *sig)
836 {
837  int result = 1;
838 
841  DetectEngineThreadCtx *det_ctx = NULL;
842 
843  memset(&dtv, 0, sizeof(DecodeThreadVars));
844  memset(&th_v, 0, sizeof(th_v));
846 
848  if (de_ctx == NULL) {
849  result=0;
850  goto end;
851  }
852 
853  de_ctx->flags |= DE_QUIET;
854 
856  if (s == NULL) {
857  result = 0;
858  goto end;
859  }
860 
862  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
863 
864  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
865  if (PacketAlertCheck(p, s->id) != 1) {
866  result = 0;
867  goto end;
868  }
869 
870 end:
871  if (det_ctx != NULL)
872  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
873  if (de_ctx != NULL)
876  return result;
877 }
878 
879 uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
880 {
881  FlowLookupStruct fls;
882  memset(&fls, 0, sizeof(fls));
883  ThreadVars tv;
884  memset(&tv, 0, sizeof(tv));
885 
886  uint32_t i = start;
887  uint8_t payload[] = "Payload";
888  for (; i < end; i++) {
889  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
890  if (dir == 0) {
891  p->src.addr_data32[0] = i;
892  p->dst.addr_data32[0] = i + 1;
893  } else {
894  p->src.addr_data32[0] = i + 1;
895  p->dst.addr_data32[0] = i;
896  }
897  FlowHandlePacket(&tv, &fls, p);
898  if (p->flow != NULL) {
900  }
901 
902  /* Now the queues should be updated */
903  UTHFreePacket(p);
904  }
905 
906  Flow *f;
907  while ((f = FlowQueuePrivateGetFromTop(&fls.spare_queue))) {
908  FlowFree(f);
909  }
910  while ((f = FlowQueuePrivateGetFromTop(&fls.work_queue))) {
911  FlowFree(f);
912  }
913 
914  return i;
915 }
916 
917 /** \brief parser a sig and see if the expected result is correct */
918 int UTHParseSignature(const char *str, bool expect)
919 {
922  de_ctx->flags |= DE_QUIET;
923 
925  if (expect)
926  FAIL_IF_NULL(s);
927  else
928  FAIL_IF_NOT_NULL(s);
929 
931  PASS;
932 }
933 
934 /*
935  * unittests for the unittest helpers
936  */
937 
938 /**
939  * \brief CheckUTHTestPacket wrapper to check packets for unittests
940  */
941 static int CheckUTHTestPacket(Packet *p, uint8_t ipproto)
942 {
943  uint16_t sport = 41424;
944  uint16_t dport = 80;
945  uint8_t payload[] = "Payload";
946 
947  uint8_t len = sizeof(payload);
948 
949  if (p == NULL)
950  return 0;
951 
952  if (p->payload_len != len)
953  return 0;
954 
955  if (strncmp((char *)payload, (char *)p->payload, len) != 0)
956  return 0;
957 
958  if (p->src.family != AF_INET)
959  return 0;
960  if (p->dst.family != AF_INET)
961  return 0;
962  if (p->proto != ipproto)
963  return 0;
964 
965  switch(ipproto) {
966  case IPPROTO_UDP: {
967  const UDPHdr *udph = PacketGetUDP(p);
968  if (udph == NULL)
969  return 0;
970  if (SCNtohs(udph->uh_sport) != sport)
971  return 0;
972  if (SCNtohs(udph->uh_dport) != dport)
973  return 0;
974  break;
975  }
976  case IPPROTO_TCP: {
977  const TCPHdr *tcph = PacketGetTCP(p);
978  if (tcph == NULL)
979  return 0;
980  if (SCNtohs(tcph->th_sport) != sport)
981  return 0;
982  if (SCNtohs(tcph->th_dport) != dport)
983  return 0;
984  break;
985  }
986  }
987  return 1;
988 }
989 
990 #ifdef HAVE_MEMMEM
991 #include <string.h>
992 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
993  return memmem(big, big_len, little, little_len);
994 }
995 #else
996 #include "util-spm-bs.h"
997 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
998  return BasicSearch(big, big_len, little, little_len);
999 }
1000 #endif //HAVE_MEMMEM
1001 
1002 /**
1003  * \brief UTHBuildPacketRealTest01 wrapper to check packets for unittests
1004  */
1005 static int UTHBuildPacketRealTest01(void)
1006 {
1007  uint8_t payload[] = "Payload";
1008 
1009  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_TCP,
1010  "192.168.1.5", "192.168.1.1", 41424, 80);
1011 
1012  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1013  UTHFreePacket(p);
1014 
1015  return ret;
1016 }
1017 
1018 /**
1019  * \brief UTHBuildPacketRealTest02 wrapper to check packets for unittests
1020  */
1021 static int UTHBuildPacketRealTest02(void)
1022 {
1023  uint8_t payload[] = "Payload";
1024 
1025  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_UDP,
1026  "192.168.1.5", "192.168.1.1", 41424, 80);
1027 
1028  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1029  UTHFreePacket(p);
1030  return ret;
1031 }
1032 
1033 /**
1034  * \brief UTHBuildPacketTest01 wrapper to check packets for unittests
1035  */
1036 static int UTHBuildPacketTest01(void)
1037 {
1038  uint8_t payload[] = "Payload";
1039 
1040  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
1041 
1042  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1043  UTHFreePacket(p);
1044 
1045  return ret;
1046 }
1047 
1048 /**
1049  * \brief UTHBuildPacketTest02 wrapper to check packets for unittests
1050  */
1051 static int UTHBuildPacketTest02(void)
1052 {
1053  uint8_t payload[] = "Payload";
1054 
1055  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_UDP);
1056 
1057  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1058  UTHFreePacket(p);
1059 
1060  return ret;
1061 }
1062 
1063 /**
1064  * \brief UTHBuildPacketOfFlowsTest01 wrapper to check packets for unittests
1065  */
1066 static int UTHBuildPacketOfFlowsTest01(void)
1067 {
1068  int result = 0;
1069 
1071  uint32_t flow_spare_q_len = FlowSpareGetPoolSize();
1072 
1073  UTHBuildPacketOfFlows(0, 100, 0);
1074 
1075  if (FlowSpareGetPoolSize() != flow_spare_q_len - 100)
1076  result = 0;
1077  else
1078  result = 1;
1079  FlowShutdown();
1080 
1081  return result;
1082 }
1083 
1084 
1085 /**
1086  * \brief UTHBuildPacketSrcDstTest01 wrapper to check packets for unittests
1087  */
1088 static int UTHBuildPacketSrcDstTest01(void)
1089 {
1090  uint8_t payload[] = "Payload";
1091 
1092  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_TCP,
1093  "192.168.1.5", "192.168.1.1");
1094 
1095  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1096  UTHFreePacket(p);
1097 
1098  return ret;
1099 }
1100 
1101 /**
1102  * \brief UTHBuildPacketSrcDstTest02 wrapper to check packets for unittests
1103  */
1104 static int UTHBuildPacketSrcDstTest02(void)
1105 {
1106  uint8_t payload[] = "Payload";
1107 
1108  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_UDP,
1109  "192.168.1.5", "192.168.1.1");
1110 
1111  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1112  UTHFreePacket(p);
1113 
1114  return ret;
1115 }
1116 
1117 /**
1118  * \brief UTHBuildPacketSrcDstPortsTest01 wrapper to check packets for unittests
1119  */
1120 static int UTHBuildPacketSrcDstPortsTest01(void)
1121 {
1122  uint8_t payload[] = "Payload";
1123 
1124  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_TCP,
1125  41424, 80);
1126 
1127  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1128  UTHFreePacket(p);
1129 
1130  return ret;
1131 }
1132 
1133 /**
1134  * \brief UTHBuildPacketSrcDstPortsTest02 wrapper to check packets for unittests
1135  */
1136 static int UTHBuildPacketSrcDstPortsTest02(void)
1137 {
1138  uint8_t payload[] = "Payload";
1139 
1140  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_UDP,
1141  41424, 80);
1142 
1143  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1144  UTHFreePacket(p);
1145 
1146  return ret;
1147 }
1148 
1149 #endif /* UNITTESTS */
1150 
1152 {
1153 #ifdef UNITTESTS
1154  UtRegisterTest("UTHBuildPacketRealTest01", UTHBuildPacketRealTest01);
1155  UtRegisterTest("UTHBuildPacketRealTest02", UTHBuildPacketRealTest02);
1156  UtRegisterTest("UTHBuildPacketTest01", UTHBuildPacketTest01);
1157  UtRegisterTest("UTHBuildPacketTest02", UTHBuildPacketTest02);
1158  UtRegisterTest("UTHBuildPacketSrcDstTest01", UTHBuildPacketSrcDstTest01);
1159  UtRegisterTest("UTHBuildPacketSrcDstTest02", UTHBuildPacketSrcDstTest02);
1160  UtRegisterTest("UTHBuildPacketSrcDstPortsTest01",
1161  UTHBuildPacketSrcDstPortsTest01);
1162  UtRegisterTest("UTHBuildPacketSrcDstPortsTest02",
1163  UTHBuildPacketSrcDstPortsTest02);
1164  UtRegisterTest("UTHBuildPacketOfFlowsTest01", UTHBuildPacketOfFlowsTest01);
1165 
1166 #endif /* UNITTESTS */
1167 }
1168 
UPDATE_DIR_BOTH
@ UPDATE_DIR_BOTH
Definition: stream-tcp-reassemble.h:58
FlowLookupStruct_::work_queue
FlowQueuePrivate work_queue
Definition: flow.h:546
Packet_::proto
uint8_t proto
Definition: decode.h:537
UTHParseSignature
int UTHParseSignature(const char *str, bool expect)
parser a sig and see if the expected result is correct
Definition: util-unittest-helper.c:918
TcpStream_
Definition: stream-tcp-private.h:106
MPM_AC
@ MPM_AC
Definition: util-mpm.h:38
len
uint8_t len
Definition: app-layer-dnp3.h:2
UTHmemsearch
void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len)
Definition: util-unittest-helper.c:997
TCPHdr_::th_dport
uint16_t th_dport
Definition: decode-tcp.h:151
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
UDPHdr_::uh_dport
uint16_t uh_dport
Definition: decode-udp.h:44
TcpStream_::isn
uint32_t isn
Definition: stream-tcp-private.h:113
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1310
UTHAddStreamToFlow
int UTHAddStreamToFlow(Flow *f, int direction, uint8_t *data, uint32_t data_len)
Definition: util-unittest-helper.c:509
FlowSpareGetPoolSize
uint32_t FlowSpareGetPoolSize(void)
Definition: flow-spare-pool.c:46
flow-util.h
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
name
const char * name
Definition: detect-engine-proto.c:48
Flow_::proto
uint8_t proto
Definition: flow.h:376
Packet_::payload
uint8_t * payload
Definition: decode.h:619
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:144
Packet_::flags
uint32_t flags
Definition: decode.h:561
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:354
UTHBuildPacketSrcDst
Packet * UTHBuildPacketSrcDst(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst)
UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs and defaulting ports.
Definition: util-unittest-helper.c:405
UTHSetIPV4Hdr
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
Definition: util-unittest-helper.c:253
TCPHdr_::th_win
uint16_t th_win
Definition: decode-tcp.h:156
UTHSetIPv4Address
uint32_t UTHSetIPv4Address(const char *str)
return the uint32_t for a ipv4 address string
Definition: util-unittest-helper.c:275
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
UTHPacketMatchSigMpm
int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
Definition: util-unittest-helper.c:770
TcpStreamCnf_::sbcnf
StreamingBufferConfig sbcnf
Definition: stream-tcp.h:89
FlowLookupStruct_
Definition: flow.h:542
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2760
PacketCopyDataOffset
int PacketCopyDataOffset(Packet *p, uint32_t offset, const uint8_t *data, uint32_t datalen)
Copy data to Packet payload at given offset.
Definition: decode.c:341
ICMPV4Hdr
struct ICMPV4Hdr_ ICMPV4Hdr
SCSigSignatureOrderingModuleCleanup
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
Definition: detect-engine-sigorder.c:946
PacketL3::hdrs
union PacketL3::Hdrs hdrs
UTHCheckPacketMatchResults
int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[], uint32_t results[], int numsigs)
UTHCheckPacketMatches: function to check if a packet match some sids.
Definition: util-unittest-helper.c:620
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:835
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
Definition: util-streaming-buffer.c:1097
FlowHandlePacket
void FlowHandlePacket(ThreadVars *tv, FlowLookupStruct *fls, Packet *p)
Entry point for packet flow handling.
Definition: flow.c:563
UTHSetTCPHdr
void UTHSetTCPHdr(Packet *p, TCPHdr *tcph)
Definition: util-unittest-helper.c:263
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:243
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2971
SCSigOrderSignatures
int SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
Definition: detect-engine-sigorder.c:802
p
Packet * p
Definition: fuzz_iprep.c:21
UTHBuildPacketSrcDstPorts
Packet * UTHBuildPacketSrcDstPorts(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, uint16_t sport, uint16_t dport)
UTHBuildPacketSrcDstPorts is a wrapper that build packets specifying src and dst ports and defaulting...
Definition: util-unittest-helper.c:441
UTHSetIPV6Hdr
void UTHSetIPV6Hdr(Packet *p, IPV6Hdr *ip6h)
Definition: util-unittest-helper.c:258
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3595
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:229
util-spm-bs.h
UTHBuildPacketReal
Packet * UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst, uint16_t sport, uint16_t dport)
UTHBuildPacketReal is a function that create tcp/udp packets for unittests specifying ip and port sou...
Definition: util-unittest-helper.c:138
Flow_::protoctx
void * protoctx
Definition: flow.h:433
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:99
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:620
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:271
UTHBuildPacketOfFlows
uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
Definition: util-unittest-helper.c:879
IPV4Hdr
struct IPV4Hdr_ IPV4Hdr
PacketL3::Hdrs::ip6h
IPV6Hdr * ip6h
Definition: decode.h:450
UTHAssignFlow
void UTHAssignFlow(Packet *p, Flow *f)
Definition: util-unittest-helper.c:488
TcpStream_::last_ack
uint32_t last_ack
Definition: stream-tcp-private.h:115
IPV4Hdr_::ip_len
uint16_t ip_len
Definition: decode-ipv4.h:75
flow-spare-pool.h
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:577
SET_PKT_LEN
#define SET_PKT_LEN(p, len)
Definition: decode.h:214
UTHMatchPackets
int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
Definition: util-unittest-helper.c:729
TCPHdr_::th_sport
uint16_t th_sport
Definition: decode-tcp.h:150
UTHBuildFlow
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:496
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
UTHBuildPacketIPV6Real
Packet * UTHBuildPacketIPV6Real(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst, uint16_t sport, uint16_t dport)
UTHBuildPacketReal is a function that create tcp/udp packets for unittests specifying ip and port sou...
Definition: util-unittest-helper.c:299
PACKET_L4_TCP
@ PACKET_L4_TCP
Definition: decode.h:465
TCPHdr_::th_flags
uint8_t th_flags
Definition: decode-tcp.h:155
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
UTHMatchPacketsWithResults
int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
Definition: util-unittest-helper.c:688
DetectEngineThreadCtx_
Definition: detect.h:1291
UDPHdr_::uh_len
uint16_t uh_len
Definition: decode-udp.h:45
Packet_::ts
SCTime_t ts
Definition: decode.h:569
TCPHdr_::th_offx2
uint8_t th_offx2
Definition: decode-tcp.h:154
UTHRegisterTests
void UTHRegisterTests(void)
Definition: util-unittest-helper.c:1151
MPM_HS
@ MPM_HS
Definition: util-mpm.h:40
UTHAddSessionToFlow
int UTHAddSessionToFlow(Flow *f, uint32_t ts_isn, uint32_t tc_isn)
Definition: util-unittest-helper.c:525
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:210
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3506
Packet_::sp
Port sp
Definition: decode.h:522
SCSigRegisterSignatureOrderingFuncs
void SCSigRegisterSignatureOrderingFuncs(DetectEngineCtx *de_ctx)
Lets you register the Signature ordering functions. The order in which the functions are registered s...
Definition: detect-engine-sigorder.c:926
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:222
TH_ACK
#define TH_ACK
Definition: decode-tcp.h:38
TestHelperBufferToFile
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
writes the contents of a buffer into a file
Definition: util-unittest-helper.c:103
util-time.h
FlowQueuePrivateGetFromTop
Flow * FlowQueuePrivateGetFromTop(FlowQueuePrivate *fqc)
Definition: flow-queue.c:151
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:325
BasicSearch
uint8_t * BasicSearch(const uint8_t *haystack, uint32_t haystack_len, const uint8_t *needle, uint16_t needle_len)
Basic search improved. Limits are better handled, so it doesn't start searches that wont fit in the r...
Definition: util-spm-bs.c:49
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:515
detect-engine-build.h
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:153
stream-tcp-private.h
ICMPV4Hdr_
Definition: decode-icmpv4.h:165
detect-engine-alert.h
Packet_::l4
struct PacketL4 l4
Definition: decode.h:615
Port
uint16_t Port
Definition: decode.h:219
STREAMING_BUFFER_INITIALIZER
#define STREAMING_BUFFER_INITIALIZER
Definition: util-streaming-buffer.h:137
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2295
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:34
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1333
UTHFreeFlow
void UTHFreeFlow(Flow *flow)
Definition: util-unittest-helper.c:501
StreamingBuffer_
Definition: util-streaming-buffer.h:108
IPV4Hdr_
Definition: decode-ipv4.h:72
FlowLookupStruct_::spare_queue
FlowQueuePrivate spare_queue
Definition: flow.h:544
PacketL3::type
enum PacketL3Types type
Definition: decode.h:444
Packet_::flow
struct Flow_ * flow
Definition: decode.h:563
PacketL4::type
enum PacketL4Types type
Definition: decode.h:476
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:439
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:85
FLOW_IPV6
#define FLOW_IPV6
Definition: flow.h:101
UTHBuildPacketIPV6SrcDst
Packet * UTHBuildPacketIPV6SrcDst(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst)
UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs and defaulting ports (IPV6)
Definition: util-unittest-helper.c:423
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:721
TcpStream_::sb
StreamingBuffer sb
Definition: stream-tcp-private.h:135
UDPHdr
struct UDPHdr_ UDPHdr
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3751
Packet_::app_update_direction
uint8_t app_update_direction
Definition: decode.h:549
UDPHdr_::uh_sport
uint16_t uh_sport
Definition: decode-udp.h:43
UDPHdr_
Definition: decode-udp.h:42
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:297
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
UTHGenericTest
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perform a generic check taking care of as maximum common unittest eleme...
Definition: util-unittest-helper.c:580
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:261
detect-engine-sigorder.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
PacketL4::L4Hdrs::tcph
TCPHdr * tcph
Definition: decode.h:480
Packet_::l3
struct PacketL3 l3
Definition: decode.h:614
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:296
str
#define str(s)
Definition: suricata-common.h:316
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:994
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:472
Signature_::id
uint32_t id
Definition: detect.h:720
PACKET_L3_IPV6
@ PACKET_L3_IPV6
Definition: decode.h:439
StreamTcpSessionCleanup
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition: stream-tcp.c:337
detect-parse.h
src
uint16_t src
Definition: app-layer-dnp3.h:5
Signature_
Signature container.
Definition: detect.h:675
payload_len
uint16_t payload_len
Definition: stream-tcp-private.h:1
PacketL4::hdrs
union PacketL4::L4Hdrs hdrs
UTHBuildPacketFromEth
Packet * UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize)
UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes.
Definition: util-unittest-helper.c:381
SCConfSet
int SCConfSet(const char *name, const char *val)
Set a configuration value.
Definition: conf.c:242
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2721
Address_::family
char family
Definition: decode.h:114
Packet_::dst
Address dst
Definition: decode.h:520
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:43
TestHelperBuildFlow
Flow * TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:52
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1272
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:975
UTHAppendSigs
int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
UTHAppendSigs: Add sigs to the detection_engine checking for errors.
Definition: util-unittest-helper.c:653
TCPHdr
struct TCPHdr_ TCPHdr
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
UTHRemoveSessionFromFlow
int UTHRemoveSessionFromFlow(Flow *f)
Definition: util-unittest-helper.c:545
TcpSession_
Definition: stream-tcp-private.h:283
Packet_::dp
Port dp
Definition: decode.h:530
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
IPV4Hdr_::ip_proto
uint8_t ip_proto
Definition: decode-ipv4.h:79
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1429
IPV4Hdr_::ip_verhl
uint8_t ip_verhl
Definition: decode-ipv4.h:73
DecodeEthernet
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-ethernet.c:42
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:119
TCPHdr_
Definition: decode-tcp.h:149
Packet_::src
Address src
Definition: decode.h:519
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:455