From d919d4cc4adb958a6f4c361c7199357929570153 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 21 Apr 2022 18:53:05 -0500 Subject: [PATCH] Update: Finish writing unit tests for f_file. --- level_0/f_file/data/build/settings-mocks | 1 + level_0/f_file/tests/unit/c/mock-file.c | 5 ++ level_0/f_file/tests/unit/c/mock-file.h | 1 + level_0/f_file/tests/unit/c/test-file-umask_get.c | 45 ++++-------- level_0/f_file/tests/unit/c/test-file-umask_get.h | 13 ++-- level_0/f_file/tests/unit/c/test-file-umask_set.c | 38 +--------- level_0/f_file/tests/unit/c/test-file-umask_set.h | 14 ---- level_0/f_file/tests/unit/c/test-file-write.c | 82 +++++++++++++++++++--- .../f_file/tests/unit/c/test-file-write_block.c | 65 +++++++++++++---- .../f_file/tests/unit/c/test-file-write_range.c | 68 ++++++++++++++---- .../f_file/tests/unit/c/test-file-write_until.c | 76 ++++++++++++++++---- level_0/f_file/tests/unit/c/test-file.c | 29 +++++--- 12 files changed, 291 insertions(+), 146 deletions(-) diff --git a/level_0/f_file/data/build/settings-mocks b/level_0/f_file/data/build/settings-mocks index 59a86e7..729cf57 100644 --- a/level_0/f_file/data/build/settings-mocks +++ b/level_0/f_file/data/build/settings-mocks @@ -107,4 +107,5 @@ flags -Wl,--wrap=symlinkat flags -Wl,--wrap=unlink flags -Wl,--wrap=unlinkat flags -Wl,--wrap=utimensat +flags -Wl,--wrap=umask flags -Wl,--wrap=write diff --git a/level_0/f_file/tests/unit/c/mock-file.c b/level_0/f_file/tests/unit/c/mock-file.c index 992b19d..34056e6 100644 --- a/level_0/f_file/tests/unit/c/mock-file.c +++ b/level_0/f_file/tests/unit/c/mock-file.c @@ -693,6 +693,11 @@ int __wrap_utimensat(int dirfd, const char *pathname, const struct timespec time return mock_type(int); } +mode_t __wrap_umask(mode_t mask) { + + return mock_type(mode_t); +} + ssize_t __wrap_write(int fd, const void *buf, size_t count) { const bool failure = mock_type(bool); diff --git a/level_0/f_file/tests/unit/c/mock-file.h b/level_0/f_file/tests/unit/c/mock-file.h index 9559784..a33f134 100644 --- a/level_0/f_file/tests/unit/c/mock-file.h +++ b/level_0/f_file/tests/unit/c/mock-file.h @@ -76,6 +76,7 @@ extern int __wrap_symlink(const char *target, const char *linkpath); extern int __wrap_symlinkat(const char *target, int newdirfd, const char *linkpath); extern int __wrap_unlink(const char *pathname); extern int __wrap_unlinkat(int dirfd, const char *pathname, int flags); +extern mode_t __wrap_umask(mode_t mask); extern int __wrap_utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags); extern ssize_t __wrap_write(int fd, const void *buf, size_t count); diff --git a/level_0/f_file/tests/unit/c/test-file-umask_get.c b/level_0/f_file/tests/unit/c/test-file-umask_get.c index c31f88d..9f405d0 100644 --- a/level_0/f_file/tests/unit/c/test-file-umask_get.c +++ b/level_0/f_file/tests/unit/c/test-file-umask_get.c @@ -5,46 +5,29 @@ extern "C" { #endif -void test__f_file_umask_get__fails(void **state) { +#ifndef _di_level_0_parameter_checking_ + void test__f_file_umask_get__parameter_checking(void **state) { - int errnos[] = { - mock_errno_generic, - }; + { + const f_status_t status = f_file_umask_get(0); - f_status_t statuss[] = { - F_failure, - }; - - for (int i = 0; i < 1; ++i) { - - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); - - //const f_status_t status = f_file_umask_get(path, F_false, &id); - - //assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for -} - -void test__f_file_umask_get__returns_data_not(void **state) { - - { - //const f_status_t status = f_file_umask_get(f_string_empty_s); - - //assert_int_equal(status, F_data_not); + assert_int_equal(F_status_set_fine(status), F_parameter); + } } -} +#endif // _di_level_0_parameter_checking_ void test__f_file_umask_get__works(void **state) { { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + will_return(__wrap_umask, F_file_mode_all_rw_d); + will_return(__wrap_umask, 0); + + mode_t mode = 0; - //const f_status_t status = f_file_umask_get(); + const mode_t status = f_file_umask_get(&mode); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none); + assert_int_equal(mode, F_file_mode_all_rw_d); } } diff --git a/level_0/f_file/tests/unit/c/test-file-umask_get.h b/level_0/f_file/tests/unit/c/test-file-umask_get.h index 664517e..b3574e8 100644 --- a/level_0/f_file/tests/unit/c/test-file-umask_get.h +++ b/level_0/f_file/tests/unit/c/test-file-umask_get.h @@ -11,18 +11,13 @@ #define _TEST__F_file_umask_get_h /** - * Test that function fails. + * Test that parameter checking works as expected. * * @see f_file_umask_get() */ -extern void test__f_file_umask_get__fails(void **state); - -/** - * Test that function works but the path is empty. - * - * @see f_file_umask_get() - */ -extern void test__f_file_umask_get__returns_data_not(void **state); +#ifndef _di_level_0_parameter_checking_ + extern void test__f_file_umask_get__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ /** * Test that function works. diff --git a/level_0/f_file/tests/unit/c/test-file-umask_set.c b/level_0/f_file/tests/unit/c/test-file-umask_set.c index 1ba5e86..7fe792f 100644 --- a/level_0/f_file/tests/unit/c/test-file-umask_set.c +++ b/level_0/f_file/tests/unit/c/test-file-umask_set.c @@ -5,46 +5,14 @@ extern "C" { #endif -void test__f_file_umask_set__fails(void **state) { - - int errnos[] = { - mock_errno_generic, - }; - - f_status_t statuss[] = { - F_failure, - }; - - for (int i = 0; i < 1; ++i) { - - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); - - //const f_status_t status = f_file_umask_set(path, F_false, &id); - - //assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for -} - -void test__f_file_umask_set__returns_data_not(void **state) { - - { - //const f_status_t status = f_file_umask_set(f_string_empty_s); - - //assert_int_equal(status, F_data_not); - } -} - void test__f_file_umask_set__works(void **state) { { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + will_return(__wrap_umask, 0); - //const f_status_t status = f_file_umask_set(); + const mode_t status = f_file_umask_set(F_file_mode_all_rw_d); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none); } } diff --git a/level_0/f_file/tests/unit/c/test-file-umask_set.h b/level_0/f_file/tests/unit/c/test-file-umask_set.h index e177fdb..483119a 100644 --- a/level_0/f_file/tests/unit/c/test-file-umask_set.h +++ b/level_0/f_file/tests/unit/c/test-file-umask_set.h @@ -11,20 +11,6 @@ #define _TEST__F_file_umask_set_h /** - * Test that function fails. - * - * @see f_file_umask_set() - */ -extern void test__f_file_umask_set__fails(void **state); - -/** - * Test that function works but the path is empty. - * - * @see f_file_umask_set() - */ -extern void test__f_file_umask_set__returns_data_not(void **state); - -/** * Test that function works. * * @see f_file_umask_set() diff --git a/level_0/f_file/tests/unit/c/test-file-write.c b/level_0/f_file/tests/unit/c/test-file-write.c index 4b1158e..3830b4f 100644 --- a/level_0/f_file/tests/unit/c/test-file-write.c +++ b/level_0/f_file/tests/unit/c/test-file-write.c @@ -7,22 +7,61 @@ extern "C" { void test__f_file_write__fails(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + int errnos[] = { + EAGAIN, + EBADF, + EFAULT, + EINTR, + EINVAL, + EIO, + EISDIR, + EWOULDBLOCK, mock_errno_generic, }; f_status_t statuss[] = { - F_failure, + F_block, + F_file_descriptor, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_type_directory, + F_block, + F_file_write, }; - for (int i = 0; i < 1; ++i) { + for (int i = 0; i < 9; ++i) { + + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; + + will_return(__wrap_write, true); + will_return(__wrap_write, errnos[i]); + + const f_status_t status = f_file_write(file, path, 0); + + assert_int_equal(F_status_set_fine(status), statuss[i]); + } // for + + for (int i = 0; i < 9; ++i) { + + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, 1); - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + will_return(__wrap_write, true); + will_return(__wrap_write, errnos[i]); - //const f_status_t status = f_file_write(path, F_false, &id); + const f_status_t status = f_file_write(file, path, 0); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + assert_int_equal(F_status_set_fine(status), statuss[i]); } // for } @@ -74,14 +113,35 @@ void test__f_file_write__returns_data_not(void **state) { void test__f_file_write__works(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + f_file_t file = f_file_t_initialize; + file.size_write = path.used; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + const f_status_t status = f_file_write(file, path, 0); + + assert_int_equal(status, F_none_eos); + } + + { + f_file_t file = f_file_t_initialize; + file.size_write = path.used / 2; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, path.used / 2); + + will_return(__wrap_write, false); + will_return(__wrap_write, path.used / 2); - //const f_status_t status = f_file_write(); + const f_status_t status = f_file_write(file, path, 0); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none_eos); } } diff --git a/level_0/f_file/tests/unit/c/test-file-write_block.c b/level_0/f_file/tests/unit/c/test-file-write_block.c index a821e43..ef7bd8c 100644 --- a/level_0/f_file/tests/unit/c/test-file-write_block.c +++ b/level_0/f_file/tests/unit/c/test-file-write_block.c @@ -7,22 +7,44 @@ extern "C" { void test__f_file_write_block__fails(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + int errnos[] = { + EAGAIN, + EBADF, + EFAULT, + EINTR, + EINVAL, + EIO, + EISDIR, + EWOULDBLOCK, mock_errno_generic, }; f_status_t statuss[] = { - F_failure, + F_block, + F_file_descriptor, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_type_directory, + F_block, + F_file_write, }; - for (int i = 0; i < 1; ++i) { + for (int i = 0; i < 9; ++i) { + + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + will_return(__wrap_write, true); + will_return(__wrap_write, errnos[i]); - //const f_status_t status = f_file_write_block(path, F_false, &id); + const f_status_t status = f_file_write_block(file, path, 0); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + assert_int_equal(F_status_set_fine(status), statuss[i]); } // for } @@ -42,7 +64,8 @@ void test__f_file_write_block__fails(void **state) { void test__f_file_write_block__returns_file_closed(void **state) { - const f_file_t file = f_file_t_initialize; + f_file_t file = f_file_t_initialize; + file.size_write = 1; { const f_status_t status = f_file_write_block(file, f_string_empty_s, 0); @@ -74,14 +97,32 @@ void test__f_file_write_block__returns_data_not(void **state) { void test__f_file_write_block__works(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + + { + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, 1); + + const f_status_t status = f_file_write_block(file, path, 0); + + assert_int_equal(status, F_none_stop); + } + { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + f_file_t file = f_file_t_initialize; + file.size_write = path.used; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); - //const f_status_t status = f_file_write_block(); + const f_status_t status = f_file_write_block(file, path, 0); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none_eos); } } diff --git a/level_0/f_file/tests/unit/c/test-file-write_range.c b/level_0/f_file/tests/unit/c/test-file-write_range.c index 3ad8423..5c3bcb3 100644 --- a/level_0/f_file/tests/unit/c/test-file-write_range.c +++ b/level_0/f_file/tests/unit/c/test-file-write_range.c @@ -7,22 +7,45 @@ extern "C" { void test__f_file_write_range__fails(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + const f_string_range_t range = macro_f_string_range_t_initialize(0, 0); + int errnos[] = { + EAGAIN, + EBADF, + EFAULT, + EINTR, + EINVAL, + EIO, + EISDIR, + EWOULDBLOCK, mock_errno_generic, }; f_status_t statuss[] = { - F_failure, + F_block, + F_file_descriptor, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_type_directory, + F_block, + F_file_write, }; - for (int i = 0; i < 1; ++i) { + for (int i = 0; i < 9; ++i) { - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; - //const f_status_t status = f_file_write_range(path, F_false, &id); + will_return(__wrap_write, true); + will_return(__wrap_write, errnos[i]); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + const f_status_t status = f_file_write_range(file, path, range, 0); + + assert_int_equal(F_status_set_fine(status), statuss[i]); } // for } @@ -44,9 +67,11 @@ void test__f_file_write_range__fails(void **state) { void test__f_file_write_range__returns_file_closed(void **state) { - const f_file_t file = f_file_t_initialize; + f_file_t file = f_file_t_initialize; const f_string_range_t range = f_string_range_t_initialize; + file.size_write = 1; + { const f_status_t status = f_file_write_range(file, f_string_empty_s, range, 0); @@ -121,14 +146,33 @@ void test__f_file_write_range__returns_data_not(void **state) { void test__f_file_write_range__works(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + const f_string_range_t range = macro_f_string_range_t_initialize(0, 0); + + { + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + const f_status_t status = f_file_write_range(file, path, range, 0); + + assert_int_equal(status, F_none_stop); + } + { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + f_file_t file = f_file_t_initialize; + file.size_write = path.used; + file.id = 0; - //const f_status_t status = f_file_write_range(); + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + const f_status_t status = f_file_write_range(file, path, range, 0); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none_eos); } } diff --git a/level_0/f_file/tests/unit/c/test-file-write_until.c b/level_0/f_file/tests/unit/c/test-file-write_until.c index 70412e1..efcb932 100644 --- a/level_0/f_file/tests/unit/c/test-file-write_until.c +++ b/level_0/f_file/tests/unit/c/test-file-write_until.c @@ -7,22 +7,44 @@ extern "C" { void test__f_file_write_until__fails(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + int errnos[] = { + EAGAIN, + EBADF, + EFAULT, + EINTR, + EINVAL, + EIO, + EISDIR, + EWOULDBLOCK, mock_errno_generic, }; f_status_t statuss[] = { - F_failure, + F_block, + F_file_descriptor, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_type_directory, + F_block, + F_file_write, }; - for (int i = 0; i < 1; ++i) { + for (int i = 0; i < 9; ++i) { + + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + will_return(__wrap_write, true); + will_return(__wrap_write, errnos[i]); - //const f_status_t status = f_file_write_until(path, F_false, &id); + const f_status_t status = f_file_write_until(file, path, 1, 0); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + assert_int_equal(F_status_set_fine(status), statuss[i]); } // for } @@ -42,10 +64,11 @@ void test__f_file_write_until__fails(void **state) { void test__f_file_write_until__returns_file_closed(void **state) { - const f_file_t file = f_file_t_initialize; + f_file_t file = f_file_t_initialize; + file.size_write = 1; { - const f_status_t status = f_file_write_until(file, f_string_empty_s, 0, 0); + const f_status_t status = f_file_write_until(file, f_string_empty_s, 1, 0); assert_int_equal(F_status_set_fine(status), F_file_closed); } @@ -106,14 +129,41 @@ void test__f_file_write_until__returns_data_not(void **state) { void test__f_file_write_until__works(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + f_file_t file = f_file_t_initialize; + file.size_write = 1; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); + + const f_status_t status = f_file_write_until(file, path, path.used, 0); + + assert_int_equal(status, F_none_eos); + } + + { + f_file_t file = f_file_t_initialize; + file.size_write = path.used; + file.id = 0; + + will_return(__wrap_write, false); + will_return(__wrap_write, file.size_write); - //const f_status_t status = f_file_write_until(); + const f_status_t status = f_file_write_until(file, path, path.used, 0); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none_eos); } } diff --git a/level_0/f_file/tests/unit/c/test-file.c b/level_0/f_file/tests/unit/c/test-file.c index 7740470..d540b93 100644 --- a/level_0/f_file/tests/unit/c/test-file.c +++ b/level_0/f_file/tests/unit/c/test-file.c @@ -323,19 +323,30 @@ int main(void) { cmocka_unit_test(test__f_file_type_at__returns_data_not), cmocka_unit_test(test__f_file_type_at__works), -/* - // f_file_umask_get + cmocka_unit_test(test__f_file_umask_get__works), - // f_file_umask_set + cmocka_unit_test(test__f_file_umask_set__works), - // f_file_write + cmocka_unit_test(test__f_file_write__fails), + cmocka_unit_test(test__f_file_write__returns_file_closed), + cmocka_unit_test(test__f_file_write__returns_data_not), + cmocka_unit_test(test__f_file_write__works), - // f_file_write_block + cmocka_unit_test(test__f_file_write_block__fails), + cmocka_unit_test(test__f_file_write_block__returns_file_closed), + cmocka_unit_test(test__f_file_write_block__returns_data_not), + cmocka_unit_test(test__f_file_write_block__works), - // f_file_write_until + cmocka_unit_test(test__f_file_write_range__fails), + cmocka_unit_test(test__f_file_write_range__returns_file_closed), + cmocka_unit_test(test__f_file_write_range__returns_data_not), + cmocka_unit_test(test__f_file_write_range__works), + + cmocka_unit_test(test__f_file_write_until__fails), + cmocka_unit_test(test__f_file_write_until__returns_file_closed), + cmocka_unit_test(test__f_file_write_until__returns_data_not), + cmocka_unit_test(test__f_file_write_until__works), - // f_file_write_range -*/ #ifndef _di_level_0_parameter_checking_ // f_file_access() doesn't use parameter checking. // f_file_access_at() doesn't use parameter checking. @@ -407,7 +418,7 @@ int main(void) { // f_file_touch_at() doesn't use parameter checking. cmocka_unit_test(test__f_file_type__parameter_checking), cmocka_unit_test(test__f_file_type_at__parameter_checking), - // f_file_umask_get() doesn't use parameter checking. + cmocka_unit_test(test__f_file_umask_get__parameter_checking), // f_file_umask_set() doesn't use parameter checking. cmocka_unit_test(test__f_file_write__parameter_checking), cmocka_unit_test(test__f_file_write_block__parameter_checking), -- 1.8.3.1