]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Add basic tests for path_split_prefix_filename()
authorPhilip Withnall <pwithnall@gnome.org>
Mon, 9 Feb 2026 12:13:51 +0000 (12:13 +0000)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 9 Feb 2026 15:26:40 +0000 (16:26 +0100)
These aren’t anything comprehensive, but provide some basic assurances
that it’s working correctly. In particular, they test its behaviour when
*both* the prefix and filename components are requested.

Split out from the original version of this function which was part
of #40236.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
src/test/test-path-util.c

index 41a652648a35a1321c9e12bfdb73d6d39a96756c..ef4f29172d838b1868cea3fdc408bc1b37869c08 100644 (file)
@@ -1027,7 +1027,7 @@ TEST(last_path_component) {
 }
 
 static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
-        _cleanup_free_ char *k = NULL;
+        _cleanup_free_ char *k = NULL, *k2 = NULL;
         int r;
 
         r = path_extract_filename(input, &k);
@@ -1037,6 +1037,13 @@ static void test_path_extract_filename_one(const char *input, const char *output
                  strnull(output), ret < 0 ? STRERROR(ret) : "-");
         ASSERT_STREQ(k, output);
         assert_se(r == ret);
+
+        /* Extra safety check: make sure that path_split_prefix_filename() behaves the same */
+        r = path_split_prefix_filename(input, NULL, &k2);
+        if (r >= 0) {
+                ASSERT_STREQ(k2, k);
+                assert_se(r == ret);
+        }
 }
 
 TEST(path_extract_filename) {
@@ -1071,7 +1078,7 @@ TEST(path_extract_filename) {
 }
 
 static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
-        _cleanup_free_ char *k = NULL;
+        _cleanup_free_ char *k = NULL, *k2 = NULL;
         int r;
 
         r = path_extract_directory(input, &k);
@@ -1082,10 +1089,18 @@ static void test_path_extract_directory_one(const char *input, const char *outpu
         ASSERT_STREQ(k, output);
         assert_se(r == ret);
 
+        /* Extra safety check: make sure that path_split_prefix_filename() behaves the same.
+         * We can’t check the return value from it though as that differs based on the filename component.
+         * We can only assert that if path_extract_directory() fails, then
+         * path_split_prefix_filename() must also fail. */
+        r = path_split_prefix_filename(input, &k2, NULL);
+        ASSERT_STREQ(k2, k);
+        assert_se(!(ret < 0) || r < 0);
+
         /* Extra safety check: let's make sure that if we split out the filename too (and it works) the
          * joined parts are identical to the original again */
         if (r >= 0) {
-                _cleanup_free_ char *f = NULL;
+                _cleanup_free_ char *f = NULL, *k3 = NULL, *f2 = NULL;
 
                 r = path_extract_filename(input, &f);
                 if (r >= 0) {
@@ -1094,6 +1109,13 @@ static void test_path_extract_directory_one(const char *input, const char *outpu
                         assert_se(j = path_join(k, f));
                         assert_se(path_equal(input, j));
                 }
+
+                /* And the same, but for path_split_prefix_filename() */
+                r = path_split_prefix_filename(input, &k3, &f2);
+                if (r >= 0) {
+                        ASSERT_STREQ(k3, k);
+                        ASSERT_STREQ(f2, f);
+                }
         }
 }