]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
conf: new function: ConfNodeHasChildren
authorJason Ish <ish@unx.ca>
Tue, 30 Jan 2018 21:22:10 +0000 (15:22 -0600)
committerJason Ish <ish@unx.ca>
Wed, 31 Jan 2018 12:33:46 +0000 (06:33 -0600)
Test if a configuration node has any children, indicating
that it is a non-empty map or sequence.

src/conf.c
src/conf.h

index 4d7ad84be5d125ce157bf362af71aff13ec9e3fb..eae52161fde9dcec25f643199391148b405bac3f 100644 (file)
@@ -782,6 +782,25 @@ void ConfDump(void)
     ConfNodeDump(root, NULL);
 }
 
+/**
+ * \brief Check if a node has any children.
+ *
+ * Checks if the provided node has any children. Any node that is a
+ * YAML map or array will have children.
+ *
+ * \param node The node to check.
+ *
+ * \retval true if node has children
+ * \retval false if node does not have children
+ */
+bool ConfNodeHasChildren(const ConfNode *node)
+{
+    if (TAILQ_EMPTY(&node->head)) {
+        return false;
+    }
+    return true;
+}
+
 /**
  * \brief Lookup a child configuration node by name.
  *
@@ -1419,6 +1438,29 @@ static int ConfSetFromStringTest(void)
     PASS;
 }
 
+static int ConfNodeHasChildrenTest(void)
+{
+    ConfCreateContextBackup();
+    ConfInit();
+
+    /* Set a plain key with value. */
+    ConfSet("no-children", "value");
+    ConfNode *n = ConfGetNode("no-children");
+    FAIL_IF_NULL(n);
+    FAIL_IF(ConfNodeHasChildren(n));
+
+    /* Set a key with a sub key to a value. This makes the first key a
+     * map. */
+    ConfSet("parent.child", "value");
+    n = ConfGetNode("parent");
+    FAIL_IF_NULL(n);
+    FAIL_IF(!ConfNodeHasChildren(n));
+
+    ConfDeInit();
+    ConfRestoreContextBackup();
+    PASS;
+}
+
 void ConfRegisterTests(void)
 {
     UtRegisterTest("ConfTestGetNonExistant", ConfTestGetNonExistant);
@@ -1442,6 +1484,7 @@ void ConfRegisterTests(void)
     UtRegisterTest("ConfNodePruneTest", ConfNodePruneTest);
     UtRegisterTest("ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
     UtRegisterTest("ConfSetFromStringTest", ConfSetFromStringTest);
+    UtRegisterTest("ConfNodeHasChildrenTest", ConfNodeHasChildrenTest);
 }
 
 #endif /* UNITTESTS */
index e44a45d5ce2e09edc662c7dd77ed7c174f5c441b..daedbf9248188ae79a0c12f3f9a2af1793bd88a1 100644 (file)
@@ -81,6 +81,7 @@ int ConfValIsTrue(const char *val);
 int ConfValIsFalse(const char *val);
 void ConfNodePrune(ConfNode *node);
 int ConfRemove(const char *name);
+bool ConfNodeHasChildren(const ConfNode *node);
 
 ConfNode *ConfGetChildWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name);
 ConfNode *ConfNodeLookupKeyValue(const ConfNode *base, const char *key, const char *value);