]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
reputation: move unit tests
authorGiuseppe Longo <giuseppe@glongo.it>
Thu, 7 Mar 2019 15:38:47 +0000 (16:38 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 14 Mar 2019 15:26:25 +0000 (16:26 +0100)
UTs are moved inside "test/" directory,
and reworked to improve readability and reduce lines of code.

src/reputation.c
src/tests/reputation.c [new file with mode: 0644]

index 280770cedf309a04a122d700264c9c66ec7f5128..560ffdd394e9e412ec92288afd7c5743e8f25b82 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2018 Open Information Security Foundation
+/* Copyright (C) 2007-2019 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
@@ -680,224 +680,7 @@ void SRepDestroy(DetectEngineCtx *de_ctx) {
 }
 
 #ifdef UNITTESTS
-#include "conf-yaml-loader.h"
-#include "detect-engine.h"
-#include "stream-tcp-private.h"
-#include "stream-tcp-reassemble.h"
-#include "stream-tcp.h"
-#include "util-unittest-helper.h"
-
-static int SRepTest01(void)
-{
-    char str[] = "1.2.3.4,1,2";
-    int result = 0;
-    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL) {
-        return 0;
-    }
-
-    SRepInit(de_ctx);
-    Address a;
-    uint8_t cat = 0, value = 0;
-    if (SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 0) {
-        goto end;
-    }
-
-    char ipstr[16];
-    PrintInet(AF_INET, (const void *)&a.address, ipstr, sizeof(ipstr));
-
-    if (strcmp(ipstr, "1.2.3.4") != 0)
-        goto end;
-
-    if (cat != 1)
-        goto end;
-
-    if (value != 2)
-        goto end;
-
-    result = 1;
-
-end:
-    DetectEngineCtxFree(de_ctx);
-    return result;
-}
-
-static int SRepTest02(void)
-{
-    char str[] = "1.1.1.1,";
-    int result = 0;
-    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL) {
-        return 0;
-    }
-
-    SRepInit(de_ctx);
-    Address a;
-    uint8_t cat = 0, value = 0;
-    if (SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0) {
-        goto end;
-    }
-    result = 1;
-
-end:
-    DetectEngineCtxFree(de_ctx);
-    return result;
-}
-
-static int SRepTest03(void)
-{
-    char str[] = "1,Shortname,Long Name";
-
-    uint8_t cat = 0;
-    char shortname[SREP_SHORTNAME_LEN];
-
-    if (SRepCatSplitLine(str, &cat, shortname, sizeof(shortname)) != 0) {
-        printf("split failed: ");
-        return 0;
-    }
-
-    if (strcmp(shortname, "Shortname") != 0) {
-        printf("%s != Shortname: ", shortname);
-        return 0;
-    }
-
-    if (cat != 1) {
-        printf("cat 1 != %u: ", cat);
-        return 0;
-    }
-
-    return 1;
-}
-
-static int SRepTest04(void)
-{
-    int result = 0;
-
-    DetectEngineCtx *de_ctx;
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL) {
-        goto end;
-    }
-    SRepInit(de_ctx);
-
-    char str[] = "10.0.0.0/16,1,2";
-
-    Address a;
-    uint8_t cat = 0, value = 0;
-    if (SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1) {
-        goto end;
-    }
-
-    result = 1;
-
-end:
-    DetectEngineCtxFree(de_ctx);
-    return result;
-}
-
-static int SRepTest05(void)
-{
-    Packet *p = NULL;
-    int result = 0;
-    uint8_t *buf = (uint8_t *)"Hi all!";
-    uint16_t buflen = strlen((char *)buf);
-
-    p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
-    if (p == NULL) {
-        return result;
-    }
-
-    p->src.addr_data32[0] = UTHSetIPv4Address("10.0.0.1");
-
-    DetectEngineCtx *de_ctx;
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL) {
-        return result;
-    }
-    SRepInit(de_ctx);
-
-    char str[] = "10.0.0.0/16,1,20";
-
-    Address a;
-    uint8_t cat = 0, value = 0;
-    if (SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1) {
-        goto end;
-    }
-    cat = 1;
-    value = SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0);
-    if (value != 20) {
-        goto end;
-    }
-    result = 1;
-
-end:
-    UTHFreePacket(p);
-    DetectEngineCtxFree(de_ctx);
-    return result;
-}
-
-static int SRepTest06(void)
-{
-    Packet *p = NULL;
-    int result = 0;
-    uint8_t *buf = (uint8_t *)"Hi all!";
-    uint16_t buflen = strlen((char *)buf);
-
-    p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
-    if (p == NULL) {
-        return result;
-    }
-
-    p->src.addr_data32[0] = UTHSetIPv4Address("192.168.0.1");
-
-    DetectEngineCtx *de_ctx;
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL) {
-        return result;
-    }
-    SRepInit(de_ctx);
-
-    char str[] =
-        "0.0.0.0/0,1,10\n"
-        "192.168.0.0/16,2,127";
-
-    Address a;
-    uint8_t cat = 0, value = 0;
-    if (SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1) {
-        goto end;
-    }
-    cat = 1;
-    value = SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0);
-    if (value != 10) {
-        goto end;
-    }
-    result = 1;
-
-end:
-    UTHFreePacket(p);
-    DetectEngineCtxFree(de_ctx);
-    return result;
-}
-
-static int SRepTest07(void) {
-    char str[] = "2000:0000:0000:0000:0000:0000:0000:0001,";
-    int result = 0;
-    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL) {
-        return 0;
-    }
-
-    SRepInit(de_ctx);
-    Address a;
-    uint8_t cat = 0, value = 0;
-    if (SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0) {
-        goto end;
-    }
-    result = 1;
-end:
-    DetectEngineCtxFree(de_ctx);
-    return result;
-}
+#include "tests/reputation.c"
 #endif
 
 #if 0
@@ -2308,37 +2091,3 @@ error:
 
 #endif /* UNITTESTS */
 #endif
-
-/** Register the following unittests for the Reputation module */
-void SCReputationRegisterTests(void)
-{
-#ifdef UNITTESTS
-#if 0
-    UtRegisterTest("SCReputationTestIPV4AddRemoveHost01",
-                   SCReputationTestIPV4AddRemoveHost01);
-    UtRegisterTest("SCReputationTestIPV6AddRemoveHost01",
-                   SCReputationTestIPV6AddRemoveHost01);
-
-    UtRegisterTest("SCReputationTestIPV4BestExactMatch01",
-                   SCReputationTestIPV4BestExactMatch01);
-
-    UtRegisterTest("SCReputationTestIPV4AddRemoveHost02",
-                   SCReputationTestIPV4AddRemoveHost02);
-    UtRegisterTest("SCReputationTestIPV6AddRemoveHost02",
-                   SCReputationTestIPV6AddRemoveHost02);
-
-    UtRegisterTest("SCReputationTestIPV4Update01",
-                   SCReputationTestIPV4Update01);
-    UtRegisterTest("SCReputationTestIPV6Update01",
-                   SCReputationTestIPV6Update01);
-#endif
-    UtRegisterTest("SRepTest01", SRepTest01);
-    UtRegisterTest("SRepTest02", SRepTest02);
-    UtRegisterTest("SRepTest03", SRepTest03);
-    UtRegisterTest("SRepTest04", SRepTest04);
-    UtRegisterTest("SRepTest05", SRepTest05);
-    UtRegisterTest("SRepTest06", SRepTest06);
-    UtRegisterTest("SRepTest07", SRepTest07);
-#endif /* UNITTESTS */
-}
-
diff --git a/src/tests/reputation.c b/src/tests/reputation.c
new file mode 100644 (file)
index 0000000..fe7db43
--- /dev/null
@@ -0,0 +1,156 @@
+/* Copyright (C) 2019 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
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ *
+ * \author Giuseppe Longo <giuseppe@glongo.it>
+ *
+ */
+
+#include "conf-yaml-loader.h"
+#include "detect-engine.h"
+#include "stream-tcp-private.h"
+#include "stream-tcp-reassemble.h"
+#include "stream-tcp.h"
+#include "util-unittest-helper.h"
+
+#define TEST_INIT                                                       \
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();                    \
+    FAIL_IF(de_ctx == NULL);                                            \
+    SRepInit(de_ctx);                                                   \
+                                                                        \
+    Address a;                                                          \
+    uint8_t cat = 0, value = 0;
+
+#define TEST_INIT_WITH_PACKET(ip)                                       \
+    uint8_t *buf = (uint8_t *)"Hi all!";                                \
+    uint16_t buflen = strlen((char *)buf);                              \
+    Packet *p = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);    \
+    FAIL_IF(p == NULL);                                                 \
+    p->src.addr_data32[0] = UTHSetIPv4Address(ip);                      \
+    TEST_INIT
+
+#define TEST_CLEANUP                                                    \
+    DetectEngineCtxFree(de_ctx);
+
+#define TEST_CLEANUP_WITH_PACKET                                        \
+    UTHFreePacket(p);                                                   \
+    TEST_CLEANUP
+
+static int SRepTest01(void)
+{
+    TEST_INIT;
+
+    char ipstr[16];
+    char str[] = "1.2.3.4,1,2";
+    FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 0);
+    PrintInet(AF_INET, (const void *)&a.address, ipstr, sizeof(ipstr));
+    FAIL_IF(strcmp(ipstr, "1.2.3.4") != 0);
+    FAIL_IF(cat != 1);
+    FAIL_IF(value != 2);
+
+    TEST_CLEANUP;
+    PASS;
+}
+
+static int SRepTest02(void)
+{
+    TEST_INIT;
+
+    char str[] = "1.1.1.1,";
+    FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0);
+
+    TEST_CLEANUP;
+    PASS;
+}
+
+static int SRepTest03(void)
+{
+    char str[] = "1,Shortname,Long Name";
+    uint8_t cat = 0;
+    char shortname[SREP_SHORTNAME_LEN];
+
+    FAIL_IF(SRepCatSplitLine(str, &cat, shortname, sizeof(shortname)) != 0);
+    FAIL_IF(strcmp(shortname, "Shortname") != 0);
+    FAIL_IF(cat != 1);
+
+    PASS;
+}
+
+static int SRepTest04(void)
+{
+    TEST_INIT;
+
+    char str[] = "10.0.0.0/16,1,2";
+    FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
+
+    TEST_CLEANUP;
+    PASS;
+}
+
+static int SRepTest05(void)
+{
+    TEST_INIT_WITH_PACKET("10.0.0.1");
+
+    char str[] = "10.0.0.0/16,1,20";
+    FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
+
+    cat = 1;
+    FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 20);
+
+    TEST_CLEANUP_WITH_PACKET;
+    PASS;
+}
+
+static int SRepTest06(void)
+{
+    TEST_INIT_WITH_PACKET("192.168.0.1");
+
+    char str[] =
+        "0.0.0.0/0,1,10\n"
+        "192.168.0.0/16,2,127";
+
+    FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) != 1);
+
+    cat = 1;
+    FAIL_IF(SRepCIDRGetIPRepSrc(de_ctx->srepCIDR_ctx, p, cat, 0) != 10);
+
+    TEST_CLEANUP_WITH_PACKET;
+    PASS;
+}
+
+static int SRepTest07(void) {
+    TEST_INIT;
+
+    char str[] = "2000:0000:0000:0000:0000:0000:0000:0001,";
+    FAIL_IF(SRepSplitLine(de_ctx->srepCIDR_ctx, str, &a, &cat, &value) == 0);
+
+    TEST_CLEANUP;
+    PASS;
+}
+
+/** Register the following unittests for the Reputation module */
+void SCReputationRegisterTests(void)
+{
+    UtRegisterTest("SRepTest01", SRepTest01);
+    UtRegisterTest("SRepTest02", SRepTest02);
+    UtRegisterTest("SRepTest03", SRepTest03);
+    UtRegisterTest("SRepTest04", SRepTest04);
+    UtRegisterTest("SRepTest05", SRepTest05);
+    UtRegisterTest("SRepTest06", SRepTest06);
+    UtRegisterTest("SRepTest07", SRepTest07);
+}