1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "platform_features.h"
22 #include "gtest/gtest.h"
23 #include "cainterface.h"
24 #include "cautilinterface.h"
26 #include "oic_string.h"
27 #include "oic_malloc.h"
29 #define CA_TRANSPORT_ADAPTER_SCOPE 1000
31 class CATests : public testing::Test {
35 CAInitialize(CA_ADAPTER_IP);
38 virtual void TearDown()
44 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
45 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
46 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
47 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
48 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
49 CAResult_t checkSelectNetwork();
51 void request_handler(const CAEndpoint_t * /*object*/,
52 const CARequestInfo_t * /*requestInfo*/)
57 void response_handler(const CAEndpoint_t * /*object*/,
58 const CAResponseInfo_t * /*responseInfo*/)
63 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
65 if(!object || !errorInfo)
70 //error handling shall be added
74 void adapter_handler(CATransportAdapter_t /*adapter*/,
79 void connection_handler(const CAEndpoint_t * /*endpoint*/,
84 static char* addr = NULL;
85 static CAEndpoint_t* tempRep = NULL;
86 static CARequestInfo_t requestInfo;
87 static CAInfo_t requestData;
88 static CAInfo_t responseData;
89 static CAResponseInfo_t responseInfo;
90 static CAToken_t tempToken = NULL;
91 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
92 static const char ADDRESS[] = "10.11.12.13";
93 static const uint16_t PORT = 4545;
95 static const char NORMAL_INFO_DATA[] =
96 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
97 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
101 // Iotivity Device Identity.
102 const unsigned char IDENTITY[] = ("1111111111111111");
104 // PSK between this device and peer device.
105 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
107 // Internal API. Invoked by CA stack to retrieve credentials from this module
108 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
109 const unsigned char *desc, size_t desc_len,
110 unsigned char *result, size_t result_length)
112 printf("CAGetDtlsPskCredentials IN\n");
123 case CA_DTLS_PSK_HINT:
124 case CA_DTLS_PSK_IDENTITY:
126 if (result_length < sizeof(IDENTITY))
128 printf("ERROR : Wrong value for result for storing IDENTITY");
132 memcpy(result, IDENTITY, sizeof(IDENTITY));
133 ret = sizeof(IDENTITY);
136 case CA_DTLS_PSK_KEY:
138 if ((desc_len == sizeof(IDENTITY)) &&
139 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
141 if (result_length < sizeof(RS_CLIENT_PSK))
143 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
147 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
148 ret = sizeof(RS_CLIENT_PSK);
154 printf("Wrong value passed for PSK_CRED_TYPE.");
159 printf("CAGetDtlsPskCredentials OUT\n");
162 #endif //__WITH_DTLS__
165 TEST(InitializeTest, CAInitializeTest)
167 EXPECT_EQ(CA_STATUS_OK, CAInitialize(CA_ADAPTER_IP));
172 TEST_F(CATests, TerminateTest)
176 char* check = (char *) "terminate success";
177 EXPECT_STREQ(check, "terminate success");
179 CAInitialize(CA_ADAPTER_IP);
182 // CAStartListeningServer TC
183 TEST_F(CATests, StartListeningServerTestWithNonSelect)
185 EXPECT_EQ(CA_STATUS_FAILED, CAStartListeningServer());
188 // CAStartListeningServer TC
189 TEST_F(CATests, StartListeningServerTest)
191 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
192 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
195 // CAStopListeningServer TC
196 TEST_F(CATests, CAStopListeningServerTestWithNonSelect)
198 EXPECT_EQ(CA_STATUS_FAILED, CAStopListeningServer());
201 // CAStopListeningServer TC
202 TEST_F(CATests, CAStopListeningServerTest)
204 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
205 EXPECT_EQ(CA_STATUS_OK, CAStopListeningServer());
208 // CARegisterHandlerTest TC
209 TEST_F(CATests, RegisterHandlerTest)
211 CARegisterHandler(request_handler, response_handler, error_handler);
212 char* check = (char *) "registerHandler success";
213 EXPECT_STREQ(check, "registerHandler success");
216 // CACreateRemoteEndpoint TC
217 TEST_F(CATests, CreateRemoteEndpointTestGood)
219 addr = (char *) ADDRESS;
221 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
224 CADestroyEndpoint(tempRep);
228 // check remoteEndpoint and values of remoteEndpoint
229 TEST_F(CATests, CreateRemoteEndpointTestValues)
231 addr = (char *) ADDRESS;
233 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
235 EXPECT_TRUE(tempRep != NULL);
237 CADestroyEndpoint(tempRep);
242 TEST_F(CATests, GenerateTokenTestGood)
244 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
246 CADestroyToken(tempToken);
249 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
250 TEST_F(CATests, GenerateTokenTestBad)
252 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
256 // check destroyed token
257 TEST_F(CATests, DestroyTokenTest)
259 CAGenerateToken(&tempToken, tokenLength);
260 CADestroyToken(tempToken);
262 char * check = (char *) "destroy success";
263 EXPECT_STREQ(check, "destroy success");
266 TEST_F(CATests, SendRequestTestWithInvalidAddress)
268 CARegisterHandler(request_handler, response_handler, error_handler);
270 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
271 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "10.11.13.13.14", PORT, &tempRep);
273 memset(&requestData, 0, sizeof(CAInfo_t));
274 CAGenerateToken(&tempToken, tokenLength);
275 requestData.token = tempToken;
276 requestData.tokenLength = tokenLength;
277 requestData.type = CA_MSG_CONFIRM;
279 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
280 requestInfo.method = CA_GET;
281 requestInfo.info = requestData;
283 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
285 CADestroyToken(tempToken);
286 CADestroyEndpoint(tempRep);
287 free(requestData.payload);
292 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
294 addr = (char *) ADDRESS;
295 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
297 memset(&requestData, 0, sizeof(CAInfo_t));
298 CAGenerateToken(&tempToken, tokenLength);
299 requestData.token = tempToken;
300 requestData.tokenLength = tokenLength;
302 size_t length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
303 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
304 if(!requestData.payload)
306 CADestroyToken(tempToken);
307 FAIL() << "requestData.payload allocation failed";
309 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
310 requestData.payloadSize = length + 1;
311 requestData.type = CA_MSG_NONCONFIRM;
313 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
314 requestInfo.method = CA_GET;
315 requestInfo.info = requestData;
317 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
319 CADestroyToken(tempToken);
320 CADestroyEndpoint(tempRep);
321 free(requestData.payload);
325 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
326 TEST_F(CATests, SendRequestTestWithNullAddr)
328 addr = (char *) ADDRESS;
329 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
331 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
333 CADestroyEndpoint(tempRep);
337 TEST_F(CATests, SendResponseTestWithInvalidCode)
339 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
341 addr = (char *) ADDRESS;
342 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
344 memset(&responseData, 0, sizeof(CAInfo_t));
345 responseData.type = CA_MSG_RESET;
346 responseData.messageId = 1;
347 responseData.payload = (CAPayload_t)OICMalloc(sizeof("response payload"));
348 responseData.dataType = CA_RESPONSE_DATA;
350 EXPECT_TRUE(responseData.payload != NULL);
352 if (responseData.payload)
354 CAGenerateToken(&tempToken, tokenLength);
355 requestData.token = tempToken;
356 requestData.tokenLength = tokenLength;
358 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
359 responseInfo.result = CA_CONTENT;
360 responseInfo.info = responseData;
362 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
364 CADestroyToken(tempToken);
365 CADestroyEndpoint(tempRep);
366 OICFree(responseData.payload);
372 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
374 addr = (char *) ADDRESS;
375 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
377 memset(&responseData, 0, sizeof(CAInfo_t));
378 responseData.type = CA_MSG_NONCONFIRM;
379 responseData.messageId = 1;
380 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
381 responseData.dataType = CA_RESPONSE_DATA;
383 EXPECT_TRUE(responseData.payload != NULL);
384 if(!responseData.payload)
386 CADestroyEndpoint(tempRep);
390 memcpy(responseData.payload, "response payload", sizeof("response payload"));
391 responseData.payloadSize = sizeof("response payload");
393 CAGenerateToken(&tempToken, tokenLength);
394 requestData.token = tempToken;
395 requestData.tokenLength = tokenLength;
397 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
398 responseInfo.result = CA_CONTENT;
399 responseInfo.info = responseData;
401 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
403 CADestroyToken(tempToken);
404 CADestroyEndpoint(tempRep);
405 free(responseData.payload);
409 // check return value when address is NULL as multicast
410 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
413 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
415 memset(&responseData, 0, sizeof(CAInfo_t));
416 responseData.type = CA_MSG_NONCONFIRM;
417 responseData.messageId = 1;
418 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
419 responseData.dataType = CA_RESPONSE_DATA;
420 EXPECT_TRUE(responseData.payload != NULL);
422 if(!responseData.payload)
424 CADestroyEndpoint(tempRep);
428 memcpy(responseData.payload, "response payload", sizeof("response payload"));
429 responseData.payloadSize = sizeof("response payload");
431 CAGenerateToken(&tempToken, tokenLength);
432 requestData.token = tempToken;
433 requestData.tokenLength = tokenLength;
435 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
436 responseInfo.result = CA_CONTENT;
437 responseInfo.info = responseData;
439 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
441 CADestroyToken(tempToken);
442 CADestroyEndpoint(tempRep);
443 free (responseData.payload);
447 // check return value NULL is passed instead of a valid CAResponseInfo_t address
448 TEST_F(CATests, SendResponseTest)
450 addr = (char *) ADDRESS;
451 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
453 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
455 CADestroyEndpoint(tempRep);
459 // CASelectNewwork TC
460 TEST_F(CATests, SelectNetworkTestGood)
462 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
465 CAResult_t checkSelectNetwork()
467 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
469 if (CA_STATUS_OK == res)
471 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
474 if (CA_NOT_SUPPORTED == res)
476 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
483 // check return value when selected network is disable
484 TEST_F(CATests, SelectNetworkTestBad)
486 //Select disable network
487 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
488 CA_TRANSPORT_ADAPTER_SCOPE));
491 // check return value when selected network is disable
492 TEST_F(CATests, UnSelectNetworkTest)
494 //UnSelect disable network
495 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
496 CA_TRANSPORT_ADAPTER_SCOPE));
499 // CAHandlerRequestResponse TC
500 TEST_F(CATests, HandlerRequestResponseTest)
502 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
505 // CAGetNetworkInformation TC
506 TEST_F (CATests, GetNetworkInformationTest)
508 uint32_t tempSize = 0;
509 CAEndpoint_t *tempInfo = NULL;
511 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
512 EXPECT_EQ(CA_STATUS_OK, CAGetNetworkInformation(&tempInfo, &tempSize));
514 // @todo: if this api is supported on windows platform, it should be changed.
516 for (uint32_t index = 0; index < tempSize; index++)
518 EXPECT_TRUE(tempInfo[index].adapter != 0);
519 EXPECT_TRUE(strlen(tempInfo[index].addr) != 0);
526 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
529 EXPECT_EQ(CA_STATUS_OK, CAregisterPskCredentialsHandler(CAGetDtlsPskCredentials));
533 // CARegisterNetworkMonitorHandler TC
534 TEST_F(CATests, RegisterNetworkMonitorHandler)
536 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
537 connection_handler));
540 // CASetAutoConnectionDeviceInfo TC
541 TEST_F(CATests, SetAutoConnectionDeviceInfo)
543 addr = (char *) ADDRESS;
545 #if defined(__ANDROID__) && defined(LE_ADAPTER)
546 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
548 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
552 // CAUnsetAutoConnectionDeviceInfo TC
553 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
555 addr = (char *) ADDRESS;
557 #if defined(__ANDROID__) && defined(LE_ADAPTER)
558 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
560 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
564 TEST(CASetPortNumberTest, CASetPortNumberToAssign)
566 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV4, 5683));
567 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV6, 5683));
568 EXPECT_EQ(CA_STATUS_OK,
569 CASetPortNumberToAssign(CA_ADAPTER_IP,
570 static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE), 5683));
571 EXPECT_EQ(CA_STATUS_OK,
572 CASetPortNumberToAssign(CA_ADAPTER_IP,
573 static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE), 5683));
576 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV4, 5683));
577 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV6, 5683));
581 TEST(CAGetPortNumberTest, CAGetPortNumberToAssign)
583 ASSERT_EQ(static_cast<uint16_t>(0),
584 CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV4));
585 ASSERT_EQ(static_cast<uint16_t>(0),
586 CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV6));
587 ASSERT_EQ(static_cast<uint16_t>(0),
588 CAGetAssignedPortNumber(CA_ADAPTER_IP,
589 static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE)));
590 ASSERT_EQ(static_cast<uint16_t>(0),
591 CAGetAssignedPortNumber(CA_ADAPTER_IP,
592 static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE)));
594 ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4));
595 ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV6));