diff options
author | Naoto Yamaguchi <naoto.yamaguchi@aisin.co.jp> | 2022-07-21 07:14:31 +0900 |
---|---|---|
committer | Naoto Yamaguchi <naoto.yamaguchi@aisin.co.jp> | 2022-08-05 00:35:12 +0900 |
commit | b0ed05f610e4bf70eae7929163e1ac8794bd15b0 (patch) | |
tree | fa0d21d6fec4e8b6c0393776afbabdb592e78c41 | |
parent | eb7fddaec547c4c9d47cee77bd5419d34ab5f1df (diff) |
Update test case
Ths patch update tase case
Bug-AGL: SPEC-4500
Signed-off-by: Naoto Yamaguchi <naoto.yamaguchi@aisin.co.jp>
Change-Id: I27b4a537d522198f5f180ce22fb7f0af95b3ddb8
-rw-r--r-- | test/Makefile.am | 23 | ||||
-rw-r--r-- | test/file_util_test.cpp | 4 | ||||
-rw-r--r-- | test/fileop_test_unit.cpp | 323 | ||||
-rw-r--r-- | test/fileop_test_unit_memory.cpp | 36 | ||||
-rw-r--r-- | test/fileop_test_utils.cpp | 133 | ||||
-rw-r--r-- | test/interface_test.cpp | 302 | ||||
-rw-r--r-- | test/interface_test_filebreak.cpp | 1266 | ||||
-rw-r--r-- | test/interface_test_unit.cpp | 4 | ||||
-rw-r--r-- | test/interface_test_unit_memory.cpp | 40 | ||||
-rw-r--r-- | test/mock/memory_mock.hpp | 53 | ||||
-rw-r--r-- | test/mock/syscall_io_mock.hpp | 14 | ||||
-rwxr-xr-x | tools/run-test.sh | 6 |
12 files changed, 1935 insertions, 269 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index d48109a..7904988 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,10 +2,12 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} bin_PROGRAMS = \ - interface_test interface_test_unit \ + interface_test interface_test_filebreak \ + interface_test_unit interface_test_unit_memory \ fileop_test_utils \ fileop_test_set_get_remove \ fileop_test_unit \ + fileop_test_unit_memory \ file_util_test interface_test_SOURCES = \ @@ -14,12 +16,24 @@ interface_test_SOURCES = \ ../lib/file-util.c \ ../lib/fileop.c +interface_test_filebreak_SOURCES = \ + interface_test_filebreak.cpp \ + ../lib/static-configurator.c \ + ../lib/file-util.c \ + ../lib/fileop.c + interface_test_unit_SOURCES = \ interface_test_unit.cpp \ ../lib/static-configurator.c \ ../lib/file-util.c \ ../lib/fileop.c +interface_test_unit_memory_SOURCES = \ + interface_test_unit_memory.cpp \ + ../lib/static-configurator.c \ + ../lib/file-util.c \ + ../lib/fileop.c + fileop_test_utils_SOURCES = \ fileop_test_utils.cpp \ ../lib/static-configurator.c \ @@ -30,11 +44,15 @@ fileop_test_set_get_remove_SOURCES = \ ../lib/static-configurator.c \ ../lib/file-util.c - fileop_test_unit_SOURCES = \ fileop_test_unit.cpp \ ../lib/static-configurator.c +fileop_test_unit_memory_SOURCES = \ + fileop_test_unit_memory.cpp \ + ../lib/static-configurator.c \ + ../lib/file-util.c + file_util_test_SOURCES = \ file_util_test.cpp @@ -51,7 +69,6 @@ interface_test_LDADD = \ # C compiler options CFLAGS = \ -g \ - -fpermissive \ -fsanitize=address -coverage \ -I$(top_srcdir)/lib \ -I$(top_srcdir)/include \ diff --git a/test/file_util_test.cpp b/test/file_util_test.cpp index f78fbe2..b9c0a7a 100644 --- a/test/file_util_test.cpp +++ b/test/file_util_test.cpp @@ -1,8 +1,8 @@ /** * SPDX-License-Identifier: Apache-2.0 * - * @file data-pool-service-test.c - * @brief Unit test fot data-pool-service-test.c + * @file file_util_test.cpp + * @brief Unit test fot file-util.c */ #include <gtest/gtest.h> #include "mock/syscall_io_mock.hpp" diff --git a/test/fileop_test_unit.cpp b/test/fileop_test_unit.cpp index 5bedf34..83d8cdc 100644 --- a/test/fileop_test_unit.cpp +++ b/test/fileop_test_unit.cpp @@ -21,6 +21,9 @@ struct fileop_test_unit_test : Test, SyscallIOMockBase {}; ssize_t g_safe_read_ret = 0; ssize_t safe_read(int fd, void *buf, size_t count) { + if (g_safe_read_ret == sizeof(s_refop_file_header)) { + refop_header_create((s_refop_file_header*)buf, 100, refop_get_config_data_size_limit()+1); + } return g_safe_read_ret; } @@ -118,139 +121,223 @@ TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__safe_write_error) free(dmybuf); free(handle); } -#if 0 //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__stat_error) +TEST_F(fileop_test_unit_test, unit_test_refop_file_rotation__stat_error) { - refop_error_t ret = REFOP_SUCCESS; - refop_handle_t handle = NULL; + int ret = -1; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + + //dummy + g_safe_read_ret = 0; + g_safe_write_ret = 0; + g_safe_write_ret = 0; - //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); + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_file_rotation(handle); + ASSERT_EQ(-1, ret); + + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_file_rotation(handle); + ASSERT_EQ(-1, ret); + + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(SetErrnoAndReturn(EACCES, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_file_rotation(handle); + ASSERT_EQ(-1, ret); + + free(handle); } //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__pathcheck_error) +TEST_F(fileop_test_unit_test, unit_test_refop_file_rotation__open_error) { - refop_error_t ret = REFOP_SUCCESS; - refop_handle_t handle = NULL; + int ret = -1; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + + //dummy + g_safe_read_ret = 0; + g_safe_write_ret = 0; + g_safe_write_ret = 0; + + // use a4 mode + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + EXPECT_CALL(sysiom, rename(_, _)) + .WillOnce(Return(0)); + EXPECT_CALL(sysiom, open(_, _)) + .WillOnce(Return(-1)); + ret = refop_file_rotation(handle); + ASSERT_EQ(0, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_test, unit_test_refop_file_rotation__a1_a2_a3_a4) +{ + int ret = -1; + refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle)); + + //dummy + g_safe_read_ret = 0; + g_safe_write_ret = 0; + g_safe_write_ret = 0; + strncpy(handle->backupfile1,"backup1",sizeof(handle->backupfile1)); + strncpy(handle->latestfile,"latestfile",sizeof(handle->backupfile1)); + strncpy(handle->newfile,"newfile",sizeof(handle->backupfile1)); + + // a1 mode + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(Return(0)) + .WillOnce(Return(0)); + EXPECT_CALL(sysiom, unlink(handle->backupfile1)) + .WillOnce(Return(0)); + EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1)) + .WillOnce(Return(0)); + EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile)) + .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_file_rotation(handle); + ASSERT_EQ(0, ret); + + // a2 mode + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(Return(0)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + //EXPECT_CALL(sysiom, unlink(handle->backupfile1)) + // .WillOnce(Return(0)); + EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1)) + .WillOnce(Return(0)); + EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile)) + .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_file_rotation(handle); + ASSERT_EQ(0, ret); + + // a3 mode + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(Return(0)); + //EXPECT_CALL(sysiom, unlink(handle->backupfile1)) + // .WillOnce(Return(0)); + //EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1)) + // .WillOnce(Return(0)); + EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile)) + .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_file_rotation(handle); + ASSERT_EQ(0, ret); + + // a4 mode + EXPECT_CALL(sysiom, stat(_, _)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)) + .WillOnce(SetErrnoAndReturn(ENOENT, -1)); + //EXPECT_CALL(sysiom, unlink(handle->backupfile1)) + // .WillOnce(Return(0)); + //EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1)) + // .WillOnce(Return(0)); + EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile)) + .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_file_rotation(handle); + ASSERT_EQ(0, ret); + + free(handle); +} +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_test, fileop_test_unit_test_refop_file_get_with_validation__1st_open_error) +{ + int ret = -1; //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); + char testfilename[] = "/tmp/test.bin"; + uint8_t *pbuf = NULL; + int64_t sz = 256 * 1024; + int64_t szr = 0; + + pbuf = (uint8_t*)malloc(sz); + + EXPECT_CALL(sysiom, open(_,_)).WillOnce(SetErrnoAndReturn(EACCES, -1)); + ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr); + ASSERT_EQ(-6, ret); + + EXPECT_CALL(sysiom, open(_,_)).WillOnce(SetErrnoAndReturn(ENOMEM, -1)); + ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr); + ASSERT_EQ(-6, ret); + + EXPECT_CALL(sysiom, open(_,_)).WillOnce(SetErrnoAndReturn(ENOENT, -1)); + ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr); + ASSERT_EQ(-1, ret); + + free(pbuf); } //-------------------------------------------------------------------------------------------------------- -TEST_F(interface_test, interface_test_refop_create_redundancy_handle__success) +TEST_F(fileop_test_unit_test, fileop_test_unit_test_refop_file_get_with_validation__safe_read_error) { - struct refop_halndle *hndl; - refop_error_t ret = REFOP_SUCCESS; - refop_handle_t handle = NULL; + int ret = -1; //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); + char testfilename[] = "/tmp/test.bin"; + uint8_t *pbuf = NULL; + int64_t sz = 256 * 1024; + int64_t szr = 0; - //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); + pbuf = (uint8_t*)malloc(sz); + + g_safe_read_ret = 0; + g_safe_write_ret = 0; + + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100)); + g_safe_read_ret = sizeof(s_refop_file_header)*2; + EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0)); + + ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr); + ASSERT_EQ(-2, ret); + + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(200)); + g_safe_read_ret = sizeof(s_refop_file_header)/2; + EXPECT_CALL(sysiom, close(200)).WillOnce(Return(0)); + + ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr); + ASSERT_EQ(-2, ret); + + free(pbuf); } //-------------------------------------------------------------------------------------------------------- -#endif -/*TEST_F(data_pool_test_others, test_data_pool_test_data_pool_unlock__false) +TEST_F(fileop_test_unit_test, fileop_test_unit_test_refop_file_get_with_validation__header_error) { - bool ret = true; + int ret = -1; + + //dummy data + char testfilename[] = "/tmp/test.bin"; + uint8_t *pbuf = NULL; + int64_t sz = 256 * 1024; + int64_t szr = 0; + + pbuf = (uint8_t*)malloc(sz); + + g_safe_read_ret = 0; + g_safe_write_ret = 0; + + EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100)); + g_safe_read_ret = sizeof(s_refop_file_header); + EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0)); + + ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr); + ASSERT_EQ(-4, ret); + + free(pbuf); +} - EXPECT_CALL(lpm, pthread_mutex_unlock(_)) - .WillOnce(Return(-1)); - ret = data_pool_unlock(); - ASSERT_EQ(false, ret); -}*/ diff --git a/test/fileop_test_unit_memory.cpp b/test/fileop_test_unit_memory.cpp new file mode 100644 index 0000000..4ebe22b --- /dev/null +++ b/test/fileop_test_unit_memory.cpp @@ -0,0 +1,36 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * + * @file fileop_test_unit_memory.cpp + * @brief Unit test fot fileop.c + */ +#include <gtest/gtest.h> +#include "mock/syscall_io_mock.hpp" +#include "mock/memory_mock.hpp" + +// Test Terget files --------------------------------------- +extern "C" { +#define malloc(X) unittest_malloc(X) +#include "../lib/fileop.c" +#undef malloc +} +// Test Terget files --------------------------------------- +using namespace ::testing; + +struct fileop_test_unit_memory_test : Test, SyscallIOMockBase, MemoryMockBase {}; + +//-------------------------------------------------------------------------------------------------------- +TEST_F(fileop_test_unit_memory_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(memorym, malloc(_)).WillOnce(Return(NULL)); + ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit()); + ASSERT_EQ(-1, ret); + + free(dmybuf); + free(handle); +} diff --git a/test/fileop_test_utils.cpp b/test/fileop_test_utils.cpp index ca2f6dd..f95fa6f 100644 --- a/test/fileop_test_utils.cpp +++ b/test/fileop_test_utils.cpp @@ -1,8 +1,8 @@ /** * SPDX-License-Identifier: Apache-2.0 * - * @file data-pool-service-test.c - * @brief Unit test fot data-pool-service-test.c + * @file fileop_test_utils.cpp + * @brief Unit test fot fileop.c */ #include <gtest/gtest.h> #include "mock/syscall_io_mock.hpp" @@ -15,7 +15,6 @@ extern "C" { using namespace ::testing; struct fileop_test_utils : Test, SyscallIOMockBase {}; -//struct data_pool_test_set_get_others : Test {}; //-------------------------------------------------------------------------------------------------------- TEST_F(fileop_test_utils, fileop_test_utils_refop_header_create__success) @@ -145,131 +144,3 @@ TEST_F(fileop_test_utils, fileop_test_utils_refop_file_test__stat_error) ret = refop_file_test(testfilename); ASSERT_EQ(0, ret); } - -#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 diff --git a/test/interface_test.cpp b/test/interface_test.cpp index dec50bc..8d2df17 100644 --- a/test/interface_test.cpp +++ b/test/interface_test.cpp @@ -15,7 +15,15 @@ using namespace ::testing; struct interface_test : Test {}; +//dummy data +static const char directry[] = "/tmp/refop-test/"; +static const char file[] = "test.bin"; +static const char newfile[] = "/tmp/refop-test/test.bin.tmp"; +static const char latestfile[] = "/tmp/refop-test/test.bin"; +static const char backupfile[] = "/tmp/refop-test/test.bin.bk1"; + //-------------------------------------------------------------------------------------------------------- +// Interface test for data set with some data pattern. TEST_F(interface_test, interface_test_refop_set_redundancy_data__success) { struct refop_halndle *hndl; @@ -23,16 +31,17 @@ TEST_F(interface_test, interface_test_refop_set_redundancy_data__success) refop_handle_t handle = NULL; //dummy data - char directry[] = "/tmp/refop-test/"; - char file[] = "test.bin"; uint8_t *pbuf = NULL; int64_t sz = 1 * 1024 * 1024; + //clean up (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); pbuf = (uint8_t*)malloc(sz); - //short directry string ret = refop_create_redundancy_handle(&handle, directry, file); ASSERT_EQ(REFOP_SUCCESS, ret); @@ -58,6 +67,7 @@ TEST_F(interface_test, interface_test_refop_set_redundancy_data__success) free(pbuf); } //-------------------------------------------------------------------------------------------------------- +// Interface test for data get. TEST_F(interface_test, interface_test_refop_get_redundancy_data__success) { struct refop_halndle *hndl; @@ -65,21 +75,37 @@ TEST_F(interface_test, interface_test_refop_get_redundancy_data__success) refop_handle_t handle = NULL; //dummy data - char directry[] = "/tmp/refop-test/"; - char file[] = "test.bin"; uint8_t *pbuf = NULL; int64_t sz = 1 * 1024 * 1024; int64_t szr = 0; + //clean up (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); pbuf = (uint8_t*)malloc(sz); - //short directry string ret = refop_create_redundancy_handle(&handle, directry, file); ASSERT_EQ(REFOP_SUCCESS, ret); ret = refop_get_redundancy_data(handle, pbuf, sz, &szr); + ASSERT_EQ(REFOP_NOENT, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_get_redundancy_data(handle, pbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + + memset(pbuf,0x00,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_get_redundancy_data(handle, pbuf, sz, &szr); ASSERT_EQ(REFOP_SUCCESS, ret); ASSERT_EQ(sz, szr); @@ -89,6 +115,7 @@ TEST_F(interface_test, interface_test_refop_get_redundancy_data__success) free(pbuf); } //-------------------------------------------------------------------------------------------------------- +// Interface test for data rotate. TEST_F(interface_test, interface_test_refop_set_and_get) { struct refop_halndle *hndl; @@ -108,12 +135,15 @@ TEST_F(interface_test, interface_test_refop_set_and_get) int64_t szr = 0; int checker = 0; + //clean up (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); pbuf = (uint8_t*)malloc(sz); prbuf = (uint8_t*)malloc(sz); - //short directry string ret = refop_create_redundancy_handle(&handle, directry, file); ASSERT_EQ(REFOP_SUCCESS, ret); @@ -195,6 +225,7 @@ TEST_F(interface_test, interface_test_refop_set_and_get) free(prbuf); } //-------------------------------------------------------------------------------------------------------- +// Interface test for data rotate with small data read. TEST_F(interface_test, interface_test_refop_set_and_get_smallread) { struct refop_halndle *hndl; @@ -214,12 +245,15 @@ TEST_F(interface_test, interface_test_refop_set_and_get_smallread) int64_t szr = 0; int checker = 0; + //clean up (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); pbuf = (uint8_t*)malloc(sz); prbuf = (uint8_t*)malloc(sz); - //short directry string ret = refop_create_redundancy_handle(&handle, directry, file); ASSERT_EQ(REFOP_SUCCESS, ret); @@ -252,7 +286,6 @@ TEST_F(interface_test, interface_test_refop_set_and_get_smallread) ret = refop_set_redundancy_data(handle, pbuf, sz); ASSERT_EQ(REFOP_SUCCESS, ret); - // a2 unlink(backupfile); ret = refop_get_redundancy_data(handle, prbuf, sz/2, &szr); @@ -301,6 +334,7 @@ TEST_F(interface_test, interface_test_refop_set_and_get_smallread) free(prbuf); } //-------------------------------------------------------------------------------------------------------- +// Interface test for data rotate with large data read. TEST_F(interface_test, interface_test_refop_set_and_get_largeread) { struct refop_halndle *hndl; @@ -320,12 +354,15 @@ TEST_F(interface_test, interface_test_refop_set_and_get_largeread) int64_t szr = 0; int checker = 0; + //clean up (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); pbuf = (uint8_t*)malloc(sz); prbuf = (uint8_t*)malloc(sz); - //short directry string ret = refop_create_redundancy_handle(&handle, directry, file); ASSERT_EQ(REFOP_SUCCESS, ret); @@ -358,7 +395,6 @@ TEST_F(interface_test, interface_test_refop_set_and_get_largeread) ret = refop_set_redundancy_data(handle, pbuf, sz); ASSERT_EQ(REFOP_SUCCESS, ret); - // a2 unlink(backupfile); ret = refop_get_redundancy_data(handle, prbuf, sz*2, &szr); @@ -407,3 +443,247 @@ TEST_F(interface_test, interface_test_refop_set_and_get_largeread) free(prbuf); } //-------------------------------------------------------------------------------------------------------- +// Interface test for data pick up with small data read. +TEST_F(interface_test, interface_test_no_file_f1) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | - | + // f3 | - | o | + // f4 | - | - | + + // f1 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xff); + } + ASSERT_EQ(0, checker); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// Interface test for data pick up with small data read. +TEST_F(interface_test, interface_test_no_file_f2) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | - | + // f3 | - | o | + // f4 | - | - | + + (void)unlink(backupfile); + + // f2 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xff); + } + ASSERT_EQ(0, checker); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// Interface test for data pick up with small data read. +TEST_F(interface_test, interface_test_no_file_f3) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | - | + // f3 | - | o | + // f4 | - | - | + + (void)unlink(latestfile); + + // f3 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_RECOVER, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0); + } + ASSERT_EQ(0, checker); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// Interface test for data pick up with small data read. +TEST_F(interface_test, interface_test_no_file_f4) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | - | + // f3 | - | o | + // f4 | - | - | + + (void)unlink(latestfile); + (void)unlink(backupfile); + + // f4 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_NOENT, ret); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} diff --git a/test/interface_test_filebreak.cpp b/test/interface_test_filebreak.cpp new file mode 100644 index 0000000..fb37411 --- /dev/null +++ b/test/interface_test_filebreak.cpp @@ -0,0 +1,1266 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * + * @file interface_test.cpp + * @brief Public interface test fot refop + */ +#include <gtest/gtest.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> + +// Test Terget files --------------------------------------- +extern "C" { +#include "../lib/libredundancyfileop.c" +} + +#include "file-util.h" + +// Test Terget files --------------------------------------- +using namespace ::testing; + +struct interface_test_filebreak : Test {}; + +static const char directry[] = "/tmp/refop-test/"; +static const char file[] = "test.bin"; +static const char newfile[] = "/tmp/refop-test/test.bin.tmp"; +static const char latestfile[] = "/tmp/refop-test/test.bin"; +static const char backupfile[] = "/tmp/refop-test/test.bin.bk1"; + +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_magic(const char *file); +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_version(const char *file); +int breakfile_header_version_inv(const char *file); +int breakfile_header_version_val(const char *file); +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_crc16(const char *file); +int breakfile_header_crc16_inv(const char *file); +int breakfile_header_crc16_val(const char *file); +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_size(const char *file); +int breakfile_header_size_inv(const char *file); +int breakfile_header_size_up(const char *file); +int breakfile_header_size_down(const char *file); + +//-------------------------------------------------------------------------------------------------------- +// File update algorithm test +TEST_F(interface_test_filebreak, interface_test_filebreak_file_rotate) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 64 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // Operation algorithm + // Current Next + // | latest | backup | | latest | backup | + // a1 | 1 | 2 | | new | 1 | + // a2 | 1 | x | | new | 1 | + // a3 | x | 2 | | new | 2 | + // a4 | x | x | | new | x | + + // a1 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xff); + } + ASSERT_EQ(0, checker); + + memset(pbuf,0xa1,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + + // a2 + iret = breakfile_header_magic(backupfile); + ASSERT_EQ(0, ret); + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xa1); + } + ASSERT_EQ(0, checker); + + memset(pbuf,0xa2,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // a3 + iret = breakfile_header_magic(latestfile); + ASSERT_EQ(0, ret); + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_RECOVER, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xa1); + } + ASSERT_EQ(0, checker); + + memset(pbuf,0xa3,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // a4 + iret = breakfile_header_magic(backupfile); + ASSERT_EQ(0, ret); + iret = breakfile_header_magic(latestfile); + ASSERT_EQ(0, ret); + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + memset(pbuf,0xa4,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// Latest file header error test +TEST_F(interface_test_filebreak, interface_test_filebreak_file_break_all) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 64 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + //-------------------------------------------------------------------------------------------------------- + // magic break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_magic(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + //-------------------------------------------------------------------------------------------------------- + // version break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_version(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // version_inv break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_version_inv(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // version val break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_version_inv(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + //-------------------------------------------------------------------------------------------------------- + // crc16 break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_crc16(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // crc16_inv break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_crc16_inv(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // crc16 val break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_crc16_val(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + //-------------------------------------------------------------------------------------------------------- + // size break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // size inv break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size_inv(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // size val break 1 + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size_up(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + // size val break 2 + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size_down(latestfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// Backup file header error test +TEST_F(interface_test_filebreak, interface_test_filebreak_backup_file_break_all) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 64 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + //-------------------------------------------------------------------------------------------------------- + // magic break + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,1,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_magic(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + //-------------------------------------------------------------------------------------------------------- + // version break + memset(pbuf,2,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_version(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // version_inv break + memset(pbuf,3,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_version_inv(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // version val break + memset(pbuf,4,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_version_inv(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + //-------------------------------------------------------------------------------------------------------- + // crc16 break + memset(pbuf,5,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_crc16(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // crc16_inv break + memset(pbuf,6,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_crc16_inv(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // crc16 val break + memset(pbuf,7,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_crc16_val(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + //-------------------------------------------------------------------------------------------------------- + // size break + memset(pbuf,8,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // size inv break + memset(pbuf,9,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size_inv(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // size val break 1 + memset(pbuf,10,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size_up(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // size val break 2 + memset(pbuf,11,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + iret = breakfile_header_size_down(backupfile); + ASSERT_EQ(0, ret); + + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// File error test (f1) +TEST_F(interface_test_filebreak, interface_test_filebreak_break_file_f1) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | x | + // f3 | x | o | + // f4 | x | x | + + // f1 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xff); + } + ASSERT_EQ(0, checker); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// File error test (f2) +TEST_F(interface_test_filebreak, interface_test_filebreak_break_file_f2) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | x | + // f3 | x | o | + // f4 | x | x | + + iret = breakfile_header_crc16(backupfile); + ASSERT_EQ(0, ret); + + // f2 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_SUCCESS, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0xff); + } + ASSERT_EQ(0, checker); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// File error test (f3) +TEST_F(interface_test_filebreak, interface_test_filebreak_break_file_f3) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | x | + // f3 | x | o | + // f4 | x | x | + + iret = breakfile_header_crc16(latestfile); + ASSERT_EQ(0, ret); + + // f3 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_RECOVER, ret); + ASSERT_EQ(sz, szr); + for(int i=0;i < szr;i++) { + checker += (prbuf[i] - (uint8_t)0); + } + ASSERT_EQ(0, checker); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// File error test (f4) +TEST_F(interface_test_filebreak, interface_test_filebreak_break_file_f4) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f1 | o | o | + // f2 | o | x | + // f3 | x | o | + // f4 | x | x | + + iret = breakfile_header_crc16(latestfile); + ASSERT_EQ(0, ret); + + iret = breakfile_header_crc16(backupfile); + ASSERT_EQ(0, ret); + + // f4 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// No file entry test (f2) +TEST_F(interface_test_filebreak, interface_test_filebreak_del_and_break_file_f2) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f2 | - | x | + // f3 | x | - | + + (void)unlink(latestfile); + + iret = breakfile_header_crc16(backupfile); + ASSERT_EQ(0, ret); + + // f2 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +// No file entry test (f3) +TEST_F(interface_test_filebreak, interface_test_filebreak_del_and_break_file_f3) +{ + struct refop_halndle *hndl; + refop_error_t ret = REFOP_SUCCESS; + refop_handle_t handle = NULL; + int iret = 0; + + //dummy data + uint8_t *pbuf = NULL; + uint8_t *prbuf = NULL; + int64_t sz = 4 * 1024; + int64_t szr = 0; + int checker = 0; + + //clean up + (void)mkdir(directry, 0777); + (void)unlink(newfile); + (void)unlink(latestfile); + (void)unlink(backupfile); + + pbuf = (uint8_t*)malloc(sz); + prbuf = (uint8_t*)malloc(sz); + + //short directry string + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + memset(pbuf,0xff,sz); + ret = refop_set_redundancy_data(handle, pbuf, sz); + ASSERT_EQ(REFOP_SUCCESS, ret); + + // file pattern + // | latest | backup | + // f2 | - | x | + // f3 | x | - | + + (void)unlink(backupfile); + + iret = breakfile_header_crc16(latestfile); + ASSERT_EQ(0, ret); + + // f3 + ret = refop_get_redundancy_data(handle, prbuf, sz, &szr); + ASSERT_EQ(REFOP_BROAKEN, ret); + + ret = refop_remove_redundancy_data(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + ret = refop_release_redundancy_handle(handle); + ASSERT_EQ(REFOP_SUCCESS, ret); + + free(pbuf); + free(prbuf); +} +//-------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_magic(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.magic = (uint32_t)(~REFOP_FILE_HEADER_MAGIC); + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_version(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.version = (uint32_t)(~REFOP_FILE_HEADER_VERSION_V1); + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_version_inv(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.version_inv = (uint32_t)(REFOP_FILE_HEADER_VERSION_V1); + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_version_val(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.version = (uint32_t)(0x55443322); + head.version_inv = (uint32_t)(~head.version); + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_crc16(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.crc16 = head.crc16 + 1; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_crc16_inv(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.crc16_inv = head.crc16_inv + 1; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_crc16_val(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.crc16 = head.crc16 + 1; + head.crc16_inv = ~head.crc16; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} +//-------------------------------------------------------------------------------------------------------- +int breakfile_header_size(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.size = head.size + 1; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_size_inv(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.size_inv = head.size_inv + 1; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_size_up(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.size = head.size + 100; + head.size_inv = ~head.size; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + +int breakfile_header_size_down(const char *file) +{ + s_refop_file_header head = {0}; + ssize_t size = 0; + int fd = -1; + + fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_read(fd, &head, sizeof(head)); + if (size != sizeof(head)) { + close(fd); + return -1; + } + + (void)close(fd); + + // break + head.size = head.size - 100; + head.size_inv = ~head.size; + // break + + fd = open(file, (O_CLOEXEC | O_WRONLY | O_NOFOLLOW)); + if (fd < 0) + return -1; + + size = safe_write(fd, &head, sizeof(head)); + if (size < 0) { + (void)close(fd); + return -1; + } + + (void)close(fd); + + return 0; +} + diff --git a/test/interface_test_unit.cpp b/test/interface_test_unit.cpp index 3dc5f70..6a17e20 100644 --- a/test/interface_test_unit.cpp +++ b/test/interface_test_unit.cpp @@ -1,8 +1,8 @@ /** * SPDX-License-Identifier: Apache-2.0 * - * @file data-pool-service-test.c - * @brief Unit test fot data-pool-service-test.c + * @file interface_test_unit.cpp + * @brief Unit test fot libredundancyfileop.c */ #include <gtest/gtest.h> #include "mock/syscall_io_mock.hpp" diff --git a/test/interface_test_unit_memory.cpp b/test/interface_test_unit_memory.cpp new file mode 100644 index 0000000..ec61c13 --- /dev/null +++ b/test/interface_test_unit_memory.cpp @@ -0,0 +1,40 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * + * @file interface_test_unit_memory.cpp + * @brief Unit test fot libredundancyfileop.c + */ +#include <gtest/gtest.h> +#include "mock/syscall_io_mock.hpp" +#include "mock/memory_mock.hpp" + +// Test Terget files --------------------------------------- +extern "C" { +#define malloc(X) unittest_malloc(X) +#include "../lib/libredundancyfileop.c" +#undef malloc +} +// Test Terget files --------------------------------------- +using namespace ::testing; + +struct interface_test_unit_memory_test : Test, SyscallIOMockBase, MemoryMockBase {}; + +//-------------------------------------------------------------------------------------------------------- +TEST_F(interface_test_unit_memory_test, interface_test_unit_memory_test_refop_create_redundancy_handle__malloc_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)); + EXPECT_CALL(memorym, malloc(_)).WillOnce(Return(NULL)); + ret = refop_create_redundancy_handle(&handle, directry, file); + ASSERT_EQ(REFOP_SYSERROR, ret); +} diff --git a/test/mock/memory_mock.hpp b/test/mock/memory_mock.hpp new file mode 100644 index 0000000..e140840 --- /dev/null +++ b/test/mock/memory_mock.hpp @@ -0,0 +1,53 @@ +#include <gmock/gmock.h> +#include <functional> + +#include <stdlib.h> + +/* +void *malloc(size_t size); +void free(void *ptr); +*/ +static std::function<void*(size_t size)> _malloc; +static std::function<void(void *ptr)> _free; + +class MemoryMocker { +public: + MemoryMocker() { + _malloc = [this](size_t size) { + return malloc(size); + }; + _free = [this](void *ptr) { + return free(ptr); + }; + } + + ~MemoryMocker() { + _malloc = {}; + _free = {}; + } + + MOCK_CONST_METHOD1(malloc, void*(size_t)); + MOCK_CONST_METHOD1(free, void(void *)); +}; + +class MemoryMockBase { +protected: + MemoryMocker memorym; +}; + +#ifdef __cplusplus +extern "C" { +#endif +static void* unittest_malloc(size_t size) +{ + return _malloc(size); +} +/* +static void free(void *ptr) +{ + _free(ptr); +} +*/ +#ifdef __cplusplus +} +#endif diff --git a/test/mock/syscall_io_mock.hpp b/test/mock/syscall_io_mock.hpp index 61e1840..6eb021a 100644 --- a/test/mock/syscall_io_mock.hpp +++ b/test/mock/syscall_io_mock.hpp @@ -5,6 +5,7 @@ #include <sys/socket.h> #include <sys/types.h> #include <sys/stat.h> +#include <stdio.h> /* int open(const char *pathname, int flags); @@ -28,10 +29,11 @@ static std::function<int(int)> _fsync; /* int unlink(const char *pathname); int stat(const char *pathname, struct stat *buf); +int rename(const char *oldpath, const char *newpath); */ static std::function<int(const char *)> _unlink; static std::function<int(const char *pathname, struct stat *buf)> _stat; - +static std::function<int(const char *oldpath, const char *newpath)> _rename; /* int socket(int socket_family, int socket_type, int protocol); @@ -73,6 +75,9 @@ public: _stat = [this](const char *pathname, struct stat *buf) { return stat(pathname, buf); }; + _rename = [this](const char *oldpath, const char *newpath){ + return rename(oldpath, newpath); + }; _socket = [this](int socket_family, int socket_type, int protocol){ return socket(socket_family, socket_type, protocol); @@ -100,6 +105,7 @@ public: _unlink = {}; _stat = {}; + _rename = {}; _socket = {}; _bind = {}; @@ -117,6 +123,7 @@ public: MOCK_CONST_METHOD1(unlink, int(const char *)); MOCK_CONST_METHOD2(stat, int(const char *pathname, struct stat *buf)); + MOCK_CONST_METHOD2(rename, int(const char *oldpath, const char *newpath)); MOCK_CONST_METHOD3(socket, int(int socket_family, int socket_type, int protocol)); MOCK_CONST_METHOD3(bind, int(int sockfd, const struct sockaddr *addr,socklen_t addrlen)); @@ -167,6 +174,11 @@ static int stat(const char *pathname, struct stat *buf) return _stat(pathname, buf); } +static int rename(const char *oldpath, const char *newpath) +{ + return _rename(oldpath, newpath); +} + static int socket(int socket_family, int socket_type, int protocol) { return _socket(socket_family, socket_type, protocol); diff --git a/tools/run-test.sh b/tools/run-test.sh index 2bebbd2..a9284f5 100755 --- a/tools/run-test.sh +++ b/tools/run-test.sh @@ -1,8 +1,12 @@ #!/bin/sh -e -./test/interface_test_unit ./test/fileop_test_utils ./test/file_util_test ./test/interface_test ./test/fileop_test_set_get_remove ./test/fileop_test_unit +./test/fileop_test_unit_memory +./test/interface_test_unit +./test/interface_test_filebreak +./test/interface_test_unit_memory + |