Blame SOURCES/33e0595b1c7cf8fa0e7ca3a353f4380c1307dc25.patch

bb4389
From 33e0595b1c7cf8fa0e7ca3a353f4380c1307dc25 Mon Sep 17 00:00:00 2001
bb4389
From: David Rheinsberg <david.rheinsberg@gmail.com>
bb4389
Date: Thu, 5 May 2022 10:50:31 +0200
bb4389
Subject: [PATCH] test-config: add tests for some config samples
bb4389
bb4389
Add infrastructure to easily parse config-samples in our test. This
bb4389
allows us to add any reports about broken configurations easily, and
bb4389
making sure we will not run into the same issues again.
bb4389
bb4389
Signed-off-by: David Rheinsberg <david.rheinsberg@gmail.com>
bb4389
---
bb4389
 src/launch/test-config.c | 97 +++++++++++++++++++++++++++++++++++++---
bb4389
 1 file changed, 91 insertions(+), 6 deletions(-)
bb4389
bb4389
diff --git a/src/launch/test-config.c b/src/launch/test-config.c
bb4389
index 0401a434..c2f8765e 100644
bb4389
--- a/src/launch/test-config.c
bb4389
+++ b/src/launch/test-config.c
bb4389
@@ -9,6 +9,7 @@
bb4389
 #include "launch/config.h"
bb4389
 #include "launch/nss-cache.h"
bb4389
 #include "util/dirwatch.h"
bb4389
+#include "util/syscall.h"
bb4389
 
bb4389
 static const char *test_type2str[_CONFIG_NODE_N] = {
bb4389
         [CONFIG_NODE_BUSCONFIG]         = "busconfig",
bb4389
@@ -35,12 +36,23 @@ static const char *test_type2str[_CONFIG_NODE_N] = {
bb4389
         [CONFIG_NODE_ASSOCIATE]         = "associate",
bb4389
 };
bb4389
 
bb4389
-static void print_config(const char *path) {
bb4389
+static int config_memfd(const char *data) {
bb4389
+        ssize_t n;
bb4389
+        int fd;
bb4389
+
bb4389
+        fd = syscall_memfd_create("dbus-broker-test-config", 0);
bb4389
+        c_assert(fd >= 0);
bb4389
+        n = write(fd, data, strlen(data));
bb4389
+        c_assert(n == (ssize_t)strlen(data));
bb4389
+
bb4389
+        return fd;
bb4389
+}
bb4389
+
bb4389
+static int parse_config(ConfigRoot **rootp, const char *path) {
bb4389
         _c_cleanup_(config_parser_deinit) ConfigParser parser = CONFIG_PARSER_NULL(parser);
bb4389
         _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
bb4389
         _c_cleanup_(nss_cache_deinit) NSSCache nss_cache = NSS_CACHE_INIT;
bb4389
         _c_cleanup_(dirwatch_freep) Dirwatch *dirwatch = NULL;
bb4389
-        ConfigNode *i_node;
bb4389
         int r;
bb4389
 
bb4389
         r = dirwatch_new(&dirwatch);
bb4389
@@ -49,6 +61,32 @@ static void print_config(const char *path) {
bb4389
         config_parser_init(&parser);
bb4389
 
bb4389
         r = config_parser_read(&parser, &root, path, &nss_cache, dirwatch);
bb4389
+        if (r)
bb4389
+                return r;
bb4389
+
bb4389
+        *rootp = root;
bb4389
+        root = NULL;
bb4389
+        return 0;
bb4389
+}
bb4389
+
bb4389
+static int parse_config_inline(ConfigRoot **rootp, const char *data) {
bb4389
+        _c_cleanup_(c_closep) int fd = -1;
bb4389
+        _c_cleanup_(c_freep) char *path = NULL;
bb4389
+        int r;
bb4389
+
bb4389
+        fd = config_memfd(data);
bb4389
+        r = asprintf(&path, "/proc/self/fd/%d", fd);
bb4389
+        c_assert(r > 0);
bb4389
+
bb4389
+        return parse_config(rootp, path);
bb4389
+}
bb4389
+
bb4389
+static void print_config(const char *path) {
bb4389
+        _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
bb4389
+        ConfigNode *i_node;
bb4389
+        int r;
bb4389
+
bb4389
+        r = parse_config(&root, path);
bb4389
         c_assert(!r);
bb4389
 
bb4389
         c_list_for_each_entry(i_node, &root->node_list, root_link) {
bb4389
@@ -56,18 +94,65 @@ static void print_config(const char *path) {
bb4389
         }
bb4389
 }
bb4389
 
bb4389
-static void test_config(void) {
bb4389
+static void test_config_base(void) {
bb4389
         _c_cleanup_(config_parser_deinit) ConfigParser parser = CONFIG_PARSER_NULL(parser);
bb4389
 
bb4389
         config_parser_init(&parser);
bb4389
         config_parser_deinit(&parser);
bb4389
 }
bb4389
 
bb4389
+static void test_config_sample0(void) {
bb4389
+        _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
bb4389
+        const char *data;
bb4389
+        int r;
bb4389
+
bb4389
+        data =
bb4389
+" \
bb4389
+
bb4389
+	\"htt\">\
bb4389
+<busconfig>\
bb4389
+	<policy user=\"root\">\
bb4389
+		<allow own_prefix=\"oramd\"/>\
bb4389
+		<allow send_interface=\"d\"/>\
bb4389
+	</policy>\
bb4389
+		<user ix=\"d\"/>\
bb4389
+	</cy>";
bb4389
+
bb4389
+        r = parse_config_inline(&root, data);
bb4389
+        c_assert(r == CONFIG_E_INVALID);
bb4389
+}
bb4389
+
bb4389
+static void test_config_sample1(void) {
bb4389
+        _c_cleanup_(config_root_freep) ConfigRoot *root = NULL;
bb4389
+        const char *data;
bb4389
+        int r;
bb4389
+
bb4389
+        data =
bb4389
+" \
bb4389
+
bb4389
+	\"htt\">\
bb4389
+<busconfig>\
bb4389
+	<policy user=\"root\">\
bb4389
+		<allow own_prefix=\"oramd\"/>\
bb4389
+		<allow send_interface=\"d\"/>\
bb4389
+	</policy>\
bb4389
+	<policy context=\"default\"/>		<user ix=\"d\"/>\
bb4389
+	</policy>\
bb4389
+</busconfig>";
bb4389
+
bb4389
+        r = parse_config_inline(&root, data);
bb4389
+        c_assert(r == CONFIG_E_INVALID);
bb4389
+}
bb4389
+
bb4389
 int main(int argc, char **argv) {
bb4389
-        if (argc < 2)
bb4389
-                test_config();
bb4389
-        else
bb4389
+        if (argc > 1) {
bb4389
                 print_config(argv[1]);
bb4389
+                return 0;
bb4389
+        }
bb4389
+
bb4389
+        test_config_base();
bb4389
+        test_config_sample0();
bb4389
+        test_config_sample1();
bb4389
 
bb4389
         return 0;
bb4389
 }