From: Kevin Day Date: Wed, 20 Apr 2022 03:22:55 +0000 (-0500) Subject: Progress: Continue witing f_file unit tests. X-Git-Tag: 0.5.10~193 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=ef9b42806ab8b4ad996a1c4a54675e7a6207c681;p=fll Progress: Continue witing f_file unit tests. --- diff --git a/level_0/f_file/data/build/settings-mocks b/level_0/f_file/data/build/settings-mocks index be55dfe..c566667 100644 --- a/level_0/f_file/data/build/settings-mocks +++ b/level_0/f_file/data/build/settings-mocks @@ -65,11 +65,16 @@ flags -Wl,--wrap=close flags -Wl,--wrap=faccessat flags -Wl,--wrap=fchmodat flags -Wl,--wrap=fchownat +flags -Wl,--wrap=fclose +flags -Wl,--wrap=fdopen flags -Wl,--wrap=feof flags -Wl,--wrap=ferror +flags -Wl,--wrap=fflush flags -Wl,--wrap=fileno flags -Wl,--wrap=flockfile +flags -Wl,--wrap=fopen flags -Wl,--wrap=fread_unlocked +flags -Wl,--wrap=freopen flags -Wl,--wrap=fstat flags -Wl,--wrap=fstatat flags -Wl,--wrap=fsync diff --git a/level_0/f_file/data/build/settings-tests b/level_0/f_file/data/build/settings-tests index ed6919e..9108db4 100644 --- a/level_0/f_file/data/build/settings-tests +++ b/level_0/f_file/data/build/settings-tests @@ -25,7 +25,7 @@ build_language c build_libraries -lc -lcmocka build_libraries-individual -lf_memory -lf_string -lf_file -build_sources_program test-file-access.c test-file-access_at.c test-file-clone.c test-file-close.c test-file-close_flush.c test-file-copy.c test-file-create.c test-file-create_at.c test-file-create_device.c test-file-create_device_at.c test-file-create_fifo.c test-file-create_fifo_at.c test-file-create_node.c test-file-create_node_at.c test-file-descriptor.c test-file-exists.c test-file-exists_at.c test-file-flush.c test-file-group_read.c test-file-is.c test-file-is_at.c test-file-link.c test-file-link_at.c test-file-link_hard.c test-file-link_hard_at.c test-file-link_read.c test-file-link_read_at.c test-file-mode_determine.c test-file-mode_from_string.c test-file-mode_read.c test-file-mode_read_at.c test-file-mode_set.c test-file-mode_set_at.c test-file-mode_to_mode.c test-file-name_base.c test-file-name_directory.c test-file-open.c test-file-open_at.c test-file-owner_read.c test-file-read.c test-file-read_block.c test-file-read_until.c test-file-remove.c test-file-remove_at.c test-file-rename.c test-file-rename_at.c test-file-role_change.c test-file-role_change_at.c test-file-seek.c test-file-size.c test-file-size_at.c test-file-size_by_id.c test-file-stat.c test-file-stat_at.c test-file-stat_by_id.c test-file-stream_close.c test-file-stream_descriptor.c test-file-stream_open.c test-file-stream_read.c test-file-stream_read_block.c test-file-stream_read_until.c test-file-stream_reopen.c test-file-stream_write.c test-file-stream_write_block.c test-file-stream_write_until.c test-file-stream_write_range.c test-file-touch.c test-file-touch_at.c test-file-type.c test-file-type_at.c test-file-umask_get.c test-file-umask_set.c test-file-write.c test-file-write_block.c test-file-write_until.c test-file-write_range.c +build_sources_program test-file-access.c test-file-access_at.c test-file-clone.c test-file-close.c test-file-close_flush.c test-file-copy.c test-file-create.c test-file-create_at.c test-file-create_device.c test-file-create_device_at.c test-file-create_fifo.c test-file-create_fifo_at.c test-file-create_node.c test-file-create_node_at.c test-file-descriptor.c test-file-exists.c test-file-exists_at.c test-file-flush.c test-file-group_read.c test-file-is.c test-file-is_at.c test-file-link.c test-file-link_at.c test-file-link_hard.c test-file-link_hard_at.c test-file-link_read.c test-file-link_read_at.c test-file-mode_determine.c test-file-mode_from_string.c test-file-mode_read.c test-file-mode_read_at.c test-file-mode_set.c test-file-mode_set_at.c test-file-mode_to_mode.c test-file-name_base.c test-file-name_directory.c test-file-open.c test-file-open_at.c test-file-owner_read.c test-file-read.c test-file-read_block.c test-file-read_until.c test-file-remove.c test-file-remove_at.c test-file-rename.c test-file-rename_at.c test-file-role_change.c test-file-role_change_at.c test-file-seek.c test-file-size.c test-file-size_at.c test-file-size_by_id.c test-file-stat.c test-file-stat_at.c test-file-stat_by_id.c test-file-stream_close.c test-file-stream_open_descriptor.c test-file-stream_open.c test-file-stream_read.c test-file-stream_read_block.c test-file-stream_read_until.c test-file-stream_reopen.c test-file-stream_write.c test-file-stream_write_block.c test-file-stream_write_until.c test-file-stream_write_range.c test-file-touch.c test-file-touch_at.c test-file-type.c test-file-type_at.c test-file-umask_get.c test-file-umask_set.c test-file-write.c test-file-write_block.c test-file-write_until.c test-file-write_range.c build_sources_program test-file.c build_script no 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 8e2e2aa..d976f42 100644 --- a/level_0/f_file/tests/unit/c/mock-file.c +++ b/level_0/f_file/tests/unit/c/mock-file.c @@ -121,6 +121,58 @@ int __wrap_fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, i return mock_type(int); } +int __wrap_fclose(FILE *stream) { + + const bool failure = mock_type(bool); + + if (failure) { + errno = mock_type(int); + + return EOF; + } + + return mock_type(int); +} + +FILE *__wrap_fopen(const char *pathname, const char *mode) { + + const bool failure = mock_type(bool); + + if (failure) { + errno = mock_type(int); + + return 0; + } + + return mock_type(FILE *); +} + +FILE *__wrap_fdopen(int fd, const char *mode) { + + const bool failure = mock_type(bool); + + if (failure) { + errno = mock_type(int); + + return 0; + } + + return mock_type(FILE *); +} + +FILE *__wrap_freopen(const char *pathname, const char *mode, FILE *stream) { + + const bool failure = mock_type(bool); + + if (failure) { + errno = mock_type(int); + + return 0; + } + + return mock_type(FILE *); +} + int __wrap_feof_unlocked(FILE *stream) { const bool failure = mock_type(bool); @@ -147,6 +199,19 @@ int __wrap_ferror_unlocked(FILE *stream) { return mock_type(int); } +int __wrap_fflush(FILE *stream) { + + const bool failure = mock_type(bool); + + if (failure) { + errno = mock_type(int); + + return EOF; + } + + return mock_type(int); +} + int __wrap_fileno(FILE *stream) { return mock_type(int); } 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 11029b3..6a0bd98 100644 --- a/level_0/f_file/tests/unit/c/mock-file.h +++ b/level_0/f_file/tests/unit/c/mock-file.h @@ -37,8 +37,13 @@ extern int __wrap_fchmod(const char *pathname, mode_t mode); extern int __wrap_fchmodat(int dirfd, const char *pathname, mode_t mode, int flags); extern int __wrap_fchown(int fd, uid_t owner, gid_t group); extern int __wrap_fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags); +extern int __wrap_fclose(FILE *stream); +extern FILE *__wrap_fopen(const char *pathname, const char *mode); +extern FILE *__wrap_fdopen(int fd, const char *mode); +extern FILE *__wrap_freopen(const char *pathname, const char *mode, FILE *stream); extern int __wrap_feof_unlocked(FILE *stream); extern int __wrap_ferror_unlocked(FILE *stream); +extern int __wrap_fflush(FILE *stream); extern int __wrap_fileno(FILE *stream); extern void __wrap_flockfile(FILE *filehandle); extern size_t __wrap_fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream); diff --git a/level_0/f_file/tests/unit/c/test-file-clone.c b/level_0/f_file/tests/unit/c/test-file-clone.c index 1d9401d..1bc42db 100644 --- a/level_0/f_file/tests/unit/c/test-file-clone.c +++ b/level_0/f_file/tests/unit/c/test-file-clone.c @@ -495,49 +495,6 @@ void test__f_file_clone__fails_for_regular(void **state) { int errnos[] = { EBADF, EDQUOT, - EIO, - ENOSPC, - EROFS, - mock_errno_generic, - }; - - f_status_t statuss[] = { - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - }; - - struct stat statistics; - - memset(&statistics, 0, sizeof(struct stat)); - - statistics.st_mode = 1 | F_file_type_regular_d; - - for (int i = 0; i < 6; ++i) { - - will_return(__wrap_stat, false); - will_return(__wrap_stat, &statistics); - will_return(__wrap_stat, 0); - - will_return(__wrap_open, false); - will_return(__wrap_open, 0); - - will_return(__wrap_fsync, true); - will_return(__wrap_fsync, errnos[i]); - - const f_status_t status = f_file_clone(path, path, 0, 0); - - assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for - } - - { - int errnos[] = { - EBADF, - EDQUOT, EINTR, EIO, ENOSPC, diff --git a/level_0/f_file/tests/unit/c/test-file-copy.c b/level_0/f_file/tests/unit/c/test-file-copy.c index 490ff6e..cadcc34 100644 --- a/level_0/f_file/tests/unit/c/test-file-copy.c +++ b/level_0/f_file/tests/unit/c/test-file-copy.c @@ -852,50 +852,6 @@ void test__f_file_copy__fails_for_regular(void **state) { { int errnos[] = { EBADF, - EDQUOT, - EIO, - ENOSPC, - EROFS, - mock_errno_generic, - }; - - f_status_t statuss[] = { - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - }; - - struct stat statistics; - - memset(&statistics, 0, sizeof(struct stat)); - - statistics.st_mode = 1 | F_file_type_regular_d; - statistics.st_size = 1; - - for (int i = 0; i < 6; ++i) { - - will_return(__wrap_stat, false); - will_return(__wrap_stat, &statistics); - will_return(__wrap_stat, 0); - - will_return(__wrap_open, false); - will_return(__wrap_open, 0); - - will_return(__wrap_fsync, true); - will_return(__wrap_fsync, errnos[i]); - - const f_status_t status = f_file_copy(path, path, mode, 0, 0); - - assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for - } - - { - int errnos[] = { - EBADF, EINTR, EIO, ENOSPC, diff --git a/level_0/f_file/tests/unit/c/test-file-create.c b/level_0/f_file/tests/unit/c/test-file-create.c index e11fc49..5713f65 100644 --- a/level_0/f_file/tests/unit/c/test-file-create.c +++ b/level_0/f_file/tests/unit/c/test-file-create.c @@ -73,39 +73,6 @@ void test__f_file_create__fails(void **state) { int errnos[] = { EBADF, EDQUOT, - EIO, - ENOSPC, - EROFS, - mock_errno_generic, - }; - - f_status_t statuss[] = { - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - }; - - for (int i = 0; i < 6; ++i) { - - will_return(__wrap_open, false); - will_return(__wrap_open, 0); - - will_return(__wrap_fsync, true); - will_return(__wrap_fsync, errnos[i]); - - const f_status_t status = f_file_create(path, 0, F_false); - - assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for - } - - { - int errnos[] = { - EBADF, - EDQUOT, EINTR, EIO, ENOSPC, diff --git a/level_0/f_file/tests/unit/c/test-file-create_at.c b/level_0/f_file/tests/unit/c/test-file-create_at.c index d398f18..84f0ce3 100644 --- a/level_0/f_file/tests/unit/c/test-file-create_at.c +++ b/level_0/f_file/tests/unit/c/test-file-create_at.c @@ -74,39 +74,6 @@ void test__f_file_create_at__fails(void **state) { { int errnos[] = { EBADF, - EDQUOT, - EIO, - ENOSPC, - EROFS, - mock_errno_generic, - }; - - f_status_t statuss[] = { - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - F_file_synchronize, - }; - - for (int i = 0; i < 6; ++i) { - - will_return(__wrap_openat, false); - will_return(__wrap_openat, 0); - - will_return(__wrap_fsync, true); - will_return(__wrap_fsync, errnos[i]); - - const f_status_t status = f_file_create_at(0, path, 0, F_false); - - assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for - } - - { - int errnos[] = { - EBADF, EINTR, EIO, ENOSPC, diff --git a/level_0/f_file/tests/unit/c/test-file-flush.c b/level_0/f_file/tests/unit/c/test-file-flush.c index 73bc941..7e4362c 100644 --- a/level_0/f_file/tests/unit/c/test-file-flush.c +++ b/level_0/f_file/tests/unit/c/test-file-flush.c @@ -10,6 +10,7 @@ void test__f_file_flush__fails(void **state) { int errnos[] = { EBADF, EDQUOT, + EINVAL, EIO, ENOSPC, EROFS, @@ -19,13 +20,14 @@ void test__f_file_flush__fails(void **state) { f_status_t statuss[] = { F_file_descriptor, F_filesystem_quota_block, + F_supported_not, F_input_output, F_space_not, F_supported_not, - F_failure, + F_file_synchronize, }; - for (int i = 0; i < 6; ++i) { + for (int i = 0; i < 7; ++i) { will_return(__wrap_fsync, true); will_return(__wrap_fsync, errnos[i]); diff --git a/level_0/f_file/tests/unit/c/test-file-size_by_id.c b/level_0/f_file/tests/unit/c/test-file-size_by_id.c index 2b289fb..16bfd1b 100644 --- a/level_0/f_file/tests/unit/c/test-file-size_by_id.c +++ b/level_0/f_file/tests/unit/c/test-file-size_by_id.c @@ -41,8 +41,6 @@ void test__f_file_size_by_id__fails(void **state) { off_t size = 0; - printf("\nDEBUG: i=%d\n", i); - will_return(__wrap_fstat, true); will_return(__wrap_fstat, errnos[i]); diff --git a/level_0/f_file/tests/unit/c/test-file-stat_by_id.c b/level_0/f_file/tests/unit/c/test-file-stat_by_id.c index 6fd4a08..723b2f9 100644 --- a/level_0/f_file/tests/unit/c/test-file-stat_by_id.c +++ b/level_0/f_file/tests/unit/c/test-file-stat_by_id.c @@ -43,8 +43,6 @@ void test__f_file_stat_by_id__fails(void **state) { memset(&result, 0, sizeof(struct stat)); - printf("\nDEBUG: i=%d\n", i); - will_return(__wrap_fstat, true); will_return(__wrap_fstat, errnos[i]); diff --git a/level_0/f_file/tests/unit/c/test-file-stream_close.c b/level_0/f_file/tests/unit/c/test-file-stream_close.c index 07360c5..a860c35 100644 --- a/level_0/f_file/tests/unit/c/test-file-stream_close.c +++ b/level_0/f_file/tests/unit/c/test-file-stream_close.c @@ -5,24 +5,180 @@ extern "C" { #endif -void test__f_file_stream_close__fails(void **state) { +void test__f_file_stream_close__fails_for_file_descriptor(void **state) { + + int errno_flushs[] = { + EBADF, + EDQUOT, + EINVAL, + EIO, + ENOSPC, + EROFS, + }; + + int errno_closes[] = { + EBADF, + EDQUOT, + EINTR, + EIO, + ENOSPC, + mock_errno_generic, + }; + + f_status_t status_flushs[] = { + F_file_descriptor, + F_filesystem_quota_block, + F_supported_not, + F_input_output, + F_space_not, + F_supported_not, + }; + + f_status_t status_closes[] = { + F_file_descriptor, + F_filesystem_quota_block, + F_interrupt, + F_input_output, + F_space_not, + F_file_close, + }; + + for (uint8_t flush = 0; flush < 2; ++flush) { + + for (int i = 0; i < 6; ++i) { + + f_file_t file = f_file_t_initialize; + file.id = 0; + file.stream = 0; + + if (flush) { + if (flush == 1) { + will_return(__wrap_fsync, true); + will_return(__wrap_fsync, errno_flushs[i]); + } + else { + will_return(__wrap_fsync, false); + will_return(__wrap_fsync, 0); + } + + will_return(__wrap_close, true); + will_return(__wrap_close, errno_closes[i]); + } + else { + will_return(__wrap_close, true); + will_return(__wrap_close, errno_closes[i]); + } + + const f_status_t status = f_file_stream_close(flush, &file); + + assert_int_equal(F_status_set_fine(status), status_closes[i]); + + assert_int_equal(file.id, -1); + assert_int_equal(file.stream, 0); + } // for + } // for +} + +void test__f_file_stream_close__fails_for_stream(void **state) { int errnos[] = { + EACCES, + EAGAIN, + EBADF, + EFBIG, + EDEADLK, + EDESTADDRREQ, + EDQUOT, + EFAULT, + EINTR, + EINVAL, + EIO, + EMFILE, + ENOLCK, + ENOSPC, + ENOTDIR, + EPERM, + EPIPE, + EWOULDBLOCK, mock_errno_generic, }; - f_status_t statuss[] = { - F_failure, + f_status_t status_flushs[] = { + F_access_denied, + F_block, + F_file_descriptor, + F_file_overflow, + F_deadlock, + F_socket_not, + F_space_not, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_descriptor_max, + F_lock, + F_space_not, + F_file_type_not_directory, + F_prohibited, + F_pipe_not, + F_block, + F_file_synchronize, + }; + + f_status_t status_closes[] = { + F_access_denied, + F_block, + F_file_descriptor, + F_file_overflow, + F_deadlock, + F_socket_not, + F_space_not, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_descriptor_max, + F_lock, + F_space_not, + F_file_type_not_directory, + F_prohibited, + F_pipe_not, + F_block, + F_file_close, }; - for (int i = 0; i < 1; ++i) { + for (uint8_t flush = 0; flush < 2; ++flush) { + + for (int i = 0; i < 19; ++i) { + + f_file_t file = f_file_t_initialize; + file.id = 0; + file.stream = stdout; + + if (flush) { + if (flush == 1) { + will_return(__wrap_fflush, true); + will_return(__wrap_fflush, errnos[i]); + } + else { + will_return(__wrap_fflush, false); + will_return(__wrap_fflush, 0); + } - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + will_return(__wrap_fclose, true); + will_return(__wrap_fclose, errnos[i]); + } + else { + will_return(__wrap_fclose, true); + will_return(__wrap_fclose, errnos[i]); + } - //const f_status_t status = f_file_stream_close(path, F_false, &id); + const f_status_t status = f_file_stream_close(flush, &file); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + assert_int_equal(F_status_set_fine(status), status_closes[i]); + assert_int_equal(file.id, -1); + assert_int_equal(file.stream, 0); + } // for } // for } @@ -40,13 +196,73 @@ void test__f_file_stream_close__fails(void **state) { void test__f_file_stream_close__works(void **state) { { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + f_file_t file = f_file_t_initialize; + + file.id = 0; + file.stream = stdout; + + will_return(__wrap_fclose, false); + will_return(__wrap_fclose, 0); + + const f_status_t status = f_file_stream_close(F_false, &file); + + assert_int_equal(status, F_none); + assert_int_equal(file.id, -1); + assert_int_equal(file.stream, 0); + } + + { + f_file_t file = f_file_t_initialize; + + file.id = 0; + file.stream = 0; + + will_return(__wrap_close, false); + will_return(__wrap_close, 0); + + const f_status_t status = f_file_stream_close(F_false, &file); + + assert_int_equal(status, F_none); + assert_int_equal(file.id, -1); + assert_int_equal(file.stream, 0); + } + + { + f_file_t file = f_file_t_initialize; + + file.id = 0; + file.stream = stdout; + + will_return(__wrap_fflush, false); + will_return(__wrap_fflush, 0); + + will_return(__wrap_fclose, false); + will_return(__wrap_fclose, 0); + + const f_status_t status = f_file_stream_close(F_true, &file); + + assert_int_equal(status, F_none); + assert_int_equal(file.id, -1); + assert_int_equal(file.stream, 0); + } + + { + f_file_t file = f_file_t_initialize; + + file.id = 0; + file.stream = 0; + + will_return(__wrap_fsync, false); + will_return(__wrap_fsync, 0); + + will_return(__wrap_close, false); + will_return(__wrap_close, 0); - //const f_status_t status = f_file_stream_close(); + const f_status_t status = f_file_stream_close(F_true, &file); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none); + assert_int_equal(file.id, -1); + assert_int_equal(file.stream, 0); } } diff --git a/level_0/f_file/tests/unit/c/test-file-stream_close.h b/level_0/f_file/tests/unit/c/test-file-stream_close.h index 52baafe..5eeffc0 100644 --- a/level_0/f_file/tests/unit/c/test-file-stream_close.h +++ b/level_0/f_file/tests/unit/c/test-file-stream_close.h @@ -11,11 +11,18 @@ #define _TEST__F_file_stream_close_h /** - * Test that function fails. + * Test that function fails for a file using the file descriptor. * * @see f_file_stream_close() */ -extern void test__f_file_stream_close__fails(void **state); +extern void test__f_file_stream_close__fails_for_file_descriptor(void **state); + +/** + * Test that function fails for a file stream. + * + * @see f_file_stream_close() + */ +extern void test__f_file_stream_close__fails_for_stream(void **state); /** * Test that parameter checking works as expected. diff --git a/level_0/f_file/tests/unit/c/test-file-stream_descriptor.c b/level_0/f_file/tests/unit/c/test-file-stream_descriptor.c deleted file mode 100644 index 303e6f4..0000000 --- a/level_0/f_file/tests/unit/c/test-file-stream_descriptor.c +++ /dev/null @@ -1,66 +0,0 @@ -#include "test-file.h" -#include "test-file-stream_descriptor.h" - -#ifdef __cplusplus -extern "C" { -#endif - -void test__f_file_stream_descriptor__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_stream_descriptor(path, F_false, &id); - - //assert_int_equal(F_status_set_fine(status), statuss[i]); - } // for -} - -#ifndef _di_level_0_parameter_checking_ - void test__f_file_stream_descriptor__parameter_checking(void **state) { - - { - const f_status_t status = f_file_stream_descriptor(f_string_empty_s, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - } -#endif // _di_level_0_parameter_checking_ - -void test__f_file_stream_descriptor__returns_data_not(void **state) { - - { - f_file_t file = f_file_t_initialize; - - const f_status_t status = f_file_stream_descriptor(f_string_empty_s, &file); - - assert_int_equal(status, F_data_not); - } -} - -void test__f_file_stream_descriptor__works(void **state) { - - { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); - - //const f_status_t status = f_file_stream_descriptor(); - - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); - } -} - -#ifdef __cplusplus -} // extern "C" -#endif diff --git a/level_0/f_file/tests/unit/c/test-file-stream_descriptor.h b/level_0/f_file/tests/unit/c/test-file-stream_descriptor.h deleted file mode 100644 index 66b5058..0000000 --- a/level_0/f_file/tests/unit/c/test-file-stream_descriptor.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * FLL - Level 0 - * - * Project: File - * API Version: 0.5 - * Licenses: lgpl-2.1-or-later - * - * Test the file project. - */ -#ifndef _TEST__F_file_stream_descriptor_h -#define _TEST__F_file_stream_descriptor_h - -/** - * Test that function fails. - * - * @see f_file_stream_descriptor() - */ -extern void test__f_file_stream_descriptor__fails(void **state); - -/** - * Test that parameter checking works as expected. - * - * @see f_file_stream_descriptor() - */ -#ifndef _di_level_0_parameter_checking_ - extern void test__f_file_stream_descriptor__parameter_checking(void **state); -#endif // _di_level_0_parameter_checking_ - -/** - * Test that function works but the path is empty. - * - * @see f_file_stream_descriptor() - */ -extern void test__f_file_stream_descriptor__returns_data_not(void **state); - -/** - * Test that function works. - * - * @see f_file_stream_descriptor() - */ -extern void test__f_file_stream_descriptor__works(void **state); - -#endif // _TEST__F_file_stream_descriptor_h diff --git a/level_0/f_file/tests/unit/c/test-file-stream_open_descriptor.c b/level_0/f_file/tests/unit/c/test-file-stream_open_descriptor.c new file mode 100644 index 0000000..7d4d997 --- /dev/null +++ b/level_0/f_file/tests/unit/c/test-file-stream_open_descriptor.c @@ -0,0 +1,126 @@ +#include "test-file.h" +#include "test-file-stream_open_descriptor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_file_stream_open_descriptor__fails(void **state) { + + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + + int errnos[] = { + EACCES, + EAGAIN, + EBADF, + EFBIG, + EDEADLK, + EDESTADDRREQ, + EDQUOT, + EFAULT, + EINTR, + EINVAL, + EIO, + EMFILE, + ENOLCK, + ENOMEM, + ENOSPC, + ENOTDIR, + EPERM, + EPIPE, + EWOULDBLOCK, + mock_errno_generic, + }; + + f_status_t statuss[] = { + F_access_denied, + F_block, + F_file_descriptor, + F_file_overflow, + F_deadlock, + F_socket_not, + F_space_not, + F_buffer, + F_interrupt, + F_parameter, + F_input_output, + F_file_descriptor_max, + F_lock, + F_memory_not, + F_space_not, + F_file_type_not_directory, + F_prohibited, + F_pipe_not, + F_block, + F_failure, + }; + + for (int i = 0; i < 20; ++i) { + + f_file_t file = f_file_t_initialize; + + will_return(__wrap_fdopen, true); + will_return(__wrap_fdopen, errnos[i]); + + const f_status_t status = f_file_stream_open_descriptor(path, &file); + + assert_int_equal(F_status_set_fine(status), statuss[i]); + } // for + + for (int i = 0; i < 20; ++i) { + + f_file_t file = f_file_t_initialize; + + will_return(__wrap_fdopen, true); + will_return(__wrap_fdopen, errnos[i]); + + const f_status_t status = f_file_stream_open_descriptor(f_string_empty_s, &file); + + assert_int_equal(F_status_set_fine(status), statuss[i]); + } // for +} + +#ifndef _di_level_0_parameter_checking_ + void test__f_file_stream_open_descriptor__parameter_checking(void **state) { + + { + const f_status_t status = f_file_stream_open_descriptor(f_string_empty_s, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_file_stream_open_descriptor__works(void **state) { + + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + const int id = 1; + + { + f_file_t file = f_file_t_initialize; + + will_return(__wrap_fdopen, false); + will_return(__wrap_fdopen, id); + + const f_status_t status = f_file_stream_open_descriptor(path, &file); + + assert_int_equal(status, F_none); + assert_int_equal(file.id, id); + } + + { + f_file_t file = f_file_t_initialize; + + will_return(__wrap_fdopen, false); + will_return(__wrap_fdopen, id); + + const f_status_t status = f_file_stream_open_descriptor(f_string_empty_s, &file); + + assert_int_equal(status, F_none); + assert_int_equal(file.id, id); + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_file/tests/unit/c/test-file-stream_open_descriptor.h b/level_0/f_file/tests/unit/c/test-file-stream_open_descriptor.h new file mode 100644 index 0000000..a2deb6d --- /dev/null +++ b/level_0/f_file/tests/unit/c/test-file-stream_open_descriptor.h @@ -0,0 +1,36 @@ +/** + * FLL - Level 0 + * + * Project: File + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the file project. + */ +#ifndef _TEST__F_file_stream_open_descriptor_h +#define _TEST__F_file_stream_open_descriptor_h + +/** + * Test that function fails. + * + * @see f_file_stream_open_descriptor() + */ +extern void test__f_file_stream_open_descriptor__fails(void **state); + +/** + * Test that parameter checking works as expected. + * + * @see f_file_stream_open_descriptor() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_file_stream_open_descriptor__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_file_stream_open_descriptor() + */ +extern void test__f_file_stream_open_descriptor__works(void **state); + +#endif // _TEST__F_file_stream_open_descriptor_h 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 1ef2b7d..6ef4f47 100644 --- a/level_0/f_file/tests/unit/c/test-file.c +++ b/level_0/f_file/tests/unit/c/test-file.c @@ -259,12 +259,15 @@ int main(void) { cmocka_unit_test(test__f_file_stat_by_id__returns_file_closed), cmocka_unit_test(test__f_file_stat_by_id__works), -/* - - // f_file_stream_close + cmocka_unit_test(test__f_file_stream_close__fails_for_file_descriptor), + cmocka_unit_test(test__f_file_stream_close__fails_for_stream), + cmocka_unit_test(test__f_file_stream_close__works), - // f_file_stream_descriptor + // __wrap_fdopen does not appear to be working. + //cmocka_unit_test(test__f_file_stream_open_descriptor__fails), + //cmocka_unit_test(test__f_file_stream_open_descriptor__works), +/* // f_file_stream_open // f_file_stream_read @@ -364,7 +367,7 @@ int main(void) { cmocka_unit_test(test__f_file_stat_at__parameter_checking), cmocka_unit_test(test__f_file_stat_by_id__parameter_checking), cmocka_unit_test(test__f_file_stream_close__parameter_checking), - cmocka_unit_test(test__f_file_stream_descriptor__parameter_checking), + cmocka_unit_test(test__f_file_stream_open_descriptor__parameter_checking), cmocka_unit_test(test__f_file_stream_open__parameter_checking), cmocka_unit_test(test__f_file_stream_read__parameter_checking), cmocka_unit_test(test__f_file_stream_read_block__parameter_checking), diff --git a/level_0/f_file/tests/unit/c/test-file.h b/level_0/f_file/tests/unit/c/test-file.h index 1969b64..8741d5d 100644 --- a/level_0/f_file/tests/unit/c/test-file.h +++ b/level_0/f_file/tests/unit/c/test-file.h @@ -82,7 +82,7 @@ #include "test-file-stat_at.h" #include "test-file-stat_by_id.h" #include "test-file-stream_close.h" -#include "test-file-stream_descriptor.h" +#include "test-file-stream_open_descriptor.h" #include "test-file-stream_open.h" #include "test-file-stream_read.h" #include "test-file-stream_read_block.h"