+static void check_p_d_u(const char *path, int code, const char *result) {
+ _cleanup_free_ char *unit = NULL;
+
+ assert_se(cg_path_decode_unit(path, &unit) == code);
+ assert_se(streq_ptr(unit, result));
+}
+
+static void test_path_decode_unit(void) {
+ check_p_d_u("getty@.service/tty2", 0, "getty@tty2.service");
+ check_p_d_u("getty@.service/tty2/xxx", 0, "getty@tty2.service");
+ check_p_d_u("getty@.service/", -EINVAL, NULL);
+ check_p_d_u("getty@.service", -EINVAL, NULL);
+ check_p_d_u("getty.service", 0, "getty.service");
+ check_p_d_u("getty", -EINVAL, NULL);
+}
+
+static void check_p_g_u(const char *path, int code, const char *result) {
+ _cleanup_free_ char *unit = NULL;
+
+ assert_se(cg_path_get_unit(path, &unit) == code);
+ assert_se(streq_ptr(unit, result));
+}
+
+static void check_p_g_u_u(const char *path, int code, const char *result) {
+ _cleanup_free_ char *unit = NULL;
+
+ assert_se(cg_path_get_user_unit(path, &unit) == code);
+ assert_se(streq_ptr(unit, result));
+}
+
+static void test_path_get_unit(void) {
+ check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service");
+ check_p_g_u("/system/getty@.service/tty5", 0, "getty@tty5.service");
+ check_p_g_u("/system/getty@.service/tty5/aaa/bbb", 0, "getty@tty5.service");
+ check_p_g_u("/system/getty@.service/tty5/", 0, "getty@tty5.service");
+ check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service");
+ check_p_g_u("sadfdsafsda", -ENOENT, NULL);
+ check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);