]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
conf: add unittest for WithDefault functions. 262/head
authorEric Leblond <eric@regit.org>
Mon, 21 Jan 2013 16:27:46 +0000 (17:27 +0100)
committerEric Leblond <eric@regit.org>
Mon, 21 Jan 2013 16:27:46 +0000 (17:27 +0100)
src/conf.c

index 10a50d5fd1249f56b9fdff3f620f90c14d6ada23..bccdbc2be6cd40191675f7e3a1e1bde7962a9f7e 100644 (file)
@@ -1045,6 +1045,94 @@ ConfNodeLookupChildValueTest(void)
     return 1;
 }
 
+static int ConfGetChildValueWithDefaultTest(void)
+{
+    char  *val;
+    int ret = 1;
+    ConfCreateContextBackup();
+    ConfInit();
+    ConfSet("af-packet.0.interface", "eth0", 1);
+    ConfSet("af-packet.1.interface", "default", 1);
+    ConfSet("af-packet.1.cluster-type", "cluster_cpu", 1);
+
+    ConfNode *root = ConfGetNode("af-packet.0");
+    ConfNode *dflt = ConfGetNode("af-packet.1");
+    ConfGetChildValueWithDefault(root, dflt, "cluster-type", &val);
+    if (strcmp(val, "cluster_cpu")) {
+        ConfDeInit();
+        ConfRestoreContextBackup();
+        return 0;
+    }
+
+    ConfSet("af-packet.0.cluster-type", "cluster_flow", 1);
+    ConfGetChildValueWithDefault(root, dflt, "cluster-type", &val);
+
+    if (strcmp(val, "cluster_flow")) {
+        ret = 0;
+    }
+    ConfDeInit();
+    ConfRestoreContextBackup();
+    return ret;
+}
+
+static int ConfGetChildValueIntWithDefaultTest(void)
+{
+    intmax_t val;
+    ConfCreateContextBackup();
+    ConfInit();
+    ConfSet("af-packet.0.interface", "eth0", 1);
+    ConfSet("af-packet.1.interface", "default", 1);
+    ConfSet("af-packet.1.threads", "2", 1);
+
+    ConfNode *root = ConfGetNode("af-packet.0");
+    ConfNode *dflt = ConfGetNode("af-packet.1");
+    ConfGetChildValueIntWithDefault(root, dflt, "threads", &val);
+    if (val != 2) {
+        ConfDeInit();
+        ConfRestoreContextBackup();
+        return 0;
+    }
+
+    ConfSet("af-packet.0.threads", "1", 1);
+    ConfGetChildValueIntWithDefault(root, dflt, "threads", &val);
+
+    ConfDeInit();
+    ConfRestoreContextBackup();
+    if (val != 1) {
+        return 0;
+    }
+    return 1;
+}
+
+static int ConfGetChildValueBoolWithDefaultTest(void)
+{
+    int val;
+    ConfCreateContextBackup();
+    ConfInit();
+    ConfSet("af-packet.0.interface", "eth0", 1);
+    ConfSet("af-packet.1.interface", "default", 1);
+    ConfSet("af-packet.1.use-mmap", "yes", 1);
+
+    ConfNode *root = ConfGetNode("af-packet.0");
+    ConfNode *dflt = ConfGetNode("af-packet.1");
+    ConfGetChildValueBoolWithDefault(root, dflt, "use-mmap", &val);
+    if (val == 0) {
+        ConfDeInit();
+        ConfRestoreContextBackup();
+        return 0;
+    }
+
+    ConfSet("af-packet.0.use-mmap", "no", 1);
+    ConfGetChildValueBoolWithDefault(root, dflt, "use-mmap", &val);
+
+    ConfDeInit();
+    ConfRestoreContextBackup();
+    if (val) {
+        return 0;
+    }
+    return 1;
+}
+
 /**
  * Test the removal of a configuration node.
  */
@@ -1120,6 +1208,9 @@ ConfRegisterTests(void)
     UtRegisterTest("ConfNodeLookupChildTest", ConfNodeLookupChildTest, 1);
     UtRegisterTest("ConfNodeLookupChildValueTest", ConfNodeLookupChildValueTest, 1);
     UtRegisterTest("ConfNodeRemoveTest", ConfNodeRemoveTest, 1);
+    UtRegisterTest("ConfGetChildValueWithDefaultTest", ConfGetChildValueWithDefaultTest, 1);
+    UtRegisterTest("ConfGetChildValueIntWithDefaultTest", ConfGetChildValueIntWithDefaultTest, 1);
+    UtRegisterTest("ConfGetChildValueBoolWithDefaultTest", ConfGetChildValueBoolWithDefaultTest, 1);
 }
 
 #endif /* UNITTESTS */