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 #endif
125 #ifdef UNITTESTS
126 
127 /**
128  * \brief return the uint32_t for a ipv4 address string
129  *
130  * \param str Valid ipaddress in string form (e.g. 1.2.3.4)
131  *
132  * \retval uint the uin32_t representation
133  */
134 uint32_t UTHSetIPv4Address(const char *str)
135 {
136  struct in_addr in;
137  if (inet_pton(AF_INET, str, &in) != 1) {
138  printf("invalid IPv6 address %s\n", str);
139  exit(EXIT_FAILURE);
140  }
141  return (uint32_t)in.s_addr;
142 }
143 
144 /**
145  * \brief UTHBuildPacketReal is a function that create tcp/udp packets for unittests
146  * specifying ip and port sources and destinations (IPV6)
147  *
148  * \param payload pointer to the payloadd buffer
149  * \param payload_len pointer to the length of the payload
150  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
151  * \param src pointer to a string containing the ip source
152  * \param dst pointer to a string containing the ip destination
153  * \param sport pointer to a string containing the port source
154  * \param dport pointer to a string containing the port destination
155  *
156  * \retval Packet pointer to the built in packet
157  */
158 Packet *UTHBuildPacketIPV6Real(uint8_t *payload, uint16_t payload_len,
159  uint8_t ipproto, const char *src, const char *dst,
160  uint16_t sport, uint16_t dport)
161 {
162  uint32_t in[4];
163 
164  Packet *p = PacketGetFromAlloc();
165  if (unlikely(p == NULL))
166  return NULL;
167 
168  p->ts = TimeGet();
169 
170  p->src.family = AF_INET6;
171  p->dst.family = AF_INET6;
172  p->payload = payload;
174  p->proto = ipproto;
175 
176  p->ip6h = SCMalloc(sizeof(IPV6Hdr));
177  if (p->ip6h == NULL)
178  goto error;
179  memset(p->ip6h, 0, sizeof(IPV6Hdr));
180  p->ip6h->s_ip6_nxt = ipproto;
181  p->ip6h->s_ip6_plen = htons(payload_len + sizeof(TCPHdr));
182 
183  if (inet_pton(AF_INET6, src, &in) != 1)
184  goto error;
185  p->src.addr_data32[0] = in[0];
186  p->src.addr_data32[1] = in[1];
187  p->src.addr_data32[2] = in[2];
188  p->src.addr_data32[3] = in[3];
189  p->sp = sport;
190  p->ip6h->s_ip6_src[0] = in[0];
191  p->ip6h->s_ip6_src[1] = in[1];
192  p->ip6h->s_ip6_src[2] = in[2];
193  p->ip6h->s_ip6_src[3] = in[3];
194 
195  if (inet_pton(AF_INET6, dst, &in) != 1)
196  goto error;
197  p->dst.addr_data32[0] = in[0];
198  p->dst.addr_data32[1] = in[1];
199  p->dst.addr_data32[2] = in[2];
200  p->dst.addr_data32[3] = in[3];
201  p->dp = dport;
202  p->ip6h->s_ip6_dst[0] = in[0];
203  p->ip6h->s_ip6_dst[1] = in[1];
204  p->ip6h->s_ip6_dst[2] = in[2];
205  p->ip6h->s_ip6_dst[3] = in[3];
206 
207  p->tcph = SCMalloc(sizeof(TCPHdr));
208  if (p->tcph == NULL)
209  goto error;
210  memset(p->tcph, 0, sizeof(TCPHdr));
211  p->tcph->th_sport = htons(sport);
212  p->tcph->th_dport = htons(dport);
213 
214  SET_PKT_LEN(p, sizeof(IPV6Hdr) + sizeof(TCPHdr) + payload_len);
215  return p;
216 
217 error:
218  if (p != NULL) {
219  if (p->ip6h != NULL) {
220  SCFree(p->ip6h);
221  }
222  if (p->tcph != NULL) {
223  SCFree(p->tcph);
224  }
225  SCFree(p);
226  }
227  return NULL;
228 }
229 
230 /**
231  * \brief UTHBuildPacketReal is a function that create tcp/udp packets for unittests
232  * specifying ip and port sources and destinations
233  *
234  * \param payload pointer to the payloadd buffer
235  * \param payload_len pointer to the length of the payload
236  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
237  * \param src pointer to a string containing the ip source
238  * \param dst pointer to a string containing the ip destination
239  * \param sport pointer to a string containing the port source
240  * \param dport pointer to a string containing the port destination
241  *
242  * \retval Packet pointer to the built in packet
243  */
244 Packet *UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len,
245  uint8_t ipproto, const char *src, const char *dst,
246  uint16_t sport, uint16_t dport)
247 {
248  struct in_addr in;
249 
250  Packet *p = PacketGetFromAlloc();
251  if (unlikely(p == NULL))
252  return NULL;
253 
254  p->ts = TimeGet();
255 
256  p->src.family = AF_INET;
257  p->dst.family = AF_INET;
258  p->payload = payload;
259  p->payload_len = payload_len;
260  p->proto = ipproto;
261 
262  if (inet_pton(AF_INET, src, &in) != 1)
263  goto error;
264  p->src.addr_data32[0] = in.s_addr;
265  p->sp = sport;
266 
267  if (inet_pton(AF_INET, dst, &in) != 1)
268  goto error;
269  p->dst.addr_data32[0] = in.s_addr;
270  p->dp = dport;
271 
272  p->ip4h = (IPV4Hdr *)GET_PKT_DATA(p);
273  if (p->ip4h == NULL)
274  goto error;
275 
276  p->ip4h->s_ip_src.s_addr = p->src.addr_data32[0];
277  p->ip4h->s_ip_dst.s_addr = p->dst.addr_data32[0];
278  p->ip4h->ip_proto = ipproto;
279  p->ip4h->ip_verhl = sizeof(IPV4Hdr);
280  p->proto = ipproto;
281 
282  int hdr_offset = sizeof(IPV4Hdr);
283  switch (ipproto) {
284  case IPPROTO_UDP:
285  p->udph = (UDPHdr *)(GET_PKT_DATA(p) + sizeof(IPV4Hdr));
286  if (p->udph == NULL)
287  goto error;
288 
289  p->udph->uh_sport = sport;
290  p->udph->uh_dport = dport;
291  hdr_offset += sizeof(UDPHdr);
292  break;
293  case IPPROTO_TCP:
294  p->tcph = (TCPHdr *)(GET_PKT_DATA(p) + sizeof(IPV4Hdr));
295  if (p->tcph == NULL)
296  goto error;
297 
298  p->tcph->th_sport = htons(sport);
299  p->tcph->th_dport = htons(dport);
300  hdr_offset += sizeof(TCPHdr);
301  break;
302  case IPPROTO_ICMP:
303  p->icmpv4h = (ICMPV4Hdr *)(GET_PKT_DATA(p) + sizeof(IPV4Hdr));
304  if (p->icmpv4h == NULL)
305  goto error;
306 
307  hdr_offset += sizeof(ICMPV4Hdr);
308  break;
309  default:
310  break;
311  /* TODO: Add more protocols */
312  }
313 
314  if (payload && payload_len) {
315  PacketCopyDataOffset(p, hdr_offset, payload, payload_len);
316  }
317  SET_PKT_LEN(p, hdr_offset + payload_len);
318  p->payload = GET_PKT_DATA(p)+hdr_offset;
319 
320  return p;
321 
322 error:
323  SCFree(p);
324  return NULL;
325 }
326 
327 /**
328  * \brief UTHBuildPacket is a wrapper that build packets with default ip
329  * and port fields
330  *
331  * \param payload pointer to the payloadd buffer
332  * \param payload_len pointer to the length of the payload
333  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
334  *
335  * \retval Packet pointer to the built in packet
336  */
337 Packet *UTHBuildPacket(uint8_t *payload, uint16_t payload_len,
338  uint8_t ipproto)
339 {
340  return UTHBuildPacketReal(payload, payload_len, ipproto,
341  "192.168.1.5", "192.168.1.1",
342  41424, 80);
343 }
344 
345 /**
346  * \brief UTHBuildPacketArrayFromEth is a wrapper that build a packets from an array of
347  * packets in ethernet rawbytes. Hint: It also share the flows.
348  *
349  * \param raw_eth pointer to the array of ethernet packets in rawbytes
350  * \param pktsize pointer to the array of sizes corresponding to each buffer pointed
351  * from pktsize.
352  * \param numpkts number of packets in the array
353  *
354  * \retval Packet pointer to the array of built in packets; NULL if something fail
355  */
356 Packet **UTHBuildPacketArrayFromEth(uint8_t *raw_eth[], int *pktsize, int numpkts)
357 {
359  ThreadVars th_v;
360  if (raw_eth == NULL || pktsize == NULL || numpkts <= 0) {
361  SCLogError("The arrays cant be null, and the number"
362  " of packets should be grater thatn zero");
363  return NULL;
364  }
365  Packet **p = NULL;
366  p = SCMalloc(sizeof(Packet *) * numpkts);
367  if (unlikely(p == NULL))
368  return NULL;
369 
370  memset(&dtv, 0, sizeof(DecodeThreadVars));
371  memset(&th_v, 0, sizeof(th_v));
372 
373  int i = 0;
374  for (; i < numpkts; i++) {
375  p[i] = PacketGetFromAlloc();
376  if (p[i] == NULL) {
377  SCFree(p);
378  return NULL;
379  }
380  DecodeEthernet(&th_v, &dtv, p[i], raw_eth[i], pktsize[i]);
381  }
382  return p;
383 }
384 
385 /**
386  * \brief UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes
387  *
388  * \param raw_eth pointer to the rawbytes containing an ethernet packet
389  * (and any other headers inside)
390  * \param pktsize pointer to the length of the payload
391  *
392  * \retval Packet pointer to the built in packet; NULL if something fail
393  */
394 Packet *UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize)
395 {
397  ThreadVars th_v;
398  Packet *p = PacketGetFromAlloc();
399  if (unlikely(p == NULL))
400  return NULL;
401  memset(&dtv, 0, sizeof(DecodeThreadVars));
402  memset(&th_v, 0, sizeof(th_v));
403 
404  DecodeEthernet(&th_v, &dtv, p, raw_eth, pktsize);
405  return p;
406 }
407 
408 /**
409  * \brief UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs
410  * and defaulting ports
411  *
412  * \param payload pointer to the payloadd buffer
413  * \param payload_len pointer to the length of the payload
414  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
415  *
416  * \retval Packet pointer to the built in packet
417  */
418 Packet *UTHBuildPacketSrcDst(uint8_t *payload, uint16_t payload_len,
419  uint8_t ipproto, const char *src, const char *dst)
420 {
421  return UTHBuildPacketReal(payload, payload_len, ipproto,
422  src, dst,
423  41424, 80);
424 }
425 
426 /**
427  * \brief UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs
428  * and defaulting ports (IPV6)
429  *
430  * \param payload pointer to the payload buffer
431  * \param payload_len pointer to the length of the payload
432  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
433  *
434  * \retval Packet pointer to the built in packet
435  */
436 Packet *UTHBuildPacketIPV6SrcDst(uint8_t *payload, uint16_t payload_len,
437  uint8_t ipproto, const char *src, const char *dst)
438 {
439  return UTHBuildPacketIPV6Real(payload, payload_len, ipproto,
440  src, dst,
441  41424, 80);
442 }
443 
444 /**
445  * \brief UTHBuildPacketSrcDstPorts is a wrapper that build packets specifying
446  * src and dst ports and defaulting IPs
447  *
448  * \param payload pointer to the payloadd buffer
449  * \param payload_len pointer to the length of the payload
450  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
451  *
452  * \retval Packet pointer to the built in packet
453  */
454 Packet *UTHBuildPacketSrcDstPorts(uint8_t *payload, uint16_t payload_len,
455  uint8_t ipproto, uint16_t sport, uint16_t dport)
456 {
457  return UTHBuildPacketReal(payload, payload_len, ipproto,
458  "192.168.1.5", "192.168.1.1",
459  sport, dport);
460 }
461 
462 /**
463  * \brief UTHFreePackets: function to release the allocated data
464  * from UTHBuildPacket and the packet itself
465  *
466  * \param p pointer to the Packet
467  */
468 void UTHFreePackets(Packet **p, int numpkts)
469 {
470  if (p == NULL)
471  return;
472 
473  int i = 0;
474  for (; i < numpkts; i++) {
475  UTHFreePacket(p[i]);
476  }
477 }
478 
479 /**
480  * \brief UTHFreePacket: function to release the allocated data
481  * from UTHBuildPacket and the packet itself
482  *
483  * \param p pointer to the Packet
484  */
486 {
487  if (p == NULL)
488  return;
489 #if 0 // VJ we now use one buffer
490  switch (p->proto) {
491  case IPPROTO_UDP:
492  if (p->udph != NULL)
493  SCFree(p->udph);
494  if (p->ip4h != NULL)
495  SCFree(p->ip4h);
496  break;
497  case IPPROTO_TCP:
498  if (p->tcph != NULL)
499  SCFree(p->tcph);
500  if (p->ip4h != NULL)
501  SCFree(p->ip4h);
502  break;
503  case IPPROTO_ICMP:
504  if (p->ip4h != NULL)
505  SCFree(p->ip4h);
506  break;
507  /* TODO: Add more protocols */
508  }
509 #endif
510  SCFree(p);
511 }
512 
514 {
515  if (p && f) {
516  p->flow = f;
517  p->flags |= PKT_HAS_FLOW;
518  }
519 }
520 
521 Flow *UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
522 {
523  return TestHelperBuildFlow(family, src, dst, sp, dp);
524 }
525 
526 void UTHFreeFlow(Flow *flow)
527 {
528  if (flow != NULL) {
529  SCFree(flow);//FlowFree(flow);
530  }
531 }
532 
533 int UTHAddStreamToFlow(Flow *f, int direction,
534  uint8_t *data, uint32_t data_len)
535 {
536  FAIL_IF_NULL(f);
537  FAIL_IF_NOT(f->proto == IPPROTO_TCP);
539  TcpSession *ssn = f->protoctx;
540 
541  StreamingBufferSegment seg;
542  TcpStream *stream = direction == 0 ? &ssn->client : &ssn->server;
543  int r = StreamingBufferAppend(&stream->sb, &stream_config.sbcnf, &seg, data, data_len);
544  FAIL_IF_NOT(r == 0);
545  stream->last_ack += data_len;
546  return 1;
547 }
548 
550  uint32_t ts_isn,
551  uint32_t tc_isn)
552 {
553  FAIL_IF_NULL(f);
554 
555  TcpSession *ssn = SCCalloc(1, sizeof(*ssn));
556  FAIL_IF_NULL(ssn);
557 
559  ssn->client.sb = x;
560  ssn->server.sb = x;
561 
562  ssn->client.isn = ts_isn;
563  ssn->server.isn = tc_isn;
564 
565  f->protoctx = ssn;
566  return 1;
567 }
568 
570 {
571  FAIL_IF_NULL(f);
572  FAIL_IF_NOT(f->proto == IPPROTO_TCP);
573  TcpSession *ssn = f->protoctx;
574  FAIL_IF_NULL(ssn);
576  SCFree(ssn);
577  f->protoctx = NULL;
578  return 1;
579 }
580 
581 /**
582  * \brief UTHGenericTest: function that perfom a generic check taking care of
583  * as maximum common unittest elements as possible.
584  * It will create a detection engine, append an array
585  * of signatures an check the spected results for each
586  * of them, it check matches for an array of packets
587  *
588  * \param pkt pointer to the array of packets
589  * \param numpkts number of packets to match
590  * \param sigs array of char* pointing to signatures to load
591  * \param numsigs number of signatures to load and check
592  * \param results pointer to arrays of numbers, each of them foreach packet
593  * to check if sids matches that packet as expected with
594  * that number of times or not. The size of results should be
595  * numpkts * numsigs * sizeof(uint16_t *)
596  *
597  * Example:
598  * result[1][3] would mean the number of times the pkt[1]
599  * match the sid[3]
600  *
601  * \retval int 1 if the match of all the sids is the specified has the
602  * specified results; 0 if not
603  */
604 int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
605 {
606 
607  int result = 0;
608  if (pkt == NULL || sigs == NULL || numpkts == 0
609  || sids == NULL || results == NULL || numsigs == 0) {
610  SCLogError("Arguments invalid, that the pointer/arrays are not NULL, and the number of "
611  "signatures and packets is > 0");
612  goto end;
613  }
615  if (de_ctx == NULL) {
616  goto end;
617  }
618  de_ctx->flags |= DE_QUIET;
619 
620  if (UTHAppendSigs(de_ctx, sigs, numsigs) == 0)
621  goto cleanup;
622 
623  result = UTHMatchPacketsWithResults(de_ctx, pkt, numpkts, sids, results, numsigs);
624 
625 cleanup:
627 end:
628  return result;
629 }
630 
631 /**
632  * \brief UTHCheckPacketMatches: function to check if a packet match some sids
633  *
634  *
635  * \param p pointer to the Packet
636  * \param sigs array of char* pointing to signatures to load
637  * \param numsigs number of signatures to load from the array
638  * \param results pointer to an array of numbers to check if sids matches
639  * that number of times or not.
640  *
641  * \retval int 1 if the match of all the sids is the specified has the
642  * specified results; 0 if not
643  */
644 int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[],
645  uint32_t results[], int numsids)
646 {
647  if (p == NULL || sids == NULL) {
648  SCLogError("Arguments invalid, check if the "
649  "packet is NULL, and if the array contain sids is set");
650  return 0;
651  }
652 
653  int i = 0;
654  int res = 1;
655  for (; i < numsids; i++) {
656  uint32_t r = PacketAlertCheck(p, sids[i]);
657  if (r != results[i]) {
658  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, and not %" PRIu32 " as expected",
659  sids[i], r, results[i]);
660  res = 0;
661  } else {
662  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, as expected", sids[i], r);
663  }
664  }
665  return res;
666 }
667 
668 /**
669  * \brief UTHAppendSigs: Add sigs to the detection_engine checking for errors
670  *
671  * \param de_ctx pointer to the DetectEngineCtx used
672  * \param sigs array of char* pointing to signatures to load
673  * \param numsigs number of signatures to load from the array
674  * (size of the array)
675  *
676  * \retval int 0 if we have errors; 1 if all the signatures loaded succesfuly
677  */
678 int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
679 {
680  BUG_ON(de_ctx == NULL);
681  BUG_ON(numsigs <= 0);
682  BUG_ON(sigs == NULL);
683 
684  for (int i = 0; i < numsigs; i++) {
685  if (sigs[i] == NULL) {
686  SCLogError("Check the signature"
687  " at position %d",
688  i);
689  return 0;
690  }
691  Signature *s = DetectEngineAppendSig(de_ctx, sigs[i]);
692  if (s == NULL) {
693  SCLogError("Check the signature at"
694  " position %d (%s)",
695  i, sigs[i]);
696  return 0;
697  }
698  }
699  return 1;
700 }
701 
702 /**
703  * \test UTHMatchPacketsWithResults Match a packet or a array of packets against sigs
704  * of a de_ctx, checking that each signature match match X times for certain packets
705  *
706  * \param de_ctx pointer with the signatures loaded
707  * \param p pointer to the array of packets
708  * \param num_packets number of packets in the array
709  *
710  * \retval return 1 if all goes well
711  * \retval return 0 if something fail
712  */
713 int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
714 {
715  BUG_ON(de_ctx == NULL);
716  BUG_ON(p == NULL);
717 
718  int result = 0;
720  ThreadVars th_v;
721  DetectEngineThreadCtx *det_ctx = NULL;
722  memset(&dtv, 0, sizeof(DecodeThreadVars));
723  memset(&th_v, 0, sizeof(th_v));
724 
726  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
727 
728  for (int i = 0; i < num_packets; i++) {
729  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
730  if (UTHCheckPacketMatchResults(p[i], sids, &results[(i * numsigs)], numsigs) == 0)
731  goto cleanup;
732  }
733 
734  result = 1;
735 cleanup:
736  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
737  return result;
738 }
739 
740 /**
741  * \test UTHMatchPackets Match a packet or a array of packets against sigs
742  * of a de_ctx, but note that the return value doesn't mean that we have a
743  * match, we have to check it later with PacketAlertCheck()
744  *
745  * \param de_ctx pointer with the signatures loaded
746  * \param p pointer to the array of packets
747  * \param num_packets number of packets in the array
748  *
749  * \retval return 1 if all goes well
750  * \retval return 0 if something fail
751  */
752 int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
753 {
754  BUG_ON(de_ctx == NULL);
755  BUG_ON(p == NULL);
756  int result = 1;
758  ThreadVars th_v;
759  DetectEngineThreadCtx *det_ctx = NULL;
760  memset(&dtv, 0, sizeof(DecodeThreadVars));
761  memset(&th_v, 0, sizeof(th_v));
766  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
767 
768  for (int i = 0; i < num_packets; i++)
769  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
770 
771  /* Here we don't check if the packet matched or not, because
772  * the de_ctx can have multiple signatures, and some of them may match
773  * and others may not. That check will be outside
774  */
775  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
776  if (de_ctx != NULL) SigGroupCleanup(de_ctx);
777  return result;
778 }
779 
780 /**
781  * \test Test if a packet match a signature given as string and a mpm_type
782  * Hint: Useful for unittests with only one packet and one signature
783  *
784  * \param sig pointer to the string signature to test
785  * \param sid sid number of the signature
786  *
787  * \retval return 1 if match
788  * \retval return 0 if not
789  */
790 int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
791 {
792  SCEnter();
793 
794  int result = 0;
795 
797  ThreadVars th_v;
798  DetectEngineThreadCtx *det_ctx = NULL;
799 
800  memset(&dtv, 0, sizeof(DecodeThreadVars));
801  memset(&th_v, 0, sizeof(th_v));
802 
804  if (de_ctx == NULL) {
805  printf("de_ctx == NULL: ");
806  goto end;
807  }
808 
809  de_ctx->flags |= DE_QUIET;
810  de_ctx->mpm_matcher = mpm_type;
811 
812  de_ctx->sig_list = SigInit(de_ctx, sig);
813  if (de_ctx->sig_list == NULL) {
814  printf("signature == NULL: ");
815  goto end;
816  }
817 
819  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
820 
821  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
822  if (PacketAlertCheck(p, de_ctx->sig_list->id) != 1) {
823  printf("signature didn't alert: ");
824  goto end;
825  }
826 
827  result = 1;
828 end:
829  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
831  SCReturnInt(result);
832 }
833 
834 /**
835  * \test Test if a packet match a signature given as string
836  * Hint: Useful for unittests with only one packet and one signature
837  *
838  * \param sig pointer to the string signature to test
839  * \param sid sid number of the signature
840  *
841  * \retval return 1 if match
842  * \retval return 0 if not
843  */
844 int UTHPacketMatchSig(Packet *p, const char *sig)
845 {
846  int result = 1;
847 
849 
850  ThreadVars th_v;
851  DetectEngineThreadCtx *det_ctx = NULL;
852 
853  memset(&dtv, 0, sizeof(DecodeThreadVars));
854  memset(&th_v, 0, sizeof(th_v));
855 
857  if (de_ctx == NULL) {
858  result=0;
859  goto end;
860  }
861 
862  de_ctx->flags |= DE_QUIET;
863 
864  de_ctx->sig_list = SigInit(de_ctx, sig);
865  if (de_ctx->sig_list == NULL) {
866  result = 0;
867  goto end;
868  }
869 
871  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
872 
873  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
874  if (PacketAlertCheck(p, de_ctx->sig_list->id) != 1) {
875  result = 0;
876  goto end;
877  }
878 
879 end:
880  if (de_ctx) {
883  }
884 
885  if (det_ctx != NULL)
886  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
887  if (de_ctx != NULL)
889 
890  return result;
891 }
892 
893 uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
894 {
895  FlowLookupStruct fls;
896  memset(&fls, 0, sizeof(fls));
897 
898  uint32_t i = start;
899  uint8_t payload[] = "Payload";
900  for (; i < end; i++) {
901  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
902  if (dir == 0) {
903  p->src.addr_data32[0] = i;
904  p->dst.addr_data32[0] = i + 1;
905  } else {
906  p->src.addr_data32[0] = i + 1;
907  p->dst.addr_data32[0] = i;
908  }
909  FlowHandlePacket(NULL, &fls, p);
910  if (p->flow != NULL) {
911  FLOWLOCK_UNLOCK(p->flow);
912  }
913 
914  /* Now the queues shoul be updated */
915  UTHFreePacket(p);
916  }
917 
918  Flow *f;
919  while ((f = FlowQueuePrivateGetFromTop(&fls.spare_queue))) {
920  FlowFree(f);
921  }
922  while ((f = FlowQueuePrivateGetFromTop(&fls.work_queue))) {
923  FlowFree(f);
924  }
925 
926  return i;
927 }
928 
929 /** \brief parser a sig and see if the expected result is correct */
930 int UTHParseSignature(const char *str, bool expect)
931 {
934  de_ctx->flags |= DE_QUIET;
935 
937  if (expect)
938  FAIL_IF_NULL(s);
939  else
940  FAIL_IF_NOT_NULL(s);
941 
943  PASS;
944 }
945 
946 /*
947  * unittests for the unittest helpers
948  */
949 
950 /**
951  * \brief CheckUTHTestPacket wrapper to check packets for unittests
952  */
953 static int CheckUTHTestPacket(Packet *p, uint8_t ipproto)
954 {
955  uint16_t sport = 41424;
956  uint16_t dport = 80;
957  uint8_t payload[] = "Payload";
958 
959  uint8_t len = sizeof(payload);
960 
961  if (p == NULL)
962  return 0;
963 
964  if (p->payload_len != len)
965  return 0;
966 
967  if (strncmp((char *)payload, (char *)p->payload, len) != 0)
968  return 0;
969 
970  if (p->src.family != AF_INET)
971  return 0;
972  if (p->dst.family != AF_INET)
973  return 0;
974  if (p->proto != ipproto)
975  return 0;
976 
977  switch(ipproto) {
978  case IPPROTO_UDP:
979  if (p->udph == NULL)
980  return 0;
981  if (p->udph->uh_sport != sport)
982  return 0;
983  if (p->udph->uh_dport != dport)
984  return 0;
985  break;
986  case IPPROTO_TCP:
987  if (p->tcph == NULL)
988  return 0;
989  if (SCNtohs(p->tcph->th_sport) != sport)
990  return 0;
991  if (SCNtohs(p->tcph->th_dport) != dport)
992  return 0;
993  break;
994  }
995  return 1;
996 }
997 
998 #ifdef HAVE_MEMMEM
999 #include <string.h>
1000 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
1001  return memmem(big, big_len, little, little_len);
1002 }
1003 #else
1004 #include "util-spm-bs.h"
1005 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
1006  return BasicSearch(big, big_len, little, little_len);
1007 }
1008 #endif //HAVE_MEMMEM
1009 
1010 /**
1011  * \brief UTHBuildPacketRealTest01 wrapper to check packets for unittests
1012  */
1013 static int UTHBuildPacketRealTest01(void)
1014 {
1015  uint8_t payload[] = "Payload";
1016 
1017  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_TCP,
1018  "192.168.1.5", "192.168.1.1", 41424, 80);
1019 
1020  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1021  UTHFreePacket(p);
1022 
1023  return ret;
1024 }
1025 
1026 /**
1027  * \brief UTHBuildPacketRealTest02 wrapper to check packets for unittests
1028  */
1029 static int UTHBuildPacketRealTest02(void)
1030 {
1031  uint8_t payload[] = "Payload";
1032 
1033  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_UDP,
1034  "192.168.1.5", "192.168.1.1", 41424, 80);
1035 
1036  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1037  UTHFreePacket(p);
1038  return ret;
1039 }
1040 
1041 /**
1042  * \brief UTHBuildPacketTest01 wrapper to check packets for unittests
1043  */
1044 static int UTHBuildPacketTest01(void)
1045 {
1046  uint8_t payload[] = "Payload";
1047 
1048  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
1049 
1050  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1051  UTHFreePacket(p);
1052 
1053  return ret;
1054 }
1055 
1056 /**
1057  * \brief UTHBuildPacketTest02 wrapper to check packets for unittests
1058  */
1059 static int UTHBuildPacketTest02(void)
1060 {
1061  uint8_t payload[] = "Payload";
1062 
1063  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_UDP);
1064 
1065  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1066  UTHFreePacket(p);
1067 
1068  return ret;
1069 }
1070 
1071 /**
1072  * \brief UTHBuildPacketOfFlowsTest01 wrapper to check packets for unittests
1073  */
1074 static int UTHBuildPacketOfFlowsTest01(void)
1075 {
1076  int result = 0;
1077 
1079  uint32_t flow_spare_q_len = FlowSpareGetPoolSize();
1080 
1081  UTHBuildPacketOfFlows(0, 100, 0);
1082 
1083  if (FlowSpareGetPoolSize() != flow_spare_q_len - 100)
1084  result = 0;
1085  else
1086  result = 1;
1087  FlowShutdown();
1088 
1089  return result;
1090 }
1091 
1092 
1093 /**
1094  * \brief UTHBuildPacketSrcDstTest01 wrapper to check packets for unittests
1095  */
1096 static int UTHBuildPacketSrcDstTest01(void)
1097 {
1098  uint8_t payload[] = "Payload";
1099 
1100  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_TCP,
1101  "192.168.1.5", "192.168.1.1");
1102 
1103  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1104  UTHFreePacket(p);
1105 
1106  return ret;
1107 }
1108 
1109 /**
1110  * \brief UTHBuildPacketSrcDstTest02 wrapper to check packets for unittests
1111  */
1112 static int UTHBuildPacketSrcDstTest02(void)
1113 {
1114  uint8_t payload[] = "Payload";
1115 
1116  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_UDP,
1117  "192.168.1.5", "192.168.1.1");
1118 
1119  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1120  UTHFreePacket(p);
1121 
1122  return ret;
1123 }
1124 
1125 /**
1126  * \brief UTHBuildPacketSrcDstPortsTest01 wrapper to check packets for unittests
1127  */
1128 static int UTHBuildPacketSrcDstPortsTest01(void)
1129 {
1130  uint8_t payload[] = "Payload";
1131 
1132  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_TCP,
1133  41424, 80);
1134 
1135  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1136  UTHFreePacket(p);
1137 
1138  return ret;
1139 }
1140 
1141 /**
1142  * \brief UTHBuildPacketSrcDstPortsTest02 wrapper to check packets for unittests
1143  */
1144 static int UTHBuildPacketSrcDstPortsTest02(void)
1145 {
1146  uint8_t payload[] = "Payload";
1147 
1148  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_UDP,
1149  41424, 80);
1150 
1151  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1152  UTHFreePacket(p);
1153 
1154  return ret;
1155 }
1156 
1157 #endif /* UNITTESTS */
1158 
1160 {
1161 #ifdef UNITTESTS
1162  UtRegisterTest("UTHBuildPacketRealTest01", UTHBuildPacketRealTest01);
1163  UtRegisterTest("UTHBuildPacketRealTest02", UTHBuildPacketRealTest02);
1164  UtRegisterTest("UTHBuildPacketTest01", UTHBuildPacketTest01);
1165  UtRegisterTest("UTHBuildPacketTest02", UTHBuildPacketTest02);
1166  UtRegisterTest("UTHBuildPacketSrcDstTest01", UTHBuildPacketSrcDstTest01);
1167  UtRegisterTest("UTHBuildPacketSrcDstTest02", UTHBuildPacketSrcDstTest02);
1168  UtRegisterTest("UTHBuildPacketSrcDstPortsTest01",
1169  UTHBuildPacketSrcDstPortsTest01);
1170  UtRegisterTest("UTHBuildPacketSrcDstPortsTest02",
1171  UTHBuildPacketSrcDstPortsTest02);
1172  UtRegisterTest("UTHBuildPacketOfFlowsTest01", UTHBuildPacketOfFlowsTest01);
1173 
1174 #endif /* UNITTESTS */
1175 }
1176 
FlowLookupStruct_::work_queue
FlowQueuePrivate work_queue
Definition: flow.h:547
Packet_::proto
uint8_t proto
Definition: decode.h:450
UTHParseSignature
int UTHParseSignature(const char *str, bool expect)
parser a sig and see if the expected result is correct
Definition: util-unittest-helper.c:930
TcpStream_
Definition: stream-tcp-private.h:106
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:1005
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
TcpStream_::isn
uint32_t isn
Definition: stream-tcp-private.h:113
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1003
UTHAddStreamToFlow
int UTHAddStreamToFlow(Flow *f, int direction, uint8_t *data, uint32_t data_len)
Definition: util-unittest-helper.c:533
FlowSpareGetPoolSize
uint32_t FlowSpareGetPoolSize(void)
Definition: flow-spare-pool.c:47
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
Flow_::proto
uint8_t proto
Definition: flow.h:379
Packet_::payload
uint8_t * payload
Definition: decode.h:573
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:141
Packet_::flags
uint32_t flags
Definition: decode.h:463
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:357
results
struct DetectRfbSecresult_ results[]
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:418
UTHSetIPv4Address
uint32_t UTHSetIPv4Address(const char *str)
return the uint32_t for a ipv4 address string
Definition: util-unittest-helper.c:134
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
UTHPacketMatchSigMpm
int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
Definition: util-unittest-helper.c:790
TcpStreamCnf_::sbcnf
StreamingBufferConfig sbcnf
Definition: stream-tcp.h:72
FlowLookupStruct_
Definition: flow.h:543
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2442
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:250
SCSigSignatureOrderingModuleCleanup
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
Definition: detect-engine-sigorder.c:815
DE_QUIET
#define DE_QUIET
Definition: detect.h:287
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:844
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
Definition: util-streaming-buffer.c:867
FlowHandlePacket
void FlowHandlePacket(ThreadVars *tv, FlowLookupStruct *fls, Packet *p)
Entry point for packet flow handling.
Definition: flow.c:527
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:337
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1809
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:454
SigCleanSignatures
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:46
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2434
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:115
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:244
Flow_::protoctx
void * protoctx
Definition: flow.h:447
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:97
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:574
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
UTHBuildPacketArrayFromEth
Packet ** UTHBuildPacketArrayFromEth(uint8_t *raw_eth[], int *pktsize, int numpkts)
UTHBuildPacketArrayFromEth is a wrapper that build a packets from an array of packets in ethernet raw...
Definition: util-unittest-helper.c:356
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:272
UTHBuildPacketOfFlows
uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
Definition: util-unittest-helper.c:893
IPV4Hdr
struct IPV4Hdr_ IPV4Hdr
UTHAssignFlow
void UTHAssignFlow(Packet *p, Flow *f)
Definition: util-unittest-helper.c:513
TcpStream_::last_ack
uint32_t last_ack
Definition: stream-tcp-private.h:115
flow-spare-pool.h
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:543
SET_PKT_LEN
#define SET_PKT_LEN(p, len)
Definition: decode.h:223
UTHMatchPackets
int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
Definition: util-unittest-helper.c:752
UTHBuildFlow
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:521
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:40
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:158
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:17
UTHMatchPacketsWithResults
int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
Definition: util-unittest-helper.c:713
DetectEngineThreadCtx_
Definition: detect.h:1025
Packet_::ts
SCTime_t ts
Definition: decode.h:471
SCSigOrderSignatures
void SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
Definition: detect-engine-sigorder.c:728
UTHRegisterTests
void UTHRegisterTests(void)
Definition: util-unittest-helper.c:1159
UTHAddSessionToFlow
int UTHAddSessionToFlow(Flow *f, uint32_t ts_isn, uint32_t tc_isn)
Definition: util-unittest-helper.c:549
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:219
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
UTHCheckPacketMatchResults
int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[], uint32_t results[], int numsids)
UTHCheckPacketMatches: function to check if a packet match some sids.
Definition: util-unittest-helper.c:644
Packet_::sp
Port sp
Definition: decode.h:435
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:795
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:835
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
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2129
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:289
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2019
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:48
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:428
detect-engine-build.h
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
stream-tcp-private.h
detect-engine-alert.h
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:531
Port
uint16_t Port
Definition: decode.h:229
STREAMING_BUFFER_INITIALIZER
#define STREAMING_BUFFER_INITIALIZER
Definition: util-streaming-buffer.h:138
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1951
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
UTHFreeFlow
void UTHFreeFlow(Flow *flow)
Definition: util-unittest-helper.c:526
StreamingBuffer_
Definition: util-streaming-buffer.h:109
IPV4Hdr_
Definition: decode-ipv4.h:72
FlowLookupStruct_::spare_queue
FlowQueuePrivate spare_queue
Definition: flow.h:545
Packet_::flow
struct Flow_ * flow
Definition: decode.h:465
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3153
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:403
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:83
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3367
FLOW_IPV6
#define FLOW_IPV6
Definition: flow.h:99
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:436
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:553
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:689
TcpStream_::sb
StreamingBuffer sb
Definition: stream-tcp-private.h:134
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:791
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:285
UTHGenericTest
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perfom a generic check taking care of as maximum common unittest elemen...
Definition: util-unittest-helper.c:604
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:173
detect-engine-sigorder.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:284
str
#define str(s)
Definition: suricata-common.h:280
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:664
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:485
Signature_::id
uint32_t id
Definition: detect.h:574
StreamTcpSessionCleanup
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition: stream-tcp.c:211
detect-parse.h
src
uint16_t src
Definition: app-layer-dnp3.h:5
Signature_
Signature container.
Definition: detect.h:540
payload_len
uint16_t payload_len
Definition: stream-tcp-private.h:1
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:394
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2403
Packet_::udph
UDPHdr * udph
Definition: decode.h:555
Address_::family
char family
Definition: decode.h:115
Packet_::dst
Address dst
Definition: decode.h:433
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:41
TestHelperBuildFlow
Flow * TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:52
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:786
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:678
Packet_::ip6h
IPV6Hdr * ip6h
Definition: decode.h:533
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
UTHRemoveSessionFromFlow
int UTHRemoveSessionFromFlow(Flow *f)
Definition: util-unittest-helper.c:569
TcpSession_
Definition: stream-tcp-private.h:272
Packet_::dp
Port dp
Definition: decode.h:443
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DecodeEthernet
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-ethernet.c:42
Packet_::src
Address src
Definition: decode.h:432
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:468