]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-unit-name: add usual headers and add more verbose output
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 12 Nov 2019 10:47:20 +0000 (11:47 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 12 Nov 2019 10:52:22 +0000 (11:52 +0100)
This makes it easier to see what unit_name_is_valid() returns at a glance.
The output is not whitespace clean, but I think it's good enough for a test.

src/test/test-unit-name.c

index c9bbce0d2e43ae85ba8ed3b5354353dfd467b0ad..24436cab0b987659074fa09a903e69966bb0a84d 100644 (file)
 #include "user-util.h"
 #include "util.h"
 
+static void test_unit_name_is_valid_one(const char *name, UnitNameFlags flags, bool expected) {
+        log_info("%s ( %s%s%s ): %s",
+                 name,
+                 (flags & UNIT_NAME_PLAIN) ? "plain" : "",
+                 (flags & UNIT_NAME_INSTANCE) ? " instance" : "",
+                 (flags & UNIT_NAME_TEMPLATE) ? " template" : "",
+                 yes_no(expected));
+        assert_se(unit_name_is_valid(name, flags) == expected);
+}
+
 static void test_unit_name_is_valid(void) {
-        assert_se( unit_name_is_valid("foo.service", UNIT_NAME_ANY));
-        assert_se( unit_name_is_valid("foo.service", UNIT_NAME_PLAIN));
-        assert_se(!unit_name_is_valid("foo.service", UNIT_NAME_INSTANCE));
-        assert_se(!unit_name_is_valid("foo.service", UNIT_NAME_TEMPLATE));
-        assert_se(!unit_name_is_valid("foo.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE));
-
-        assert_se( unit_name_is_valid("foo@bar.service", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("foo@bar.service", UNIT_NAME_PLAIN));
-        assert_se( unit_name_is_valid("foo@bar.service", UNIT_NAME_INSTANCE));
-        assert_se(!unit_name_is_valid("foo@bar.service", UNIT_NAME_TEMPLATE));
-        assert_se( unit_name_is_valid("foo@bar.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE));
-
-        assert_se( unit_name_is_valid("foo@bar@bar.service", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("foo@bar@bar.service", UNIT_NAME_PLAIN));
-        assert_se( unit_name_is_valid("foo@bar@bar.service", UNIT_NAME_INSTANCE));
-        assert_se(!unit_name_is_valid("foo@bar@bar.service", UNIT_NAME_TEMPLATE));
-        assert_se( unit_name_is_valid("foo@bar@bar.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE));
-
-        assert_se( unit_name_is_valid("foo@.service", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("foo@.service", UNIT_NAME_PLAIN));
-        assert_se(!unit_name_is_valid("foo@.service", UNIT_NAME_INSTANCE));
-        assert_se( unit_name_is_valid("foo@.service", UNIT_NAME_TEMPLATE));
-        assert_se( unit_name_is_valid("foo@.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE));
-        assert_se( unit_name_is_valid(".test.service", UNIT_NAME_PLAIN));
-        assert_se( unit_name_is_valid(".test@.service", UNIT_NAME_TEMPLATE));
-        assert_se( unit_name_is_valid("_strange::::.service", UNIT_NAME_ANY));
-
-        assert_se(!unit_name_is_valid(".service", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("foo.waldo", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("@.service", UNIT_NAME_ANY));
-        assert_se(!unit_name_is_valid("@piep.service", UNIT_NAME_ANY));
-
-        assert_se( unit_name_is_valid("user@1000.slice", UNIT_NAME_ANY));
-        assert_se( unit_name_is_valid("user@1000.slice", UNIT_NAME_INSTANCE));
-        assert_se(!unit_name_is_valid("user@1000.slice", UNIT_NAME_TEMPLATE));
+        log_info("/* %s */", __func__);
+
+        test_unit_name_is_valid_one("foo.service", UNIT_NAME_ANY, true);
+        test_unit_name_is_valid_one("foo.service", UNIT_NAME_PLAIN, true);
+        test_unit_name_is_valid_one("foo.service", UNIT_NAME_INSTANCE, false);
+        test_unit_name_is_valid_one("foo.service", UNIT_NAME_TEMPLATE, false);
+        test_unit_name_is_valid_one("foo.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE, false);
+
+        test_unit_name_is_valid_one("foo@bar.service", UNIT_NAME_ANY, true);
+        test_unit_name_is_valid_one("foo@bar.service", UNIT_NAME_PLAIN, false);
+        test_unit_name_is_valid_one("foo@bar.service", UNIT_NAME_INSTANCE, true);
+        test_unit_name_is_valid_one("foo@bar.service", UNIT_NAME_TEMPLATE, false);
+        test_unit_name_is_valid_one("foo@bar.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE, true);
+
+        test_unit_name_is_valid_one("foo@bar@bar.service", UNIT_NAME_ANY, true);
+        test_unit_name_is_valid_one("foo@bar@bar.service", UNIT_NAME_PLAIN, false);
+        test_unit_name_is_valid_one("foo@bar@bar.service", UNIT_NAME_INSTANCE, true);
+        test_unit_name_is_valid_one("foo@bar@bar.service", UNIT_NAME_TEMPLATE, false);
+        test_unit_name_is_valid_one("foo@bar@bar.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE, true);
+
+        test_unit_name_is_valid_one("foo@.service", UNIT_NAME_ANY, true);
+        test_unit_name_is_valid_one("foo@.service", UNIT_NAME_PLAIN, false);
+        test_unit_name_is_valid_one("foo@.service", UNIT_NAME_INSTANCE, false);
+        test_unit_name_is_valid_one("foo@.service", UNIT_NAME_TEMPLATE, true);
+        test_unit_name_is_valid_one("foo@.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE, true);
+        test_unit_name_is_valid_one(".test.service", UNIT_NAME_PLAIN, true);
+        test_unit_name_is_valid_one(".test@.service", UNIT_NAME_TEMPLATE, true);
+        test_unit_name_is_valid_one("_strange::::.service", UNIT_NAME_ANY, true);
+
+        test_unit_name_is_valid_one(".service", UNIT_NAME_ANY, false);
+        test_unit_name_is_valid_one("", UNIT_NAME_ANY, false);
+        test_unit_name_is_valid_one("foo.waldo", UNIT_NAME_ANY, false);
+        test_unit_name_is_valid_one("@.service", UNIT_NAME_ANY, false);
+        test_unit_name_is_valid_one("@piep.service", UNIT_NAME_ANY, false);
+
+        test_unit_name_is_valid_one("user@1000.slice", UNIT_NAME_ANY, true);
+        test_unit_name_is_valid_one("user@1000.slice", UNIT_NAME_INSTANCE, true);
+        test_unit_name_is_valid_one("user@1000.slice", UNIT_NAME_TEMPLATE, false);
 }
 
 static void test_unit_name_replace_instance_one(const char *pattern, const char *repl, const char *expected, int ret) {
@@ -71,7 +83,8 @@ static void test_unit_name_replace_instance_one(const char *pattern, const char
 }
 
 static void test_unit_name_replace_instance(void) {
-        puts("-------------------------------------------------");
+        log_info("/* %s */", __func__);
+
         test_unit_name_replace_instance_one("foo@.service", "waldo", "foo@waldo.service", 0);
         test_unit_name_replace_instance_one("foo@xyz.service", "waldo", "foo@waldo.service", 0);
         test_unit_name_replace_instance_one("xyz", "waldo", NULL, -EINVAL);
@@ -98,7 +111,8 @@ static void test_unit_name_from_path_one(const char *path, const char *suffix, c
 }
 
 static void test_unit_name_from_path(void) {
-        puts("-------------------------------------------------");
+        log_info("/* %s */", __func__);
+
         test_unit_name_from_path_one("/waldo", ".mount", "waldo.mount", 0);
         test_unit_name_from_path_one("/waldo/quuix", ".mount", "waldo-quuix.mount", 0);
         test_unit_name_from_path_one("/waldo/quuix/", ".mount", "waldo-quuix.mount", 0);
@@ -126,7 +140,7 @@ static void test_unit_name_from_path_instance_one(const char *pattern, const cha
 }
 
 static void test_unit_name_from_path_instance(void) {
-        puts("-------------------------------------------------");
+        log_info("/* %s */", __func__);
 
         test_unit_name_from_path_instance_one("waldo", "/waldo", ".mount", "waldo@waldo.mount", 0);
         test_unit_name_from_path_instance_one("waldo", "/waldo////quuix////", ".mount", "waldo@waldo-quuix.mount", 0);
@@ -146,6 +160,8 @@ static void test_unit_name_to_path_one(const char *unit, const char *path, int r
 }
 
 static void test_unit_name_to_path(void) {
+        log_info("/* %s */", __func__);
+
         test_unit_name_to_path_one("home.mount", "/home", 0);
         test_unit_name_to_path_one("home-lennart.mount", "/home/lennart", 0);
         test_unit_name_to_path_one("home-lennart-.mount", NULL, -EINVAL);
@@ -175,7 +191,8 @@ static void test_unit_name_mangle_one(bool allow_globs, const char *pattern, con
 }
 
 static void test_unit_name_mangle(void) {
-        puts("-------------------------------------------------");
+        log_info("/* %s */", __func__);
+
         test_unit_name_mangle_one(false, "foo.service", "foo.service", 0);
         test_unit_name_mangle_one(false, "/home", "home.mount", 1);
         test_unit_name_mangle_one(false, "/dev/sda", "dev-sda.device", 1);
@@ -198,6 +215,8 @@ static int test_unit_printf(void) {
         Unit *u;
         int r;
 
+        log_info("/* %s */", __func__);
+
         assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid);
         assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid);
         assert_se(host = gethostname_malloc());
@@ -299,6 +318,8 @@ static int test_unit_printf(void) {
 }
 
 static void test_unit_instance_is_valid(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(unit_instance_is_valid("fooBar"));
         assert_se(unit_instance_is_valid("foo-bar"));
         assert_se(unit_instance_is_valid("foo.stUff"));
@@ -312,6 +333,8 @@ static void test_unit_instance_is_valid(void) {
 }
 
 static void test_unit_prefix_is_valid(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(unit_prefix_is_valid("fooBar"));
         assert_se(unit_prefix_is_valid("foo-bar"));
         assert_se(unit_prefix_is_valid("foo.stUff"));
@@ -328,6 +351,8 @@ static void test_unit_prefix_is_valid(void) {
 static void test_unit_name_change_suffix(void) {
         char *t;
 
+        log_info("/* %s */", __func__);
+
         assert_se(unit_name_change_suffix("foo.mount", ".service", &t) == 0);
         assert_se(streq(t, "foo.service"));
         free(t);
@@ -340,6 +365,8 @@ static void test_unit_name_change_suffix(void) {
 static void test_unit_name_build(void) {
         char *t;
 
+        log_info("/* %s */", __func__);
+
         assert_se(unit_name_build("foo", "bar", ".service", &t) == 0);
         assert_se(streq(t, "foo@bar.service"));
         free(t);
@@ -354,6 +381,8 @@ static void test_unit_name_build(void) {
 }
 
 static void test_slice_name_is_valid(void) {
+        log_info("/* %s */", __func__);
+
         assert_se( slice_name_is_valid(SPECIAL_ROOT_SLICE));
         assert_se( slice_name_is_valid("foo.slice"));
         assert_se( slice_name_is_valid("foo-bar.slice"));
@@ -386,6 +415,8 @@ static void test_build_subslice(void) {
         char *a;
         char *b;
 
+        log_info("/* %s */", __func__);
+
         assert_se(slice_build_subslice(SPECIAL_ROOT_SLICE, "foo", &a) >= 0);
         assert_se(slice_build_subslice(a, "bar", &b) >= 0);
         free(a);
@@ -408,6 +439,8 @@ static void test_build_parent_slice_one(const char *name, const char *expect, in
 }
 
 static void test_build_parent_slice(void) {
+        log_info("/* %s */", __func__);
+
         test_build_parent_slice_one(SPECIAL_ROOT_SLICE, NULL, 0);
         test_build_parent_slice_one("foo.slice", SPECIAL_ROOT_SLICE, 1);
         test_build_parent_slice_one("foo-bar.slice", "foo.slice", 1);
@@ -430,6 +463,8 @@ static void test_unit_name_to_instance(void) {
         char *instance;
         int r;
 
+        log_info("/* %s */", __func__);
+
         r = unit_name_to_instance("foo@bar.service", &instance);
         assert_se(r == UNIT_NAME_INSTANCE);
         assert_se(streq(instance, "bar"));
@@ -461,6 +496,8 @@ static void test_unit_name_to_instance(void) {
 static void test_unit_name_escape(void) {
         _cleanup_free_ char *r;
 
+        log_info("/* %s */", __func__);
+
         r = unit_name_escape("ab+-c.a/bc@foo.service");
         assert_se(r);
         assert_se(streq(r, "ab\\x2b\\x2dc.a-bc\\x40foo.service"));
@@ -475,6 +512,8 @@ static void test_u_n_t_one(const char *name, const char *expected, int ret) {
 }
 
 static void test_unit_name_template(void) {
+        log_info("/* %s */", __func__);
+
         test_u_n_t_one("foo@bar.service", "foo@.service", 0);
         test_u_n_t_one("foo.mount", NULL, -EINVAL);
 }
@@ -487,6 +526,7 @@ static void test_unit_name_path_unescape_one(const char *name, const char *path,
 }
 
 static void test_unit_name_path_unescape(void) {
+        log_info("/* %s */", __func__);
 
         test_unit_name_path_unescape_one("foo", "/foo", 0);
         test_unit_name_path_unescape_one("foo-bar", "/foo/bar", 0);
@@ -510,6 +550,8 @@ static void test_unit_name_to_prefix_one(const char *input, int ret, const char
 }
 
 static void test_unit_name_to_prefix(void) {
+        log_info("/* %s */", __func__);
+
         test_unit_name_to_prefix_one("foobar.service", 0, "foobar");
         test_unit_name_to_prefix_one("", -EINVAL, NULL);
         test_unit_name_to_prefix_one("foobar", -EINVAL, NULL);
@@ -530,6 +572,8 @@ static void test_unit_name_from_dbus_path_one(const char *input, int ret, const
 }
 
 static void test_unit_name_from_dbus_path(void) {
+        log_info("/* %s */", __func__);
+
         test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/dbus_2esocket", 0, "dbus.socket");
         test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/_2d_2emount", 0, "-.mount");
         test_unit_name_from_dbus_path_one("/org/freedesktop/systemd1/unit/_2d_2eslice", 0, "-.slice");