chiark / gitweb /
sd-bus: reuse the KDBUS_CMD_FREE wrapper wherever appropriate
[elogind.git] / src / test / test-unit-name.c
index 9179081279b48d0f27a6fefaf32b19f0b89fb6cf..5c7f8b40f73f4f33d749450efaf668e3d9d72e77 100644 (file)
@@ -43,7 +43,7 @@ static void test_replacements(void) {
                 _cleanup_free_ char *t =                           \
                         unit_name_replace_instance(pattern, repl); \
                 puts(t);                                           \
-                assert(streq(t, expected));                        \
+                assert_se(streq(t, expected));                        \
         }
 
         expect("foo@.service", "waldo", "foo@waldo.service");
@@ -64,7 +64,7 @@ static void test_replacements(void) {
                 puts(t);                                           \
                 k = unit_name_to_path(t);                          \
                 puts(k);                                           \
-                assert(streq(k, expected ? expected : path));      \
+                assert_se(streq(k, expected ? expected : path));      \
         }
 
         expect("/waldo", ".mount", NULL);
@@ -80,7 +80,7 @@ static void test_replacements(void) {
                 _cleanup_free_ char *t =                                     \
                         unit_name_from_path_instance(pattern, path, suffix); \
                 puts(t);                                                     \
-                assert(streq(t, expected));                                  \
+                assert_se(streq(t, expected));                                  \
         }
 
         expect("waldo", "/waldo", ".mount", "waldo@waldo.mount");
@@ -125,27 +125,28 @@ static int test_unit_printf(void) {
         assert_se((root = getpwnam("root")));
         assert_se(asprintf(&root_uid, "%d", (int) root->pw_uid) > 0);
 
-        r = manager_new(SYSTEMD_USER, &m);
+        r = manager_new(SYSTEMD_USER, true, &m);
         if (r == -EPERM || r == -EACCES || r == -EADDRINUSE) {
                 puts("manager_new: Permission denied. Skipping test.");
                 return EXIT_TEST_SKIP;
         }
-        assert(r == 0);
+        assert_se(r == 0);
 
 #define expect(unit, pattern, expected)                                 \
         {                                                               \
                 char *e;                                                \
-                _cleanup_free_ char *t;                                 \
+                _cleanup_free_ char *t = NULL;                          \
                 assert_se(unit_full_printf(unit, pattern, &t) >= 0);    \
                 printf("result: %s\nexpect: %s\n", t, expected);        \
                 if ((e = endswith(expected, "*")))                      \
-                        assert(strncmp(t, e, e-expected));              \
+                        assert_se(strncmp(t, e, e-expected));              \
                 else                                                    \
-                        assert(streq(t, expected));                     \
+                        assert_se(streq(t, expected));                     \
         }
 
         assert_se(setenv("USER", "root", 1) == 0);
         assert_se(setenv("HOME", "/root", 1) == 0);
+        assert_se(setenv("XDG_RUNTIME_DIR", "/run/user/1/", 1) == 0);
 
         assert_se(u = unit_new(m, sizeof(Service)));
         assert_se(unit_add_name(u, "blah.service") == 0);
@@ -158,6 +159,7 @@ static int test_unit_printf(void) {
 
         /* normal unit */
         expect(u, "%n", "blah.service");
+        expect(u, "%f", "/blah");
         expect(u, "%N", "blah");
         expect(u, "%p", "blah");
         expect(u, "%P", "blah");
@@ -177,6 +179,7 @@ static int test_unit_printf(void) {
 
         expect(u2, "%n", "blah@foo-foo.service");
         expect(u2, "%N", "blah@foo-foo");
+        expect(u2, "%f", "/foo/foo");
         expect(u2, "%p", "blah");
         expect(u2, "%P", "blah");
         expect(u2, "%i", "foo-foo");
@@ -190,6 +193,7 @@ static int test_unit_printf(void) {
         expect(u2, "%t", "/run/user/*");
 
         manager_free(m);
+#undef expect
 
         return 0;
 }
@@ -259,6 +263,7 @@ static void test_unit_name_is_instance(void) {
         assert_se(unit_name_is_instance("a-c_c01Aj@b05Dii_-oioi.service"));
 
         assert_se(!unit_name_is_instance("a.service"));
+        assert_se(!unit_name_is_instance("a@.service"));
         assert_se(!unit_name_is_instance("junk"));
         assert_se(!unit_name_is_instance(""));
 }
@@ -290,6 +295,11 @@ static void test_unit_name_to_instance(void) {
         assert_se(streq(instance, "bar"));
         free(instance);
 
+        r = unit_name_to_instance("foo@.service", &instance);
+        assert_se(r >= 0);
+        assert_se(streq(instance, ""));
+        free(instance);
+
         r = unit_name_to_instance("fo0-stUff_b@b.e", &instance);
         assert_se(r >= 0);
         assert_se(streq(instance, "b"));
@@ -301,6 +311,9 @@ static void test_unit_name_to_instance(void) {
 
         r = unit_name_to_instance("fooj@unk", &instance);
         assert_se(r < 0);
+
+        r = unit_name_to_instance("foo@", &instance);
+        assert_se(r < 0);
 }
 
 static void test_unit_name_escape(void) {
@@ -311,6 +324,29 @@ static void test_unit_name_escape(void) {
         assert_se(streq(r, "ab\\x2b\\x2dc.a-bc\\x40foo.service"));
 }
 
+static void test_unit_name_template(void) {
+#define expect(name, expected) \
+        { \
+                _cleanup_free_ char *f = NULL; \
+                f = unit_name_template(name); \
+                assert_se(f); \
+                printf("got: %s, expected: %s\n", f, expected); \
+                assert_se(streq(f, expected)); \
+        }
+        expect("foo@bar.service", "foo@.service")
+        expect("foo.mount", "foo.mount")
+#undef expect
+}
+
+static void test_unit_name_is_template(void) {
+        assert_se(unit_name_is_template("foo@.service"));
+        assert_se(unit_name_is_template("bar@.path"));
+
+        assert_se(!unit_name_is_template("bar@i.mount"));
+        assert_se(!unit_name_is_template("bar@foobbbb.service"));
+        assert_se(!unit_name_is_template("barfoo.service"));
+}
+
 int main(int argc, char* argv[]) {
         int rc = 0;
         test_replacements();
@@ -323,6 +359,8 @@ int main(int argc, char* argv[]) {
         test_build_subslice();
         test_unit_name_to_instance();
         test_unit_name_escape();
+        test_unit_name_template();
+        test_unit_name_is_template();
 
         return rc;
 }