suricata
util-unittest-helper.c File Reference
#include "suricata-common.h"
#include "decode.h"
#include "flow-private.h"
#include "flow-util.h"
#include "detect.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-sigorder.h"
#include "stream-tcp.h"
#include "stream-tcp-private.h"
#include "util-debug.h"
#include "util-time.h"
#include "util-error.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-spm-bs.h"
Include dependency graph for util-unittest-helper.c:

Go to the source code of this file.

Functions

FlowTestHelperBuildFlow (int family, const char *src, const char *dst, Port sp, Port dp)
 
int TestHelperBufferToFile (const char *name, const uint8_t *data, size_t size)
 writes the contents of a buffer into a file More...
 
uint32_t UTHSetIPv4Address (const char *str)
 return the uint32_t for a ipv4 address string More...
 
PacketUTHBuildPacketIPV6Real (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 sources and destinations (IPV6) More...
 
PacketUTHBuildPacketReal (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 sources and destinations. More...
 
PacketUTHBuildPacket (uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
 UTHBuildPacket is a wrapper that build packets with default ip and port fields. More...
 
Packet ** UTHBuildPacketArrayFromEth (uint8_t *raw_eth[], int *pktsize, int numpkts)
 UTHBuildPacketArrayFromEth is a wrapper that build a packets from an array of packets in ethernet rawbytes. Hint: It also share the flows. More...
 
PacketUTHBuildPacketFromEth (uint8_t *raw_eth, uint16_t pktsize)
 UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes. More...
 
PacketUTHBuildPacketSrcDst (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. More...
 
PacketUTHBuildPacketIPV6SrcDst (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) More...
 
PacketUTHBuildPacketSrcDstPorts (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 IPs. More...
 
void UTHFreePackets (Packet **p, int numpkts)
 UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself. More...
 
void UTHFreePacket (Packet *p)
 UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself. More...
 
void UTHAssignFlow (Packet *p, Flow *f)
 
FlowUTHBuildFlow (int family, const char *src, const char *dst, Port sp, Port dp)
 
void UTHFreeFlow (Flow *flow)
 
int UTHAddStreamToFlow (Flow *f, int direction, uint8_t *data, uint32_t data_len)
 
int UTHAddSessionToFlow (Flow *f, uint32_t ts_isn, uint32_t tc_isn)
 
int UTHRemoveSessionFromFlow (Flow *f)
 
int UTHGenericTest (Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
 UTHGenericTest: function that perfom a generic check taking care of as maximum common unittest elements as possible. It will create a detection engine, append an array of signatures an check the spected results for each of them, it check matches for an array of packets. More...
 
int UTHCheckPacketMatchResults (Packet *p, uint32_t sids[], uint32_t results[], int numsids)
 UTHCheckPacketMatches: function to check if a packet match some sids. More...
 
int UTHAppendSigs (DetectEngineCtx *de_ctx, const char *sigs[], int numsigs)
 UTHAppendSigs: Add sigs to the detection_engine checking for errors. More...
 
int UTHMatchPacketsWithResults (DetectEngineCtx *de_ctx, Packet **p, int num_packets, uint32_t sids[], uint32_t *results, int numsigs)
 
int UTHMatchPackets (DetectEngineCtx *de_ctx, Packet **p, int num_packets)
 
int UTHPacketMatchSigMpm (Packet *p, char *sig, uint16_t mpm_type)
 
int UTHPacketMatchSig (Packet *p, const char *sig)
 
uint32_t UTHBuildPacketOfFlows (uint32_t start, uint32_t end, uint8_t dir)
 
int UTHParseSignature (const char *str, bool expect)
 parser a sig and see if the expected result is correct More...
 
void * UTHmemsearch (const void *big, size_t big_len, const void *little, size_t little_len)
 
void UTHRegisterTests (void)
 

Detailed Description

Author
Pablo Rincon Crespo pablo.nosp@m..rin.nosp@m.con.c.nosp@m.resp.nosp@m.o@gma.nosp@m.il.c.nosp@m.om

This file provide a set of helper functions for reducing the complexity when constructing unittests

Definition in file util-unittest-helper.c.

Function Documentation

◆ TestHelperBufferToFile()

int TestHelperBufferToFile ( const char *  name,
const uint8_t *  data,
size_t  size 
)

writes the contents of a buffer into a file

Definition at line 100 of file util-unittest-helper.c.

◆ TestHelperBuildFlow()

Flow* TestHelperBuildFlow ( int  family,
const char *  src,
const char *  dst,
Port  sp,
Port  dp 
)

Definition at line 49 of file util-unittest-helper.c.

References BUG_ON, dst, FLOW_INITIALIZE, FLOW_IPV4, FLOW_IPV6, SCFree, SCMalloc, src, and unlikely.

Referenced by LLVMFuzzerTestOneInput(), and UTHBuildFlow().

Here is the caller graph for this function:

◆ UTHAddSessionToFlow()

int UTHAddSessionToFlow ( Flow f,
uint32_t  ts_isn,
uint32_t  tc_isn 
)

◆ UTHAddStreamToFlow()

int UTHAddStreamToFlow ( Flow f,
int  direction,
uint8_t *  data,
uint32_t  data_len 
)

◆ UTHAppendSigs()

int UTHAppendSigs ( DetectEngineCtx de_ctx,
const char *  sigs[],
int  numsigs 
)

UTHAppendSigs: Add sigs to the detection_engine checking for errors.

Parameters
de_ctxpointer to the DetectEngineCtx used
sigsarray of char* pointing to signatures to load
numsigsnumber of signatures to load from the array (size of the array)
Return values
int0 if we have errors; 1 if all the signatures loaded succesfuly

Definition at line 680 of file util-unittest-helper.c.

References de_ctx, DetectEngineAppendSig(), SC_ERR_INVALID_ARGUMENT, and SCLogError.

Referenced by UTHGenericTest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UTHAssignFlow()

void UTHAssignFlow ( Packet p,
Flow f 
)

Definition at line 512 of file util-unittest-helper.c.

References Packet_::flags, Packet_::flow, and PKT_HAS_FLOW.

◆ UTHBuildFlow()

Flow* UTHBuildFlow ( int  family,
const char *  src,
const char *  dst,
Port  sp,
Port  dp 
)

Definition at line 520 of file util-unittest-helper.c.

References dst, src, and TestHelperBuildFlow().

Here is the call graph for this function:

◆ 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.

Parameters
payloadpointer to the payloadd buffer
payload_lenpointer to the length of the payload
ipprotoProtocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
Return values
Packetpointer to the built in packet

Definition at line 336 of file util-unittest-helper.c.

References payload_len, and UTHBuildPacketReal().

Referenced by UTHBuildPacketOfFlows().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UTHBuildPacketArrayFromEth()

Packet** UTHBuildPacketArrayFromEth ( uint8_t *  raw_eth[],
int *  pktsize,
int  numpkts 
)

UTHBuildPacketArrayFromEth is a wrapper that build a packets from an array of packets in ethernet rawbytes. Hint: It also share the flows.

Parameters
raw_ethpointer to the array of ethernet packets in rawbytes
pktsizepointer to the array of sizes corresponding to each buffer pointed from pktsize.
numpktsnumber of packets in the array
Return values
Packetpointer to the array of built in packets; NULL if something fail

Definition at line 355 of file util-unittest-helper.c.

References DecodeEthernet(), dtv, PacketGetFromAlloc(), SC_ERR_INVALID_ARGUMENT, SCFree, SCLogError, SCMalloc, and unlikely.

Here is the call graph for this function:

◆ UTHBuildPacketFromEth()

Packet* UTHBuildPacketFromEth ( uint8_t *  raw_eth,
uint16_t  pktsize 
)

UTHBuildPacketFromEth is a wrapper that build a packet for the rawbytes.

Parameters
raw_ethpointer to the rawbytes containing an ethernet packet (and any other headers inside)
pktsizepointer to the length of the payload
Return values
Packetpointer to the built in packet; NULL if something fail

Definition at line 393 of file util-unittest-helper.c.

References DecodeEthernet(), dtv, PacketGetFromAlloc(), and unlikely.

Here is the call graph for this function:

◆ 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 sources and destinations (IPV6)

Parameters
payloadpointer to the payloadd buffer
payload_lenpointer to the length of the payload
ipprotoProtocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
srcpointer to a string containing the ip source
dstpointer to a string containing the ip destination
sportpointer to a string containing the port source
dportpointer to a string containing the port destination
Return values
Packetpointer to the built in packet

Definition at line 155 of file util-unittest-helper.c.

References Packet_::dp, dst, Packet_::dst, Address_::family, Packet_::ip6h, PacketGetFromAlloc(), Packet_::payload, payload_len, Packet_::payload_len, Packet_::proto, SCFree, SCMalloc, SET_PKT_LEN, Packet_::sp, src, Packet_::src, Packet_::tcph, TimeGet(), Packet_::ts, and unlikely.

Referenced by UTHBuildPacketIPV6SrcDst().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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)

Parameters
payloadpointer to the payload buffer
payload_lenpointer to the length of the payload
ipprotoProtocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
Return values
Packetpointer to the built in packet

Definition at line 435 of file util-unittest-helper.c.

References dst, payload_len, src, and UTHBuildPacketIPV6Real().

Here is the call graph for this function:

◆ UTHBuildPacketOfFlows()

uint32_t UTHBuildPacketOfFlows ( uint32_t  start,
uint32_t  end,
uint8_t  dir 
)

Definition at line 930 of file util-unittest-helper.c.

References Packet_::dst, Packet_::flow, FlowHandlePacket(), FLOWLOCK_UNLOCK, SC_ATOMIC_RESET, Packet_::src, UTHBuildPacket(), and UTHFreePacket().

Here is the call graph for this function:

◆ 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 sources and destinations.

Parameters
payloadpointer to the payloadd buffer
payload_lenpointer to the length of the payload
ipprotoProtocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
srcpointer to a string containing the ip source
dstpointer to a string containing the ip destination
sportpointer to a string containing the port source
dportpointer to a string containing the port destination
Return values
Packetpointer to the built in packet

Definition at line 241 of file util-unittest-helper.c.

References COPY_TIMESTAMP, dst, GET_PKT_DATA, PacketCopyDataOffset(), PacketGetFromAlloc(), payload_len, SCFree, SET_PKT_LEN, src, TimeGet(), tv, and unlikely.

Referenced by StreamTcpUTAddPayload(), StreamTcpUTAddSegmentWithByte(), StreamTcpUTAddSegmentWithPayload(), UTHBuildPacket(), UTHBuildPacketSrcDst(), and UTHBuildPacketSrcDstPorts().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Parameters
payloadpointer to the payloadd buffer
payload_lenpointer to the length of the payload
ipprotoProtocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
Return values
Packetpointer to the built in packet

Definition at line 417 of file util-unittest-helper.c.

References dst, payload_len, src, and UTHBuildPacketReal().

Here is the call graph for this function:

◆ 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 IPs.

Parameters
payloadpointer to the payloadd buffer
payload_lenpointer to the length of the payload
ipprotoProtocols allowed atm are IPPROTO_TCP and IPPROTO_UDP
Return values
Packetpointer to the built in packet

Definition at line 453 of file util-unittest-helper.c.

References payload_len, and UTHBuildPacketReal().

Here is the call graph for this function:

◆ UTHCheckPacketMatchResults()

int UTHCheckPacketMatchResults ( Packet p,
uint32_t  sids[],
uint32_t  results[],
int  numsids 
)

UTHCheckPacketMatches: function to check if a packet match some sids.

Parameters
ppointer to the Packet
sigsarray of char* pointing to signatures to load
numsigsnumber of signatures to load from the array
resultspointer to an array of numbers to check if sids matches that number of times or not.
Return values
int1 if the match of all the sids is the specified has the specified results; 0 if not

Definition at line 646 of file util-unittest-helper.c.

References PacketAlertCheck(), res, results, SC_ERR_INVALID_ARGUMENT, SCLogError, and SCLogInfo.

Referenced by UTHMatchPacketsWithResults().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UTHFreeFlow()

void UTHFreeFlow ( Flow flow)

Definition at line 525 of file util-unittest-helper.c.

References FlowFree().

Here is the call graph for this function:

◆ UTHFreePacket()

void UTHFreePacket ( Packet p)

UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.

Parameters
ppointer to the Packet

Definition at line 484 of file util-unittest-helper.c.

References Packet_::ip4h, Packet_::proto, SCFree, Packet_::tcph, and Packet_::udph.

Referenced by StreamTcpUTAddPayload(), StreamTcpUTAddSegmentWithByte(), StreamTcpUTAddSegmentWithPayload(), UTHBuildPacketOfFlows(), and UTHFreePackets().

Here is the caller graph for this function:

◆ UTHFreePackets()

void UTHFreePackets ( Packet **  p,
int  numpkts 
)

UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.

Parameters
ppointer to the Packet

Definition at line 467 of file util-unittest-helper.c.

References UTHFreePacket().

Here is the call graph for this function:

◆ UTHGenericTest()

int UTHGenericTest ( Packet **  pkt,
int  numpkts,
const char *  sigs[],
uint32_t  sids[],
uint32_t *  results,
int  numsigs 
)

UTHGenericTest: function that perfom a generic check taking care of as maximum common unittest elements as possible. It will create a detection engine, append an array of signatures an check the spected results for each of them, it check matches for an array of packets.

Parameters
pktpointer to the array of packets
numpktsnumber of packets to match
sigsarray of char* pointing to signatures to load
numsigsnumber of signatures to load and check
resultspointer to arrays of numbers, each of them foreach packet to check if sids matches that packet as expected with that number of times or not. The size of results should be numpkts * numsigs * sizeof(uint16_t *)

Example: result[1][3] would mean the number of times the pkt[1] match the sid[3]

Return values
int1 if the match of all the sids is the specified has the specified results; 0 if not

Definition at line 603 of file util-unittest-helper.c.

References de_ctx, DE_QUIET, DetectEngineCtxFree(), DetectEngineCtxInit(), DetectEngineCtx_::flags, results, SC_ERR_INVALID_ARGUMENT, SCLogError, SigCleanSignatures(), SigGroupCleanup(), UTHAppendSigs(), and UTHMatchPacketsWithResults().

Here is the call graph for this function:

◆ UTHMatchPackets()

int UTHMatchPackets ( DetectEngineCtx de_ctx,
Packet **  p,
int  num_packets 
)
Test:
UTHMatchPackets Match a packet or a array of packets against sigs of a de_ctx, but note that the return value doesn't mean that we have a match, we have to check it later with PacketAlertCheck()
Parameters
de_ctxpointer with the signatures loaded
ppointer to the array of packets
num_packetsnumber of packets in the array
Return values
return1 if all goes well
return0 if something fail

Definition at line 769 of file util-unittest-helper.c.

References de_ctx, DetectEngineThreadCtxDeinit(), DetectEngineThreadCtxInit(), dtv, SC_ERR_INVALID_ARGUMENT, SCLogError, SCSigOrderSignatures(), SCSigRegisterSignatureOrderingFuncs(), SCSigSignatureOrderingModuleCleanup(), SigGroupBuild(), SigGroupCleanup(), and SigMatchSignatures().

Here is the call graph for this function:

◆ UTHMatchPacketsWithResults()

int UTHMatchPacketsWithResults ( DetectEngineCtx de_ctx,
Packet **  p,
int  num_packets,
uint32_t  sids[],
uint32_t *  results,
int  numsigs 
)
Test:
UTHMatchPacketsWithResults Match a packet or a array of packets against sigs of a de_ctx, checking that each signature match match X times for certain packets
Parameters
de_ctxpointer with the signatures loaded
ppointer to the array of packets
num_packetsnumber of packets in the array
Return values
return1 if all goes well
return0 if something fail

Definition at line 719 of file util-unittest-helper.c.

References de_ctx, DetectEngineThreadCtxDeinit(), DetectEngineThreadCtxInit(), dtv, results, SC_ERR_INVALID_ARGUMENT, SCLogError, SigGroupBuild(), SigMatchSignatures(), and UTHCheckPacketMatchResults().

Referenced by UTHGenericTest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UTHmemsearch()

void* UTHmemsearch ( const void *  big,
size_t  big_len,
const void *  little,
size_t  little_len 
)

Definition at line 1032 of file util-unittest-helper.c.

References BasicSearch().

Here is the call graph for this function:

◆ UTHPacketMatchSig()

int UTHPacketMatchSig ( Packet p,
const char *  sig 
)
Test:
Test if a packet match a signature given as string Hint: Useful for unittests with only one packet and one signature
Parameters
sigpointer to the string signature to test
sidsid number of the signature
Return values
return1 if match
return0 if not

Definition at line 881 of file util-unittest-helper.c.

References de_ctx, DE_QUIET, DetectEngineCtxFree(), DetectEngineCtxInit(), DetectEngineThreadCtxDeinit(), DetectEngineThreadCtxInit(), dtv, DetectEngineCtx_::flags, Signature_::id, PacketAlertCheck(), DetectEngineCtx_::sig_list, SigCleanSignatures(), SigGroupBuild(), SigGroupCleanup(), SigInit(), and SigMatchSignatures().

Here is the call graph for this function:

◆ UTHPacketMatchSigMpm()

int UTHPacketMatchSigMpm ( Packet p,
char *  sig,
uint16_t  mpm_type 
)
Test:
Test if a packet match a signature given as string and a mpm_type Hint: Useful for unittests with only one packet and one signature
Parameters
sigpointer to the string signature to test
sidsid number of the signature
Return values
return1 if match
return0 if not

Definition at line 820 of file util-unittest-helper.c.

References de_ctx, DE_QUIET, DetectEngineCtxFree(), DetectEngineCtxInit(), DetectEngineThreadCtxDeinit(), DetectEngineThreadCtxInit(), dtv, DetectEngineCtx_::flags, Signature_::id, DetectEngineCtx_::mpm_matcher, PacketAlertCheck(), SCEnter, SCReturnInt, DetectEngineCtx_::sig_list, SigCleanSignatures(), SigGroupBuild(), SigGroupCleanup(), SigInit(), and SigMatchSignatures().

Here is the call graph for this function:

◆ UTHParseSignature()

int UTHParseSignature ( const char *  str,
bool  expect 
)

parser a sig and see if the expected result is correct

Definition at line 957 of file util-unittest-helper.c.

References de_ctx, DE_QUIET, DetectEngineAppendSig(), DetectEngineCtxFree(), DetectEngineCtxInit(), FAIL_IF_NOT_NULL, FAIL_IF_NULL, DetectEngineCtx_::flags, PASS, and str.

Here is the call graph for this function:

◆ UTHRegisterTests()

void UTHRegisterTests ( void  )

Definition at line 1186 of file util-unittest-helper.c.

References UtRegisterTest().

Here is the call graph for this function:

◆ UTHRemoveSessionFromFlow()

int UTHRemoveSessionFromFlow ( Flow f)

Definition at line 568 of file util-unittest-helper.c.

References FAIL_IF_NOT, FAIL_IF_NULL, Flow_::proto, Flow_::protoctx, SCFree, and StreamTcpSessionCleanup().

Here is the call graph for this function:

◆ UTHSetIPv4Address()

uint32_t UTHSetIPv4Address ( const char *  str)

return the uint32_t for a ipv4 address string

Parameters
strValid ipaddress in string form (e.g. 1.2.3.4)
Return values
uintthe uin32_t representation

Definition at line 131 of file util-unittest-helper.c.

References str.