diff options
-rw-r--r-- | test/Makefile.am | 14 | ||||
-rw-r--r-- | test/fileop_test_set_get_remove.cpp | 474 | ||||
-rw-r--r-- | test/fileop_test_unit.cpp | 256 | ||||
-rw-r--r-- | test/interface_test_unit.cpp | 12 | ||||
-rw-r--r-- | test/mock/syscall_io_mock.hpp | 54 | ||||
-rwxr-xr-x | tools/run-test.sh | 2 |
6 files changed, 797 insertions, 15 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index 050390e..d48109a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,6 +4,8 @@ ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} bin_PROGRAMS = \ interface_test interface_test_unit \ fileop_test_utils \ + fileop_test_set_get_remove \ + fileop_test_unit \ file_util_test interface_test_SOURCES = \ @@ -23,6 +25,16 @@ fileop_test_utils_SOURCES = \ ../lib/static-configurator.c \ ../lib/file-util.c +fileop_test_set_get_remove_SOURCES = \ + fileop_test_set_get_remove.cpp \ + ../lib/static-configurator.c \ + ../lib/file-util.c + + +fileop_test_unit_SOURCES = \ + fileop_test_unit.cpp \ + ../lib/static-configurator.c + file_util_test_SOURCES = \ file_util_test.cpp @@ -39,6 +51,7 @@ interface_test_LDADD = \ # C compiler options CFLAGS = \ -g \ + -fpermissive \ -fsanitize=address -coverage \ -I$(top_srcdir)/lib \ -I$(top_srcdir)/include \ @@ -47,6 +60,7 @@ CFLAGS = \ # C++ compiler options CXXFLAGS = \ -g \ + -fpermissive \ -std=c++11 -fno-exceptions \ -fsanitize=address -coverage \ -I$(top_srcdir)/lib \ diff --git a/test/fileop_test_set_get_remove.cpp b/test/fileop_test_set_get_remove.cpp new file mode 100644 index 0000000..ae3d9f0 --- /dev/null +++ b/test/fileop_test_set_get_remove.cpp @@ -0,0 +1,474 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * + * @file fileop_test_set_get_remove.cpp + * @brief Unit test fot libredundancyfileop.c + */ +#include <gtest/gtest.h> +#include "mock/syscall_io_mock.hpp" + +// Test Terget files --------------------------------------- +extern "C" { +#include "../lib/libredundancyfileop.c" +} +// Test Terget files --------------------------------------- +using namespace ::testing; + +struct fileop_test_set_get_remove_test : Test, SyscallIOMockBase {}; + +//-------------------------------------------------------------------------------------------------------- +// stubs +int g_refop_new_file_write_ret = 0; +int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize) +{ + return g_refop_new_file_write_ret; +} + +int g_refop_file_rotation_ret = 0; +int refop_file_rotation(refop_handle_t handle) +{ + return g_refop_file_rotation_ret; +} + +int g_refop_file_pickup_ret = 0; +int refop_file_pickup(refop_handle_t handle, uint8_t *data, int64_t bufsize, int64_t *readsize) +{ + return g_refop_file_pickup_ret; +} + +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_set_redundancy_data__arg_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t dmybuf[128]; + + //dummy data + ret = refop_set_redundancy_data(NULL, NULL, -100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_set_redundancy_data(handle, NULL, -100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_set_redundancy_data(NULL, dmybuf, -100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_set_redundancy_data(NULL, NULL, 100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_set_redundancy_data(handle, dmybuf, -100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_set_redundancy_data(handle, NULL, 100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_set_redundancy_data(NULL, dmybuf, 100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_set_redundancy_data__refop_new_file_write_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t dmybuf[128]; + + //stub setup data + g_refop_new_file_write_ret = 0; + /* + * @retval 0 Succeeded. + * @retval -1 Abnormal fail. Shall not continue. + * @retval -2 Lager than size limit. + */ + g_refop_file_rotation_ret = 0; + g_refop_file_pickup_ret = 0; + + g_refop_new_file_write_ret = -1; + ret = refop_set_redundancy_data(handle, dmybuf, 100); + ASSERT_EQ(REFOP_SYSERROR, ret); + + g_refop_new_file_write_ret = -2; + ret = refop_set_redundancy_data(handle, dmybuf, 100); + ASSERT_EQ(REFOP_ARGERROR, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_set_redundancy_data__refop_file_rotation_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t dmybuf[128]; + + //stub setup data + g_refop_new_file_write_ret = 0; + g_refop_file_rotation_ret = 0; + /* + * @retval 0 Succeeded. + * @retval -1 Abnormal fail. Shall not continue. + */ + g_refop_file_pickup_ret = 0; + + g_refop_file_rotation_ret = -1; + EXPECT_CALL(sysiom, unlink(_)).WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_set_redundancy_data(handle, dmybuf, 100); + ASSERT_EQ(REFOP_SYSERROR, ret); + + g_refop_file_rotation_ret = -1; + EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0)); + ret = refop_set_redundancy_data(handle, dmybuf, 100); + ASSERT_EQ(REFOP_SYSERROR, ret); + + g_refop_file_rotation_ret = 0; + ret = refop_set_redundancy_data(handle, dmybuf, 100); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_get_redundancy_data__arg_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t dmybuf[128]; + int64_t getsize; + + //dummy data + //all error + ret = refop_get_redundancy_data(NULL, NULL, -100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + // 3 args error + ret = refop_get_redundancy_data(handle, NULL, -100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, dmybuf, -100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, NULL, 100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, NULL, -100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + // 2 args error + ret = refop_get_redundancy_data(handle, dmybuf, -100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(handle, NULL, 100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(handle, NULL, -100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, dmybuf, 100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, dmybuf, -100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, NULL, 100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + //1 arg error + ret = refop_get_redundancy_data(handle, dmybuf, 100, NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(handle, dmybuf, -100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(handle, NULL, 100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + ret = refop_get_redundancy_data(NULL, dmybuf, 100, &getsize); + ASSERT_EQ(REFOP_ARGERROR, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_get_redundancy_data__refop_file_pickup_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t dmybuf[128]; + int64_t getsize; + + //stub setup data + g_refop_new_file_write_ret = 0; + g_refop_file_rotation_ret = 0; + g_refop_file_pickup_ret = 0; + /* + * @retval 0 Succeeded. + * @retval 1 Succeeded with recover. + * @retval -1 Abnormal fail. Shall not continue. + * @retval -2 No data. + * @retval -3 Broaken data. + */ + g_refop_file_pickup_ret = -3; + ret = refop_get_redundancy_data(handle, dmybuf, 100, &getsize); + ASSERT_EQ(REFOP_BROAKEN, ret); + + g_refop_file_pickup_ret = -2; + ret = refop_get_redundancy_data(handle, dmybuf, 100, &getsize); + ASSERT_EQ(REFOP_NOENT, ret); + + g_refop_file_pickup_ret = -1; + ret = refop_get_redundancy_data(handle, dmybuf, 100, &getsize); + ASSERT_EQ(REFOP_SYSERROR, ret); + + g_refop_file_pickup_ret = 0; + ret = refop_get_redundancy_data(handle, dmybuf, 100, &getsize); + ASSERT_EQ(REFOP_SUCCESS, ret); + + g_refop_file_pickup_ret = 1; + ret = refop_get_redundancy_data(handle, dmybuf, 100, &getsize); + ASSERT_EQ(REFOP_RECOVER, ret); + + free(handle); +} + +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_remove_redundancy_data__arg_error) +{ + refop_error_t ret = REFOP_SUCCESS; + + //dummy data + ret = refop_remove_redundancy_data(NULL); + ASSERT_EQ(REFOP_ARGERROR, ret); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_remove_redundancy_data__unlink_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + + // 3 error + // EACCES - EACCES - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // 2 error + // success - EACCES - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // ENOENT - EACCES - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - success - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - ENOENT - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - EACCES - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - EACCES - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // 1 error + // success - success - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // success - ENOENT - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // ENOENT - success - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // ENOENT - ENOENT - EACCES + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // success - EACCES - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // success - EACCES - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // ENOENT - EACCES - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // ENOENT - EACCES - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - success - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(Return(0)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - success - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - ENOENT - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + // EACCES - ENOENT - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SYSERROR, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_set_get_remove_test, unit_test_refop_remove_redundancy_data__unlink_success) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + + // success - success - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // ENOENT - ENOENT - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // ENOENT - success - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // success - ENOENT - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // success - success - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // ENOENT - ENOENT - success + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // success - ENOENT - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // ENOENT - success - ENOENT + EXPECT_CALL(sysiom, unlink(_)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(handle); +} diff --git a/test/fileop_test_unit.cpp b/test/fileop_test_unit.cpp new file mode 100644 index 0000000..5bedf34 --- /dev/null +++ b/test/fileop_test_unit.cpp @@ -0,0 +1,256 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * + * @file fileop_test_unit.cpp + * @brief Unit test fot fileop.c + */ +#include <gtest/gtest.h> +#include "mock/syscall_io_mock.hpp" + +// Test Terget files --------------------------------------- +extern "C" { +#include "../lib/fileop.c" +} +// Test Terget files --------------------------------------- +using namespace ::testing; + +struct fileop_test_unit_test : Test, SyscallIOMockBase {}; + +//-------------------------------------------------------------------------------------------------------- +//stubs +ssize_t g_safe_read_ret = 0; +ssize_t safe_read(int fd, void *buf, size_t count) +{ + return g_safe_read_ret; +} + +ssize_t g_safe_write_ret = 0; +ssize_t safe_write(int fd, void *buf, size_t count) +{ + return g_safe_write_ret; +} + +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__arg_error) +{ + int ret = -1; + refop_handle_t handle = NULL; + uint8_t dmybuf[128]; + + //dummy data + + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit() + 1); + ASSERT_EQ(-2, ret); + + ret = refop_new_file_write(handle, dmybuf, 0); + ASSERT_EQ(-2, ret); + + ret = refop_new_file_write(handle, dmybuf, -1); + ASSERT_EQ(-2, ret); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__unlink_error) +{ + int ret = -1; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t dmybuf[128]; + + EXPECT_CALL(sysiom, unlink(_)).WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(-1, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__open_error) +{ + int ret = -1; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t *dmybuf = (uint8_t*)calloc(1, refop_get_config_data_size_limit()); + + EXPECT_CALL(sysiom, unlink(_)).WillOnce(SetErrnoAndReturn(ENOENT, -1)); + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(-1)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(-1, ret); + + EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0)); + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(-1)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(-1, ret); + + free(dmybuf); + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__safe_write_error) +{ + int ret = -1; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + uint8_t *dmybuf = (uint8_t*)calloc(1, refop_get_config_data_size_limit()); + + //dummy + g_safe_read_ret = 0; + g_safe_write_ret = 0; + + g_safe_write_ret = -1; + EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0)); + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100)); + EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(-1, ret); + + g_safe_write_ret = 0; + EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0)); + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100)); + EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0)); + EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(0, ret); + + g_safe_write_ret = 0; + EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0)); + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100)); + EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0)); + EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(0, ret); + + free(dmybuf); + free(handle); +} +#if 0 +//-------------------------------------------------------------------------------------------------------- +TEST_F(interface_test, interface_test_refop_create_redundancy_handle__stat_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + char directry[] = "/tmp"; + char file[] = "test.bin"; + + /* stat error case + EACCES + EFAULT + ELOOP + ENAMETOOLONG + ENOENT + ENOMEM + ENOTDIR + EOVERFLOW + */ + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_NOENT, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(EFAULT, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SYSERROR, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(ELOOP, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_NOENT, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(ENAMETOOLONG, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_ARGERROR, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_NOENT, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(ENOMEM, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SYSERROR, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(ENOTDIR, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_NOENT, ret); + + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(SetErrnoAndReturn(EOVERFLOW, -1)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SYSERROR, ret); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(interface_test, interface_test_refop_create_redundancy_handle__pathcheck_error) +{ + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + char directry[PATH_MAX]; + char file[PATH_MAX]; + + memset(directry,0,sizeof(directry)); + memset(file,0,sizeof(file)); + + //short directry string + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(Return(0)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_ARGERROR, ret); + + //short file string + strncpy(directry,"/tmp",PATH_MAX); + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(Return(0)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_ARGERROR, ret); + + // too long path + for(int i=1;i < (PATH_MAX-1);i++) + directry[i] = 'd'; + strncpy(file,"test.bin",PATH_MAX); + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(Return(0)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_ARGERROR, ret); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(interface_test, interface_test_refop_create_redundancy_handle__success) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + char directry[] = "/tmp"; + char directry2[] = "/tmp/"; + char file[] = "test.bin"; + char resultstr[] = "/tmp/test.bin"; + char resultstr_bk1[] = "/tmp/test.bin.bk1"; + char resultstr_new[] = "/tmp/test.bin.tmp"; + + //short directry string + EXPECT_CALL(sysiom, stat(directry, _)).WillOnce(Return(0)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + //data check + hndl = (struct refop_halndle *)handle; + ASSERT_EQ(0, strcmp(hndl->latestfile,resultstr)); + ASSERT_EQ(0, strcmp(hndl->backupfile1,resultstr_bk1)); + ASSERT_EQ(0, strcmp(hndl->newfile,resultstr_new)); + ASSERT_EQ(0, strcmp(hndl->basedir,directry2)); + free(handle); + + //short file string + EXPECT_CALL(sysiom, stat(directry2, _)).WillOnce(Return(0)); + ret = refop_create_redundancy_handle(&handle, directry2, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + //data check + hndl = (struct refop_halndle *)handle; + ASSERT_EQ(0, strcmp(hndl->latestfile,resultstr)); + ASSERT_EQ(0, strcmp(hndl->backupfile1,resultstr_bk1)); + ASSERT_EQ(0, strcmp(hndl->newfile,resultstr_new)); + ASSERT_EQ(0, strcmp(hndl->basedir,directry2)); + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +#endif +/*TEST_F(data_pool_test_others, test_data_pool_test_data_pool_unlock__false) +{ + bool ret = true; + + EXPECT_CALL(lpm, pthread_mutex_unlock(_)) + .WillOnce(Return(-1)); + ret = data_pool_unlock(); + ASSERT_EQ(false, ret); +}*/ diff --git a/test/interface_test_unit.cpp b/test/interface_test_unit.cpp index b9e8df1..3dc5f70 100644 --- a/test/interface_test_unit.cpp +++ b/test/interface_test_unit.cpp @@ -14,10 +14,10 @@ extern "C" { // Test Terget files --------------------------------------- using namespace ::testing; -struct interface_test : Test, SyscallIOMockBase {}; +struct interface_test_unit : Test, SyscallIOMockBase {}; //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__arg_error) +TEST_F(interface_test_unit, interface_test_unit_refop_create_redundancy_handle__arg_error) { refop_error_t ret = REFOP_SUCCESS; refop_handle_t handle = NULL; @@ -45,7 +45,7 @@ TEST_F(interface_test, interface_test_refop_create_redundancy_handle__arg_error) ASSERT_EQ(REFOP_ARGERROR, ret); } //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__stat_error) +TEST_F(interface_test_unit, interface_test_unit_refop_create_redundancy_handle__stat_error) { refop_error_t ret = REFOP_SUCCESS; refop_handle_t handle = NULL; @@ -98,7 +98,7 @@ TEST_F(interface_test, interface_test_refop_create_redundancy_handle__stat_error ASSERT_EQ(REFOP_SYSERROR, ret); } //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__pathcheck_error) +TEST_F(interface_test_unit, interface_test_unit_refop_create_redundancy_handle__pathcheck_error) { refop_error_t ret = REFOP_SUCCESS; refop_handle_t handle = NULL; @@ -130,7 +130,7 @@ TEST_F(interface_test, interface_test_refop_create_redundancy_handle__pathcheck_ ASSERT_EQ(REFOP_ARGERROR, ret); } //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__success) +TEST_F(interface_test_unit, interface_test_unit_refop_create_redundancy_handle__success) { struct refop_halndle *hndl; refop_error_t ret = REFOP_SUCCESS; @@ -171,7 +171,7 @@ TEST_F(interface_test, interface_test_refop_create_redundancy_handle__success) ASSERT_EQ(REFOP_SUCCESS, ret); } //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_release_redundancy_handle__all) +TEST_F(interface_test_unit, interface_test_unit_refop_release_redundancy_handle__all) { struct refop_halndle *hndl; refop_error_t ret = REFOP_SUCCESS; diff --git a/test/mock/syscall_io_mock.hpp b/test/mock/syscall_io_mock.hpp index 75efdb1..61e1840 100644 --- a/test/mock/syscall_io_mock.hpp +++ b/test/mock/syscall_io_mock.hpp @@ -7,6 +7,11 @@ #include <sys/stat.h> /* +int open(const char *pathname, int flags); +int open(const char *pathname, int flags, mode_t mode); +*/ +static std::function<int(const char *pathname, int flags)> _open; +/* ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); int close(int fd); @@ -16,6 +21,11 @@ static std::function<ssize_t(int fd, const void *buf, size_t count)> _write; static std::function<int(int)> _close; /* +int fsync(int fd); +*/ +static std::function<int(int)> _fsync; + +/* int unlink(const char *pathname); int stat(const char *pathname, struct stat *buf); */ @@ -39,6 +49,10 @@ static std::function<int(int sockfd, struct sockaddr *addr, class SyscallIOMocker { public: SyscallIOMocker() { + _open = [this](const char *pathname, int flags) { + return open(pathname, flags); + }; + _read = [this](int fd, void *buf, size_t count) { return read(fd, buf, count); }; @@ -49,6 +63,10 @@ public: return close(fd); }; + _fsync = [this](int fd){ + return fsync(fd); + }; + _unlink = [this](const char *pathname){ return unlink(pathname); }; @@ -72,10 +90,14 @@ public: } ~SyscallIOMocker() { + _open = {}; + _read = {}; _write = {}; _close = {}; + _fsync = {}; + _unlink = {}; _stat = {}; @@ -85,10 +107,14 @@ public: _accept4 = {}; } + MOCK_CONST_METHOD2(open, int(const char *pathname, int flags)); + MOCK_CONST_METHOD3(read, ssize_t(int fd, void *buf, size_t count)); MOCK_CONST_METHOD3(write, ssize_t(int fd, const void *buf, size_t count)); MOCK_CONST_METHOD1(close, int(int)); + MOCK_CONST_METHOD1(fsync, int(int)); + MOCK_CONST_METHOD1(unlink, int(const char *)); MOCK_CONST_METHOD2(stat, int(const char *pathname, struct stat *buf)); @@ -106,47 +132,57 @@ protected: #ifdef __cplusplus extern "C" { #endif -ssize_t read(int fd, void *buf, size_t count) +static int open(const char *pathname, int flags, ...) +{ + return _open(pathname, flags); +} + +static ssize_t read(int fd, void *buf, size_t count) { return _read(fd, buf, count); } -ssize_t write(int fd, const void *buf, size_t count) +static ssize_t write(int fd, const void *buf, size_t count) { return _write(fd, buf, count); } -int close(int fd) +static int close(int fd) { return _close(fd); } -int unlink(const char *pathname) +static int fsync(int fd) +{ + return _fsync(fd); +} + +static int unlink(const char *pathname) { return _unlink(pathname); } -int stat(const char *pathname, struct stat *buf) +static int stat(const char *pathname, struct stat *buf) { return _stat(pathname, buf); } -int socket(int socket_family, int socket_type, int protocol) +static int socket(int socket_family, int socket_type, int protocol) { return _socket(socket_family, socket_type, protocol); } -int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen) +static int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen) { return _bind(sockfd, addr, addrlen); } -int listen(int sockfd, int backlog) +static int listen(int sockfd, int backlog) { return _listen(sockfd, backlog); } -int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) +static int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { return _accept4(sockfd, addr, addrlen, flags); } diff --git a/tools/run-test.sh b/tools/run-test.sh index 489427b..2bebbd2 100755 --- a/tools/run-test.sh +++ b/tools/run-test.sh @@ -4,3 +4,5 @@ ./test/fileop_test_utils ./test/file_util_test ./test/interface_test +./test/fileop_test_set_get_remove +./test/fileop_test_unit |