From cfe857bae7efc1898ad3610011093258a42b1c13 Mon Sep 17 00:00:00 2001 From: "hyuna0213.jo" Date: Fri, 8 Jul 2016 14:24:55 +0900 Subject: [PATCH] Refactored Connectivity Abstraction code - delete unnecessary code - add new unit test case - adjusted code style - fix memory free issue Change-Id: If12f5ca8e8d03af3da92f33b655c8618946c8a6c Signed-off-by: hyuna0213.jo Reviewed-on: https://gerrit.iotivity.org/gerrit/9235 Tested-by: jenkins-iotivity Reviewed-by: jihwan seo Reviewed-by: Jaehong Jo Reviewed-by: Jon A. Cruz --- .../connectivity/common/src/camutex_pthreads.c | 10 +- .../csdk/connectivity/common/src/caremotehandler.c | 18 +- resource/csdk/connectivity/src/SConscript | 2 +- resource/csdk/connectivity/src/camessagehandler.c | 78 ++------ resource/csdk/connectivity/src/caretransmission.c | 51 +---- resource/csdk/connectivity/test/SConscript | 6 +- .../csdk/connectivity/test/ca_api_unittest.cpp | 155 +++++++++++---- .../csdk/connectivity/test/cablocktransfertest.cpp | 207 ++++++++++++++++++++- .../connectivity/test/caprotocolmessagetest.cpp | 27 +++ resource/csdk/connectivity/test/uqueue_test.cpp | 192 +++++++++++++++++++ .../csdk/connectivity/util/src/cautilinterface.c | 56 +----- 11 files changed, 576 insertions(+), 226 deletions(-) create mode 100644 resource/csdk/connectivity/test/uqueue_test.cpp diff --git a/resource/csdk/connectivity/common/src/camutex_pthreads.c b/resource/csdk/connectivity/common/src/camutex_pthreads.c index 6c166b1..ef344fd 100644 --- a/resource/csdk/connectivity/common/src/camutex_pthreads.c +++ b/resource/csdk/connectivity/common/src/camutex_pthreads.c @@ -236,15 +236,15 @@ ca_cond ca_cond_new(void) return retVal; } - #if defined(__ANDROID__) || _POSIX_TIMERS > 0 - #ifdef __ANDROID__ +#if defined(__ANDROID__) || _POSIX_TIMERS > 0 +#ifdef __ANDROID__ if (camutex_condattr_setclock) { ret = camutex_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC); - #else +#else { ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC); - #endif /* __ANDROID__ */ +#endif /* __ANDROID__ */ if(0 != ret) { OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!", @@ -254,7 +254,7 @@ ca_cond ca_cond_new(void) return retVal; } } - #endif /* defined(__ANDROID__) || _POSIX_TIMERS > 0 */ +#endif /* defined(__ANDROID__) || _POSIX_TIMERS > 0 */ ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr)); if (0 == ret) { diff --git a/resource/csdk/connectivity/common/src/caremotehandler.c b/resource/csdk/connectivity/common/src/caremotehandler.c index 61a2509..6a7fea2 100644 --- a/resource/csdk/connectivity/common/src/caremotehandler.c +++ b/resource/csdk/connectivity/common/src/caremotehandler.c @@ -247,16 +247,14 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone) //Do not free clone. we cannot declare it const, as the content is modified if ((info->token) && (0 < info->tokenLength)) { - char *temp = NULL; - // allocate token field uint8_t len = info->tokenLength; - temp = (char *) OICMalloc(len * sizeof(char)); + char *temp = (char *) OICMalloc(len * sizeof(char)); if (!temp) { OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); - return CA_MEMORY_ALLOC_FAILED; + goto exit; } memcpy(temp, info->token, len); @@ -274,8 +272,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone) if (!clone->options) { OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); - CADestroyInfoInternal(clone); - return CA_MEMORY_ALLOC_FAILED; + goto exit; } memcpy(clone->options, info->options, sizeof(CAHeaderOption_t) * info->numOptions); clone->numOptions = info->numOptions; @@ -290,8 +287,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone) if (!temp) { OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); - CADestroyInfoInternal(clone); - return CA_MEMORY_ALLOC_FAILED; + goto exit; } memcpy(temp, info->payload, info->payloadSize); @@ -309,8 +305,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone) if (!temp) { OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); - CADestroyInfoInternal(clone); - return CA_MEMORY_ALLOC_FAILED; + goto exit; } // save the resourceUri @@ -326,4 +321,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone) return CA_STATUS_OK; +exit: + CADestroyInfoInternal(clone); + return CA_MEMORY_ALLOC_FAILED; } diff --git a/resource/csdk/connectivity/src/SConscript b/resource/csdk/connectivity/src/SConscript index d2b52e8..486966d 100644 --- a/resource/csdk/connectivity/src/SConscript +++ b/resource/csdk/connectivity/src/SConscript @@ -126,7 +126,7 @@ if 'ALL' in ca_transport: if 'IP' in ca_transport: env.SConscript(os.path.join(ca_path, 'ip_adapter/SConscript')) if ca_os == 'arduino': - if with_tcp: + if with_tcp == True: transports = [ 'ip_adapter', 'tcp_adapter'] env.SConscript(dirs = [ os.path.join(ca_path, d) for d in transports ]) diff --git a/resource/csdk/connectivity/src/camessagehandler.c b/resource/csdk/connectivity/src/camessagehandler.c index d6c2c55..17b3a9b 100644 --- a/resource/csdk/connectivity/src/camessagehandler.c +++ b/resource/csdk/connectivity/src/camessagehandler.c @@ -135,8 +135,7 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); - OICFree(cadata); - return NULL; + goto exit; } OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr); @@ -147,19 +146,15 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, if (!resInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); - OICFree(cadata); - CAFreeEndpoint(ep); - return NULL; + goto exit; } CAResult_t result = CAGetResponseInfoFromPDU(data, resInfo, endpoint); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed"); - CAFreeEndpoint(ep); CADestroyResponseInfoInternal(resInfo); - OICFree(cadata); - return NULL; + goto exit; } cadata->responseInfo = resInfo; info = &resInfo->info; @@ -176,29 +171,23 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, if (!reqInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); - OICFree(cadata); - CAFreeEndpoint(ep); - return NULL; + goto exit; } CAResult_t result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed"); - CAFreeEndpoint(ep); CADestroyRequestInfoInternal(reqInfo); - OICFree(cadata); - return NULL; + goto exit; } if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId, reqInfo->info.token, reqInfo->info.tokenLength)) { OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it"); - CAFreeEndpoint(ep); CADestroyRequestInfoInternal(reqInfo); - OICFree(cadata); - return NULL; + goto exit; } cadata->requestInfo = reqInfo; @@ -216,19 +205,15 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, if (!errorInfo) { OIC_LOG(ERROR, TAG, "Memory allocation failed!"); - OICFree(cadata); - CAFreeEndpoint(ep); - return NULL; + goto exit; } CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed"); - CAFreeEndpoint(ep); OICFree(errorInfo); - OICFree(cadata); - return NULL; + goto exit; } cadata->errorInfo = errorInfo; @@ -246,6 +231,11 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT"); return cadata; + +exit: + OICFree(cadata); + CAFreeEndpoint(ep); + return NULL; } static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size) @@ -445,42 +435,9 @@ static CAResult_t CAProcessMulticastData(const CAData_t *data) return res; } } - else if (NULL != data->responseInfo) - { - OIC_LOG(DEBUG, TAG, "responseInfo is available.."); - - info = &data->responseInfo->info; - pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint, - &options, &transport); - - if (NULL != pdu) - { -#ifdef WITH_BWT - if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter)) - { - // Blockwise transfer - if (NULL != info) - { - res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options); - if (CA_STATUS_OK != res) - { - OIC_LOG(INFO, TAG, "to write block option has failed"); - goto exit; - } - } - } -#endif // WITH_BWT - } - else - { - OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU"); - CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED); - return res; - } - } else { - OIC_LOG(ERROR, TAG, "request or response info is empty"); + OIC_LOG(ERROR, TAG, "not supported message type for multicast."); return res; } @@ -941,8 +898,7 @@ static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sen if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); - CADestroyData(cadata, sizeof(CAData_t)); - return NULL; + goto exit; } cadata->remoteEndpoint = ep; @@ -950,7 +906,7 @@ static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sen return cadata; exit: - OICFree(cadata); + CADestroyData(cadata, sizeof(CAData_t)); return NULL; } @@ -1304,7 +1260,7 @@ void CAErrorHandler(const CAEndpoint_t *endpoint, } CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA); - if(!cadata) + if (!cadata) { OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!"); coap_delete_pdu(pdu); diff --git a/resource/csdk/connectivity/src/caretransmission.c b/resource/csdk/connectivity/src/caretransmission.c index 52482b3..56edbef 100644 --- a/resource/csdk/connectivity/src/caretransmission.c +++ b/resource/csdk/connectivity/src/caretransmission.c @@ -68,6 +68,7 @@ #include "caremotehandler.h" #include "caprotocolmessage.h" #include "oic_malloc.h" +#include "oic_time.h" #include "ocrandom.h" #include "logger.h" @@ -89,12 +90,6 @@ typedef struct static const uint64_t USECS_PER_SEC = 1000000; static const uint64_t MSECS_PER_SEC = 1000; -/** - * @brief getCurrent monotonic time - * @return current time in microseconds - */ -uint64_t getCurrentTimeInMicroSeconds(); - #ifndef SINGLE_THREAD /** * @brief timeout value is @@ -105,13 +100,8 @@ uint64_t getCurrentTimeInMicroSeconds(); */ static uint64_t CAGetTimeoutValue() { -#ifdef HAVE_SRANDOM - return ((DEFAULT_ACK_TIMEOUT_SEC * 1000) + ((1000 * OCGetRandomByte()) >> 8)) * - (uint64_t) 1000; -#else return ((DEFAULT_ACK_TIMEOUT_SEC * 1000) + ((1000 * OCGetRandomByte()) >> 8)) * (uint64_t) 1000; -#endif } CAResult_t CARetransmissionStart(CARetransmission_t *context) @@ -197,7 +187,7 @@ static void CACheckRetransmissionList(CARetransmission_t *context) continue; } - uint64_t currentTime = getCurrentTimeInMicroSeconds(); + uint64_t currentTime = OICGetCurrentTime(TIME_IN_US); if (CACheckTimeout(currentTime, retData)) { @@ -426,7 +416,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context, } // #2. add additional information. (time stamp, retransmission count...) - retData->timeStamp = getCurrentTimeInMicroSeconds(); + retData->timeStamp = OICGetCurrentTime(TIME_IN_US); #ifndef SINGLE_THREAD retData->timeout = CAGetTimeoutValue(); #endif @@ -641,38 +631,3 @@ CAResult_t CARetransmissionDestroy(CARetransmission_t *context) return CA_STATUS_OK; } - -uint64_t getCurrentTimeInMicroSeconds() -{ - OIC_LOG(DEBUG, TAG, "IN"); - uint64_t currentTime = 0; - -#ifdef __ANDROID__ - struct timespec getTs; - - clock_gettime(CLOCK_MONOTONIC, &getTs); - - currentTime = (getTs.tv_sec * (uint64_t)1000000000 + getTs.tv_nsec)/1000; - OIC_LOG_V(DEBUG, TAG, "current time = %lld", currentTime); -#elif defined __ARDUINO__ - currentTime = millis() * 1000; - OIC_LOG_V(DEBUG, TAG, "currtime=%lu", currentTime); -#else -#if _POSIX_TIMERS > 0 - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - currentTime = ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000; -#elif defined(_WIN32) - struct __timeb64 tb; - _ftime64_s(&tb); - currentTime = tb.time * USECS_PER_SEC + tb.millitm * MSECS_PER_SEC; -#else - struct timeval tv; - gettimeofday(&tv, NULL); - currentTime = tv.tv_sec * USECS_PER_SEC + tv.tv_usec; -#endif -#endif - - OIC_LOG(DEBUG, TAG, "OUT"); - return currentTime; -} diff --git a/resource/csdk/connectivity/test/SConscript b/resource/csdk/connectivity/test/SConscript index 0184c38..2b6a0d3 100644 --- a/resource/csdk/connectivity/test/SConscript +++ b/resource/csdk/connectivity/test/SConscript @@ -76,7 +76,8 @@ if (('IP' in target_transport) or ('ALL' in target_transport)): 'ca_api_unittest.cpp', 'camutex_tests.cpp', 'uarraylist_test.cpp', - 'ulinklist_test.cpp' + 'ulinklist_test.cpp', + 'uqueue_test.cpp' ]) else: # Include all unit test files @@ -85,7 +86,8 @@ else: 'ca_api_unittest.cpp', 'camutex_tests.cpp', 'uarraylist_test.cpp', - 'ulinklist_test.cpp' + 'ulinklist_test.cpp', + 'uqueue_test.cpp' ]) Alias("test", [catests]) diff --git a/resource/csdk/connectivity/test/ca_api_unittest.cpp b/resource/csdk/connectivity/test/ca_api_unittest.cpp index 0869390..6a94700 100644 --- a/resource/csdk/connectivity/test/ca_api_unittest.cpp +++ b/resource/csdk/connectivity/test/ca_api_unittest.cpp @@ -23,6 +23,7 @@ #include "cainterface.h" #include "cautilinterface.h" #include "cacommon.h" +#include "oic_string.h" #define CA_TRANSPORT_ADAPTER_SCOPE 1000 @@ -43,7 +44,6 @@ void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo); void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo); void adapter_handler(CATransportAdapter_t adapter, bool enabled); void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected); -CAResult_t checkGetNetworkInfo(); CAResult_t checkSelectNetwork(); void request_handler(const CAEndpoint_t * /*object*/, @@ -160,8 +160,7 @@ int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type, #endif //__WITH_DTLS__ // CAInitialize TC -// check return value -TEST(InitializeTest, TC_01_Positive_01) +TEST(InitializeTest, CAInitializeTest) { EXPECT_EQ(CA_STATUS_OK, CAInitialize()); CATerminate(); @@ -177,23 +176,34 @@ TEST_F(CATests, TerminateTest) CAInitialize(); } + +// CAStartListeningServer TC +TEST_F(CATests, StartListeningServerTestWithNonSelect) +{ + EXPECT_EQ(CA_STATUS_FAILED, CAStartListeningServer()); +} + // CAStartListeningServer TC -// check return value -TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01) +TEST_F(CATests, StartListeningServerTest) { - CASelectNetwork(CA_ADAPTER_IP); + EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer()); } -// CAStartDiscoveryServer TC -// check return value -TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01) +// CAStopListeningServer TC +TEST_F(CATests, CAStopListeningServerTestWithNonSelect) { - EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer()); + EXPECT_EQ(CA_STATUS_FAILED, CAStopListeningServer()); +} + +// CAStopListeningServer TC +TEST_F(CATests, CAStopListeningServerTest) +{ + EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); + EXPECT_EQ(CA_STATUS_OK, CAStopListeningServer()); } // CARegisterHandlerTest TC -// check return value TEST_F(CATests, RegisterHandlerTest) { CARegisterHandler(request_handler, response_handler, error_handler); @@ -202,7 +212,6 @@ TEST_F(CATests, RegisterHandlerTest) } // CACreateRemoteEndpoint TC -// check return value TEST_F(CATests, CreateRemoteEndpointTestGood) { addr = (char *) ADDRESS; @@ -228,7 +237,6 @@ TEST_F(CATests, CreateRemoteEndpointTestValues) } // CAGerateToken TC -// check return value TEST_F(CATests, GenerateTokenTestGood) { EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength)); @@ -253,8 +261,32 @@ TEST_F(CATests, DestroyTokenTest) EXPECT_STREQ(check, "destroy success"); } +TEST_F(CATests, SendRequestTestWithInvalidAddress) +{ + CARegisterHandler(request_handler, response_handler, error_handler); + + EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "10.11.13.13.14", PORT, &tempRep); + + memset(&requestData, 0, sizeof(CAInfo_t)); + CAGenerateToken(&tempToken, tokenLength); + requestData.token = tempToken; + requestData.tokenLength = tokenLength; + requestData.type = CA_MSG_CONFIRM; + + memset(&requestInfo, 0, sizeof(CARequestInfo_t)); + requestInfo.method = CA_GET; + requestInfo.info = requestData; + + EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo)); + + CADestroyToken(tempToken); + CADestroyEndpoint(tempRep); + free(requestData.payload); + tempRep = NULL; +} + // CASendRequest TC -// check return value TEST(SendRequestTest, DISABLED_TC_16_Positive_01) { addr = (char *) ADDRESS; @@ -300,8 +332,40 @@ TEST_F(CATests, SendRequestTestWithNullAddr) tempRep = NULL; } +TEST_F(CATests, SendResponseTestWithInvalidCode) +{ + EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); + + addr = (char *) ADDRESS; + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep); + + memset(&responseData, 0, sizeof(CAInfo_t)); + responseData.type = CA_MSG_RESET; + responseData.messageId = 1; + responseData.payload = (CAPayload_t)malloc(sizeof("response payload")); + + EXPECT_TRUE(responseData.payload != NULL); + + if (responseData.payload) + { + CAGenerateToken(&tempToken, tokenLength); + requestData.token = tempToken; + requestData.tokenLength = tokenLength; + + memset(&responseInfo, 0, sizeof(CAResponseInfo_t)); + responseInfo.result = CA_CONTENT; + responseInfo.info = responseData; + + EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo)); + + CADestroyToken(tempToken); + CADestroyEndpoint(tempRep); + free(responseData.payload); + tempRep = NULL; + } +} + // CASendResponse TC -// check return value TEST(SendResponseTest, DISABLED_TC_19_Positive_01) { addr = (char *) ADDRESS; @@ -388,7 +452,6 @@ TEST_F(CATests, SendResponseTest) } // CASelectNewwork TC -// check return value TEST_F(CATests, SelectNetworkTestGood) { EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork()); @@ -429,17 +492,21 @@ TEST_F(CATests, UnSelectNetworkTest) } // CAHandlerRequestResponse TC -// check return value TEST_F(CATests, HandlerRequestResponseTest) { EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse()); } // CAGetNetworkInformation TC -// check return value -TEST_F (CATests, GetNetworkInformationTestGood) +TEST_F (CATests, GetNetworkInformationTest) { - EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo()); + uint32_t tempSize = 0; + CAEndpoint_t *tempInfo = NULL; + + EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP)); + EXPECT_EQ(CA_STATUS_OK, CAGetNetworkInformation(&tempInfo, &tempSize)); + + free(tempInfo); } TEST_F(CATests, RegisterDTLSCredentialsHandlerTest) @@ -450,7 +517,6 @@ TEST_F(CATests, RegisterDTLSCredentialsHandlerTest) } // CARegisterNetworkMonitorHandler TC -// check return value TEST_F(CATests, RegisterNetworkMonitorHandler) { EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler, @@ -458,7 +524,6 @@ TEST_F(CATests, RegisterNetworkMonitorHandler) } // CASetAutoConnectionDeviceInfo TC -// check return value TEST_F(CATests, SetAutoConnectionDeviceInfo) { addr = (char *) ADDRESS; @@ -471,7 +536,6 @@ TEST_F(CATests, SetAutoConnectionDeviceInfo) } // CAUnsetAutoConnectionDeviceInfo TC -// check return value TEST_F(CATests, UnsetAutoConnectionDeviceInfo) { addr = (char *) ADDRESS; @@ -483,22 +547,37 @@ TEST_F(CATests, UnsetAutoConnectionDeviceInfo) #endif } -CAResult_t checkGetNetworkInfo() +TEST(CASetPortNumberTest, CASetPortNumberToAssign) { - CAEndpoint_t *tempInfo = NULL; - uint32_t tempSize = 0; + EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV4, 5683)); + EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV6, 5683)); + EXPECT_EQ(CA_STATUS_OK, + CASetPortNumberToAssign(CA_ADAPTER_IP, + static_cast(CA_IPV4|CA_SECURE), 5683)); + EXPECT_EQ(CA_STATUS_OK, + CASetPortNumberToAssign(CA_ADAPTER_IP, + static_cast(CA_IPV6|CA_SECURE), 5683)); - CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize); - - free(tempInfo); +#ifdef TCP_ADAPTER + EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV4, 5683)); + EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV6, 5683)); +#endif +} - if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res || - CA_NOT_SUPPORTED == res) - { - return CA_STATUS_OK; - } - else - { - return CA_STATUS_FAILED; - } +TEST(CAGetPortNumberTest, CAGetPortNumberToAssign) +{ + ASSERT_EQ(static_cast(0), + CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV4)); + ASSERT_EQ(static_cast(0), + CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV6)); + ASSERT_EQ(static_cast(0), + CAGetAssignedPortNumber(CA_ADAPTER_IP, + static_cast(CA_IPV4|CA_SECURE))); + ASSERT_EQ(static_cast(0), + CAGetAssignedPortNumber(CA_ADAPTER_IP, + static_cast(CA_IPV6|CA_SECURE))); +#ifdef TCP_ADAPTER + ASSERT_EQ(static_cast(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4)); + ASSERT_EQ(static_cast(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV6)); +#endif } diff --git a/resource/csdk/connectivity/test/cablocktransfertest.cpp b/resource/csdk/connectivity/test/cablocktransfertest.cpp index 09e7550..77cb32f 100644 --- a/resource/csdk/connectivity/test/cablocktransfertest.cpp +++ b/resource/csdk/connectivity/test/cablocktransfertest.cpp @@ -81,7 +81,58 @@ TEST_F(CABlockTransferTests, CACreateNewDataSetTest) CAData_t *getData = CAGetDataSetFromBlockDataList(currData->blockDataId); EXPECT_TRUE(getData != NULL); - CARemoveBlockDataFromList(currData->blockDataId); + EXPECT_EQ(CA_STATUS_OK, CARemoveBlockDataFromList(currData->blockDataId)); + } + + CADestroyDataSet(cadata); + coap_delete_list(options); + coap_delete_pdu(pdu); + + CADestroyToken(tempToken); + CADestroyEndpoint(tempRep); + free(requestData.payload); +} + +TEST_F(CABlockTransferTests, CARemoveBlockDataFromListWithSeed) +{ + CAEndpoint_t* tempRep = NULL; + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); + + coap_pdu_t *pdu = NULL; + coap_list_t *options = NULL; + coap_transport_type transport = coap_udp; + + CAToken_t tempToken = NULL; + CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN); + + CAInfo_t requestData; + memset(&requestData, 0, sizeof(CAInfo_t)); + requestData.token = tempToken; + requestData.tokenLength = CA_MAX_TOKEN_LEN; + requestData.type = CA_MSG_NONCONFIRM; + + requestData.payload = (CAPayload_t) calloc(LARGE_PAYLOAD_LENGTH, sizeof(char)); + if (!requestData.payload) + { + CADestroyToken(tempToken); + FAIL() << "requestData.payload allocation failed"; + } + memset(requestData.payload, '1', sizeof(requestData.payload) - 1); + requestData.payloadSize = sizeof(requestData.payload); + requestData.type = CA_MSG_NONCONFIRM; + + pdu = CAGeneratePDU(CA_GET, &requestData, tempRep, &options, &transport); + + CAData_t *cadata = CACreateNewDataSet(pdu, tempRep); + EXPECT_TRUE(cadata != NULL); + + CABlockData_t *currData = CACreateNewBlockData(cadata); + EXPECT_TRUE(currData != NULL); + + if (currData) + { + EXPECT_EQ(CA_STATUS_OK, CARemoveBlockDataFromListWithSeed(tempToken, + CA_MAX_TOKEN_LEN, 5683)); } CADestroyDataSet(cadata); @@ -124,7 +175,7 @@ TEST_F(CABlockTransferTests, CAGetBlockDataFromBlockDataListTest) CABlockData_t *data = CAGetBlockDataFromBlockDataList(currData->blockDataId); EXPECT_TRUE(data != NULL); - CARemoveBlockDataFromList(currData->blockDataId); + EXPECT_EQ(CA_STATUS_OK, CARemoveBlockDataFromList(currData->blockDataId)); } CADestroyDataSet(cadata); @@ -462,7 +513,45 @@ TEST_F(CABlockTransferTests, CAGetBlockSizeOptionFromPduTest) free(requestData.payload); } -TEST_F(CABlockTransferTests, CASetNextBlockOption1Test) +TEST_F(CABlockTransferTests, CASetNextBlockOption1WithRequest) +{ + CAEndpoint_t* tempRep = NULL; + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); + + coap_pdu_t *pdu = NULL; + coap_list_t *options = NULL; + coap_transport_type transport = coap_udp; + + CAToken_t tempToken = NULL; + CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN); + + CAInfo_t requestData; + memset(&requestData, 0, sizeof(CAInfo_t)); + requestData.token = tempToken; + requestData.tokenLength = CA_MAX_TOKEN_LEN; + requestData.type = CA_MSG_NONCONFIRM; + + pdu = CAGeneratePDU(CA_GET, &requestData, tempRep, &options, &transport); + + CAData_t *cadata = CACreateNewDataSet(pdu, tempRep); + EXPECT_TRUE(cadata != NULL); + + CABlockData_t *currData = CACreateNewBlockData(cadata); + EXPECT_TRUE(currData != NULL); + + coap_block_t block = {0, 0, 0}; + EXPECT_EQ(CA_STATUS_OK, CASetNextBlockOption1(pdu, tempRep, cadata, block, pdu->length)); + + CADestroyDataSet(cadata); + coap_delete_list(options); + coap_delete_pdu(pdu); + + CADestroyToken(tempToken); + CADestroyEndpoint(tempRep); + free(requestData.payload); +} + +TEST_F(CABlockTransferTests, CASetNextBlockOption1WithResponse) { CAEndpoint_t* tempRep = NULL; CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); @@ -492,8 +581,6 @@ TEST_F(CABlockTransferTests, CASetNextBlockOption1Test) coap_block_t block = {0, 0, 0}; EXPECT_EQ(CA_STATUS_OK, CASetNextBlockOption1(pdu, tempRep, cadata, block, pdu->length)); - EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse()); - CADestroyDataSet(cadata); coap_delete_list(options); coap_delete_pdu(pdu); @@ -503,7 +590,7 @@ TEST_F(CABlockTransferTests, CASetNextBlockOption1Test) free(responseData.payload); } -TEST_F(CABlockTransferTests, CASetNextBlockOption2Test) +TEST_F(CABlockTransferTests, CASetNextBlockOption2WithRequest) { CAEndpoint_t* tempRep = NULL; CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); @@ -540,3 +627,111 @@ TEST_F(CABlockTransferTests, CASetNextBlockOption2Test) CADestroyEndpoint(tempRep); free(requestData.payload); } + +TEST_F(CABlockTransferTests, CASetNextBlockOption2WithResponse) +{ + CAEndpoint_t* tempRep = NULL; + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); + + coap_pdu_t *pdu = NULL; + coap_list_t *options = NULL; + coap_transport_type transport = coap_udp; + + CAToken_t tempToken = NULL; + CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN); + + CAInfo_t responseData; + memset(&responseData, 0, sizeof(CAInfo_t)); + responseData.token = tempToken; + responseData.tokenLength = CA_MAX_TOKEN_LEN; + responseData.type = CA_MSG_NONCONFIRM; + responseData.messageId = 1; + + pdu = CAGeneratePDU(CA_CREATED, &responseData, tempRep, &options, &transport); + + CAData_t *cadata = CACreateNewDataSet(pdu, tempRep); + EXPECT_TRUE(cadata != NULL); + + CABlockData_t *currData = CACreateNewBlockData(cadata); + EXPECT_TRUE(currData != NULL); + + coap_block_t block = {0, 0, 0}; + EXPECT_EQ(CA_STATUS_OK, CASetNextBlockOption2(pdu, tempRep, cadata, block, pdu->length)); + + CADestroyDataSet(cadata); + coap_delete_list(options); + coap_delete_pdu(pdu); + + CADestroyToken(tempToken); + CADestroyEndpoint(tempRep); + free(responseData.payload); +} + +TEST_F(CABlockTransferTests, CAUpdatePayloadToCADataWithRequest) +{ + CAEndpoint_t* tempRep = NULL; + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); + + CAToken_t tempToken = NULL; + CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN); + + CAInfo_t requestData; + requestData.type = CA_MSG_NONCONFIRM; + requestData.token = tempToken; + requestData.tokenLength = CA_MAX_TOKEN_LEN; + requestData.options = NULL; + requestData.payload = NULL; + requestData.payloadSize = 0; + + CARequestInfo_t requestInfo; + requestInfo.method = CA_GET; + requestInfo.info = requestData; + requestInfo.isMulticast = false; + + CAData_t cadata; + cadata.type = SEND_TYPE_UNICAST; + cadata.remoteEndpoint = tempRep; + cadata.requestInfo = &requestInfo; + cadata.dataType = CA_REQUEST_DATA; + + CAPayload_t payload = (CAPayload_t) "requestPayload"; + size_t payloadLen = strlen((const char*) payload) + 1; + + EXPECT_EQ(CA_STATUS_OK, CAUpdatePayloadToCAData(&cadata, payload, payloadLen)); + + EXPECT_STREQ((const char*) payload, (const char*) cadata.requestInfo->info.payload); +} + +TEST_F(CABlockTransferTests, CAUpdatePayloadToCADataWithResponse) +{ + CAEndpoint_t* tempRep = NULL; + CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep); + + CAToken_t tempToken = NULL; + CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN); + + CAInfo_t responseData; + responseData.type = CA_MSG_NONCONFIRM; + responseData.token = tempToken; + responseData.tokenLength = CA_MAX_TOKEN_LEN; + responseData.options = NULL; + responseData.payload = NULL; + responseData.payloadSize = 0; + + CAResponseInfo_t responseInfo; + responseInfo.result = CA_VALID; + responseInfo.info = responseData; + + CAData_t cadata; + cadata.type = SEND_TYPE_UNICAST; + cadata.remoteEndpoint = tempRep; + cadata.responseInfo = &responseInfo; + cadata.dataType = CA_RESPONSE_DATA; + + CAPayload_t payload = (CAPayload_t) "responsePayload"; + size_t payloadLen = strlen((const char*) payload) + 1; + + EXPECT_EQ(CA_STATUS_OK, CAUpdatePayloadToCAData(&cadata, payload, payloadLen)); + + EXPECT_STREQ((const char*) payload, (const char*) cadata.responseInfo->info.payload); +} diff --git a/resource/csdk/connectivity/test/caprotocolmessagetest.cpp b/resource/csdk/connectivity/test/caprotocolmessagetest.cpp index 4791ce0..96fb6fa 100644 --- a/resource/csdk/connectivity/test/caprotocolmessagetest.cpp +++ b/resource/csdk/connectivity/test/caprotocolmessagetest.cpp @@ -186,3 +186,30 @@ TEST(CAProtocolMessage, CAParseURILongPath) verifyParsedOptions(cases, numCases, optlist); coap_delete_list(optlist); } + +TEST(CAProtocolMessage, CAGetTokenFromPDU) +{ + CAEndpoint_t tempRep; + memset(&tempRep, 0, sizeof(CAEndpoint_t)); + tempRep.flags = CA_DEFAULT_FLAGS; + tempRep.adapter = CA_ADAPTER_IP; + tempRep.port = 5683; + + coap_pdu_t *pdu = NULL; + coap_list_t *options = NULL; + coap_transport_type transport = coap_udp; + + CAInfo_t inData; + memset(&inData, 0, sizeof(CAInfo_t)); + inData.token = "token"; + inData.tokenLength = strlen(inData.token); + inData.type = CA_MSG_NONCONFIRM; + + pdu = CAGeneratePDU(CA_GET, &inData, &tempRep, &options, &transport); + + CAInfo_t outData; + memset(&outData, 0, sizeof(CAInfo_t)); + outData.type = CA_MSG_NONCONFIRM; + + EXPECT_EQ(CA_STATUS_OK, CAGetTokenFromPDU(pdu->hdr, &outData, &tempRep)); +} diff --git a/resource/csdk/connectivity/test/uqueue_test.cpp b/resource/csdk/connectivity/test/uqueue_test.cpp new file mode 100644 index 0000000..c393cd3 --- /dev/null +++ b/resource/csdk/connectivity/test/uqueue_test.cpp @@ -0,0 +1,192 @@ +//****************************************************************** +// +// Copyright 2016 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "gtest/gtest.h" + +#include "uqueue.h" + +class UQueueF : public testing::Test { +public: + UQueueF() : + testing::Test(), + queue(NULL) + { + } + +protected: + virtual void SetUp() + { + queue = u_queue_create(); + ASSERT_TRUE(queue != NULL); + } + + virtual void TearDown() + { + EXPECT_EQ(CA_STATUS_OK, u_queue_delete(queue)); + } + + u_queue_t *queue; +}; + +u_queue_message_t *CreateQueueMessage(void *data, uint32_t size) +{ + u_queue_message_t *message = (u_queue_message_t *) malloc(sizeof(u_queue_message_t)); + if (NULL == message) + { + return NULL; + } + + message->msg = data; + message->size = size; + + return message; +} + +TEST(UQueue, Base) +{ + u_queue_t *queue = u_queue_create(); + ASSERT_TRUE(queue != NULL); + + EXPECT_EQ(CA_STATUS_OK, u_queue_delete(queue)); +} + +TEST(UQueue, CreateMany) +{ + for (int i = 0; i < 100; ++i) + { + u_queue_t *queue = u_queue_create(); + ASSERT_TRUE(queue != NULL); + + EXPECT_EQ(CA_STATUS_OK, u_queue_delete(queue)); + } +} + +TEST(UQueue, FreeNull) +{ + EXPECT_EQ(CA_STATUS_FAILED, u_queue_delete(NULL)); +} + +TEST_F(UQueueF, Length) +{ + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); + + int dummy = 0; + u_queue_message_t *message = CreateQueueMessage(&dummy, sizeof(dummy)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + + ASSERT_EQ(static_cast(1), u_queue_get_size(queue)); + + // Add a few times without checking, just in case checking has side-effects + dummy++; + message = CreateQueueMessage(&dummy, sizeof(dummy)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + + dummy++; + message = CreateQueueMessage(&dummy, sizeof(dummy)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + + dummy++; + message = CreateQueueMessage(&dummy, sizeof(dummy)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + + ASSERT_EQ(static_cast(4), u_queue_get_size(queue)); +} + +TEST_F(UQueueF, LengthMulti) +{ + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); + + for (int i = 0; i < 1000; ++i) + { + u_queue_message_t *message = CreateQueueMessage(&i, sizeof(i)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + } + + ASSERT_EQ(static_cast(1000), u_queue_get_size(queue)); +} + +TEST_F(UQueueF, GetHead) +{ + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + u_queue_message_t *message = CreateQueueMessage(&i, sizeof(i)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + } + ASSERT_EQ(static_cast(1000), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + u_queue_message_t *value = u_queue_get_head(queue); + ASSERT_TRUE(value != NULL); + } +} + +TEST_F(UQueueF, Get) +{ + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + u_queue_message_t *message = CreateQueueMessage(&i, sizeof(i)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + } + ASSERT_EQ(static_cast(1000), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + u_queue_message_t *value = u_queue_get_element(queue); + ASSERT_TRUE(value != NULL); + } +} + +TEST_F(UQueueF, Remove) +{ + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + u_queue_message_t *message = CreateQueueMessage(&i, sizeof(i)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + } + ASSERT_EQ(static_cast(1000), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + EXPECT_EQ(CA_STATUS_OK, u_queue_remove_element(queue)); + } +} + +TEST_F(UQueueF, Reset) +{ + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); + + for (size_t i = 0; i < 1000; ++i) + { + u_queue_message_t *message = CreateQueueMessage(&i, sizeof(i)); + EXPECT_EQ(CA_STATUS_OK, u_queue_add_element(queue, message)); + } + ASSERT_EQ(static_cast(1000), u_queue_get_size(queue)); + + EXPECT_EQ(CA_STATUS_OK, u_queue_reset(queue)); + + ASSERT_EQ(static_cast(0), u_queue_get_size(queue)); +} diff --git a/resource/csdk/connectivity/util/src/cautilinterface.c b/resource/csdk/connectivity/util/src/cautilinterface.c index 5242a5c..8d64dbe 100644 --- a/resource/csdk/connectivity/util/src/cautilinterface.c +++ b/resource/csdk/connectivity/util/src/cautilinterface.c @@ -27,66 +27,12 @@ #define TAG "OIC_CA_COMMON_UTILS" -static CAAdapterStateChangedCB g_adapterStateCB = NULL; -static CAConnectionStateChangedCB g_connStateCB = NULL; - -static void CAManagerAdapterMonitorHandler(CATransportAdapter_t adapter, - CANetworkStatus_t status) -{ - if (CA_INTERFACE_DOWN == status) - { - if (g_adapterStateCB) - { - g_adapterStateCB(adapter, false); - OIC_LOG(DEBUG, TAG, "Pass the disabled adapter state to upper layer"); - } - } - else if (CA_INTERFACE_UP == status) - { - if (g_adapterStateCB) - { - g_adapterStateCB(adapter, true); - OIC_LOG(DEBUG, TAG, "Pass the enabled adapter state to upper layer"); - } - } -} - -static void CAManagerConnectionMonitorHandler(const CAEndpoint_t *info, bool isConnected) -{ - if (!info || !info->addr[0]) - { - OIC_LOG(ERROR, TAG, "remoteAddress is NULL"); - return; - } - - if (isConnected) - { - if (g_connStateCB) - { - g_connStateCB(info, isConnected); - OIC_LOG(DEBUG, TAG, "Pass the connected device info to upper layer"); - } - } - else - { - if (g_connStateCB) - { - g_connStateCB(info, isConnected); - OIC_LOG(DEBUG, TAG, "Pass the disconnected device info to upper layer"); - } - } -} - CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB, CAConnectionStateChangedCB connStateCB) { OIC_LOG(DEBUG, TAG, "CARegisterNetworkMonitorHandler"); - g_adapterStateCB = adapterStateCB; - g_connStateCB = connStateCB; - - CASetNetworkMonitorCallbacks(CAManagerAdapterMonitorHandler, - CAManagerConnectionMonitorHandler); + CASetNetworkMonitorCallbacks(adapterStateCB, connStateCB); return CA_STATUS_OK; } -- 2.7.4