Blame SOURCES/33e0595b1c7cf8fa0e7ca3a353f4380c1307dc25.patch

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