}
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);
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) {
}
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);
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) {
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);
+ }
}
}