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 payload 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 payload 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 payload 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 payload 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 payload 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 perform 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 expected 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[], uint32_t results[], int numsigs)
645 {
646  if (p == NULL || sids == NULL) {
647  SCLogError("Arguments invalid, check if the "
648  "packet is NULL, and if the array contain sids is set");
649  return 0;
650  }
651 
652  int i = 0;
653  int res = 1;
654  for (; i < numsigs; i++) {
655  uint32_t r = PacketAlertCheck(p, sids[i]);
656  if (r != results[i]) {
657  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, and not %" PRIu32 " as expected",
658  sids[i], r, results[i]);
659  res = 0;
660  } else {
661  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, as expected", sids[i], r);
662  }
663  }
664  return res;
665 }
666 
667 /**
668  * \brief UTHAppendSigs: Add sigs to the detection_engine checking for errors
669  *
670  * \param de_ctx pointer to the DetectEngineCtx used
671  * \param sigs array of char* pointing to signatures to load
672  * \param numsigs number of signatures to load from the array
673  * (size of the array)
674  *
675  * \retval int 0 if we have errors; 1 if all the signatures loaded successfully
676  */
677 int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
678 {
679  BUG_ON(de_ctx == NULL);
680  BUG_ON(numsigs <= 0);
681  BUG_ON(sigs == NULL);
682 
683  for (int i = 0; i < numsigs; i++) {
684  if (sigs[i] == NULL) {
685  SCLogError("Check the signature"
686  " at position %d",
687  i);
688  return 0;
689  }
690  Signature *s = DetectEngineAppendSig(de_ctx, sigs[i]);
691  if (s == NULL) {
692  SCLogError("Check the signature at"
693  " position %d (%s)",
694  i, sigs[i]);
695  return 0;
696  }
697  }
698  return 1;
699 }
700 
701 /**
702  * \test UTHMatchPacketsWithResults Match a packet or a array of packets against sigs
703  * of a de_ctx, checking that each signature matches X times for certain packets
704  *
705  * \param de_ctx pointer with the signatures loaded
706  * \param p pointer to the array of packets
707  * \param num_packets number of packets in the array
708  *
709  * \retval return 1 if all goes well
710  * \retval return 0 if something fail
711  */
712 int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
713 {
714  BUG_ON(de_ctx == NULL);
715  BUG_ON(p == NULL);
716 
717  int result = 0;
719  ThreadVars th_v;
720  DetectEngineThreadCtx *det_ctx = NULL;
721  memset(&dtv, 0, sizeof(DecodeThreadVars));
722  memset(&th_v, 0, sizeof(th_v));
723 
725  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
726 
727  for (int i = 0; i < num_packets; i++) {
728  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
729  if (UTHCheckPacketMatchResults(p[i], sids, &results[(i * numsigs)], numsigs) == 0)
730  goto cleanup;
731  }
732 
733  result = 1;
734 cleanup:
735  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
736  return result;
737 }
738 
739 /**
740  * \test UTHMatchPackets Match a packet or a array of packets against sigs
741  * of a de_ctx, but note that the return value doesn't mean that we have a
742  * match, we have to check it later with PacketAlertCheck()
743  *
744  * \param de_ctx pointer with the signatures loaded
745  * \param p pointer to the array of packets
746  * \param num_packets number of packets in the array
747  *
748  * \retval return 1 if all goes well
749  * \retval return 0 if something fail
750  */
751 int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
752 {
753  BUG_ON(de_ctx == NULL);
754  BUG_ON(p == NULL);
755  int result = 1;
757  ThreadVars th_v;
758  DetectEngineThreadCtx *det_ctx = NULL;
759  memset(&dtv, 0, sizeof(DecodeThreadVars));
760  memset(&th_v, 0, sizeof(th_v));
765  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
766 
767  for (int i = 0; i < num_packets; i++)
768  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
769 
770  /* Here we don't check if the packet matched or not, because
771  * the de_ctx can have multiple signatures, and some of them may match
772  * and others may not. That check will be outside
773  */
774  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
775  if (de_ctx != NULL) SigGroupCleanup(de_ctx);
776  return result;
777 }
778 
779 /**
780  * \test Test if a packet match a signature given as string and a mpm_type
781  * Hint: Useful for unittests with only one packet and one signature
782  *
783  * \param sig pointer to the string signature to test
784  * \param sid sid number of the signature
785  *
786  * \retval return 1 if match
787  * \retval return 0 if not
788  */
789 int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
790 {
791  SCEnter();
792 
793  int result = 0;
794 
796  ThreadVars th_v;
797  DetectEngineThreadCtx *det_ctx = NULL;
798 
799  memset(&dtv, 0, sizeof(DecodeThreadVars));
800  memset(&th_v, 0, sizeof(th_v));
801 
803  if (de_ctx == NULL) {
804  printf("de_ctx == NULL: ");
805  goto end;
806  }
807 
808  de_ctx->flags |= DE_QUIET;
809  de_ctx->mpm_matcher = mpm_type;
810 
811  de_ctx->sig_list = SigInit(de_ctx, sig);
812  if (de_ctx->sig_list == NULL) {
813  printf("signature == NULL: ");
814  goto end;
815  }
816 
818  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
819 
820  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
821  if (PacketAlertCheck(p, de_ctx->sig_list->id) != 1) {
822  printf("signature didn't alert: ");
823  goto end;
824  }
825 
826  result = 1;
827 end:
828  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
830  SCReturnInt(result);
831 }
832 
833 /**
834  * \test Test if a packet match a signature given as string
835  * Hint: Useful for unittests with only one packet and one signature
836  *
837  * \param sig pointer to the string signature to test
838  * \param sid sid number of the signature
839  *
840  * \retval return 1 if match
841  * \retval return 0 if not
842  */
843 int UTHPacketMatchSig(Packet *p, const char *sig)
844 {
845  int result = 1;
846 
848 
849  ThreadVars th_v;
850  DetectEngineThreadCtx *det_ctx = NULL;
851 
852  memset(&dtv, 0, sizeof(DecodeThreadVars));
853  memset(&th_v, 0, sizeof(th_v));
854 
856  if (de_ctx == NULL) {
857  result=0;
858  goto end;
859  }
860 
861  de_ctx->flags |= DE_QUIET;
862 
863  de_ctx->sig_list = SigInit(de_ctx, sig);
864  if (de_ctx->sig_list == NULL) {
865  result = 0;
866  goto end;
867  }
868 
870  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
871 
872  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
873  if (PacketAlertCheck(p, de_ctx->sig_list->id) != 1) {
874  result = 0;
875  goto end;
876  }
877 
878 end:
879  if (de_ctx) {
882  }
883 
884  if (det_ctx != NULL)
885  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
886  if (de_ctx != NULL)
888 
889  return result;
890 }
891 
892 uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
893 {
894  FlowLookupStruct fls;
895  memset(&fls, 0, sizeof(fls));
896 
897  uint32_t i = start;
898  uint8_t payload[] = "Payload";
899  for (; i < end; i++) {
900  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
901  if (dir == 0) {
902  p->src.addr_data32[0] = i;
903  p->dst.addr_data32[0] = i + 1;
904  } else {
905  p->src.addr_data32[0] = i + 1;
906  p->dst.addr_data32[0] = i;
907  }
908  FlowHandlePacket(NULL, &fls, p);
909  if (p->flow != NULL) {
910  FLOWLOCK_UNLOCK(p->flow);
911  }
912 
913  /* Now the queues should be updated */
914  UTHFreePacket(p);
915  }
916 
917  Flow *f;
918  while ((f = FlowQueuePrivateGetFromTop(&fls.spare_queue))) {
919  FlowFree(f);
920  }
921  while ((f = FlowQueuePrivateGetFromTop(&fls.work_queue))) {
922  FlowFree(f);
923  }
924 
925  return i;
926 }
927 
928 /** \brief parser a sig and see if the expected result is correct */
929 int UTHParseSignature(const char *str, bool expect)
930 {
933  de_ctx->flags |= DE_QUIET;
934 
936  if (expect)
937  FAIL_IF_NULL(s);
938  else
939  FAIL_IF_NOT_NULL(s);
940 
942  PASS;
943 }
944 
945 /*
946  * unittests for the unittest helpers
947  */
948 
949 /**
950  * \brief CheckUTHTestPacket wrapper to check packets for unittests
951  */
952 static int CheckUTHTestPacket(Packet *p, uint8_t ipproto)
953 {
954  uint16_t sport = 41424;
955  uint16_t dport = 80;
956  uint8_t payload[] = "Payload";
957 
958  uint8_t len = sizeof(payload);
959 
960  if (p == NULL)
961  return 0;
962 
963  if (p->payload_len != len)
964  return 0;
965 
966  if (strncmp((char *)payload, (char *)p->payload, len) != 0)
967  return 0;
968 
969  if (p->src.family != AF_INET)
970  return 0;
971  if (p->dst.family != AF_INET)
972  return 0;
973  if (p->proto != ipproto)
974  return 0;
975 
976  switch(ipproto) {
977  case IPPROTO_UDP:
978  if (p->udph == NULL)
979  return 0;
980  if (p->udph->uh_sport != sport)
981  return 0;
982  if (p->udph->uh_dport != dport)
983  return 0;
984  break;
985  case IPPROTO_TCP:
986  if (p->tcph == NULL)
987  return 0;
988  if (SCNtohs(p->tcph->th_sport) != sport)
989  return 0;
990  if (SCNtohs(p->tcph->th_dport) != dport)
991  return 0;
992  break;
993  }
994  return 1;
995 }
996 
997 #ifdef HAVE_MEMMEM
998 #include <string.h>
999 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
1000  return memmem(big, big_len, little, little_len);
1001 }
1002 #else
1003 #include "util-spm-bs.h"
1004 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
1005  return BasicSearch(big, big_len, little, little_len);
1006 }
1007 #endif //HAVE_MEMMEM
1008 
1009 /**
1010  * \brief UTHBuildPacketRealTest01 wrapper to check packets for unittests
1011  */
1012 static int UTHBuildPacketRealTest01(void)
1013 {
1014  uint8_t payload[] = "Payload";
1015 
1016  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_TCP,
1017  "192.168.1.5", "192.168.1.1", 41424, 80);
1018 
1019  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1020  UTHFreePacket(p);
1021 
1022  return ret;
1023 }
1024 
1025 /**
1026  * \brief UTHBuildPacketRealTest02 wrapper to check packets for unittests
1027  */
1028 static int UTHBuildPacketRealTest02(void)
1029 {
1030  uint8_t payload[] = "Payload";
1031 
1032  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_UDP,
1033  "192.168.1.5", "192.168.1.1", 41424, 80);
1034 
1035  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1036  UTHFreePacket(p);
1037  return ret;
1038 }
1039 
1040 /**
1041  * \brief UTHBuildPacketTest01 wrapper to check packets for unittests
1042  */
1043 static int UTHBuildPacketTest01(void)
1044 {
1045  uint8_t payload[] = "Payload";
1046 
1047  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
1048 
1049  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1050  UTHFreePacket(p);
1051 
1052  return ret;
1053 }
1054 
1055 /**
1056  * \brief UTHBuildPacketTest02 wrapper to check packets for unittests
1057  */
1058 static int UTHBuildPacketTest02(void)
1059 {
1060  uint8_t payload[] = "Payload";
1061 
1062  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_UDP);
1063 
1064  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1065  UTHFreePacket(p);
1066 
1067  return ret;
1068 }
1069 
1070 /**
1071  * \brief UTHBuildPacketOfFlowsTest01 wrapper to check packets for unittests
1072  */
1073 static int UTHBuildPacketOfFlowsTest01(void)
1074 {
1075  int result = 0;
1076 
1078  uint32_t flow_spare_q_len = FlowSpareGetPoolSize();
1079 
1080  UTHBuildPacketOfFlows(0, 100, 0);
1081 
1082  if (FlowSpareGetPoolSize() != flow_spare_q_len - 100)
1083  result = 0;
1084  else
1085  result = 1;
1086  FlowShutdown();
1087 
1088  return result;
1089 }
1090 
1091 
1092 /**
1093  * \brief UTHBuildPacketSrcDstTest01 wrapper to check packets for unittests
1094  */
1095 static int UTHBuildPacketSrcDstTest01(void)
1096 {
1097  uint8_t payload[] = "Payload";
1098 
1099  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_TCP,
1100  "192.168.1.5", "192.168.1.1");
1101 
1102  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1103  UTHFreePacket(p);
1104 
1105  return ret;
1106 }
1107 
1108 /**
1109  * \brief UTHBuildPacketSrcDstTest02 wrapper to check packets for unittests
1110  */
1111 static int UTHBuildPacketSrcDstTest02(void)
1112 {
1113  uint8_t payload[] = "Payload";
1114 
1115  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_UDP,
1116  "192.168.1.5", "192.168.1.1");
1117 
1118  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1119  UTHFreePacket(p);
1120 
1121  return ret;
1122 }
1123 
1124 /**
1125  * \brief UTHBuildPacketSrcDstPortsTest01 wrapper to check packets for unittests
1126  */
1127 static int UTHBuildPacketSrcDstPortsTest01(void)
1128 {
1129  uint8_t payload[] = "Payload";
1130 
1131  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_TCP,
1132  41424, 80);
1133 
1134  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1135  UTHFreePacket(p);
1136 
1137  return ret;
1138 }
1139 
1140 /**
1141  * \brief UTHBuildPacketSrcDstPortsTest02 wrapper to check packets for unittests
1142  */
1143 static int UTHBuildPacketSrcDstPortsTest02(void)
1144 {
1145  uint8_t payload[] = "Payload";
1146 
1147  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_UDP,
1148  41424, 80);
1149 
1150  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1151  UTHFreePacket(p);
1152 
1153  return ret;
1154 }
1155 
1156 #endif /* UNITTESTS */
1157 
1159 {
1160 #ifdef UNITTESTS
1161  UtRegisterTest("UTHBuildPacketRealTest01", UTHBuildPacketRealTest01);
1162  UtRegisterTest("UTHBuildPacketRealTest02", UTHBuildPacketRealTest02);
1163  UtRegisterTest("UTHBuildPacketTest01", UTHBuildPacketTest01);
1164  UtRegisterTest("UTHBuildPacketTest02", UTHBuildPacketTest02);
1165  UtRegisterTest("UTHBuildPacketSrcDstTest01", UTHBuildPacketSrcDstTest01);
1166  UtRegisterTest("UTHBuildPacketSrcDstTest02", UTHBuildPacketSrcDstTest02);
1167  UtRegisterTest("UTHBuildPacketSrcDstPortsTest01",
1168  UTHBuildPacketSrcDstPortsTest01);
1169  UtRegisterTest("UTHBuildPacketSrcDstPortsTest02",
1170  UTHBuildPacketSrcDstPortsTest02);
1171  UtRegisterTest("UTHBuildPacketOfFlowsTest01", UTHBuildPacketOfFlowsTest01);
1172 
1173 #endif /* UNITTESTS */
1174 }
1175 
FlowLookupStruct_::work_queue
FlowQueuePrivate work_queue
Definition: flow.h:533
Packet_::proto
uint8_t proto
Definition: decode.h:451
UTHParseSignature
int UTHParseSignature(const char *str, bool expect)
parser a sig and see if the expected result is correct
Definition: util-unittest-helper.c:929
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:1004
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:1007
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:365
Packet_::payload
uint8_t * payload
Definition: decode.h:574
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:464
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:343
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:801
UTHPacketMatchSigMpm
int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
Definition: util-unittest-helper.c:789
TcpStreamCnf_::sbcnf
StreamingBufferConfig sbcnf
Definition: stream-tcp.h:76
FlowLookupStruct_
Definition: flow.h:529
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2588
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:827
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:644
DE_QUIET
#define DE_QUIET
Definition: detect.h:290
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:843
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
Definition: util-streaming-buffer.c:851
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:1822
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:2493
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:433
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:96
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:575
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:263
UTHBuildPacketOfFlows
uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
Definition: util-unittest-helper.c:892
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:224
UTHMatchPackets
int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
Definition: util-unittest-helper.c:751
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: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: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:712
DetectEngineThreadCtx_
Definition: detect.h:1032
Packet_::ts
SCTime_t ts
Definition: decode.h:472
SCSigOrderSignatures
void SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
Definition: detect-engine-sigorder.c:735
UTHRegisterTests
void UTHRegisterTests(void)
Definition: util-unittest-helper.c:1158
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:220
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
Packet_::sp
Port sp
Definition: decode.h:436
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:807
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:849
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:2188
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:295
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2003
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:47
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:429
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:532
Port
uint16_t Port
Definition: decode.h:230
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:1934
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:531
Packet_::flow
struct Flow_ * flow
Definition: decode.h:466
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3300
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:409
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:3514
FLOW_IPV6
#define FLOW_IPV6
Definition: flow.h:98
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:554
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:689
TcpStream_::sb
StreamingBuffer sb
Definition: stream-tcp-private.h:135
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:806
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:295
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: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:294
str
#define str(s)
Definition: suricata-common.h:286
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:665
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:591
StreamTcpSessionCleanup
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition: stream-tcp.c:225
detect-parse.h
src
uint16_t src
Definition: app-layer-dnp3.h:5
Signature_
Signature container.
Definition: detect.h:557
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:2549
Packet_::udph
UDPHdr * udph
Definition: decode.h:556
Address_::family
char family
Definition: decode.h:116
Packet_::dst
Address dst
Definition: decode.h:434
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:848
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:677
Packet_::ip6h
IPV6Hdr * ip6h
Definition: decode.h:534
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:283
Packet_::dp
Port dp
Definition: decode.h:444
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:433
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