]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect-metadata: add unit test
authorEric Leblond <eric@regit.org>
Wed, 25 Oct 2017 19:46:05 +0000 (21:46 +0200)
committerJason Ish <ish@unx.ca>
Tue, 30 Jan 2018 19:37:56 +0000 (13:37 -0600)
src/detect-metadata.c

index ae61735c39a9584b36ae4a84dbecaac6ef404671..5ee6dba0b2b2bea8a4fd4d6939565f6bd7e07452 100644 (file)
 #include "suricata-common.h"
 #include "detect.h"
 #include "detect-parse.h"
+#include "detect-engine.h"
 #include "detect-metadata.h"
 
+#include "util-unittest.h"
+
 #define PARSE_REGEX "^\\s*([^\\s]+)\\s+([^\\s]+)(?:,\\s*([^\\s]+)\\s+([^\\s]+))*$"
 #define PARSE_TAG_REGEX "\\s*([^\\s]+)\\s+([^,]+)\\s*"
 
@@ -40,6 +43,7 @@ static pcre *parse_tag_regex;
 static pcre_extra *parse_tag_regex_study;
 
 static int DetectMetadataSetup (DetectEngineCtx *, Signature *, const char *);
+static void DetectMetadataRegisterTests(void);
 
 void DetectMetadataRegister (void)
 {
@@ -49,7 +53,7 @@ void DetectMetadataRegister (void)
     sigmatch_table[DETECT_METADATA].Match = NULL;
     sigmatch_table[DETECT_METADATA].Setup = DetectMetadataSetup;
     sigmatch_table[DETECT_METADATA].Free  = NULL;
-    sigmatch_table[DETECT_METADATA].RegisterTests = NULL;
+    sigmatch_table[DETECT_METADATA].RegisterTests = DetectMetadataRegisterTests;
 
     DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
     DetectSetupParseRegexes(PARSE_TAG_REGEX, &parse_tag_regex, &parse_tag_regex_study);
@@ -173,3 +177,44 @@ static int DetectMetadataSetup(DetectEngineCtx *de_ctx, Signature *s, const char
     return 0;
 }
 
+#ifdef UNITTESTS
+
+static int DetectMetadataParseTest01(void)
+{
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    DetectMetadata *dm;
+    FAIL_IF_NULL(de_ctx);
+
+    Signature *sig = DetectEngineAppendSig(de_ctx,
+                                           "alert tcp any any -> any any "
+                                           "(metadata: toto 1; "
+                                           "metadata: titi 2, jaivu gros_minet;"
+                                           "sid:1; rev:1;)");
+    FAIL_IF_NULL(sig);
+    FAIL_IF_NULL(sig->metadata); 
+    FAIL_IF_NULL(sig->metadata->key); 
+    FAIL_IF(strcmp("jaivu", sig->metadata->key));
+    FAIL_IF(strcmp("gros_minet", sig->metadata->value));
+    FAIL_IF_NULL(sig->metadata->next); 
+    dm = sig->metadata->next;
+    FAIL_IF(strcmp("titi", dm->key));
+    dm = dm->next;
+    FAIL_IF_NULL(dm);
+    FAIL_IF(strcmp("toto", dm->key));
+
+    DetectEngineCtxFree(de_ctx);
+    PASS;
+}
+
+#endif /* UNITTESTS */
+
+/**
+ * \brief this function registers unit tests for DetectCipService
+ */
+static void DetectMetadataRegisterTests(void)
+{
+#ifdef UNITTESTS
+    UtRegisterTest("DetectMetadataParseTest01", DetectMetadataParseTest01);
+#endif /* UNITTESTS */
+}
+