From 779ed358f2e31d553f08dcb0ca351684d906251e Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 9 Feb 2026 12:13:51 +0000 Subject: [PATCH] test: Add basic tests for path_split_prefix_filename() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/test/test-path-util.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 41a652648a3..ef4f29172d8 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -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); + } } } -- 2.47.3