]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add test for pakfire_string_endswith
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 5 Oct 2021 14:13:55 +0000 (14:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 5 Oct 2021 14:13:55 +0000 (14:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/util.c
tests/libpakfire/util.c

index f9d89d07e489cf6918595c6def78c9e816d65cc3..553c83af65ec79a0a9c061961e9af723c6ab7210 100644 (file)
@@ -167,6 +167,12 @@ int pakfire_string_startswith(const char* s, const char* prefix) {
 }
 
 int pakfire_string_endswith(const char* s, const char* suffix) {
+       // Validate input
+       if (!s || !suffix) {
+               errno = EINVAL;
+               return 1;
+       }
+
        return !strcmp(s + strlen(s) - strlen(suffix), suffix);
 }
 
index c7fd4b3757ce567565e962d24bb3bba000ce043c..ffddc897a10f0f63faae1dafe3556c2b9ef8cf1a 100644 (file)
@@ -68,6 +68,21 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_string_endswith(const struct test* t) {
+       ASSERT_TRUE(pakfire_string_endswith("ABC", "C"));
+       ASSERT_FALSE(pakfire_string_endswith("ABC", "B"));
+
+       // Check for invalid inputs
+       ASSERT_ERRNO(pakfire_string_endswith("ABC", NULL), EINVAL);
+       ASSERT_ERRNO(pakfire_string_endswith(NULL, "ABC"), EINVAL);
+       ASSERT_ERRNO(pakfire_string_endswith(NULL, NULL), EINVAL);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
 static int test_string_partition(const struct test* t) {
        char* part1;
        char* part2;
@@ -174,6 +189,7 @@ int main(int argc, char** argv) {
        testsuite_add_test(test_basename);
        testsuite_add_test(test_dirname);
        testsuite_add_test(test_string_startswith);
+       testsuite_add_test(test_string_endswith);
        testsuite_add_test(test_string_partition);
        testsuite_add_test(test_string_replace);
        testsuite_add_test(test_string_split);