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