]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
unittests: add/improve helpers for stream/flow
authorVictor Julien <victor@inliniac.net>
Wed, 19 Apr 2017 13:57:08 +0000 (15:57 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 21 Apr 2017 16:58:01 +0000 (18:58 +0200)
src/util-unittest-helper.c
src/util-unittest-helper.h

index f82da37bda99c0fe41cf46d8d212b2ec4fe24a5b..f678f129f1db447aa93f3d38e62db7fa0fb3ff01 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2017 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -36,6 +36,9 @@
 #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"
@@ -490,6 +493,54 @@ void UTHFreeFlow(Flow *flow)
     }
 }
 
+int UTHAddStreamToFlow(Flow *f, int direction,
+    uint8_t *data, uint32_t data_len)
+{
+    FAIL_IF_NULL(f);
+    FAIL_IF_NOT(f->proto == IPPROTO_TCP);
+    FAIL_IF_NULL(f->protoctx);
+    TcpSession *ssn = f->protoctx;
+
+    StreamingBufferSegment seg;
+    TcpStream *stream = direction == 0 ? &ssn->client : &ssn->server;
+    int r = StreamingBufferAppend(&stream->sb, &seg, data, data_len);
+    FAIL_IF_NOT(r == 0);
+    stream->last_ack += data_len;
+    return 1;
+}
+
+int UTHAddSessionToFlow(Flow *f,
+    uint32_t ts_isn,
+    uint32_t tc_isn)
+{
+    FAIL_IF_NULL(f);
+
+    TcpSession *ssn = SCCalloc(1, sizeof(*ssn));
+    FAIL_IF_NULL(ssn);
+
+    StreamingBuffer x = STREAMING_BUFFER_INITIALIZER(&stream_config.sbcnf);
+    ssn->client.sb = x;
+    ssn->server.sb = x;
+
+    ssn->client.isn = ts_isn;
+    ssn->server.isn = tc_isn;
+
+    f->protoctx = ssn;
+    return 1;
+}
+
+int UTHRemoveSessionFromFlow(Flow *f)
+{
+    FAIL_IF_NULL(f);
+    FAIL_IF_NOT(f->proto == IPPROTO_TCP);
+    TcpSession *ssn = f->protoctx;
+    FAIL_IF_NULL(ssn);
+    StreamTcpSessionCleanup(ssn);
+    SCFree(ssn);
+    f->protoctx = NULL;
+    return 1;
+}
+
 /**
  * \brief UTHGenericTest: function that perfom a generic check taking care of
  *                      as maximum common unittest elements as possible.
index 3b57a9e47a56edb444e23377a0f299935b863ddf..7555e7f36c444889c982e5413f55f33b4b78da37 100644 (file)
@@ -43,6 +43,9 @@ void UTHFreePackets(Packet **, int);
 
 Flow *UTHBuildFlow(int family, char *src, 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 UTHAppendSigs(DetectEngineCtx *, char **, int);
 int UTHMatchPackets(DetectEngineCtx *, Packet **, int);