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"
28 #define CA_TRANSPORT_ADAPTER_SCOPE 1000
30 class CATests : public testing::Test {
37 virtual void TearDown()
43 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
44 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
45 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
46 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
47 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
48 CAResult_t checkSelectNetwork();
50 void request_handler(const CAEndpoint_t * /*object*/,
51 const CARequestInfo_t * /*requestInfo*/)
56 void response_handler(const CAEndpoint_t * /*object*/,
57 const CAResponseInfo_t * /*responseInfo*/)
62 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
64 if(!object || !errorInfo)
69 //error handling shall be added
73 void adapter_handler(CATransportAdapter_t /*adapter*/,
78 void connection_handler(const CAEndpoint_t * /*endpoint*/,
83 static char* addr = NULL;
84 static CAEndpoint_t* tempRep = NULL;
85 static CARequestInfo_t requestInfo;
86 static CAInfo_t requestData;
87 static CAInfo_t responseData;
88 static CAResponseInfo_t responseInfo;
89 static CAToken_t tempToken = NULL;
90 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
91 static const char ADDRESS[] = "10.11.12.13";
92 static const uint16_t PORT = 4545;
94 static const char NORMAL_INFO_DATA[] =
95 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
96 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
100 // Iotivity Device Identity.
101 const unsigned char IDENTITY[] = ("1111111111111111");
103 // PSK between this device and peer device.
104 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
106 // Internal API. Invoked by CA stack to retrieve credentials from this module
107 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
108 const unsigned char *desc, size_t desc_len,
109 unsigned char *result, size_t result_length)
111 printf("CAGetDtlsPskCredentials IN\n");
122 case CA_DTLS_PSK_HINT:
123 case CA_DTLS_PSK_IDENTITY:
125 if (result_length < sizeof(IDENTITY))
127 printf("ERROR : Wrong value for result for storing IDENTITY");
131 memcpy(result, IDENTITY, sizeof(IDENTITY));
132 ret = sizeof(IDENTITY);
135 case CA_DTLS_PSK_KEY:
137 if ((desc_len == sizeof(IDENTITY)) &&
138 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
140 if (result_length < sizeof(RS_CLIENT_PSK))
142 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
146 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
147 ret = sizeof(RS_CLIENT_PSK);
153 printf("Wrong value passed for PSK_CRED_TYPE.");
158 printf("CAGetDtlsPskCredentials OUT\n");
161 #endif //__WITH_DTLS__
164 TEST(InitializeTest, CAInitializeTest)
166 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
171 TEST_F(CATests, TerminateTest)
175 char* check = (char *) "terminate success";
176 EXPECT_STREQ(check, "terminate success");
181 // CAStartListeningServer TC
182 TEST_F(CATests, StartListeningServerTestWithNonSelect)
184 EXPECT_EQ(CA_STATUS_FAILED, CAStartListeningServer());
187 // CAStartListeningServer TC
188 TEST_F(CATests, StartListeningServerTest)
190 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
191 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
194 // CAStopListeningServer TC
195 TEST_F(CATests, CAStopListeningServerTestWithNonSelect)
197 EXPECT_EQ(CA_STATUS_FAILED, CAStopListeningServer());
200 // CAStopListeningServer TC
201 TEST_F(CATests, CAStopListeningServerTest)
203 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
204 EXPECT_EQ(CA_STATUS_OK, CAStopListeningServer());
207 // CARegisterHandlerTest TC
208 TEST_F(CATests, RegisterHandlerTest)
210 CARegisterHandler(request_handler, response_handler, error_handler);
211 char* check = (char *) "registerHandler success";
212 EXPECT_STREQ(check, "registerHandler success");
215 // CACreateRemoteEndpoint TC
216 TEST_F(CATests, CreateRemoteEndpointTestGood)
218 addr = (char *) ADDRESS;
220 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
223 CADestroyEndpoint(tempRep);
227 // check remoteEndpoint and values of remoteEndpoint
228 TEST_F(CATests, CreateRemoteEndpointTestValues)
230 addr = (char *) ADDRESS;
232 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
234 EXPECT_TRUE(tempRep != NULL);
236 CADestroyEndpoint(tempRep);
241 TEST_F(CATests, GenerateTokenTestGood)
243 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
245 CADestroyToken(tempToken);
248 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
249 TEST_F(CATests, GenerateTokenTestBad)
251 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
255 // check destroyed token
256 TEST_F(CATests, DestroyTokenTest)
258 CAGenerateToken(&tempToken, tokenLength);
259 CADestroyToken(tempToken);
261 char * check = (char *) "destroy success";
262 EXPECT_STREQ(check, "destroy success");
265 TEST_F(CATests, SendRequestTestWithInvalidAddress)
267 CARegisterHandler(request_handler, response_handler, error_handler);
269 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
270 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "10.11.13.13.14", PORT, &tempRep);
272 memset(&requestData, 0, sizeof(CAInfo_t));
273 CAGenerateToken(&tempToken, tokenLength);
274 requestData.token = tempToken;
275 requestData.tokenLength = tokenLength;
276 requestData.type = CA_MSG_CONFIRM;
278 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
279 requestInfo.method = CA_GET;
280 requestInfo.info = requestData;
282 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
284 CADestroyToken(tempToken);
285 CADestroyEndpoint(tempRep);
286 free(requestData.payload);
291 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
293 addr = (char *) ADDRESS;
294 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
296 memset(&requestData, 0, sizeof(CAInfo_t));
297 CAGenerateToken(&tempToken, tokenLength);
298 requestData.token = tempToken;
299 requestData.tokenLength = tokenLength;
301 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
302 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
303 if(!requestData.payload)
305 CADestroyToken(tempToken);
306 FAIL() << "requestData.payload allocation failed";
308 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
309 requestData.payloadSize = length + 1;
310 requestData.type = CA_MSG_NONCONFIRM;
312 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
313 requestInfo.method = CA_GET;
314 requestInfo.info = requestData;
316 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
318 CADestroyToken(tempToken);
319 CADestroyEndpoint(tempRep);
320 free(requestData.payload);
324 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
325 TEST_F(CATests, SendRequestTestWithNullAddr)
327 addr = (char *) ADDRESS;
328 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
330 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
332 CADestroyEndpoint(tempRep);
336 TEST_F(CATests, SendResponseTestWithInvalidCode)
338 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
340 addr = (char *) ADDRESS;
341 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
343 memset(&responseData, 0, sizeof(CAInfo_t));
344 responseData.type = CA_MSG_RESET;
345 responseData.messageId = 1;
346 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
347 responseData.dataType = CA_RESPONSE_DATA;
349 EXPECT_TRUE(responseData.payload != NULL);
351 if (responseData.payload)
353 CAGenerateToken(&tempToken, tokenLength);
354 requestData.token = tempToken;
355 requestData.tokenLength = tokenLength;
357 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
358 responseInfo.result = CA_CONTENT;
359 responseInfo.info = responseData;
361 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
363 CADestroyToken(tempToken);
364 CADestroyEndpoint(tempRep);
365 free(responseData.payload);
371 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
373 addr = (char *) ADDRESS;
374 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
376 memset(&responseData, 0, sizeof(CAInfo_t));
377 responseData.type = CA_MSG_NONCONFIRM;
378 responseData.messageId = 1;
379 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
380 responseData.dataType = CA_RESPONSE_DATA;
382 EXPECT_TRUE(responseData.payload != NULL);
383 if(!responseData.payload)
385 CADestroyEndpoint(tempRep);
389 memcpy(responseData.payload, "response payload", sizeof("response payload"));
390 responseData.payloadSize = sizeof("response payload");
392 CAGenerateToken(&tempToken, tokenLength);
393 requestData.token = tempToken;
394 requestData.tokenLength = tokenLength;
396 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
397 responseInfo.result = CA_CONTENT;
398 responseInfo.info = responseData;
400 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
402 CADestroyToken(tempToken);
403 CADestroyEndpoint(tempRep);
404 free(responseData.payload);
408 // check return value when address is NULL as multicast
409 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
412 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
414 memset(&responseData, 0, sizeof(CAInfo_t));
415 responseData.type = CA_MSG_NONCONFIRM;
416 responseData.messageId = 1;
417 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
418 responseData.dataType = CA_RESPONSE_DATA;
419 EXPECT_TRUE(responseData.payload != NULL);
421 if(!responseData.payload)
423 CADestroyEndpoint(tempRep);
427 memcpy(responseData.payload, "response payload", sizeof("response payload"));
428 responseData.payloadSize = sizeof("response payload");
430 CAGenerateToken(&tempToken, tokenLength);
431 requestData.token = tempToken;
432 requestData.tokenLength = tokenLength;
434 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
435 responseInfo.result = CA_CONTENT;
436 responseInfo.info = responseData;
438 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
440 CADestroyToken(tempToken);
441 CADestroyEndpoint(tempRep);
442 free (responseData.payload);
446 // check return value NULL is passed instead of a valid CAResponseInfo_t address
447 TEST_F(CATests, SendResponseTest)
449 addr = (char *) ADDRESS;
450 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
452 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
454 CADestroyEndpoint(tempRep);
458 // CASelectNewwork TC
459 TEST_F(CATests, SelectNetworkTestGood)
461 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
464 CAResult_t checkSelectNetwork()
466 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
468 if (CA_STATUS_OK == res)
470 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
473 if (CA_NOT_SUPPORTED == res)
475 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
482 // check return value when selected network is disable
483 TEST_F(CATests, SelectNetworkTestBad)
485 //Select disable network
486 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
487 CA_TRANSPORT_ADAPTER_SCOPE));
490 // check return value when selected network is disable
491 TEST_F(CATests, UnSelectNetworkTest)
493 //UnSelect disable network
494 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
495 CA_TRANSPORT_ADAPTER_SCOPE));
498 // CAHandlerRequestResponse TC
499 TEST_F(CATests, HandlerRequestResponseTest)
501 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
504 // CAGetNetworkInformation TC
505 TEST_F (CATests, GetNetworkInformationTest)
507 uint32_t tempSize = 0;
508 CAEndpoint_t *tempInfo = NULL;
510 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
511 EXPECT_EQ(CA_STATUS_OK, CAGetNetworkInformation(&tempInfo, &tempSize));
513 // @todo: if this api is supported on windows platform, it should be changed.
515 for (uint32_t index = 0; index < tempSize; index++)
517 EXPECT_TRUE(tempInfo[index].adapter != 0);
518 EXPECT_TRUE(strlen(tempInfo[index].addr) != 0);
525 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
528 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
532 // CARegisterNetworkMonitorHandler TC
533 TEST_F(CATests, RegisterNetworkMonitorHandler)
535 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
536 connection_handler));
539 // CASetAutoConnectionDeviceInfo TC
540 TEST_F(CATests, SetAutoConnectionDeviceInfo)
542 addr = (char *) ADDRESS;
544 #if defined(__ANDROID__) && defined(LE_ADAPTER)
545 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
547 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
551 // CAUnsetAutoConnectionDeviceInfo TC
552 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
554 addr = (char *) ADDRESS;
556 #if defined(__ANDROID__) && defined(LE_ADAPTER)
557 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
559 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
563 TEST(CASetPortNumberTest, CASetPortNumberToAssign)
565 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV4, 5683));
566 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV6, 5683));
567 EXPECT_EQ(CA_STATUS_OK,
568 CASetPortNumberToAssign(CA_ADAPTER_IP,
569 static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE), 5683));
570 EXPECT_EQ(CA_STATUS_OK,
571 CASetPortNumberToAssign(CA_ADAPTER_IP,
572 static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE), 5683));
575 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV4, 5683));
576 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV6, 5683));
580 TEST(CAGetPortNumberTest, CAGetPortNumberToAssign)
582 ASSERT_EQ(static_cast<uint16_t>(0),
583 CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV4));
584 ASSERT_EQ(static_cast<uint16_t>(0),
585 CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV6));
586 ASSERT_EQ(static_cast<uint16_t>(0),
587 CAGetAssignedPortNumber(CA_ADAPTER_IP,
588 static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE)));
589 ASSERT_EQ(static_cast<uint16_t>(0),
590 CAGetAssignedPortNumber(CA_ADAPTER_IP,
591 static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE)));
593 ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4));
594 ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV6));