From: Kevin Day Date: Sat, 9 Apr 2022 05:25:03 +0000 (-0500) Subject: Update: Fix the f_directory unit tests and finish writing them. X-Git-Tag: 0.5.10~220 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=bdc5fe74cf3c31f022119414d7ac597feca5a58e;p=fll Update: Fix the f_directory unit tests and finish writing them. The problem with the unit tests not being run (especially on Github) appears to be the result of compiler optimizations. Fix this by disabling compiler optimizations in the mock functions and add appropriate commetns explaining this. Do not wrap/mock strncmp() and strnlen(). The directory tests are now completed. --- diff --git a/level_0/f_account/data/build/settings-mocks b/level_0/f_account/data/build/settings-mocks index abd5dd9..50b5d2e 100644 --- a/level_0/f_account/data/build/settings-mocks +++ b/level_0/f_account/data/build/settings-mocks @@ -6,6 +6,9 @@ # Therefore, this file is a work-around to inject the mocks into the library for testing purposes. # This should exactly match the "settings" file, except for the additional "-Wl,--wrap" parts and the additional mock source file. # +# The flags -o0 must be passed to prevent the compiler from optimizing away any functions being mocked (which results in the mock not happening and a real function being called). +# Alternatively, figure out which optimization that is disabled by -o0 and have that specific optimization disabled. +# build_name f_account @@ -47,7 +50,7 @@ search_exclusive yes search_shared yes search_static yes -flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses +flags -O0 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses flags-clang -Wno-logical-op-parentheses flags-test -fstack-protector diff --git a/level_0/f_capability/data/build/settings-mocks b/level_0/f_capability/data/build/settings-mocks index e25492a..9268328 100644 --- a/level_0/f_capability/data/build/settings-mocks +++ b/level_0/f_capability/data/build/settings-mocks @@ -6,6 +6,9 @@ # Therefore, this file is a work-around to inject the mocks into the library for testing purposes. # This should exactly match the "settings" file, except for the additional "-Wl,--wrap" parts and the additional mock source file. # +# The flags -o0 must be passed to prevent the compiler from optimizing away any functions being mocked (which results in the mock not happening and a real function being called). +# Alternatively, figure out which optimization that is disabled by -o0 and have that specific optimization disabled. +# build_name f_capability @@ -50,7 +53,7 @@ search_static yes #defines -D_di_libcap_ defines -D_libcap_legacy_only_ -flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses +flags -O0 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses flags-clang -Wno-logical-op-parentheses flags-test -fstack-protector diff --git a/level_0/f_color/data/build/settings-mocks b/level_0/f_color/data/build/settings-mocks index c4bc248..1bece60 100644 --- a/level_0/f_color/data/build/settings-mocks +++ b/level_0/f_color/data/build/settings-mocks @@ -6,6 +6,9 @@ # Therefore, this file is a work-around to inject the mocks into the library for testing purposes. # This should exactly match the "settings" file, except for the additional "-Wl,--wrap" parts and the additional mock source file. # +# The flags -o0 must be passed to prevent the compiler from optimizing away any functions being mocked (which results in the mock not happening and a real function being called). +# Alternatively, figure out which optimization that is disabled by -o0 and have that specific optimization disabled. +# build_name f_color @@ -47,7 +50,7 @@ search_exclusive yes search_shared yes search_static yes -flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses +flags -O0 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses flags-clang -Wno-logical-op-parentheses flags-test -fstack-protector flags_library -fPIC diff --git a/level_0/f_conversion/data/build/settings-mocks b/level_0/f_conversion/data/build/settings-mocks index ed7160e..2568b10 100644 --- a/level_0/f_conversion/data/build/settings-mocks +++ b/level_0/f_conversion/data/build/settings-mocks @@ -6,6 +6,9 @@ # Therefore, this file is a work-around to inject the mocks into the library for testing purposes. # This should exactly match the "settings" file, except for the additional "-Wl,--wrap" parts and the additional mock source file. # +# The flags -o0 must be passed to prevent the compiler from optimizing away any functions being mocked (which results in the mock not happening and a real function being called). +# Alternatively, figure out which optimization that is disabled by -o0 and have that specific optimization disabled. +# build_name f_conversion @@ -47,7 +50,7 @@ search_exclusive yes search_shared yes search_static yes -flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses +flags -O0 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses flags-clang -Wno-logical-op-parentheses flags-test -fstack-protector diff --git a/level_0/f_directory/data/build/settings-mocks b/level_0/f_directory/data/build/settings-mocks index 3eb294f..f01c157 100644 --- a/level_0/f_directory/data/build/settings-mocks +++ b/level_0/f_directory/data/build/settings-mocks @@ -6,6 +6,9 @@ # Therefore, this file is a work-around to inject the mocks into the library for testing purposes. # This should exactly match the "settings" file, except for the additional "-Wl,--wrap" parts and the additional mock source file. # +# The flags -o0 must be passed to prevent the compiler from optimizing away any functions being mocked (which results in the mock not happening and a real function being called). +# Alternatively, figure out which optimization that is disabled by -o0 and have that specific optimization disabled. +# build_name f_directory @@ -47,7 +50,7 @@ search_exclusive yes search_shared yes search_static yes -flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses +flags -O0 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-parentheses flags-clang -Wno-logical-op-parentheses flags-test -fstack-protector @@ -64,7 +67,5 @@ flags -Wl,--wrap=openat flags -Wl,--wrap=remove flags -Wl,--wrap=scandir flags -Wl,--wrap=stat -flags -Wl,--wrap=strncmp -flags -Wl,--wrap=strnlen flags -Wl,--wrap=utimensat flags -Wl,--wrap=versionsort diff --git a/level_0/f_directory/tests/unit/c/mock-directory.c b/level_0/f_directory/tests/unit/c/mock-directory.c index 4396552..16e6b64 100644 --- a/level_0/f_directory/tests/unit/c/mock-directory.c +++ b/level_0/f_directory/tests/unit/c/mock-directory.c @@ -149,6 +149,22 @@ int __wrap_stat(const char *pathname, struct stat *statbuf) { return -1; } + struct stat * const mocked_stat = mock_type(struct stat *); + + statbuf->st_dev = mocked_stat->st_dev; + statbuf->st_ino = mocked_stat->st_ino; + statbuf->st_mode = mocked_stat->st_mode; + statbuf->st_nlink = mocked_stat->st_nlink; + statbuf->st_uid = mocked_stat->st_uid; + statbuf->st_gid = mocked_stat->st_gid; + statbuf->st_rdev = mocked_stat->st_rdev; + statbuf->st_size = mocked_stat->st_size; + statbuf->st_blksize = mocked_stat->st_blksize; + statbuf->st_blocks = mocked_stat->st_blocks; + statbuf->st_atime = mocked_stat->st_atime; + statbuf->st_mtime = mocked_stat->st_mtime; + statbuf->st_ctime = mocked_stat->st_ctime; + return mock_type(int); } diff --git a/level_0/f_directory/tests/unit/c/test-directory-exists_at.c b/level_0/f_directory/tests/unit/c/test-directory-exists_at.c index 1ed62c2..eb9156a 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-exists_at.c +++ b/level_0/f_directory/tests/unit/c/test-directory-exists_at.c @@ -37,8 +37,8 @@ void test__f_directory_exists_at__fails(void **state) { for (int i = 0; i < 10; ++i) { - will_return(__wrap_stat, true); - will_return(__wrap_stat, errnos[i]); + will_return(__wrap_fstatat, true); + will_return(__wrap_fstatat, errnos[i]); const f_status_t status = f_directory_exists_at(0, path, 0); @@ -64,9 +64,9 @@ void test__f_directory_exists_at__returns_false(void **state) { memset(&statistics, 0, sizeof (struct stat)); { - will_return(__wrap_stat, false); - will_return(__wrap_stat, &statistics); - will_return(__wrap_stat, 0); + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); const f_status_t status = f_directory_exists_at(0, path, 0); @@ -85,9 +85,9 @@ void test__f_directory_exists_at__returns_true(void **state) { statistics.st_mode = 1 | S_IFDIR; { - will_return(__wrap_stat, false); - will_return(__wrap_stat, &statistics); - will_return(__wrap_stat, 0); + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); const f_status_t status = f_directory_exists_at(0, path, 0); diff --git a/level_0/f_directory/tests/unit/c/test-directory-is.c b/level_0/f_directory/tests/unit/c/test-directory-is.c index fcc3507..df82271 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-is.c +++ b/level_0/f_directory/tests/unit/c/test-directory-is.c @@ -12,6 +12,7 @@ void test__f_directory_is__fails(void **state) { int errnos[] = { EACCES, EFAULT, + ELOOP, ENAMETOOLONG, ENOENT, ENOMEM, @@ -32,7 +33,7 @@ void test__f_directory_is__fails(void **state) { F_file_stat, }; - for (int i = 0; i < 8; ++i) { + for (int i = 0; i < 9; ++i) { will_return(__wrap_stat, true); will_return(__wrap_stat, errnos[i]); diff --git a/level_0/f_directory/tests/unit/c/test-directory-is_at.c b/level_0/f_directory/tests/unit/c/test-directory-is_at.c index f690237..6c88285 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-is_at.c +++ b/level_0/f_directory/tests/unit/c/test-directory-is_at.c @@ -37,8 +37,8 @@ void test__f_directory_is_at__fails(void **state) { for (int i = 0; i < 10; ++i) { - will_return(__wrap_stat, true); - will_return(__wrap_stat, errnos[i]); + will_return(__wrap_fstatat, true); + will_return(__wrap_fstatat, errnos[i]); const f_status_t status = f_directory_is_at(0, path, 0); @@ -64,9 +64,9 @@ void test__f_directory_is_at__returns_false(void **state) { memset(&statistics, 0, sizeof (struct stat)); { - will_return(__wrap_stat, false); - will_return(__wrap_stat, &statistics); - will_return(__wrap_stat, 0); + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); const f_status_t status = f_directory_is_at(0, path, 0); @@ -85,9 +85,9 @@ void test__f_directory_is_at__returns_true(void **state) { statistics.st_mode = 1 | S_IFDIR; { - will_return(__wrap_stat, false); - will_return(__wrap_stat, &statistics); - will_return(__wrap_stat, 0); + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); const f_status_t status = f_directory_is_at(0, path, 0); diff --git a/level_0/f_directory/tests/unit/c/test-directory-list.c b/level_0/f_directory/tests/unit/c/test-directory-list.c index e36f715..c8fd80c 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-list.c +++ b/level_0/f_directory/tests/unit/c/test-directory-list.c @@ -69,7 +69,13 @@ void test__f_directory_list__returns_directory_empty(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); { + // The scandir() allocates the entire struct dirent. + struct dirent **directories = (struct dirent **) malloc(sizeof(struct dirent *)); + + memset(directories, 0, sizeof(struct dirent *)); + will_return(__wrap_scandir, false); + will_return(__wrap_scandir, directories); will_return(__wrap_scandir, 0); const f_status_t status = f_directory_list(path, 0, 0, &names); @@ -86,20 +92,18 @@ void test__f_directory_list__works(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); { - struct dirent directory; - memset(&directory, 0, sizeof(struct dirent)); - - struct dirent directories[2]; + // The scandir() allocates the entire struct dirent. + struct dirent **directories = (struct dirent **) malloc(sizeof(struct dirent *)); - memset(directories, 0, sizeof(struct dirent) * 2); + directories[0] = (struct dirent *) malloc(sizeof(struct dirent)); - directories[0] = directory; + memset(directories[0], 0, sizeof(struct dirent)); + memcpy(directories[0]->d_name, path.string, sizeof(f_char_t) * path.used); + directories[0]->d_name[path.used] = 0; will_return(__wrap_scandir, false); - will_return(__wrap_scandir, &directories); - will_return(__wrap_strnlen, 1); - will_return(__wrap_strncmp, 1); - will_return(__wrap_strncmp, 1); + will_return(__wrap_scandir, directories); + will_return(__wrap_scandir, 1); const f_status_t status = f_directory_list(path, 0, 0, &names); diff --git a/level_0/f_directory/tests/unit/c/test-directory-touch.c b/level_0/f_directory/tests/unit/c/test-directory-touch.c index 866e91f..af9d2f4 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-touch.c +++ b/level_0/f_directory/tests/unit/c/test-directory-touch.c @@ -29,7 +29,7 @@ void test__f_directory_touch__fails(void **state) { F_memory_not, F_directory_not, F_number_overflow, - F_failure, + F_file_stat, }; for (int i = 0; i < 8; ++i) { @@ -124,10 +124,17 @@ void test__f_directory_touch__fails(void **state) { F_failure, }; + struct stat statistics; + + memset(&statistics, 0, sizeof (struct stat)); + + statistics.st_mode = 1 | S_IFDIR; + for (int i = 0; i < 12; ++i) { - will_return(__wrap_mkdir, false); - will_return(__wrap_mkdir, 0); + will_return(__wrap_stat, false); + will_return(__wrap_stat, &statistics); + will_return(__wrap_stat, 0); will_return(__wrap_utimensat, true); will_return(__wrap_utimensat, errnos[i]); @@ -152,10 +159,17 @@ void test__f_directory_touch__works(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); { - will_return(__wrap_mkdir, false); - will_return(__wrap_mkdir, 0); - will_return(__wrap_utimensat, false); - will_return(__wrap_utimensat, 0); + struct stat statistics; + + memset(&statistics, 0, sizeof (struct stat)); + + statistics.st_mode = 1 | S_IFDIR; + + will_return(__wrap_stat, false); + will_return(__wrap_stat, &statistics); + will_return(__wrap_stat, 0); + will_return(__wrap_utimensat, false); + will_return(__wrap_utimensat, 0); const f_status_t status = f_directory_touch(path, 0); @@ -163,10 +177,10 @@ void test__f_directory_touch__works(void **state) { } { - will_return(__wrap_mkdir, true); - will_return(__wrap_mkdir, ENOENT); - will_return(__wrap_mkdir, false); - will_return(__wrap_mkdir, 0); + will_return(__wrap_stat, true); + will_return(__wrap_stat, ENOENT); + will_return(__wrap_mkdir, false); + will_return(__wrap_mkdir, 0); const f_status_t status = f_directory_touch(path, 0); diff --git a/level_0/f_directory/tests/unit/c/test-directory-touch_at.c b/level_0/f_directory/tests/unit/c/test-directory-touch_at.c index 3102ab2..6c941bb 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-touch_at.c +++ b/level_0/f_directory/tests/unit/c/test-directory-touch_at.c @@ -29,13 +29,13 @@ void test__f_directory_touch_at__fails(void **state) { F_memory_not, F_directory_not, F_number_overflow, - F_failure, + F_file_stat, }; for (int i = 0; i < 8; ++i) { - will_return(__wrap_stat, true); - will_return(__wrap_stat, errnos[i]); + will_return(__wrap_fstatat, true); + will_return(__wrap_fstatat, errnos[i]); const f_status_t status = f_directory_touch_at(1, path, 0, 0); @@ -82,10 +82,10 @@ void test__f_directory_touch_at__fails(void **state) { for (int i = 0; i < 15; ++i) { - will_return(__wrap_stat, true); - will_return(__wrap_stat, ENOENT); - will_return(__wrap_mkdir, true); - will_return(__wrap_mkdir, errnos[i]); + will_return(__wrap_fstatat, true); + will_return(__wrap_fstatat, ENOENT); + will_return(__wrap_mkdirat, true); + will_return(__wrap_mkdirat, errnos[i]); const f_status_t status = f_directory_touch_at(1, path, 0, 0); @@ -124,10 +124,17 @@ void test__f_directory_touch_at__fails(void **state) { F_failure, }; + struct stat statistics; + + memset(&statistics, 0, sizeof (struct stat)); + + statistics.st_mode = 1 | S_IFDIR; + for (int i = 0; i < 12; ++i) { - will_return(__wrap_mkdir, false); - will_return(__wrap_mkdir, 0); + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); will_return(__wrap_utimensat, true); will_return(__wrap_utimensat, errnos[i]); @@ -152,10 +159,17 @@ void test__f_directory_touch_at__works(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); { - will_return(__wrap_mkdir, false); - will_return(__wrap_mkdir, 0); - will_return(__wrap_utimensat, false); - will_return(__wrap_utimensat, 0); + struct stat statistics; + + memset(&statistics, 0, sizeof (struct stat)); + + statistics.st_mode = 1 | S_IFDIR; + + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); + will_return(__wrap_utimensat, false); + will_return(__wrap_utimensat, 0); const f_status_t status = f_directory_touch_at(1, path, 0, 0); @@ -163,10 +177,10 @@ void test__f_directory_touch_at__works(void **state) { } { - will_return(__wrap_mkdir, true); - will_return(__wrap_mkdir, ENOENT); - will_return(__wrap_mkdir, false); - will_return(__wrap_mkdir, 0); + will_return(__wrap_fstatat, true); + will_return(__wrap_fstatat, ENOENT); + will_return(__wrap_mkdirat, false); + will_return(__wrap_mkdirat, 0); const f_status_t status = f_directory_touch_at(1, path, 0, 0); diff --git a/level_0/f_directory/tests/unit/c/test-directory.c b/level_0/f_directory/tests/unit/c/test-directory.c index ff117b6..da29a28 100644 --- a/level_0/f_directory/tests/unit/c/test-directory.c +++ b/level_0/f_directory/tests/unit/c/test-directory.c @@ -27,35 +27,30 @@ int main(void) { cmocka_unit_test(test__f_directory_create_at__returns_data_not), cmocka_unit_test(test__f_directory_create_at__works), - // Unable to perform tests because stat() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_exists__fails), - //cmocka_unit_test(test__f_directory_exists__returns_data_not), - //cmocka_unit_test(test__f_directory_exists__returns_false), - //cmocka_unit_test(test__f_directory_exists__returns_true), - - // Unable to perform tests because stat() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_exists_at__fails), - //cmocka_unit_test(test__f_directory_exists_at__returns_data_not), - //cmocka_unit_test(test__f_directory_exists_at__returns_false), - //cmocka_unit_test(test__f_directory_exists_at__returns_true), - - // Unable to perform tests because stat() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_is__fails), - //cmocka_unit_test(test__f_directory_is__returns_data_not), - //cmocka_unit_test(test__f_directory_is__returns_false), - //cmocka_unit_test(test__f_directory_is__returns_true), - - // Unable to perform tests because stat() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_is_at__fails), - //cmocka_unit_test(test__f_directory_is_at__returns_data_not), - //cmocka_unit_test(test__f_directory_is_at__returns_false), - //cmocka_unit_test(test__f_directory_is_at__returns_true), - - // Unable to perform tests because scandir() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_list__fails), - //cmocka_unit_test(test__f_directory_list__returns_data_not), - //cmocka_unit_test(test__f_directory_list__returns_directory_empty), - //cmocka_unit_test(test__f_directory_list__works), + cmocka_unit_test(test__f_directory_exists__fails), + cmocka_unit_test(test__f_directory_exists__returns_data_not), + cmocka_unit_test(test__f_directory_exists__returns_false), + cmocka_unit_test(test__f_directory_exists__returns_true), + + cmocka_unit_test(test__f_directory_exists_at__fails), + cmocka_unit_test(test__f_directory_exists_at__returns_data_not), + cmocka_unit_test(test__f_directory_exists_at__returns_false), + cmocka_unit_test(test__f_directory_exists_at__returns_true), + + cmocka_unit_test(test__f_directory_is__fails), + cmocka_unit_test(test__f_directory_is__returns_data_not), + cmocka_unit_test(test__f_directory_is__returns_false), + cmocka_unit_test(test__f_directory_is__returns_true), + + cmocka_unit_test(test__f_directory_is_at__fails), + cmocka_unit_test(test__f_directory_is_at__returns_data_not), + cmocka_unit_test(test__f_directory_is_at__returns_false), + cmocka_unit_test(test__f_directory_is_at__returns_true), + + cmocka_unit_test(test__f_directory_list__fails), + cmocka_unit_test(test__f_directory_list__returns_data_not), + cmocka_unit_test(test__f_directory_list__returns_directory_empty), + cmocka_unit_test(test__f_directory_list__works), cmocka_unit_test(test__f_directory_open__fails), cmocka_unit_test(test__f_directory_open__returns_data_not), @@ -73,15 +68,13 @@ int main(void) { cmocka_unit_test(test__f_directory_remove_custom__returns_data_not), cmocka_unit_test(test__f_directory_remove_custom__works), - // Unable to perform tests because stat() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_touch__fails), - //cmocka_unit_test(test__f_directory_touch__returns_data_not), - //cmocka_unit_test(test__f_directory_touch__works), + cmocka_unit_test(test__f_directory_touch__fails), + cmocka_unit_test(test__f_directory_touch__returns_data_not), + cmocka_unit_test(test__f_directory_touch__works), - // Unable to perform tests because stat() appears to not properly get mocked. - //cmocka_unit_test(test__f_directory_touch_at__fails), - //cmocka_unit_test(test__f_directory_touch_at__returns_data_not), - //cmocka_unit_test(test__f_directory_touch_at__works), + cmocka_unit_test(test__f_directory_touch_at__fails), + cmocka_unit_test(test__f_directory_touch_at__returns_data_not), + cmocka_unit_test(test__f_directory_touch_at__works), #ifndef _di_level_0_parameter_checking_ // f_directory_create() doesn't use parameter checking.