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  if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto == IPPROTO_SCTP)
266  p->sp = sport;
267 
268  if (inet_pton(AF_INET, dst, &in) != 1)
269  goto error;
270  p->dst.addr_data32[0] = in.s_addr;
271  if (ipproto == IPPROTO_TCP || ipproto == IPPROTO_UDP || ipproto == IPPROTO_SCTP)
272  p->dp = dport;
273 
274  p->ip4h = (IPV4Hdr *)GET_PKT_DATA(p);
275  if (p->ip4h == NULL)
276  goto error;
277 
278  p->ip4h->s_ip_src.s_addr = p->src.addr_data32[0];
279  p->ip4h->s_ip_dst.s_addr = p->dst.addr_data32[0];
280  p->ip4h->ip_proto = ipproto;
281  p->ip4h->ip_verhl = sizeof(IPV4Hdr);
282  p->proto = ipproto;
283 
284  int hdr_offset = sizeof(IPV4Hdr);
285  switch (ipproto) {
286  case IPPROTO_UDP:
287  p->udph = (UDPHdr *)(GET_PKT_DATA(p) + sizeof(IPV4Hdr));
288  if (p->udph == NULL)
289  goto error;
290 
291  p->udph->uh_sport = sport;
292  p->udph->uh_dport = dport;
293  hdr_offset += sizeof(UDPHdr);
294  break;
295  case IPPROTO_TCP:
296  p->tcph = (TCPHdr *)(GET_PKT_DATA(p) + sizeof(IPV4Hdr));
297  if (p->tcph == NULL)
298  goto error;
299 
300  p->tcph->th_sport = htons(sport);
301  p->tcph->th_dport = htons(dport);
302  hdr_offset += sizeof(TCPHdr);
303  break;
304  case IPPROTO_ICMP:
305  p->icmpv4h = (ICMPV4Hdr *)(GET_PKT_DATA(p) + sizeof(IPV4Hdr));
306  if (p->icmpv4h == NULL)
307  goto error;
308 
309  hdr_offset += sizeof(ICMPV4Hdr);
310  break;
311  default:
312  break;
313  /* TODO: Add more protocols */
314  }
315 
316  if (payload && payload_len) {
317  PacketCopyDataOffset(p, hdr_offset, payload, payload_len);
318  }
319  SET_PKT_LEN(p, hdr_offset + payload_len);
320  p->payload = GET_PKT_DATA(p)+hdr_offset;
321  p->app_update_direction = UPDATE_DIR_BOTH;
322 
323  return p;
324 
325 error:
326  SCFree(p);
327  return NULL;
328 }
329 
330 /**
331  * \brief UTHBuildPacket is a wrapper that build packets with default ip
332  * and port fields
333  *
334  * \param payload pointer to the payload buffer
335  * \param payload_len pointer to the length of the payload
336  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
337  *
338  * \retval Packet pointer to the built in packet
339  */
340 Packet *UTHBuildPacket(uint8_t *payload, uint16_t payload_len,
341  uint8_t ipproto)
342 {
343  return UTHBuildPacketReal(payload, payload_len, ipproto,
344  "192.168.1.5", "192.168.1.1",
345  41424, 80);
346 }
347 
348 /**
349  * \brief UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes
350  *
351  * \param raw_eth pointer to the rawbytes containing an ethernet packet
352  * (and any other headers inside)
353  * \param pktsize pointer to the length of the payload
354  *
355  * \retval Packet pointer to the built in packet; NULL if something fail
356  */
357 Packet *UTHBuildPacketFromEth(uint8_t *raw_eth, uint16_t pktsize)
358 {
360  ThreadVars th_v;
361  Packet *p = PacketGetFromAlloc();
362  if (unlikely(p == NULL))
363  return NULL;
364  memset(&dtv, 0, sizeof(DecodeThreadVars));
365  memset(&th_v, 0, sizeof(th_v));
366 
367  DecodeEthernet(&th_v, &dtv, p, raw_eth, pktsize);
368  return p;
369 }
370 
371 /**
372  * \brief UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs
373  * and defaulting ports
374  *
375  * \param payload pointer to the payload buffer
376  * \param payload_len pointer to the length of the payload
377  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
378  *
379  * \retval Packet pointer to the built in packet
380  */
381 Packet *UTHBuildPacketSrcDst(uint8_t *payload, uint16_t payload_len,
382  uint8_t ipproto, const char *src, const char *dst)
383 {
384  return UTHBuildPacketReal(payload, payload_len, ipproto,
385  src, dst,
386  41424, 80);
387 }
388 
389 /**
390  * \brief UTHBuildPacketSrcDst is a wrapper that build packets specifying IPs
391  * and defaulting ports (IPV6)
392  *
393  * \param payload pointer to the payload buffer
394  * \param payload_len pointer to the length of the payload
395  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
396  *
397  * \retval Packet pointer to the built in packet
398  */
399 Packet *UTHBuildPacketIPV6SrcDst(uint8_t *payload, uint16_t payload_len,
400  uint8_t ipproto, const char *src, const char *dst)
401 {
402  return UTHBuildPacketIPV6Real(payload, payload_len, ipproto,
403  src, dst,
404  41424, 80);
405 }
406 
407 /**
408  * \brief UTHBuildPacketSrcDstPorts is a wrapper that build packets specifying
409  * src and dst ports and defaulting IPs
410  *
411  * \param payload pointer to the payload buffer
412  * \param payload_len pointer to the length of the payload
413  * \param ipproto Protocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
414  *
415  * \retval Packet pointer to the built in packet
416  */
417 Packet *UTHBuildPacketSrcDstPorts(uint8_t *payload, uint16_t payload_len,
418  uint8_t ipproto, uint16_t sport, uint16_t dport)
419 {
420  return UTHBuildPacketReal(payload, payload_len, ipproto,
421  "192.168.1.5", "192.168.1.1",
422  sport, dport);
423 }
424 
425 /**
426  * \brief UTHFreePackets: function to release the allocated data
427  * from UTHBuildPacket and the packet itself
428  *
429  * \param p pointer to the Packet
430  */
431 void UTHFreePackets(Packet **p, int numpkts)
432 {
433  if (p == NULL)
434  return;
435 
436  int i = 0;
437  for (; i < numpkts; i++) {
438  UTHFreePacket(p[i]);
439  }
440 }
441 
442 /**
443  * \brief UTHFreePacket: function to release the allocated data
444  * from UTHBuildPacket and the packet itself
445  *
446  * \param p pointer to the Packet
447  */
449 {
450  if (p == NULL)
451  return;
452  PacketFree(p);
453 }
454 
456 {
457  if (p && f) {
458  p->flow = f;
459  p->flags |= PKT_HAS_FLOW;
460  }
461 }
462 
463 Flow *UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
464 {
465  return TestHelperBuildFlow(family, src, dst, sp, dp);
466 }
467 
468 void UTHFreeFlow(Flow *flow)
469 {
470  if (flow != NULL) {
471  SCFree(flow);//FlowFree(flow);
472  }
473 }
474 
475 int UTHAddStreamToFlow(Flow *f, int direction,
476  uint8_t *data, uint32_t data_len)
477 {
478  FAIL_IF_NULL(f);
479  FAIL_IF_NOT(f->proto == IPPROTO_TCP);
481  TcpSession *ssn = f->protoctx;
482 
483  StreamingBufferSegment seg;
484  TcpStream *stream = direction == 0 ? &ssn->client : &ssn->server;
485  int r = StreamingBufferAppend(&stream->sb, &stream_config.sbcnf, &seg, data, data_len);
486  FAIL_IF_NOT(r == 0);
487  stream->last_ack += data_len;
488  return 1;
489 }
490 
492  uint32_t ts_isn,
493  uint32_t tc_isn)
494 {
495  FAIL_IF_NULL(f);
496 
497  TcpSession *ssn = SCCalloc(1, sizeof(*ssn));
498  FAIL_IF_NULL(ssn);
499 
501  ssn->client.sb = x;
502  ssn->server.sb = x;
503 
504  ssn->client.isn = ts_isn;
505  ssn->server.isn = tc_isn;
506 
507  f->protoctx = ssn;
508  return 1;
509 }
510 
512 {
513  FAIL_IF_NULL(f);
514  FAIL_IF_NOT(f->proto == IPPROTO_TCP);
515  TcpSession *ssn = f->protoctx;
516  FAIL_IF_NULL(ssn);
518  SCFree(ssn);
519  f->protoctx = NULL;
520  return 1;
521 }
522 
523 /**
524  * \brief UTHGenericTest: function that perform a generic check taking care of
525  * as maximum common unittest elements as possible.
526  * It will create a detection engine, append an array
527  * of signatures an check the expected results for each
528  * of them, it check matches for an array of packets
529  *
530  * \param pkt pointer to the array of packets
531  * \param numpkts number of packets to match
532  * \param sigs array of char* pointing to signatures to load
533  * \param numsigs number of signatures to load and check
534  * \param results pointer to arrays of numbers, each of them foreach packet
535  * to check if sids matches that packet as expected with
536  * that number of times or not. The size of results should be
537  * numpkts * numsigs * sizeof(uint16_t *)
538  *
539  * Example:
540  * result[1][3] would mean the number of times the pkt[1]
541  * match the sid[3]
542  *
543  * \retval int 1 if the match of all the sids is the specified has the
544  * specified results; 0 if not
545  */
546 int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
547 {
548 
549  int result = 0;
550  if (pkt == NULL || sigs == NULL || numpkts == 0
551  || sids == NULL || results == NULL || numsigs == 0) {
552  SCLogError("Arguments invalid, that the pointer/arrays are not NULL, and the number of "
553  "signatures and packets is > 0");
554  goto end;
555  }
557  if (de_ctx == NULL) {
558  goto end;
559  }
560  de_ctx->flags |= DE_QUIET;
561 
562  if (UTHAppendSigs(de_ctx, sigs, numsigs) == 0)
563  goto cleanup;
564 
565  result = UTHMatchPacketsWithResults(de_ctx, pkt, numpkts, sids, results, numsigs);
566 
567 cleanup:
569 end:
570  return result;
571 }
572 
573 /**
574  * \brief UTHCheckPacketMatches: function to check if a packet match some sids
575  *
576  *
577  * \param p pointer to the Packet
578  * \param sigs array of char* pointing to signatures to load
579  * \param numsigs number of signatures to load from the array
580  * \param results pointer to an array of numbers to check if sids matches
581  * that number of times or not.
582  *
583  * \retval int 1 if the match of all the sids is the specified has the
584  * specified results; 0 if not
585  */
586 int UTHCheckPacketMatchResults(Packet *p, uint32_t sids[], uint32_t results[], int numsigs)
587 {
588  if (p == NULL || sids == NULL) {
589  SCLogError("Arguments invalid, check if the "
590  "packet is NULL, and if the array contain sids is set");
591  return 0;
592  }
593 
594  int i = 0;
595  int res = 1;
596  for (; i < numsigs; i++) {
597  uint32_t r = PacketAlertCheck(p, sids[i]);
598  if (r != results[i]) {
599  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, and not %" PRIu32 " as expected",
600  sids[i], r, results[i]);
601  res = 0;
602  } else {
603  SCLogInfo("Sid %" PRIu32 " matched %" PRIu32 " times, as expected", sids[i], r);
604  }
605  }
606  return res;
607 }
608 
609 /**
610  * \brief UTHAppendSigs: Add sigs to the detection_engine checking for errors
611  *
612  * \param de_ctx pointer to the DetectEngineCtx used
613  * \param sigs array of char* pointing to signatures to load
614  * \param numsigs number of signatures to load from the array
615  * (size of the array)
616  *
617  * \retval int 0 if we have errors; 1 if all the signatures loaded successfully
618  */
619 int UTHAppendSigs(DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
620 {
621  BUG_ON(de_ctx == NULL);
622  BUG_ON(numsigs <= 0);
623  BUG_ON(sigs == NULL);
624 
625  for (int i = 0; i < numsigs; i++) {
626  if (sigs[i] == NULL) {
627  SCLogError("Check the signature"
628  " at position %d",
629  i);
630  return 0;
631  }
632  Signature *s = DetectEngineAppendSig(de_ctx, sigs[i]);
633  if (s == NULL) {
634  SCLogError("Check the signature at"
635  " position %d (%s)",
636  i, sigs[i]);
637  return 0;
638  }
639  }
640  return 1;
641 }
642 
643 /**
644  * \test UTHMatchPacketsWithResults Match a packet or a array of packets against sigs
645  * of a de_ctx, checking that each signature matches X times for certain packets
646  *
647  * \param de_ctx pointer with the signatures loaded
648  * \param p pointer to the array of packets
649  * \param num_packets number of packets in the array
650  *
651  * \retval return 1 if all goes well
652  * \retval return 0 if something fail
653  */
654 int UTHMatchPacketsWithResults(DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
655 {
656  BUG_ON(de_ctx == NULL);
657  BUG_ON(p == NULL);
658 
659  int result = 0;
661  ThreadVars th_v;
662  DetectEngineThreadCtx *det_ctx = NULL;
663  memset(&dtv, 0, sizeof(DecodeThreadVars));
664  memset(&th_v, 0, sizeof(th_v));
665 
667  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
668 
669  for (int i = 0; i < num_packets; i++) {
670  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
671  if (UTHCheckPacketMatchResults(p[i], sids, &results[(i * numsigs)], numsigs) == 0)
672  goto cleanup;
673  }
674 
675  result = 1;
676 cleanup:
677  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
678  return result;
679 }
680 
681 /**
682  * \test UTHMatchPackets Match a packet or a array of packets against sigs
683  * of a de_ctx, but note that the return value doesn't mean that we have a
684  * match, we have to check it later with PacketAlertCheck()
685  *
686  * \param de_ctx pointer with the signatures loaded
687  * \param p pointer to the array of packets
688  * \param num_packets number of packets in the array
689  *
690  * \retval return 1 if all goes well
691  * \retval return 0 if something fail
692  */
693 int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
694 {
695  BUG_ON(de_ctx == NULL);
696  BUG_ON(p == NULL);
697  int result = 1;
699  ThreadVars th_v;
700  DetectEngineThreadCtx *det_ctx = NULL;
701  memset(&dtv, 0, sizeof(DecodeThreadVars));
702  memset(&th_v, 0, sizeof(th_v));
707  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
708 
709  for (int i = 0; i < num_packets; i++)
710  SigMatchSignatures(&th_v, de_ctx, det_ctx, p[i]);
711 
712  /* Here we don't check if the packet matched or not, because
713  * the de_ctx can have multiple signatures, and some of them may match
714  * and others may not. That check will be outside
715  */
716  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
717  if (de_ctx != NULL) SigGroupCleanup(de_ctx);
718  return result;
719 }
720 
721 /**
722  * \test Test if a packet match a signature given as string and a mpm_type
723  * Hint: Useful for unittests with only one packet and one signature
724  *
725  * \param sig pointer to the string signature to test
726  * \param sid sid number of the signature
727  *
728  * \retval return 1 if match
729  * \retval return 0 if not
730  */
731 int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
732 {
733  SCEnter();
734 
735  int result = 0;
736 
738  ThreadVars th_v;
739  DetectEngineThreadCtx *det_ctx = NULL;
740 
741  memset(&dtv, 0, sizeof(DecodeThreadVars));
742  memset(&th_v, 0, sizeof(th_v));
743 
745  if (de_ctx == NULL) {
746  printf("de_ctx == NULL: ");
747  goto end;
748  }
749 
750  de_ctx->flags |= DE_QUIET;
751  de_ctx->mpm_matcher = mpm_type;
752 
753  de_ctx->sig_list = SigInit(de_ctx, sig);
754  if (de_ctx->sig_list == NULL) {
755  printf("signature == NULL: ");
756  goto end;
757  }
758 
760  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
761 
762  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
763  if (PacketAlertCheck(p, de_ctx->sig_list->id) != 1) {
764  printf("signature didn't alert: ");
765  goto end;
766  }
767 
768  result = 1;
769 end:
770  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
772  SCReturnInt(result);
773 }
774 
775 /**
776  * \test Test if a packet match a signature given as string
777  * Hint: Useful for unittests with only one packet and one signature
778  *
779  * \param sig pointer to the string signature to test
780  * \param sid sid number of the signature
781  *
782  * \retval return 1 if match
783  * \retval return 0 if not
784  */
785 int UTHPacketMatchSig(Packet *p, const char *sig)
786 {
787  int result = 1;
788 
790 
791  ThreadVars th_v;
792  DetectEngineThreadCtx *det_ctx = NULL;
793 
794  memset(&dtv, 0, sizeof(DecodeThreadVars));
795  memset(&th_v, 0, sizeof(th_v));
796 
798  if (de_ctx == NULL) {
799  result=0;
800  goto end;
801  }
802 
803  de_ctx->flags |= DE_QUIET;
804 
805  de_ctx->sig_list = SigInit(de_ctx, sig);
806  if (de_ctx->sig_list == NULL) {
807  result = 0;
808  goto end;
809  }
810 
812  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
813 
814  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
815  if (PacketAlertCheck(p, de_ctx->sig_list->id) != 1) {
816  result = 0;
817  goto end;
818  }
819 
820 end:
821  if (de_ctx) {
824  }
825 
826  if (det_ctx != NULL)
827  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
828  if (de_ctx != NULL)
830 
831  return result;
832 }
833 
834 uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
835 {
836  FlowLookupStruct fls;
837  memset(&fls, 0, sizeof(fls));
838 
839  uint32_t i = start;
840  uint8_t payload[] = "Payload";
841  for (; i < end; i++) {
842  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
843  if (dir == 0) {
844  p->src.addr_data32[0] = i;
845  p->dst.addr_data32[0] = i + 1;
846  } else {
847  p->src.addr_data32[0] = i + 1;
848  p->dst.addr_data32[0] = i;
849  }
850  FlowHandlePacket(NULL, &fls, p);
851  if (p->flow != NULL) {
852  FLOWLOCK_UNLOCK(p->flow);
853  }
854 
855  /* Now the queues should be updated */
856  UTHFreePacket(p);
857  }
858 
859  Flow *f;
860  while ((f = FlowQueuePrivateGetFromTop(&fls.spare_queue))) {
861  FlowFree(f);
862  }
863  while ((f = FlowQueuePrivateGetFromTop(&fls.work_queue))) {
864  FlowFree(f);
865  }
866 
867  return i;
868 }
869 
870 /** \brief parser a sig and see if the expected result is correct */
871 int UTHParseSignature(const char *str, bool expect)
872 {
875  de_ctx->flags |= DE_QUIET;
876 
878  if (expect)
879  FAIL_IF_NULL(s);
880  else
881  FAIL_IF_NOT_NULL(s);
882 
884  PASS;
885 }
886 
887 /*
888  * unittests for the unittest helpers
889  */
890 
891 /**
892  * \brief CheckUTHTestPacket wrapper to check packets for unittests
893  */
894 static int CheckUTHTestPacket(Packet *p, uint8_t ipproto)
895 {
896  uint16_t sport = 41424;
897  uint16_t dport = 80;
898  uint8_t payload[] = "Payload";
899 
900  uint8_t len = sizeof(payload);
901 
902  if (p == NULL)
903  return 0;
904 
905  if (p->payload_len != len)
906  return 0;
907 
908  if (strncmp((char *)payload, (char *)p->payload, len) != 0)
909  return 0;
910 
911  if (p->src.family != AF_INET)
912  return 0;
913  if (p->dst.family != AF_INET)
914  return 0;
915  if (p->proto != ipproto)
916  return 0;
917 
918  switch(ipproto) {
919  case IPPROTO_UDP:
920  if (p->udph == NULL)
921  return 0;
922  if (p->udph->uh_sport != sport)
923  return 0;
924  if (p->udph->uh_dport != dport)
925  return 0;
926  break;
927  case IPPROTO_TCP:
928  if (p->tcph == NULL)
929  return 0;
930  if (SCNtohs(p->tcph->th_sport) != sport)
931  return 0;
932  if (SCNtohs(p->tcph->th_dport) != dport)
933  return 0;
934  break;
935  }
936  return 1;
937 }
938 
939 #ifdef HAVE_MEMMEM
940 #include <string.h>
941 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
942  return memmem(big, big_len, little, little_len);
943 }
944 #else
945 #include "util-spm-bs.h"
946 void * UTHmemsearch(const void *big, size_t big_len, const void *little, size_t little_len) {
947  return BasicSearch(big, big_len, little, little_len);
948 }
949 #endif //HAVE_MEMMEM
950 
951 /**
952  * \brief UTHBuildPacketRealTest01 wrapper to check packets for unittests
953  */
954 static int UTHBuildPacketRealTest01(void)
955 {
956  uint8_t payload[] = "Payload";
957 
958  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_TCP,
959  "192.168.1.5", "192.168.1.1", 41424, 80);
960 
961  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
962  UTHFreePacket(p);
963 
964  return ret;
965 }
966 
967 /**
968  * \brief UTHBuildPacketRealTest02 wrapper to check packets for unittests
969  */
970 static int UTHBuildPacketRealTest02(void)
971 {
972  uint8_t payload[] = "Payload";
973 
974  Packet *p = UTHBuildPacketReal(payload, sizeof(payload), IPPROTO_UDP,
975  "192.168.1.5", "192.168.1.1", 41424, 80);
976 
977  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
978  UTHFreePacket(p);
979  return ret;
980 }
981 
982 /**
983  * \brief UTHBuildPacketTest01 wrapper to check packets for unittests
984  */
985 static int UTHBuildPacketTest01(void)
986 {
987  uint8_t payload[] = "Payload";
988 
989  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_TCP);
990 
991  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
992  UTHFreePacket(p);
993 
994  return ret;
995 }
996 
997 /**
998  * \brief UTHBuildPacketTest02 wrapper to check packets for unittests
999  */
1000 static int UTHBuildPacketTest02(void)
1001 {
1002  uint8_t payload[] = "Payload";
1003 
1004  Packet *p = UTHBuildPacket(payload, sizeof(payload), IPPROTO_UDP);
1005 
1006  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1007  UTHFreePacket(p);
1008 
1009  return ret;
1010 }
1011 
1012 /**
1013  * \brief UTHBuildPacketOfFlowsTest01 wrapper to check packets for unittests
1014  */
1015 static int UTHBuildPacketOfFlowsTest01(void)
1016 {
1017  int result = 0;
1018 
1020  uint32_t flow_spare_q_len = FlowSpareGetPoolSize();
1021 
1022  UTHBuildPacketOfFlows(0, 100, 0);
1023 
1024  if (FlowSpareGetPoolSize() != flow_spare_q_len - 100)
1025  result = 0;
1026  else
1027  result = 1;
1028  FlowShutdown();
1029 
1030  return result;
1031 }
1032 
1033 
1034 /**
1035  * \brief UTHBuildPacketSrcDstTest01 wrapper to check packets for unittests
1036  */
1037 static int UTHBuildPacketSrcDstTest01(void)
1038 {
1039  uint8_t payload[] = "Payload";
1040 
1041  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_TCP,
1042  "192.168.1.5", "192.168.1.1");
1043 
1044  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1045  UTHFreePacket(p);
1046 
1047  return ret;
1048 }
1049 
1050 /**
1051  * \brief UTHBuildPacketSrcDstTest02 wrapper to check packets for unittests
1052  */
1053 static int UTHBuildPacketSrcDstTest02(void)
1054 {
1055  uint8_t payload[] = "Payload";
1056 
1057  Packet *p = UTHBuildPacketSrcDst(payload, sizeof(payload), IPPROTO_UDP,
1058  "192.168.1.5", "192.168.1.1");
1059 
1060  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1061  UTHFreePacket(p);
1062 
1063  return ret;
1064 }
1065 
1066 /**
1067  * \brief UTHBuildPacketSrcDstPortsTest01 wrapper to check packets for unittests
1068  */
1069 static int UTHBuildPacketSrcDstPortsTest01(void)
1070 {
1071  uint8_t payload[] = "Payload";
1072 
1073  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_TCP,
1074  41424, 80);
1075 
1076  int ret = CheckUTHTestPacket(p, IPPROTO_TCP);
1077  UTHFreePacket(p);
1078 
1079  return ret;
1080 }
1081 
1082 /**
1083  * \brief UTHBuildPacketSrcDstPortsTest02 wrapper to check packets for unittests
1084  */
1085 static int UTHBuildPacketSrcDstPortsTest02(void)
1086 {
1087  uint8_t payload[] = "Payload";
1088 
1089  Packet *p = UTHBuildPacketSrcDstPorts(payload, sizeof(payload), IPPROTO_UDP,
1090  41424, 80);
1091 
1092  int ret = CheckUTHTestPacket(p, IPPROTO_UDP);
1093  UTHFreePacket(p);
1094 
1095  return ret;
1096 }
1097 
1098 #endif /* UNITTESTS */
1099 
1101 {
1102 #ifdef UNITTESTS
1103  UtRegisterTest("UTHBuildPacketRealTest01", UTHBuildPacketRealTest01);
1104  UtRegisterTest("UTHBuildPacketRealTest02", UTHBuildPacketRealTest02);
1105  UtRegisterTest("UTHBuildPacketTest01", UTHBuildPacketTest01);
1106  UtRegisterTest("UTHBuildPacketTest02", UTHBuildPacketTest02);
1107  UtRegisterTest("UTHBuildPacketSrcDstTest01", UTHBuildPacketSrcDstTest01);
1108  UtRegisterTest("UTHBuildPacketSrcDstTest02", UTHBuildPacketSrcDstTest02);
1109  UtRegisterTest("UTHBuildPacketSrcDstPortsTest01",
1110  UTHBuildPacketSrcDstPortsTest01);
1111  UtRegisterTest("UTHBuildPacketSrcDstPortsTest02",
1112  UTHBuildPacketSrcDstPortsTest02);
1113  UtRegisterTest("UTHBuildPacketOfFlowsTest01", UTHBuildPacketOfFlowsTest01);
1114 
1115 #endif /* UNITTESTS */
1116 }
1117 
UPDATE_DIR_BOTH
@ UPDATE_DIR_BOTH
Definition: stream-tcp-reassemble.h:58
FlowLookupStruct_::work_queue
FlowQueuePrivate work_queue
Definition: flow.h:543
Packet_::proto
uint8_t proto
Definition: decode.h:459
UTHParseSignature
int UTHParseSignature(const char *str, bool expect)
parser a sig and see if the expected result is correct
Definition: util-unittest-helper.c:871
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:946
TCPHdr_::th_dport
uint16_t th_dport
Definition: decode-tcp.h:144
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
UDPHdr_::uh_dport
uint16_t uh_dport
Definition: decode-udp.h:44
TcpStream_::isn
uint32_t isn
Definition: stream-tcp-private.h:113
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1022
UTHAddStreamToFlow
int UTHAddStreamToFlow(Flow *f, int direction, uint8_t *data, uint32_t data_len)
Definition: util-unittest-helper.c:475
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:373
Packet_::payload
uint8_t * payload
Definition: decode.h:586
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:474
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:351
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:381
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:839
UTHPacketMatchSigMpm
int UTHPacketMatchSigMpm(Packet *p, char *sig, uint16_t mpm_type)
Definition: util-unittest-helper.c:731
TcpStreamCnf_::sbcnf
StreamingBufferConfig sbcnf
Definition: stream-tcp.h:77
FlowLookupStruct_
Definition: flow.h:539
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2533
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:306
ICMPV4Hdr
struct ICMPV4Hdr_ ICMPV4Hdr
SCSigSignatureOrderingModuleCleanup
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
Definition: detect-engine-sigorder.c:825
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:586
DE_QUIET
#define DE_QUIET
Definition: detect.h:324
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:785
StreamingBufferAppend
int StreamingBufferAppend(StreamingBuffer *sb, const StreamingBufferConfig *cfg, StreamingBufferSegment *seg, const uint8_t *data, uint32_t data_len)
Definition: util-streaming-buffer.c:1077
FlowHandlePacket
void FlowHandlePacket(ThreadVars *tv, FlowLookupStruct *fls, Packet *p)
Entry point for packet flow handling.
Definition: flow.c:508
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:340
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1897
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:417
SigCleanSignatures
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:54
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2620
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:219
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:441
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:97
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:587
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:268
UTHBuildPacketOfFlows
uint32_t UTHBuildPacketOfFlows(uint32_t start, uint32_t end, uint8_t dir)
Definition: util-unittest-helper.c:834
IPV4Hdr
struct IPV4Hdr_ IPV4Hdr
UTHAssignFlow
void UTHAssignFlow(Packet *p, Flow *f)
Definition: util-unittest-helper.c:455
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:522
SET_PKT_LEN
#define SET_PKT_LEN(p, len)
Definition: decode.h:225
UTHMatchPackets
int UTHMatchPackets(DetectEngineCtx *de_ctx, Packet **p, int num_packets)
Definition: util-unittest-helper.c:693
TCPHdr_::th_sport
uint16_t th_sport
Definition: decode-tcp.h:143
UTHBuildFlow
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:463
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:654
DetectEngineThreadCtx_
Definition: detect.h:1095
Packet_::ts
SCTime_t ts
Definition: decode.h:485
SCSigOrderSignatures
void SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
Definition: detect-engine-sigorder.c:733
UTHRegisterTests
void UTHRegisterTests(void)
Definition: util-unittest-helper.c:1100
UTHAddSessionToFlow
int UTHAddSessionToFlow(Flow *f, uint32_t ts_isn, uint32_t tc_isn)
Definition: util-unittest-helper.c:491
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:221
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
Packet_::sp
Port sp
Definition: decode.h:444
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:805
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:842
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:190
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:2314
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2218
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:437
detect-engine-build.h
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
stream-tcp-private.h
ICMPV4Hdr_
Definition: decode-icmpv4.h:165
detect-engine-alert.h
Port
uint16_t Port
Definition: decode.h:230
STREAMING_BUFFER_INITIALIZER
#define STREAMING_BUFFER_INITIALIZER
Definition: util-streaming-buffer.h:137
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2149
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
UTHFreeFlow
void UTHFreeFlow(Flow *flow)
Definition: util-unittest-helper.c:468
StreamingBuffer_
Definition: util-streaming-buffer.h:108
IPV4Hdr_
Definition: decode-ipv4.h:72
FlowLookupStruct_::spare_queue
FlowQueuePrivate spare_queue
Definition: flow.h:541
Packet_::flow
struct Flow_ * flow
Definition: decode.h:476
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3244
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:414
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:82
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3454
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:399
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:567
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:670
TcpStream_::sb
StreamingBuffer sb
Definition: stream-tcp-private.h:135
UDPHdr
struct UDPHdr_ UDPHdr
UDPHdr_::uh_sport
uint16_t uh_sport
Definition: decode-udp.h:43
UDPHdr_
Definition: decode-udp.h:42
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:847
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:546
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:229
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:291
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:685
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:448
Signature_::id
uint32_t id
Definition: detect.h:631
StreamTcpSessionCleanup
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition: stream-tcp.c:329
detect-parse.h
src
uint16_t src
Definition: app-layer-dnp3.h:5
Signature_
Signature container.
Definition: detect.h:596
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:357
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2494
Packet_::udph
UDPHdr * udph
Definition: decode.h:569
Address_::family
char family
Definition: decode.h:117
Packet_::dst
Address dst
Definition: decode.h:442
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:42
TestHelperBuildFlow
Flow * TestHelperBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
Definition: util-unittest-helper.c:52
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:948
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:841
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:619
TCPHdr
struct TCPHdr_ TCPHdr
Packet_::ip6h
IPV6Hdr * ip6h
Definition: decode.h:547
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
UTHRemoveSessionFromFlow
int UTHRemoveSessionFromFlow(Flow *f)
Definition: util-unittest-helper.c:511
TcpSession_
Definition: stream-tcp-private.h:283
Packet_::dp
Port dp
Definition: decode.h:452
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
TCPHdr_
Definition: decode-tcp.h:142
Packet_::src
Address src
Definition: decode.h:441
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:431