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!",
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)
{
//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);
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;
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);
if (!temp)
{
OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
- CADestroyInfoInternal(clone);
- return CA_MEMORY_ALLOC_FAILED;
+ goto exit;
}
// save the resourceUri
return CA_STATUS_OK;
+exit:
+ CADestroyInfoInternal(clone);
+ return CA_MEMORY_ALLOC_FAILED;
}
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 ])
if (!ep)
{
OIC_LOG(ERROR, TAG, "endpoint clone failed");
- OICFree(cadata);
- return NULL;
+ goto exit;
}
OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
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;
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;
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;
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)
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;
}
if (!ep)
{
OIC_LOG(ERROR, TAG, "endpoint clone failed");
- CADestroyData(cadata, sizeof(CAData_t));
- return NULL;
+ goto exit;
}
cadata->remoteEndpoint = ep;
return cadata;
exit:
- OICFree(cadata);
+ CADestroyData(cadata, sizeof(CAData_t));
return NULL;
}
}
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);
#include "caremotehandler.h"
#include "caprotocolmessage.h"
#include "oic_malloc.h"
+#include "oic_time.h"
#include "ocrandom.h"
#include "logger.h"
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
*/
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)
continue;
}
- uint64_t currentTime = getCurrentTimeInMicroSeconds();
+ uint64_t currentTime = OICGetCurrentTime(TIME_IN_US);
if (CACheckTimeout(currentTime, retData))
{
}
// #2. add additional information. (time stamp, retransmission count...)
- retData->timeStamp = getCurrentTimeInMicroSeconds();
+ retData->timeStamp = OICGetCurrentTime(TIME_IN_US);
#ifndef SINGLE_THREAD
retData->timeout = CAGetTimeoutValue();
#endif
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;
-}
'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
'ca_api_unittest.cpp',
'camutex_tests.cpp',
'uarraylist_test.cpp',
- 'ulinklist_test.cpp'
+ 'ulinklist_test.cpp',
+ 'uqueue_test.cpp'
])
Alias("test", [catests])
#include "cainterface.h"
#include "cautilinterface.h"
#include "cacommon.h"
+#include "oic_string.h"
#define CA_TRANSPORT_ADAPTER_SCOPE 1000
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*/,
#endif //__WITH_DTLS__
// CAInitialize TC
-// check return value
-TEST(InitializeTest, TC_01_Positive_01)
+TEST(InitializeTest, CAInitializeTest)
{
EXPECT_EQ(CA_STATUS_OK, CAInitialize());
CATerminate();
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);
}
// CACreateRemoteEndpoint TC
-// check return value
TEST_F(CATests, CreateRemoteEndpointTestGood)
{
addr = (char *) ADDRESS;
}
// CAGerateToken TC
-// check return value
TEST_F(CATests, GenerateTokenTestGood)
{
EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
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;
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;
}
// CASelectNewwork TC
-// check return value
TEST_F(CATests, SelectNetworkTestGood)
{
EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
}
// 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)
}
// CARegisterNetworkMonitorHandler TC
-// check return value
TEST_F(CATests, RegisterNetworkMonitorHandler)
{
EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
}
// CASetAutoConnectionDeviceInfo TC
-// check return value
TEST_F(CATests, SetAutoConnectionDeviceInfo)
{
addr = (char *) ADDRESS;
}
// CAUnsetAutoConnectionDeviceInfo TC
-// check return value
TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
{
addr = (char *) ADDRESS;
#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<CATransportFlags_t>(CA_IPV4|CA_SECURE), 5683));
+ EXPECT_EQ(CA_STATUS_OK,
+ CASetPortNumberToAssign(CA_ADAPTER_IP,
+ static_cast<CATransportFlags_t>(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<uint16_t>(0),
+ CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV4));
+ ASSERT_EQ(static_cast<uint16_t>(0),
+ CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV6));
+ ASSERT_EQ(static_cast<uint16_t>(0),
+ CAGetAssignedPortNumber(CA_ADAPTER_IP,
+ static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE)));
+ ASSERT_EQ(static_cast<uint16_t>(0),
+ CAGetAssignedPortNumber(CA_ADAPTER_IP,
+ static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE)));
+#ifdef TCP_ADAPTER
+ ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4));
+ ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV6));
+#endif
}
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);
CABlockData_t *data = CAGetBlockDataFromBlockDataList(currData->blockDataId);
EXPECT_TRUE(data != NULL);
- CARemoveBlockDataFromList(currData->blockDataId);
+ EXPECT_EQ(CA_STATUS_OK, CARemoveBlockDataFromList(currData->blockDataId));
}
CADestroyDataSet(cadata);
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);
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);
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);
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);
+}
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));
+}
--- /dev/null
+//******************************************************************
+//
+// 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<uint32_t>(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<uint32_t>(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<uint32_t>(4), u_queue_get_size(queue));
+}
+
+TEST_F(UQueueF, LengthMulti)
+{
+ ASSERT_EQ(static_cast<uint32_t>(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<uint32_t>(1000), u_queue_get_size(queue));
+}
+
+TEST_F(UQueueF, GetHead)
+{
+ ASSERT_EQ(static_cast<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(1000), u_queue_get_size(queue));
+
+ EXPECT_EQ(CA_STATUS_OK, u_queue_reset(queue));
+
+ ASSERT_EQ(static_cast<uint32_t>(0), u_queue_get_size(queue));
+}
#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;
}