diff --git a/.gitignore b/.gitignore index a695d6d3..bac9fe1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,26 @@ +SI/ +*.log +*.o +*.so +*.si4project/ +*.a +*.d build/ -support/aws-sdk-cpp-master/ -example/demo/log/ -example/demo/build/ +.vscode +.idea +core.* +*_log.* +cmake-build-* +GPATH +GTAGS +GRTAGS +src/tags +support/aws-sdk-cpp-master +example/demo/build +example/performance/build +example/demo/log example/demo/hos_write_complete -example/demo/hos_write_demo -example/performance/build/ -example/performance/log example/performance/HosClientPerformance -example/test/ -gtest/build/ -example/demo/valgrind.log +example/performance/log +example/test version.txt -.vscode/launch.json diff --git a/gtest/CMakeLists.txt b/gtest/CMakeLists.txt index 8cd3ab35..09cde228 100644 --- a/gtest/CMakeLists.txt +++ b/gtest/CMakeLists.txt @@ -6,9 +6,14 @@ include_directories("/opt/MESA/include") link_directories("/opt/MESA/lib") #link_libraries(hos-client-cpp gtest gtest_main pthread) +# coverage +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage") + add_definitions(-g -W -Wall -std=c++11) -#add_executable(gtest_hos_client gtest_hos_init_instance.cpp gtest_hos_get_instance.cpp) -#add_executable(gtest_hos_client gtest_hos_write.cpp) +#add_executable(gtest_hos_client gtest_hos_init_instance.cpp gtest_hos_get_instance.cpp gtest_hos_close_fd.cpp gtest_hos_open_fd.cpp) +#add_executable(gtest_hos_client CheckHosClient.cpp gtest_hos_upload_buff.cpp) add_executable(gtest_hos_client ${SRCS}) target_link_libraries(gtest_hos_client hos-client-cpp gtest gtest_main pthread) diff --git a/gtest/CheckHosClient.cpp b/gtest/CheckHosClient.cpp new file mode 100644 index 00000000..d3a5b236 --- /dev/null +++ b/gtest/CheckHosClient.cpp @@ -0,0 +1,77 @@ +#include "CheckHosClient.h" + +void CheckStructHosConfigT(hos_config_t *actual, hos_config_t *expect) +{ + EXPECT_STREQ(actual->accesskeyid, expect->accesskeyid); + EXPECT_STREQ(actual->secretkey, expect->secretkey); + EXPECT_STREQ(actual->log_path, expect->log_path); + EXPECT_EQ(actual->log_level, expect->log_level); + EXPECT_EQ(actual->pool_thread_size, expect->pool_thread_size); + EXPECT_EQ(actual->cache_count, expect->cache_count); + EXPECT_EQ(actual->cache_size, expect->cache_size); + EXPECT_EQ(actual->fs2_fmt, expect->fs2_fmt); + EXPECT_STREQ(actual->fs2_ip, expect->fs2_ip); + EXPECT_STREQ(actual->fs2_path, expect->fs2_path); + EXPECT_EQ(actual->fs2_port, expect->fs2_port); + EXPECT_STREQ(actual->ip, expect->ip); + EXPECT_EQ(actual->port, expect->port); + EXPECT_EQ(actual->thread_num, expect->thread_num); + EXPECT_EQ(actual->timeout, expect->timeout); +} + +void CheckStructHosFunc(hos_func_thread_t *actual, hos_func_thread_t *expect) +{ + //EXPECT_EQ(actual->fd_thread, expect->fd_thread); + EXPECT_EQ(actual->fd_thread_status, expect->fd_thread_status); + //CheckStructFs2Info(actual->fs2_info, expect->fs2_info); + EXPECT_EQ(actual->fs2_status, expect->fs2_status); + //EXPECT_EQ(actual->fs2_thread, expect->fs2_thread); +} + +void CheckStructGHosHandle(hos_client_handle_t *actual, hos_client_handle_t *expect) +{ + //EXPECT_STREQ(actual->buckets.c_str(), expect->buckets.c_str()); + int bucketNum = actual->buckets.size() > expect->buckets.size() ? expect->buckets.size() : actual->buckets.size(); + for (int i = 0; i < bucketNum; i++) + { + EXPECT_STREQ(actual->buckets.at(i).GetName().c_str(), expect->buckets.at(i).GetName().c_str()); + } + EXPECT_EQ(actual->count, expect->count); + //EXPECT_TRUE(actual->log != NULL); + //EXPECT_TRUE(actual->S3Client != NULL); + CheckStructHosConfigT(&actual->hos_config, &expect->hos_config); + CheckStructHosFunc(&actual->hos_func, &expect->hos_func); +} + +void CheckStructGHosFdContext(hos_fd_context_t *actual, hos_fd_context_t *expect) +{ + if (actual == NULL || expect == NULL) + { + EXPECT_TRUE(actual == expect); + } + else + { + EXPECT_STREQ(actual->bucket, expect->bucket); + //EXPECT_TRUE(actual->cache == NULL); + EXPECT_EQ(actual->cache_count, expect->cache_count); + EXPECT_EQ(actual->cache_rest, expect->cache_rest); + EXPECT_EQ(actual->callback, expect->callback); + EXPECT_EQ(actual->fd, expect->fd); + EXPECT_EQ(actual->fd_status, expect->fd_status); + EXPECT_EQ(actual->mode, expect->mode); + EXPECT_STREQ(actual->object, expect->object); + EXPECT_EQ(actual->overtime, expect->overtime); + EXPECT_EQ(actual->position, expect->position); + EXPECT_EQ(actual->recive_cnt, expect->recive_cnt); + EXPECT_EQ(actual->timeout, expect->timeout); + EXPECT_EQ(actual->userdata, expect->userdata); + } +} + +void CheckHosInstance(hos_instance actual, hos_instance expect) +{ + EXPECT_EQ(actual->result, expect->result); + EXPECT_EQ(actual->error_code, expect->error_code); + EXPECT_STREQ(actual->error_message, expect->error_message); + EXPECT_STREQ(actual->hos_url_prefix, expect->hos_url_prefix); +} \ No newline at end of file diff --git a/gtest/CheckHosClient.h b/gtest/CheckHosClient.h new file mode 100644 index 00000000..8894f987 --- /dev/null +++ b/gtest/CheckHosClient.h @@ -0,0 +1,14 @@ +#ifndef __CHECKHOS_CLIENT_H__ +#define __CHECKHOS_CLIENT_H__ + +#include +#include "../src/hos_client.h" +#include "../src/hos_common.h" + +void CheckStructHosConfigT(hos_config_t *actual, hos_config_t *expect); +void CheckStructHosFunc(hos_func_thread_t *actual, hos_func_thread_t *expect); +void CheckStructGHosHandle(hos_client_handle_t *actual, hos_client_handle_t *expect); +void CheckStructGHosFdContext(hos_fd_context_t *actual, hos_fd_context_t *expect); +void CheckHosInstance(hos_instance actual, hos_instance expect); + +#endif \ No newline at end of file diff --git a/gtest/common/gtest_hos_common.h b/gtest/common/gtest_hos_common.h new file mode 100644 index 00000000..b0e2c751 --- /dev/null +++ b/gtest/common/gtest_hos_common.h @@ -0,0 +1,67 @@ +#ifndef __GTEST_HOS_COMMON_H__ +#define __GTEST_HOS_COMMON_H__ + +#include +#include + +#ifndef MAX_HOS_STRING_LEN +#define MAX_HOS_STRING_LEN 1024 +#endif + +typedef struct data_info_s +{ + size_t *tx_pkts; + size_t *tx_bytes; + size_t *rx_pkts; + size_t *rx_bytes; + size_t *tx_failed_pkts; + size_t *tx_failed_bytes; + size_t *cache; +}data_info_t; + +enum +{ + FS2_DATA_FLOW_STATE = 0, + FS2_POOL_THREAD_STATE, + FS2_RECORD_EVENTS, +}; + +typedef struct hos_config_s +{ + char ip[INET6_ADDRSTRLEN]; + char fs2_ip[INET6_ADDRSTRLEN]; + char accesskeyid[MAX_HOS_STRING_LEN]; + char secretkey[MAX_HOS_STRING_LEN]; + char log_path[MAX_HOS_STRING_LEN]; + char fs2_path[MAX_HOS_STRING_LEN]; + + uint32_t port; + uint32_t fs2_port; + uint32_t fs2_fmt; + uint32_t log_level; + uint32_t pool_thread_size; + uint32_t thread_num; + uint32_t cache_size; + uint32_t cache_count; + uint32_t timeout; +}hos_config_t; + +typedef struct hos_client_handle_s +{ + void *S3Client; + void *buckets; + void *executor; + size_t count; /* 记录了有多少个对象在使用hos */ + hos_config_t hos_config; +}hos_client_handle_t; + +typedef unsigned long size_t; +typedef struct hos_client_handle_s hos_client_handle_t; +typedef struct hos_fd_context_s hos_fd_context_t; + +extern struct hos_instance_s g_hos_instance; +extern hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle +extern hos_fd_context_t **g_fd_context; +extern size_t (*g_fd_info)[65536]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd + +#endif \ No newline at end of file diff --git a/gtest/conf/default.conf b/gtest/conf/default.conf index f2446c97..8f4e081f 100644 --- a/gtest/conf/default.conf +++ b/gtest/conf/default.conf @@ -1,5 +1,5 @@ [hos_default_conf] -hos_serverip=192.168.44.12 +hos_serverip=127.0.0.1 hos_serverport=9098 hos_accesskeyid="default" hos_secretkey="default" @@ -9,8 +9,6 @@ hos_log_level=30 #default hos_poolsize=10 #default -hos_thread_sum=32 -#default hos_cache_size=102400 #default hos_cache_count=10 @@ -25,18 +23,16 @@ hos_fs2_format=0 #defaul [hos_sync_conf] -hos_serverip=192.168.44.12 +hos_serverip=127.0.0.1 hos_serverport=9098 hos_accesskeyid="default" hos_secretkey="default" -hos_log_path="./log/hoslog" +hos_log_path="./hoslog" #default hos_log_level=30 #default hos_poolsize=0 #default -hos_thread_sum=32 -#default hos_cache_size=102400 #default hos_cache_count=10 @@ -51,18 +47,16 @@ hos_fs2_format=0 #default [hos_no_fs2_conf] -hos_serverip=192.168.44.12 +hos_serverip=127.0.0.1 hos_serverport=9098 hos_accesskeyid="default" hos_secretkey="default" -hos_log_path="./log/hoslog" +hos_log_path="./hoslog" #default hos_log_level=30 #default hos_poolsize=10 #default -hos_thread_sum=32 -#default hos_cache_size=102400 #default hos_cache_count=10 @@ -75,14 +69,12 @@ hos_fs2_format=0 #default [hos_default_conf] -hos_log_path="./log/hoslog" +hos_log_path="./hoslog" #default hos_log_level=30 #default hos_poolsize=10 #default -hos_thread_sum=32 -#default hos_cache_size=102400 #default hos_cache_count=10 @@ -101,14 +93,12 @@ hos_serverip=192.168.40.146 hos_serverport=9098 hos_accesskeyid="default" hos_secretkey="default" -hos_log_path="./log/hoslog" +hos_log_path="./hoslog" #default hos_log_level=30 #default hos_poolsize=0 #default -hos_thread_sum=32 -#default hos_cache_size=102400 #default hos_cache_count=10 diff --git a/gtest/gtest_hos_close_fd.cpp b/gtest/gtest_hos_close_fd.cpp index f6172030..003cc608 100644 --- a/gtest/gtest_hos_close_fd.cpp +++ b/gtest/gtest_hos_close_fd.cpp @@ -1,55 +1,132 @@ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" + +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} + +static void gtest_hos_fd_init(hos_fd_context_t *fd_info) +{ + memset(fd_info, 0, sizeof(hos_fd_context_t)); + fd_info->bucket = (char *)HOS_BUCKET; + fd_info->object = (char *)"object"; + fd_info->cache_count = 10; + fd_info->cache_rest = g_hos_handle.hos_config.cache_size; + fd_info->callback = NULL; + fd_info->fd = 3; + fd_info->fd_status = HOS_FD_REGISTER; + fd_info->mode = BUFF_MODE; + fd_info->overtime = 0; + fd_info->position = 0; + fd_info->recive_cnt = 0; + fd_info->timeout = g_hos_handle.hos_config.timeout; + fd_info->userdata = NULL; +} TEST(hos_close_fd, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + size_t fd = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info); + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); + int ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_close_fd, paramer_error) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int fd = hos_open_fd(NULL, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + CheckStructGHosFdContext(g_fd_context[0], NULL); + int ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_close_fd, not_init_instance) @@ -60,19 +137,30 @@ TEST(hos_close_fd, not_init_instance) TEST(hos_close_fd, fd_not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_close_fd(7, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } \ No newline at end of file diff --git a/gtest/gtest_hos_get_instance.cpp b/gtest/gtest_hos_get_instance.cpp index 4639ea0b..22f93496 100644 --- a/gtest/gtest_hos_get_instance.cpp +++ b/gtest/gtest_hos_get_instance.cpp @@ -1,32 +1,77 @@ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" + +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} TEST(hos_get_instance, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_instance hos_instance = hos_get_instance(); - EXPECT_EQ(hos_instance->result, false); + memset(&expect_hos_instance, 0, sizeof(hos_instance_s)); + expect_hos_instance.result = false; + CheckHosInstance(hos_instance, &expect_hos_instance); + hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + hos_instance = hos_get_instance(); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + expect_hos_handle.count++; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + hos_shutdown_instance(); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - hos_shutdown_instance(); - EXPECT_EQ(hos_instance->result, false); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + CheckHosInstance(hos_instance, &expect_hos_instance); + expect_hos_handle.count--; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + int ret = hos_shutdown_instance(); + EXPECT_EQ(ret, HOS_CLIENT_OK); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } diff --git a/gtest/gtest_hos_init_instance.cpp b/gtest/gtest_hos_init_instance.cpp index 7d1fdaff..a70a68fb 100644 --- a/gtest/gtest_hos_init_instance.cpp +++ b/gtest/gtest_hos_init_instance.cpp @@ -3,72 +3,159 @@ > Author: pxz > Created Time: Tue 29 Sep 2020 10:20:49 AM CST ************************************************************************/ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" TEST(hos_init_instance, normal) { hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + hos_instance_s expect_hos_instance; + expect_hos_instance.result = true; + expect_hos_instance.error_code = 0; + expect_hos_instance.error_message[0] ='\0'; + expect_hos_instance.hos_url_prefix = "http://127.0.0.1:9098/hos/"; + CheckHosInstance(hos_instance, &expect_hos_instance); + + hos_client_handle_t expect_hos_handle; + memset(&expect_hos_handle, 0, sizeof(expect_hos_handle)); + expect_hos_handle.buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + expect_hos_handle.buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + expect_hos_handle.count = 1; + memcpy(expect_hos_handle.hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(expect_hos_handle.hos_config.secretkey, "default", strlen("default")+1); + expect_hos_handle.hos_config.cache_count = 10; + expect_hos_handle.hos_config.cache_size = 102400; + expect_hos_handle.hos_config.fs2_fmt = 0; + memcpy(expect_hos_handle.hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(expect_hos_handle.hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + expect_hos_handle.hos_config.fs2_port = 10086; + memcpy(expect_hos_handle.hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + expect_hos_handle.hos_config.log_level = 30; + memcpy(expect_hos_handle.hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + expect_hos_handle.hos_config.pool_thread_size = 10; + expect_hos_handle.hos_config.port = 9098; + expect_hos_handle.hos_config.thread_num = 1; + expect_hos_handle.hos_config.timeout = 1000; + expect_hos_handle.hos_func.fd_thread_status = 0; + expect_hos_handle.hos_func.fs2_status = 1; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.error_code = 0; + expect_hos_instance.error_message[0] ='\0'; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } TEST(hos_init_instance, param_error) { hos_instance hos_instance = hos_init_instance(NULL, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, false); - EXPECT_EQ(hos_instance->error_code, HOS_PARAMETER_ERROR); - EXPECT_STREQ(hos_instance->error_message, "param error:conf_path:(null), module:hos_default_conf, thread_num:1, bucket:hos_test_bucket"); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + + hos_instance_s expect_hos_instance; + expect_hos_instance.result = false; + expect_hos_instance.error_code = HOS_PARAMETER_ERROR; + const char *err_msg = "param error:conf_path:(null), module:hos_default_conf, thread_num:1, bucket:firewall_hos_bucket"; + memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); } TEST(hos_init_instance, no_fs2) { hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_no_fs2_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + hos_instance_s expect_hos_instance; + expect_hos_instance.result = true; + expect_hos_instance.error_code = 0; + expect_hos_instance.error_message[0] ='\0'; + expect_hos_instance.hos_url_prefix = "http://127.0.0.1:9098/hos/"; + CheckHosInstance(hos_instance, &expect_hos_instance); + + hos_client_handle_t expect_hos_handle; + memset(&expect_hos_handle, 0, sizeof(expect_hos_handle)); + expect_hos_handle.buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + expect_hos_handle.buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + expect_hos_handle.count = 1; + memcpy(expect_hos_handle.hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(expect_hos_handle.hos_config.secretkey, "default", strlen("default")+1); + expect_hos_handle.hos_config.cache_count = 10; + expect_hos_handle.hos_config.cache_size = 102400; + expect_hos_handle.hos_config.fs2_fmt = 0; + expect_hos_handle.hos_config.fs2_ip[0] = '\0'; + expect_hos_handle.hos_config.fs2_path[0] = '\0'; + //memcpy(expect_hos_handle.hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(expect_hos_handle.hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + //expect_hos_handle.hos_config.fs2_port = 10086; + expect_hos_handle.hos_config.fs2_port = 0; + memcpy(expect_hos_handle.hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + expect_hos_handle.hos_config.log_level = 30; + memcpy(expect_hos_handle.hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + expect_hos_handle.hos_config.pool_thread_size = 10; + expect_hos_handle.hos_config.port = 9098; + expect_hos_handle.hos_config.thread_num = 1; + expect_hos_handle.hos_config.timeout = 1000; + expect_hos_handle.hos_func.fd_thread_status = 0; + expect_hos_handle.hos_func.fs2_status = 0; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.error_code = 0; + expect_hos_instance.error_message[0] ='\0'; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } TEST(hos_init_instance, bucket_not_exits) { hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, "hos_bucket_not_exits"); - EXPECT_EQ(hos_instance->result, false); - EXPECT_EQ(hos_instance->error_code, HOS_BUCKET_NOT_EXIST); - EXPECT_STREQ(hos_instance->error_message, "bucket:hos_bucket_not_exits not exits."); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + + hos_instance_s expect_hos_instance; + expect_hos_instance.result = false; + expect_hos_instance.error_code = HOS_BUCKET_NOT_EXIST; + const char *err_msg = "bucket:hos_bucket_not_exits not exits."; + memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + hos_client_handle_t expect_hos_handle; + memset(&expect_hos_handle, 0, sizeof(expect_hos_handle)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } TEST(hos_init_instance, conf_error) { hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_conf", 1, "hos_bucket_not_exits"); - EXPECT_EQ(hos_instance->result, false); - EXPECT_EQ(hos_instance->error_code, HOS_CONF_ERROR); - EXPECT_STREQ(hos_instance->error_message, "hos param error:hos ip:, hos port:0, accesskeyid:, secretkey:"); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + hos_instance_s expect_hos_instance; + expect_hos_instance.result = false; + expect_hos_instance.error_code = HOS_CONF_ERROR; + const char *err_msg = "hos param error:hos ip:, hos port:0, accesskeyid:, secretkey:"; + memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); } TEST(hos_init_instance, server_conn_failed) { hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_server_conf", 1, "hos_bucket_not_exits"); - EXPECT_EQ(hos_instance->result, false); - EXPECT_EQ(hos_instance->error_code, NETWORK_CONNECTION); - EXPECT_STREQ(hos_instance->error_message, "curlCode: 7, Couldn't connect to server"); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + hos_instance_s expect_hos_instance; + expect_hos_instance.result = false; + expect_hos_instance.error_code = NETWORK_CONNECTION; + const char *err_msg = "curlCode: 7, Couldn't connect to server"; + memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); } \ No newline at end of file diff --git a/gtest/gtest_hos_open_fd.cpp b/gtest/gtest_hos_open_fd.cpp index 7b0bbbed..ac2fb947 100644 --- a/gtest/gtest_hos_open_fd.cpp +++ b/gtest/gtest_hos_open_fd.cpp @@ -1,107 +1,223 @@ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" + +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} + +static void gtest_hos_fd_init(hos_fd_context_t *fd_info) +{ + memset(fd_info, 0, sizeof(hos_fd_context_t)); + fd_info->bucket = (char *)HOS_BUCKET; + fd_info->object = (char *)"object"; + fd_info->cache_count = 10; + fd_info->cache_rest = g_hos_handle.hos_config.cache_size; + fd_info->callback = NULL; + fd_info->fd = 3; + fd_info->fd_status = HOS_FD_REGISTER; + fd_info->mode = BUFF_MODE; + fd_info->overtime = 0; + fd_info->position = 0; + fd_info->recive_cnt = 0; + fd_info->timeout = g_hos_handle.hos_config.timeout; + fd_info->userdata = NULL; +} TEST(hos_open_fd, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info[2]; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + size_t fd = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[0]); + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + size_t fd1 = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 1, BUFF_MODE); EXPECT_EQ(fd1, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[1]); + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + int ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], NULL); + ret = hos_close_fd(fd1, 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[1], NULL); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_open_fd, paramer_error) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int fd = hos_open_fd(NULL, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + CheckStructGHosFdContext(g_fd_context[0], NULL); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_open_fd, over_threadnums) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 3, BUFF_MODE); EXPECT_EQ(fd, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + CheckStructGHosFdContext(g_fd_context[2], NULL); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_open_fd, fd_not_enough) { int i = 0, fd = 0; + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + gtest_hos_fd_init(&expect_fd_info); + hos_fd_context_t * prev = NULL; + hos_fd_context_t * current = NULL; for (i = 0; i < 65533; i++) { - fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE); + fd = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, i+3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + expect_fd_info.fd = i+3; + if (i == 0) + { + current = g_fd_context[0]; + } + else + { + current = (hos_fd_context_t *)prev->hh.next; + } + CheckStructGHosFdContext(current, &expect_fd_info); + prev = current; + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, HOS_FD_NOT_ENOUGH); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ(prev->hh.next, (void *)NULL); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_open_fd, not_init_instance) diff --git a/gtest/gtest_hos_shutdown_instance.cpp b/gtest/gtest_hos_shutdown_instance.cpp index 7cdd1ec6..2d469d77 100644 --- a/gtest/gtest_hos_shutdown_instance.cpp +++ b/gtest/gtest_hos_shutdown_instance.cpp @@ -3,24 +3,69 @@ > Author: pxz > Created Time: Tue 29 Sep 2020 10:20:49 AM CST ************************************************************************/ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" + +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} + TEST(hos_shutdown_instance, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_shutdown_instance, no_init) @@ -31,19 +76,30 @@ TEST(hos_shutdown_instance, no_init) TEST(hos_shutdown_instance, shutdown_more) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } diff --git a/gtest/gtest_hos_upload_buff.cpp b/gtest/gtest_hos_upload_buff.cpp index 3d4e8733..ddde47b5 100644 --- a/gtest/gtest_hos_upload_buff.cpp +++ b/gtest/gtest_hos_upload_buff.cpp @@ -1,10 +1,44 @@ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" #define HOS_BUFF "This is a googletest" +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} + static void hos_callback(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -25,59 +59,92 @@ static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char TEST(hos_upload_buff, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_upload_buf(HOS_BUCKET, "object", HOS_BUFF, strlen(HOS_BUFF), hos_callback, (void *)"object", 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_buff, bucket_not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_upload_buf("bucket_not_exits", "object", HOS_BUFF, strlen(HOS_BUFF), hos_bucket_not_exits_cb, (void *)"object", 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_buff, param_error) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_upload_buf(NULL, "object", HOS_BUFF, strlen(HOS_BUFF), hos_callback, (void *)"object", 0); EXPECT_EQ(ret, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_buff, not_init_instance) diff --git a/gtest/gtest_hos_upload_file.cpp b/gtest/gtest_hos_upload_file.cpp index 2049b5df..db83b798 100644 --- a/gtest/gtest_hos_upload_file.cpp +++ b/gtest/gtest_hos_upload_file.cpp @@ -1,10 +1,44 @@ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" #define HOS_BUFF "Makefile" +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} + static void hos_callback(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -18,66 +52,99 @@ static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char { SUCCEED(); EXPECT_EQ(result, false); - EXPECT_STREQ(bucket, HOS_BUCKET); + EXPECT_STREQ(bucket, HOS_CONF); EXPECT_STREQ(object, (char *)userdata); EXPECT_STREQ(error, "The specified bucket does not exist."); } TEST(hos_upload_file, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_upload_file(HOS_BUCKET, HOS_BUFF, hos_callback, (void *)HOS_BUFF, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_file, param_error) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_upload_file(NULL, "object", hos_callback, (void *)"object", 0); EXPECT_EQ(ret, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_file, file_not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_upload_file(HOS_BUCKET, "file_not_exits", hos_callback, (void *)"object", 0); EXPECT_EQ(ret, HOS_FILE_NOT_EXIST); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_file, not_init_instance) @@ -88,19 +155,30 @@ TEST(hos_upload_file, not_init_instance) TEST(hos_upload_file, bucket_not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - int ret = hos_upload_file(HOS_BUCKET, HOS_CONF, hos_bucket_not_exits_cb, (void *)HOS_CONF, 0); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + int ret = hos_upload_file(HOS_CONF, HOS_CONF, hos_bucket_not_exits_cb, (void *)HOS_CONF, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } diff --git a/gtest/gtest_hos_verify_bucket.cpp b/gtest/gtest_hos_verify_bucket.cpp index 0a281193..e397f907 100644 --- a/gtest/gtest_hos_verify_bucket.cpp +++ b/gtest/gtest_hos_verify_bucket.cpp @@ -3,50 +3,104 @@ > Author: pxz > Created Time: Tue 29 Sep 2020 10:32:14 AM CST ************************************************************************/ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" + +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} TEST(hos_verify_bucket, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=1; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + bool result = hos_verify_bucket(HOS_BUCKET); EXPECT_EQ(result, true); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_verify_bucket, not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=1; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + bool result = hos_verify_bucket("hos_not_exits_bucket"); EXPECT_EQ(result, false); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_verify_bucket, no_instance) diff --git a/gtest/gtest_hos_write.cpp b/gtest/gtest_hos_write.cpp index 96602090..19419f9b 100644 --- a/gtest/gtest_hos_write.cpp +++ b/gtest/gtest_hos_write.cpp @@ -1,11 +1,63 @@ -#include -#include "hos_client.h" +#include +#include "CheckHosClient.h" #define HOS_CONF "../conf/default.conf" -#define HOS_BUCKET "hos_test_bucket" +#define HOS_BUCKET "firewall_hos_bucket" #define HOS_BUFF "This a googleTest" #define HOS_FILE "../conf/default.conf" +static void gtest_hos_handle_init(hos_client_handle_t *hos_handle) +{ + memset(hos_handle, 0, sizeof(hos_client_handle_t)); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("session_record_hos_bucket")); + hos_handle->buckets.push_back(Aws::S3::Model::Bucket().WithName("firewall_hos_bucket")); + hos_handle->count = 1; + memcpy(hos_handle->hos_config.accesskeyid, "default", strlen("default")+1); + memcpy(hos_handle->hos_config.secretkey, "default", strlen("default")+1); + hos_handle->hos_config.cache_count = 10; + hos_handle->hos_config.cache_size = 102400; + hos_handle->hos_config.fs2_fmt = 0; + memcpy(hos_handle->hos_config.fs2_ip, "127.0.0.1", strlen("127.0.0.1")+1); + memcpy(hos_handle->hos_config.fs2_path, "./log/hos_fs2_log", strlen("./log/hos_fs2_log")+1); + hos_handle->hos_config.fs2_port = 10086; + memcpy(hos_handle->hos_config.ip, "127.0.0.1", strlen("127.0.0.1")+1); + hos_handle->hos_config.log_level = 30; + memcpy(hos_handle->hos_config.log_path, "./hoslog", strlen("./hoslog")+1); + hos_handle->hos_config.pool_thread_size = 10; + hos_handle->hos_config.port = 9098; + hos_handle->hos_config.thread_num = 1; + hos_handle->hos_config.timeout = 1000; + hos_handle->hos_func.fd_thread_status = 0; + hos_handle->hos_func.fs2_status = 1; +} + +static void gtest_hos_instance_init(hos_instance instance) +{ + memset(instance, 0, sizeof(hos_instance_s)); + instance->result = true; + instance->error_code = 0; + instance->error_message[0] ='\0'; + instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; +} + +static void gtest_hos_fd_init(hos_fd_context_t *fd_info) +{ + memset(fd_info, 0, sizeof(hos_fd_context_t)); + fd_info->bucket = (char *)HOS_BUCKET; + fd_info->object = (char *)"object"; + fd_info->cache_count = 10; + fd_info->cache_rest = g_hos_handle.hos_config.cache_size; + fd_info->callback = NULL; + fd_info->fd = 3; + fd_info->fd_status = HOS_FD_REGISTER; + fd_info->mode = BUFF_MODE; + fd_info->overtime = 0; + fd_info->position = 0; + fd_info->recive_cnt = 0; + fd_info->timeout = g_hos_handle.hos_config.timeout; + fd_info->userdata = NULL; +} + static void hos_callback(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -14,6 +66,7 @@ static void hos_callback(bool result, const char *bucket, const char *object, co EXPECT_STREQ(object, (char *)userdata); EXPECT_STREQ(error, NULL); } + static void hos_write_buff_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -22,6 +75,7 @@ static void hos_write_buff_cb(bool result, const char *bucket, const char *objec EXPECT_STREQ(object, (char *)userdata); EXPECT_STREQ(error, NULL); } + static void hos_write_append_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -30,6 +84,7 @@ static void hos_write_append_cb(bool result, const char *bucket, const char *obj EXPECT_STREQ(object, (char *)userdata); EXPECT_STREQ(error, NULL); } + static void hos_write_file_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -38,6 +93,7 @@ static void hos_write_file_cb(bool result, const char *bucket, const char *objec EXPECT_STREQ(object, (char *)userdata); EXPECT_STREQ(error, NULL); } + static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata) { SUCCEED(); @@ -49,369 +105,562 @@ static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char TEST(hos_write, normal) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info[3]; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 3, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=3; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + size_t fd = hos_open_fd(HOS_BUCKET, "object_buff", hos_write_buff_cb, (void *)"object_buff", 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[0]); + expect_fd_info[0].callback = (void *)hos_write_buff_cb; + expect_fd_info[0].object = (char *)"object_buff"; + expect_fd_info[0].userdata = (void *)"object_buff"; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + int ret = hos_write(fd, HOS_BUFF, strlen(HOS_BUFF), 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + expect_fd_info[0].fd_status = 2; + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + EXPECT_TRUE(g_fd_context[0][0].cache == NULL); size_t fd1 = hos_open_fd(HOS_BUCKET, "object_append", hos_write_append_cb, (void *)"object_append", 1, BUFF_MODE | APPEND_MODE); EXPECT_EQ(fd1, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[1]); + expect_fd_info[1].callback = (void *)hos_write_append_cb; + expect_fd_info[1].object = (char *)"object_append"; + expect_fd_info[1].userdata = (void *)"object_append"; + expect_fd_info[1].mode = BUFF_MODE | APPEND_MODE; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0); + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + expect_fd_info[1].cache_count--; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); + + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + expect_fd_info[1].cache_count--; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); size_t fd2 = hos_open_fd(HOS_BUCKET, "object_file", hos_write_file_cb, (void *)"object_file", 2, FILE_MODE); EXPECT_EQ(fd2, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[2]); + expect_fd_info[2].callback = (void *)hos_write_file_cb; + expect_fd_info[2].object = (char *)"object_file"; + expect_fd_info[2].userdata = (void *)"object_file"; + expect_fd_info[2].mode = FILE_MODE; + CheckStructGHosFdContext(g_fd_context[2], &expect_fd_info[2]); + ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[2].fd_status = 2; + //CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd1, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd2, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, bucket_not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info[3]; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 3, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=3; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + size_t fd = hos_open_fd("bucket_not_exits", "object_buff", hos_bucket_not_exits_cb, (void *)"object_buff", 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[0]); + expect_fd_info[0].callback = (void *)hos_bucket_not_exits_cb; + expect_fd_info[0].userdata = (void *)"object_buff"; + expect_fd_info[0].bucket = (char *)"bucket_not_exits"; + expect_fd_info[0].object = (char *)"object_buff"; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + int ret = hos_write(fd, HOS_BUFF, strlen(HOS_BUFF), 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //expect_fd_info[0].cache_rest -= strlen(HOS_BUFF); + //expect_fd_info[0].cache_count--; + expect_fd_info[0].fd_status = 2; + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + EXPECT_TRUE(g_fd_context[0][0].cache == NULL); size_t fd1 = hos_open_fd("bucket_not_exits", "object_append", hos_bucket_not_exits_cb, (void *)"object_append", 1, BUFF_MODE | APPEND_MODE); EXPECT_EQ(fd1, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[1]); + expect_fd_info[1].callback = (void *) hos_bucket_not_exits_cb; + expect_fd_info[1].userdata = (void *)"object_append"; + expect_fd_info[1].bucket = (char *)"bucket_not_exits"; + expect_fd_info[1].object = (char *)"object_append"; + expect_fd_info[1].mode = BUFF_MODE | APPEND_MODE; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0); + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + expect_fd_info[1].cache_count--; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); + + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + expect_fd_info[1].cache_count--; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); size_t fd2 = hos_open_fd("bucket_not_exits", "object_file", hos_bucket_not_exits_cb, (void *)"object_file", 2, FILE_MODE); EXPECT_EQ(fd2, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[2]); + expect_fd_info[2].callback = (void *) hos_bucket_not_exits_cb; + expect_fd_info[2].userdata = (void *)"object_file"; + expect_fd_info[2].bucket = (char *)"bucket_not_exits"; + expect_fd_info[2].object = (char *)"object_file"; + expect_fd_info[2].mode = FILE_MODE; + CheckStructGHosFdContext(g_fd_context[2], &expect_fd_info[2]); + + ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 2); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + expect_fd_info[2].fd_status = 2; + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[2], &expect_fd_info[2]); + EXPECT_TRUE(g_fd_context[2][0].cache == NULL); ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd1, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd2, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, sync_mode) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info[3]; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", 3, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=3; + expect_hos_handle.hos_config.pool_thread_size = 0; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + size_t fd = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[0]); + expect_fd_info[0].object = (char *)"object_buff"; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + int ret = hos_write(fd, HOS_BUFF, strlen(HOS_BUFF), 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + EXPECT_TRUE(g_fd_context[0][0].cache == NULL); size_t fd1 = hos_open_fd(HOS_BUCKET, "object_append", NULL, NULL, 1, BUFF_MODE | APPEND_MODE); EXPECT_EQ(fd1, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[1]); + expect_fd_info[1].object = (char *)"object_append"; + expect_fd_info[1].mode = BUFF_MODE | APPEND_MODE; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0); + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_count--; + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); + + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_count--; + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); size_t fd2 = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 2, FILE_MODE); EXPECT_EQ(fd2, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd2, HOS_FILE, strlen(HOS_BUFF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[2]); + expect_fd_info[2].object = (char *)"object_buff"; + expect_fd_info[2].mode = FILE_MODE; + CheckStructGHosFdContext(g_fd_context[2], &expect_fd_info[2]); + ret = hos_write(fd2, HOS_FILE, strlen(HOS_BUFF), 2); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[2].fd_status = 2; + //CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[2][0].cache == NULL); ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd1, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd2, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, sync_mode_bucket_not_exits) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info[3]; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", 3, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=3; + expect_hos_handle.hos_config.pool_thread_size = 0; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + size_t fd = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[0]); + expect_fd_info[0].object = (char *)"object_buff"; + expect_fd_info[0].mode = BUFF_MODE; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + int ret = hos_write(fd, HOS_BUFF, strlen(HOS_CONF), 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + expect_fd_info[0].fd_status = 2; + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[0]); + EXPECT_TRUE(g_fd_context[0][0].cache == NULL); size_t fd1 = hos_open_fd(HOS_BUCKET, "object_append", NULL, NULL, 1, BUFF_MODE | APPEND_MODE); EXPECT_EQ(fd1, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[1]); + expect_fd_info[1].object = (char *)"object_append"; + expect_fd_info[1].mode = BUFF_MODE | APPEND_MODE; + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_CONF), 0); + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd1, HOS_BUFF, strlen(HOS_CONF), 0); - EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_count--; + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); - size_t fd2 = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 2, FILE_MODE); + ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 1); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[1].cache_count--; + expect_fd_info[1].cache_rest -= strlen(HOS_BUFF); + CheckStructGHosFdContext(g_fd_context[1], &expect_fd_info[1]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); + + size_t fd2 = hos_open_fd(HOS_BUCKET, "object_file", NULL, NULL, 2, FILE_MODE); EXPECT_EQ(fd2, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[2]); + expect_fd_info[2].object = (char *)"object_file"; + expect_fd_info[2].mode = FILE_MODE; + CheckStructGHosFdContext(g_fd_context[2], &expect_fd_info[2]); + ret = hos_write(fd2, HOS_FILE, strlen(HOS_FILE), 2); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + expect_fd_info[2].fd_status = 2; + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[2], &expect_fd_info[2]); + EXPECT_TRUE(g_fd_context[2][0].cache == NULL); + ret = hos_close_fd(fd, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd1, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_close_fd(fd2, 0); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, paramer_error) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - int fd = hos_open_fd(NULL, "object", hos_callback, NULL, 0, BUFF_MODE); - EXPECT_EQ(fd, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + int fd = hos_open_fd(HOS_BUCKET, "object_buff", hos_callback, NULL, 0, BUFF_MODE); + EXPECT_EQ(fd, 3); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info); + expect_fd_info.object = (char *)"object_buff"; + expect_fd_info.callback = (void *)hos_callback; + expect_fd_info.mode = BUFF_MODE; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); + int ret = hos_write(0, HOS_BUFF, strlen(HOS_CONF), 0); EXPECT_EQ(ret, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, fd_not_find) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + int ret = hos_write(3, HOS_BUFF, strlen(HOS_CONF), 0); EXPECT_EQ(ret, HOS_HASH_NOT_FIND); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + CheckStructGHosFdContext(g_fd_context[0], NULL); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, file_not_exit) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, FILE_MODE); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + int fd = hos_open_fd(HOS_CONF, "object_file", NULL, NULL, 0, FILE_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - int ret = hos_write(3, "not_exit_file", strlen(HOS_CONF), 0); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info); + expect_fd_info.bucket = (char *)HOS_CONF; + expect_fd_info.object = (char *)"object_file"; + expect_fd_info.mode = FILE_MODE; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); + + int ret = hos_write(fd, "not_exit_file", strlen(HOS_CONF), 0); EXPECT_EQ(ret, HOS_FILE_NOT_EXIST); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, over_threadnums) { + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info; + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); - int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=2; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + int fd = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(fd, 3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info); + expect_fd_info.object = (char *)"object"; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); + int ret = hos_write(3, HOS_BUFF, strlen(HOS_CONF), 6); EXPECT_EQ(ret, HOS_PARAMETER_ERROR); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + //CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info); + ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_write, not_init_instance) @@ -429,6 +678,9 @@ static void *hos_function(void *ptr) int fd[HOS_FD_NUMS_LOCAL] = {0}; char object[HOS_FD_NUMS_LOCAL][1024]; int ret = 0; + hos_instance_s expect_hos_instance; + hos_client_handle_t expect_hos_handle; + hos_fd_context_t expect_fd_info[32][20]; { hos_instance = hos_get_instance(); @@ -437,44 +689,57 @@ static void *hos_function(void *ptr) hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 32, HOS_BUCKET); } } - EXPECT_EQ(hos_instance->result, true); - EXPECT_EQ(hos_instance->error_code, HOS_CLIENT_OK); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + gtest_hos_instance_init(&expect_hos_instance); + CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle); + expect_hos_handle.hos_config.thread_num=32; + expect_hos_handle.count = thread_id + 1; + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); for (i = 0; i < 20; i++) { snprintf(object[i], 1024, "object_%lu_%d", thread_id, i); - fd[i] = hos_open_fd(HOS_CONF, object[i], hos_callback, object[i], 0, BUFF_MODE | APPEND_MODE); - EXPECT_EQ(fd[i], i+3); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + fd[i] = hos_open_fd(HOS_BUCKET, object[i], hos_callback, object[i], 0, BUFF_MODE | APPEND_MODE); + EXPECT_EQ(fd[i], i + 3); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + gtest_hos_fd_init(&expect_fd_info[thread_id][i]); + expect_fd_info[thread_id][i].object = object[i]; + expect_fd_info[thread_id][i].mode = BUFF_MODE | APPEND_MODE; + expect_fd_info[thread_id][i].callback = (void *)hos_callback; + CheckStructGHosFdContext(g_fd_context[0], &expect_fd_info[thread_id][i]); } for (i = 0; i < 20; i++) { ret = hos_write(fd[i], HOS_BUFF, strlen(HOS_BUFF), i); EXPECT_EQ(ret, HOS_CLIENT_OK); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + expect_fd_info[thread_id][i].cache_rest -= strlen(HOS_BUFF); + expect_fd_info[thread_id][i].cache_count--; + CheckStructGHosFdContext(&g_fd_context[thread_id][i], &expect_fd_info[thread_id][i]); + EXPECT_TRUE(g_fd_context[1][0].cache != NULL); } for (i = 0; i < 20; i++) { ret = hos_close_fd(fd[i], i); EXPECT_EQ(ret, 0); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/"); + CheckHosInstance(hos_instance, &expect_hos_instance); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } ret = hos_shutdown_instance(); EXPECT_EQ(ret, 0); - EXPECT_EQ(hos_instance->error_code, 0); - EXPECT_STREQ(hos_instance->error_message, ""); - EXPECT_STREQ(hos_instance->hos_url_prefix, NULL); + expect_hos_instance.result = 0; + expect_hos_instance.hos_url_prefix = NULL; + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + EXPECT_EQ((void *)g_fd_context, (void *)NULL); pthread_exit(NULL); #undef HOS_FD_NUMS_LOCAL diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b2dbae1..557d66ca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,24 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC -std=c++11") include_directories(${CMAKE_INSTALL_PREFIX}/include/MESA) link_directories(${CMAKE_INSTALL_PREFIX}/lib) -add_library(${lib_name}_shared SHARED hos_client.cpp hos_hash.cpp) +option(HOS_MOCK "If enabled, the SDK will be built using a MOCK .cpp file for S3." OFF) + +file(GLOB HOS_HEADERS "*.h") +file(GLOB HOS_SOURCE "*.cpp") +if (HOS_MOCK) + add_definitions(-DHOS_MOCK) + file(GLOB HOS_MOCK_HEADERS "mock/hos_mock.h") + file(GLOB HOS_MOCK_SOURCE "mock/hos_mock.cpp") +endif() + +file(GLOB HOS_SRC + ${HOS_SOURCE} + ${HOS_HEADERS} + ${HOS_MOCK_SOURCE} + ${HOS_MOCK_HEADERS} +) + +add_library(${lib_name}_shared SHARED ${HOS_SRC}) target_link_libraries(${lib_name}_shared "-Wl,--whole-archive" libaws-c-common.a diff --git a/src/hos_client.cpp b/src/hos_client.cpp index 5132182d..ace52c9f 100644 --- a/src/hos_client.cpp +++ b/src/hos_client.cpp @@ -8,119 +8,32 @@ extern "C" #include #include #include -#include } -#include -#include #include #include -#include -#include #include #include -#include #include #include #include #include +#ifdef HOS_MOCK +#include "mock/hos_mock.h" +#endif #include "hos_client.h" -#include "hos_hash.h" -#include "field_stat2.h" #include "MESA_handle_logger.h" #include "MESA_prof_load.h" +#include "hos_common.h" -#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 410) -#define atomic_add(x,y) __sync_add_and_fetch((x),(y)) -#define atomic_read(x) __sync_add_and_fetch((x),0) -#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y)) -#else -#define atomic_add(x,y) ((*(x))+=(y)) -#define atomic_read(x) (*(x)) -#define atomic_sub(x,y) ((*(x))-=(y)) -#endif - -#define MAX_HOS_STRING_LEN 1024 -#define HOS_ERROR_MESSAGE_SIZE (MAX_HOS_STRING_LEN - 1) -#define MAX_HOS_CLIENT_FD_NUM 65535 -#define HOS_LOG_PATH "./tsglog/hoslog" - -typedef struct data_info_s -{ - size_t *tx_pkts; - size_t *tx_bytes; - size_t *rx_pkts; - size_t *rx_bytes; - size_t *tx_failed_pkts; - size_t *tx_failed_bytes; - size_t *cache; -}data_info_t; - -typedef struct fs2_info_s -{ - screen_stat_handle_t fs2_handle; - int *line_ids; - int *column_ids; - void *reserved; //预留给每个fs2 handle用来存储自定义的数据 -}fs2_info_t; - -enum -{ - FS2_DATA_FLOW_STATE = 0, - FS2_POOL_THREAD_STATE, - FS2_RECORD_EVENTS, -}; - -typedef struct hos_config_s -{ - char ip[INET6_ADDRSTRLEN]; - char fs2_ip[INET6_ADDRSTRLEN]; - char accesskeyid[MAX_HOS_STRING_LEN]; - char secretkey[MAX_HOS_STRING_LEN]; - char log_path[MAX_HOS_STRING_LEN]; - char fs2_path[MAX_HOS_STRING_LEN]; - - uint32_t port; - uint32_t fs2_port; - uint32_t fs2_fmt; - uint32_t log_level; - uint32_t pool_thread_size; - uint32_t thread_num; - uint32_t cache_size; - uint32_t cache_count; - uint32_t timeout; -}hos_config_t; - -typedef struct hos_func_thread_s -{ - /* fd 管理线程 */ - pthread_t fd_thread; - int fd_thread_status; - /* fs2 管理线程 */ - fs2_info_t fs2_info[FS2_RECORD_EVENTS]; //0: data info; 1: fd info; 2 cache info; 3 PoolThread state - pthread_t fs2_thread; - int fs2_status; -#define HOS_FS2_START 1 -#define HOS_FS2_STOP 2 -}hos_func_thread_t; - -typedef struct hos_client_handle_s -{ - Aws::S3::S3Client *S3Client; - Aws::Vector buckets; - std::shared_ptr executor; - size_t count; /* 记录了有多少个对象在使用hos */ - hos_config_t hos_config; - hos_func_thread_t hos_func; - void *log; -}hos_client_handle_t; - -static struct hos_instance_s g_hos_instance; -static hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle +struct hos_instance_s g_hos_instance; +hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle static std::mutex m_client_lock; -static hos_fd_context_t **g_fd_context; -static size_t (*g_fd_info)[MAX_HOS_CLIENT_FD_NUM + 1]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd +hos_fd_context_t **g_fd_context; +size_t (*g_fd_info)[MAX_HOS_CLIENT_FD_NUM + 1]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd static Aws::SDKOptions g_options; +static void *hos_fd_manage(void *ptr); + static inline size_t get_current_ms() { struct timespec timenow; @@ -268,7 +181,11 @@ static void hos_client_create() //同步模式 } + #ifndef HOS_MOCK g_hos_handle.S3Client = new Aws::S3::S3Client(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false); + #else + g_hos_handle.S3Client = new Aws::S3::S3ClientMock(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false); + #endif /* 获取当前用户的所有的buckets */ Aws::S3::Model::ListBucketsOutcome outcome = g_hos_handle.S3Client->ListBuckets(); @@ -571,7 +488,7 @@ static int hos_putobject_async(Aws::S3::Model::PutObjectRequest& request, size_t sprintf(buf, "%lu %lu %lu", thread_id, fd, stream_len); context->SetUUID(buf); - Aws::S3::S3Client& S3Client = *(g_hos_handle.S3Client); + auto &S3Client = *(g_hos_handle.S3Client); ret = S3Client.PutObjectAsync(request, PutObjectAsyncFinished, context); if (ret) { @@ -605,7 +522,7 @@ static int hos_putobject_sync(Aws::S3::Model::PutObjectRequest& request, size_t hos_func_thread_t *hos_func = &g_hos_handle.hos_func; data_info_t *data_info = NULL; - Aws::S3::S3Client& S3Client = *(g_hos_handle.S3Client); + auto& S3Client = *(g_hos_handle.S3Client); Aws::S3::Model::PutObjectOutcome Outcome = S3Client.PutObject(request); if (Outcome.IsSuccess()) { @@ -650,6 +567,7 @@ hos_instance hos_get_instance() g_hos_instance.result = true; return &g_hos_instance; } + memset(&g_hos_instance, 0, sizeof(g_hos_instance)); g_hos_instance.result = false; return &g_hos_instance; } @@ -742,7 +660,7 @@ int hos_create_bucket(const char *bucket) "error:bucket:%s, s3client:%s", bucket, g_hos_handle.S3Client?"not null":"null"); return HOS_PARAMETER_ERROR; } - Aws::S3::S3Client& S3Client = *g_hos_handle.S3Client; + auto& S3Client = *g_hos_handle.S3Client; /* 本地检查是否已经存在该bucket */ for (Aws::S3::Model::Bucket& new_bucket : g_hos_handle.buckets) @@ -777,7 +695,6 @@ int hos_create_bucket(const char *bucket) static int hos_upload_stream(const char *bucket, const char *object, const char *data, size_t data_len, put_finished_callback callback, void *userdata, size_t thread_id) { - char buf[128]; data_info_t *data_info = NULL; hos_config_t *hos_conf = &g_hos_handle.hos_config; hos_func_thread_t *hos_func = &g_hos_handle.hos_func; @@ -833,6 +750,15 @@ static int hos_upload_stream(const char *bucket, const char *object, const char hos_fd_context_t info = {fd, 0, (char *)bucket, (char *)object, (void *)callback, userdata, NULL, 0, 0, 0 }; add_fd_context(&g_fd_context[thread_id], &info); + { + std::lock_guard locker(m_client_lock); + if (g_hos_handle.hos_func.fd_thread == 0) + { + g_hos_handle.hos_func.fd_thread_status = 0; + pthread_create(&g_hos_handle.hos_func.fd_thread, NULL, hos_fd_manage, NULL); + } + } + if (hos_conf->pool_thread_size > 0) { ret = hos_putobject_async(request, data_len, thread_id, fd, bucket, object); @@ -942,13 +868,14 @@ int hos_open_fd(const char *bucket, const char *object, put_finished_callback ca { MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s", - g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null")); + g_hos_instance.result, (g_hos_handle.S3Client == NULL)?("null"):("not null")); return HOS_INSTANCE_NOT_INIT; } if ((bucket == NULL) || (object == NULL) || (thread_id > g_hos_handle.hos_config.thread_num) || strlen(bucket) == 0 || strlen(object) == 0) { MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_open_fd", - "bucket:%s, obejct:%s, thread_id:%s", + "bucket:%s, obejct:%s, thread_id:%d", + //(bucket == NULL)?"null":bucket, (object == NULL)?"null":object, thread_id); bucket, object, thread_id); return HOS_PARAMETER_ERROR; } @@ -1007,7 +934,7 @@ int hos_write(size_t fd, const char *stream, size_t stream_len, size_t thread_id if ((fd < 3) || fd > MAX_HOS_CLIENT_FD_NUM || (stream == NULL) || (thread_id > hos_conf->thread_num)) { MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, - "hos_write", "error: fd:%d, stream:%s, stream_len:%s, thread_id:%d.", + "hos_write", "error: fd:%d, stream:%s, stream_len:%d, thread_id:%d.", fd, stream?"not null":"null", stream_len, thread_id); return HOS_PARAMETER_ERROR; } diff --git a/src/hos_common.h b/src/hos_common.h new file mode 100644 index 00000000..85d415e8 --- /dev/null +++ b/src/hos_common.h @@ -0,0 +1,107 @@ +#ifndef __HOS_COMMON_H__ +#define __HOS_COMMON_H__ + +#include +#include +#include "field_stat2.h" +#include "hos_hash.h" +#include +#include +#include +#include + +#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 410) +#define atomic_add(x,y) __sync_add_and_fetch((x),(y)) +#define atomic_read(x) __sync_add_and_fetch((x),0) +#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y)) +#else +#define atomic_add(x,y) ((*(x))+=(y)) +#define atomic_read(x) (*(x)) +#define atomic_sub(x,y) ((*(x))-=(y)) +#endif + +#define MAX_HOS_STRING_LEN 1024 +#define HOS_ERROR_MESSAGE_SIZE (MAX_HOS_STRING_LEN - 1) +#define MAX_HOS_CLIENT_FD_NUM 65535 +#define HOS_LOG_PATH "./tsglog/hoslog" + +typedef struct data_info_s +{ + size_t *tx_pkts; + size_t *tx_bytes; + size_t *rx_pkts; + size_t *rx_bytes; + size_t *tx_failed_pkts; + size_t *tx_failed_bytes; + size_t *cache; +}data_info_t; + +typedef struct fs2_info_s +{ + screen_stat_handle_t fs2_handle; + int *line_ids; + int *column_ids; + void *reserved; //预留给每个fs2 handle用来存储自定义的数据 +}fs2_info_t; + +enum +{ + FS2_DATA_FLOW_STATE = 0, + FS2_POOL_THREAD_STATE, + FS2_RECORD_EVENTS, +}; + +typedef struct hos_config_s +{ + char ip[INET6_ADDRSTRLEN]; + char fs2_ip[INET6_ADDRSTRLEN]; + char accesskeyid[MAX_HOS_STRING_LEN]; + char secretkey[MAX_HOS_STRING_LEN]; + char log_path[MAX_HOS_STRING_LEN]; + char fs2_path[MAX_HOS_STRING_LEN]; + + uint32_t port; + uint32_t fs2_port; + uint32_t fs2_fmt; + uint32_t log_level; + uint32_t pool_thread_size; + uint32_t thread_num; + uint32_t cache_size; + uint32_t cache_count; + uint32_t timeout; +}hos_config_t; + +typedef struct hos_func_thread_s +{ + /* fd 管理线程 */ + pthread_t fd_thread; + int fd_thread_status; + /* fs2 管理线程 */ + fs2_info_t fs2_info[FS2_RECORD_EVENTS]; //0: data info; 1: fd info; 2 cache info; 3 PoolThread state + pthread_t fs2_thread; + int fs2_status; +#define HOS_FS2_START 1 +#define HOS_FS2_STOP 2 +}hos_func_thread_t; + +typedef struct hos_client_handle_s +{ + #ifndef HOS_MOCK + Aws::S3::S3Client *S3Client; + #else + Aws::S3::S3ClientMock *S3Client; + #endif + Aws::Vector buckets; + std::shared_ptr executor; + size_t count; /* 记录了有多少个对象在使用hos */ + hos_config_t hos_config; + hos_func_thread_t hos_func; + void *log; +}hos_client_handle_t; + +extern struct hos_instance_s g_hos_instance; +extern hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle +extern hos_fd_context_t **g_fd_context; +extern size_t (*g_fd_info)[MAX_HOS_CLIENT_FD_NUM + 1]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd + +#endif \ No newline at end of file diff --git a/src/mock/hos_mock.cpp b/src/mock/hos_mock.cpp new file mode 100644 index 00000000..0e349965 --- /dev/null +++ b/src/mock/hos_mock.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "hos_mock.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Aws; +using namespace Aws::Auth; +using namespace Aws::Client; +using namespace Aws::S3; +using namespace Aws::S3::Model; +using namespace Aws::Http; +using namespace Aws::Utils::Xml; + +S3ClientMock::S3ClientMock(const AWSCredentials &credentials, const Client::ClientConfiguration &clientConfiguration, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy signPayloads, bool useVirtualAddressing) +{ + Aws::String accessKeyId = credentials.GetAWSAccessKeyId(); + Aws::String secretKey = credentials.GetAWSSecretKey(); + m_hosBuckets.push_back(Bucket().WithName("session_record_hos_bucket")); + m_hosBuckets.push_back(Bucket().WithName("firewall_hos_bucket")); + + init(clientConfiguration); + if (memcmp(clientConfiguration.endpointOverride.c_str(), "http://127.0.0.1:9098/hos/", clientConfiguration.endpointOverride.length()) != 0) + { + m_errorCode = S3Errors::NETWORK_CONNECTION; + } + if (memcmp(clientConfiguration.endpointOverride.c_str(), "127.0.0.2", clientConfiguration.endpointOverride.length()) == 0) + { + m_mockMode = 2; + m_errorCode = S3Errors::REQUEST_TIMEOUT; + } + if ((int)m_errorCode == 0) + { + if (memcmp(accessKeyId.c_str(), "default", accessKeyId.length()) != 0) + { + m_errorCode = S3Errors::INVALID_ACCESS_KEY_ID; + } + else if (memcmp(secretKey.c_str(), "default", secretKey.length()) != 0) + { + m_errorCode = S3Errors::INVALID_SIGNATURE; + } + } +} + +S3ClientMock::~S3ClientMock() +{ +} + +void S3ClientMock::init(const ClientConfiguration &config) +{ + SetServiceClientName("S3"); + if (config.endpointOverride.empty()) + { + } + else + { + OverrideEndpoint(config.endpointOverride); + } + m_errorCode = (Aws::S3::S3Errors)0; +} + +ListBucketsOutcome S3ClientMock::ListBuckets() const +{ + switch (m_errorCode) + { + case (S3Errors)0: + return ListBucketsOutcome(Aws::S3::Model::ListBucketsResult().WithBuckets(m_hosBuckets)); + case S3Errors::NETWORK_CONNECTION: + return ListBucketsOutcome(Aws::Client::AWSError(S3Errors::NETWORK_CONNECTION, "NETWORK_CONNECTION", "curlCode: 7, Couldn't connect to server", false)); + case S3Errors::REQUEST_TIMEOUT: + return ListBucketsOutcome(Aws::Client::AWSError(S3Errors::REQUEST_TIMEOUT, "REQUEST_TIMEOUT", "RequestTimeout", false)); + case S3Errors::INVALID_ACCESS_KEY_ID: + return ListBucketsOutcome(Aws::Client::AWSError(S3Errors::INVALID_ACCESS_KEY_ID, "INVALID_ACCESS_KEY_ID", "invalid_access_key_id", false)); + case S3Errors::INVALID_SIGNATURE: + return ListBucketsOutcome(Aws::Client::AWSError(S3Errors::INVALID_SIGNATURE, "INVALID_SIGNATURE", "Ivalid Signature", false)); + default: + return ListBucketsOutcome(Aws::Client::AWSError(S3Errors::UNKNOWN, "UNKNOWN", "unknown", false)); + } +} + +CreateBucketOutcome S3ClientMock::CreateBucket(const CreateBucketRequest &request) const +{ + return CreateBucketOutcome(); +} + +PutObjectOutcome S3ClientMock::PutObject(const PutObjectRequest &request) const +{ + if (!request.BucketHasBeenSet()) + { + AWS_LOGSTREAM_ERROR("PutObject", "Required field: Bucket, is not set"); + return PutObjectOutcome(Aws::Client::AWSError(S3Errors::MISSING_PARAMETER, "MISSING_PARAMETER", "Missing required field [Bucket]", false)); + } + if (!request.KeyHasBeenSet()) + { + AWS_LOGSTREAM_ERROR("PutObject", "Required field: Key, is not set"); + return PutObjectOutcome(Aws::Client::AWSError(S3Errors::MISSING_PARAMETER, "MISSING_PARAMETER", "Missing required field [Key]", false)); + } + + auto bucket = request.GetBucket().c_str(); + for (auto &new_bucket : m_hosBuckets) + { + if (strcmp(new_bucket.GetName().c_str(), bucket) == 0) + { + return PutObjectOutcome(Aws::S3::Model::PutObjectResult()); + } + } + return PutObjectOutcome(Aws::Client::AWSError(S3Errors::NO_SUCH_BUCKET, "NO_SUCH_BUCKET", "The specified bucket does not exist.", false)); +} + +bool S3ClientMock::PutObjectAsync(const PutObjectRequest &request, const PutObjectResponseReceivedHandler &handler, const std::shared_ptr &context) const +{ + handler(this, request, PutObject(request), context); + return true; +} \ No newline at end of file diff --git a/src/mock/hos_mock.h b/src/mock/hos_mock.h new file mode 100644 index 00000000..05310ed5 --- /dev/null +++ b/src/mock/hos_mock.h @@ -0,0 +1,24 @@ +#include + +namespace Aws +{ + namespace S3 + { + class AWS_S3_API S3ClientMock : public S3Client + { + public: + S3ClientMock(const Aws::Auth::AWSCredentials &credentials, const Aws::Client::ClientConfiguration &clientConfiguration, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy signPayloads, bool useVirtualAddressing); + ~S3ClientMock(); + Model::ListBucketsOutcome ListBuckets() const; + Model::CreateBucketOutcome CreateBucket(const Model::CreateBucketRequest& request) const; + Model::PutObjectOutcome PutObject(const Model::PutObjectRequest& request) const; + bool PutObjectAsync(const Model::PutObjectRequest& request, const PutObjectResponseReceivedHandler& handler, const std::shared_ptr& context = nullptr) const; + + private: + void init(const Aws::Client::ClientConfiguration &ClientConfiguration); + size_t m_mockMode; + S3Errors m_errorCode; + Aws::Vector m_hosBuckets; + }; // class S3ClientMock + } // namespace S3 +}