]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-conf-parser.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / test / test-conf-parser.c
index aa6e4f1a953e91b1bef5fbe9606648d4a5914ba7..1738938fce61230b15fbcf9b1c975415878d6500 100644 (file)
@@ -1,18 +1,13 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2015 Ronny Chevalier
-***/
 
 #include "conf-parser.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "log.h"
 #include "macro.h"
 #include "string-util.h"
 #include "strv.h"
+#include "tmpfile-util.h"
 #include "util.h"
 
 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
@@ -215,45 +210,6 @@ static void test_config_parse_iec_uint64(void) {
         assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
 }
 
-static void test_config_parse_join_controllers(void) {
-        int r;
-        _cleanup_(strv_free_freep) char ***c = NULL;
-        char ***c2;
-
-        /* Test normal operation */
-        r = config_parse_join_controllers(NULL, "example.conf", 11, "Section", 10, "JoinControllers", 0, "cpu,cpuacct net_cls,netprio", &c, NULL);
-        assert_se(r == 0);
-        assert_se(c);
-        assert_se(strv_length(c[0]) == 2);
-        assert_se(strv_equal(c[0], STRV_MAKE("cpu", "cpuacct")));
-        assert_se(strv_length(c[1]) == 2);
-        assert_se(strv_equal(c[1], STRV_MAKE("net_cls", "netprio")));
-        assert_se(c[2] == NULL);
-
-        /* Test special case of no mounted controllers */
-        r = config_parse_join_controllers(NULL, "example.conf", 12, "Section", 10, "JoinControllers", 0, "", &c, NULL);
-        assert_se(r == 0);
-        assert_se(c);
-        assert_se(strv_equal(c[0], STRV_MAKE_EMPTY));
-        assert_se(c[1] == NULL);
-
-        /* Test merging of overlapping lists */
-        r = config_parse_join_controllers(NULL, "example.conf", 13, "Section", 10, "JoinControllers", 0, "a,b b,c", &c, NULL);
-        assert_se(r == 0);
-        assert_se(c);
-        assert_se(strv_length(c[0]) == 3);
-        assert_se(strv_contains(c[0], "a"));
-        assert_se(strv_contains(c[0], "b"));
-        assert_se(strv_contains(c[0], "c"));
-        assert_se(c[1] == NULL);
-
-        /* Test ignoring of bad lines */
-        c2 = c;
-        r = config_parse_join_controllers(NULL, "example.conf", 14, "Section", 10, "JoinControllers", 0, "a,\"b ", &c, NULL);
-        assert_se(r < 0);
-        assert_se(c == c2);
-}
-
 #define x10(x) x x x x x x x x x x
 #define x100(x) x10(x10(x))
 #define x1000(x) x10(x100(x))
@@ -279,6 +235,30 @@ static const char* const config_file[] = {
         "2\\\n"
         "3\n",
 
+        "[Section]\n"
+        "#hogehoge\\\n"      /* continuation is ignored in comment */
+        "setting1=1\\\n"     /* normal continuation */
+        "2\\\n"
+        "3\n",
+
+        "[Section]\n"
+        "setting1=1\\\n"     /* normal continuation */
+        "#hogehoge\\\n"      /* commented out line in continuation is ignored */
+        "2\\\n"
+        "3\n",
+
+        "[Section]\n"
+        "   #hogehoge\\\n"   /* whitespaces before comments */
+        "   setting1=1\\\n"  /* whitespaces before key */
+        "2\\\n"
+        "3\n",
+
+        "[Section]\n"
+        "   setting1=1\\\n"  /* whitespaces before key */
+        "   #hogehoge\\\n"   /* commented out line prefixed with whitespaces in continuation */
+        "2\\\n"
+        "3\n",
+
         "[Section]\n"
         "setting1=1\\\n"     /* continuation with extra trailing backslash at the end */
         "2\\\n"
@@ -316,9 +296,9 @@ static const char* const config_file[] = {
 
 static void test_config_parse(unsigned i, const char *s) {
         _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX";
-        int fd, r;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *setting1 = NULL;
+        int r;
 
         const ConfigTableItem items[] = {
                 { "Section", "setting1",  config_parse_string,   0, &setting1},
@@ -327,12 +307,9 @@ static void test_config_parse(unsigned i, const char *s) {
 
         log_info("== %s[%i] ==", __func__, i);
 
-        fd = mkostemp_safe(name);
-        assert_se(fd >= 0);
-        assert_se((size_t) write(fd, s, strlen(s)) == strlen(s));
-
-        assert_se(lseek(fd, 0, SEEK_SET) == 0);
-        assert_se(f = fdopen(fd, "r"));
+        assert_se(fmkostemp_safe(name, "r+", &f) == 0);
+        assert_se(fwrite(s, strlen(s), 1, f) == 1);
+        rewind(f);
 
         /*
         int config_parse(const char *unit,
@@ -358,27 +335,27 @@ static void test_config_parse(unsigned i, const char *s) {
                 assert_se(streq(setting1, "1"));
                 break;
 
-        case 4 ... 5:
+        case 4 ... 9:
                 assert_se(r == 0);
                 assert_se(streq(setting1, "1 2 3"));
                 break;
 
-        case 6:
+        case 10:
                 assert_se(r == 0);
                 assert_se(streq(setting1, "1\\\\ \\\\2"));
                 break;
 
-        case 7:
+        case 11:
                 assert_se(r == 0);
                 assert_se(streq(setting1, x1000("ABCD")));
                 break;
 
-        case 8 ... 9:
+        case 12 ... 13:
                 assert_se(r == 0);
                 assert_se(streq(setting1, x1000("ABCD") " foobar"));
                 break;
 
-        case 10 ... 11:
+        case 14 ... 15:
                 assert_se(r == -ENOBUFS);
                 assert_se(setting1 == NULL);
                 break;
@@ -403,7 +380,6 @@ int main(int argc, char **argv) {
         test_config_parse_sec();
         test_config_parse_nsec();
         test_config_parse_iec_uint64();
-        test_config_parse_join_controllers();
 
         for (i = 0; i < ELEMENTSOF(config_file); i++)
                 test_config_parse(i, config_file[i]);