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 {
32 virtual void SetUp() {
36 virtual void TearDown()
42 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
43 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
44 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
45 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
46 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
47 CAResult_t checkSelectNetwork();
49 void request_handler(const CAEndpoint_t * /*object*/,
50 const CARequestInfo_t * /*requestInfo*/)
55 void response_handler(const CAEndpoint_t * /*object*/,
56 const CAResponseInfo_t * /*responseInfo*/)
61 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
63 if(!object || !errorInfo)
68 //error handling shall be added
72 void adapter_handler(CATransportAdapter_t /*adapter*/,
77 void connection_handler(const CAEndpoint_t * /*endpoint*/,
82 static char* addr = NULL;
83 static CAEndpoint_t* tempRep = NULL;
84 static CARequestInfo_t requestInfo;
85 static CAInfo_t requestData;
86 static CAInfo_t responseData;
87 static CAResponseInfo_t responseInfo;
88 static CAToken_t tempToken = NULL;
89 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
90 static const char ADDRESS[] = "10.11.12.13";
91 static const uint16_t PORT = 4545;
93 static const char NORMAL_INFO_DATA[] =
94 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
95 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
99 // Iotivity Device Identity.
100 const unsigned char IDENTITY[] = ("1111111111111111");
102 // PSK between this device and peer device.
103 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
105 // Internal API. Invoked by CA stack to retrieve credentials from this module
106 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
107 const unsigned char *desc, size_t desc_len,
108 unsigned char *result, size_t result_length)
110 printf("CAGetDtlsPskCredentials IN\n");
121 case CA_DTLS_PSK_HINT:
122 case CA_DTLS_PSK_IDENTITY:
124 if (result_length < sizeof(IDENTITY))
126 printf("ERROR : Wrong value for result for storing IDENTITY");
130 memcpy(result, IDENTITY, sizeof(IDENTITY));
131 ret = sizeof(IDENTITY);
134 case CA_DTLS_PSK_KEY:
136 if ((desc_len == sizeof(IDENTITY)) &&
137 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
139 if (result_length < sizeof(RS_CLIENT_PSK))
141 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
145 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
146 ret = sizeof(RS_CLIENT_PSK);
152 printf("Wrong value passed for PSK_CRED_TYPE.");
157 printf("CAGetDtlsPskCredentials OUT\n");
160 #endif //__WITH_DTLS__
163 TEST(InitializeTest, CAInitializeTest)
165 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
170 TEST_F(CATests, TerminateTest)
174 char* check = (char *) "terminate success";
175 EXPECT_STREQ(check, "terminate success");
180 // CAStartListeningServer TC
181 TEST_F(CATests, StartListeningServerTestWithNonSelect)
183 EXPECT_EQ(CA_STATUS_FAILED, CAStartListeningServer());
186 // CAStartListeningServer TC
187 TEST_F(CATests, StartListeningServerTest)
189 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
190 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
193 // CAStopListeningServer TC
194 TEST_F(CATests, CAStopListeningServerTestWithNonSelect)
196 EXPECT_EQ(CA_STATUS_FAILED, CAStopListeningServer());
199 // CAStopListeningServer TC
200 TEST_F(CATests, CAStopListeningServerTest)
202 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
203 EXPECT_EQ(CA_STATUS_OK, CAStopListeningServer());
206 // CARegisterHandlerTest TC
207 TEST_F(CATests, RegisterHandlerTest)
209 CARegisterHandler(request_handler, response_handler, error_handler);
210 char* check = (char *) "registerHandler success";
211 EXPECT_STREQ(check, "registerHandler success");
214 // CACreateRemoteEndpoint TC
215 TEST_F(CATests, CreateRemoteEndpointTestGood)
217 addr = (char *) ADDRESS;
219 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
222 CADestroyEndpoint(tempRep);
226 // check remoteEndpoint and values of remoteEndpoint
227 TEST_F(CATests, CreateRemoteEndpointTestValues)
229 addr = (char *) ADDRESS;
231 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
233 EXPECT_TRUE(tempRep != NULL);
235 CADestroyEndpoint(tempRep);
240 TEST_F(CATests, GenerateTokenTestGood)
242 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
244 CADestroyToken(tempToken);
247 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
248 TEST_F(CATests, GenerateTokenTestBad)
250 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
254 // check destroyed token
255 TEST_F(CATests, DestroyTokenTest)
257 CAGenerateToken(&tempToken, tokenLength);
258 CADestroyToken(tempToken);
260 char * check = (char *) "destroy success";
261 EXPECT_STREQ(check, "destroy success");
264 TEST_F(CATests, SendRequestTestWithInvalidAddress)
266 CARegisterHandler(request_handler, response_handler, error_handler);
268 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
269 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "10.11.13.13.14", PORT, &tempRep);
271 memset(&requestData, 0, sizeof(CAInfo_t));
272 CAGenerateToken(&tempToken, tokenLength);
273 requestData.token = tempToken;
274 requestData.tokenLength = tokenLength;
275 requestData.type = CA_MSG_CONFIRM;
277 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
278 requestInfo.method = CA_GET;
279 requestInfo.info = requestData;
281 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
283 CADestroyToken(tempToken);
284 CADestroyEndpoint(tempRep);
285 free(requestData.payload);
290 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
292 addr = (char *) ADDRESS;
293 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
295 memset(&requestData, 0, sizeof(CAInfo_t));
296 CAGenerateToken(&tempToken, tokenLength);
297 requestData.token = tempToken;
298 requestData.tokenLength = tokenLength;
300 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
301 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
302 if(!requestData.payload)
304 CADestroyToken(tempToken);
305 FAIL() << "requestData.payload allocation failed";
307 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
308 requestData.payloadSize = length + 1;
309 requestData.type = CA_MSG_NONCONFIRM;
311 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
312 requestInfo.method = CA_GET;
313 requestInfo.info = requestData;
315 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
317 CADestroyToken(tempToken);
318 CADestroyEndpoint(tempRep);
319 free(requestData.payload);
323 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
324 TEST_F(CATests, SendRequestTestWithNullAddr)
326 addr = (char *) ADDRESS;
327 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
329 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
331 CADestroyEndpoint(tempRep);
335 TEST_F(CATests, SendResponseTestWithInvalidCode)
337 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
339 addr = (char *) ADDRESS;
340 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
342 memset(&responseData, 0, sizeof(CAInfo_t));
343 responseData.type = CA_MSG_RESET;
344 responseData.messageId = 1;
345 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
347 EXPECT_TRUE(responseData.payload != NULL);
349 if (responseData.payload)
351 CAGenerateToken(&tempToken, tokenLength);
352 requestData.token = tempToken;
353 requestData.tokenLength = tokenLength;
355 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
356 responseInfo.result = CA_CONTENT;
357 responseInfo.info = responseData;
359 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
361 CADestroyToken(tempToken);
362 CADestroyEndpoint(tempRep);
363 free(responseData.payload);
369 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
371 addr = (char *) ADDRESS;
372 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
374 memset(&responseData, 0, sizeof(CAInfo_t));
375 responseData.type = CA_MSG_NONCONFIRM;
376 responseData.messageId = 1;
377 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
379 EXPECT_TRUE(responseData.payload != NULL);
380 if(!responseData.payload)
382 CADestroyEndpoint(tempRep);
386 memcpy(responseData.payload, "response payload", sizeof("response payload"));
387 responseData.payloadSize = sizeof("response payload");
389 CAGenerateToken(&tempToken, tokenLength);
390 requestData.token = tempToken;
391 requestData.tokenLength = tokenLength;
393 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
394 responseInfo.result = CA_CONTENT;
395 responseInfo.info = responseData;
397 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
399 CADestroyToken(tempToken);
400 CADestroyEndpoint(tempRep);
401 free(responseData.payload);
405 // check return value when address is NULL as multicast
406 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
409 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
411 memset(&responseData, 0, sizeof(CAInfo_t));
412 responseData.type = CA_MSG_NONCONFIRM;
413 responseData.messageId = 1;
414 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
415 EXPECT_TRUE(responseData.payload != NULL);
417 if(!responseData.payload)
419 CADestroyEndpoint(tempRep);
423 memcpy(responseData.payload, "response payload", sizeof("response payload"));
424 responseData.payloadSize = sizeof("response payload");
426 CAGenerateToken(&tempToken, tokenLength);
427 requestData.token = tempToken;
428 requestData.tokenLength = tokenLength;
430 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
431 responseInfo.result = CA_CONTENT;
432 responseInfo.info = responseData;
434 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
436 CADestroyToken(tempToken);
437 CADestroyEndpoint(tempRep);
438 free (responseData.payload);
442 // check return value NULL is passed instead of a valid CAResponseInfo_t address
443 TEST_F(CATests, SendResponseTest)
445 addr = (char *) ADDRESS;
446 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
448 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
450 CADestroyEndpoint(tempRep);
454 // CASelectNewwork TC
455 TEST_F(CATests, SelectNetworkTestGood)
457 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
460 CAResult_t checkSelectNetwork()
462 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
464 if (CA_STATUS_OK == res)
466 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
469 if (CA_NOT_SUPPORTED == res)
471 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
478 // check return value when selected network is disable
479 TEST_F(CATests, SelectNetworkTestBad)
481 //Select disable network
482 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
483 CA_TRANSPORT_ADAPTER_SCOPE));
486 // check return value when selected network is disable
487 TEST_F(CATests, UnSelectNetworkTest)
489 //UnSelect disable network
490 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
491 CA_TRANSPORT_ADAPTER_SCOPE));
494 // CAHandlerRequestResponse TC
495 TEST_F(CATests, HandlerRequestResponseTest)
497 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
500 // CAGetNetworkInformation TC
501 TEST_F (CATests, GetNetworkInformationTest)
503 uint32_t tempSize = 0;
504 CAEndpoint_t *tempInfo = NULL;
506 EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
507 EXPECT_EQ(CA_STATUS_OK, CAGetNetworkInformation(&tempInfo, &tempSize));
512 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
515 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
519 // CARegisterNetworkMonitorHandler TC
520 TEST_F(CATests, RegisterNetworkMonitorHandler)
522 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
523 connection_handler));
526 // CASetAutoConnectionDeviceInfo TC
527 TEST_F(CATests, SetAutoConnectionDeviceInfo)
529 addr = (char *) ADDRESS;
531 #if defined(__ANDROID__) && defined(LE_ADAPTER)
532 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
534 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
538 // CAUnsetAutoConnectionDeviceInfo TC
539 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
541 addr = (char *) ADDRESS;
543 #if defined(__ANDROID__) && defined(LE_ADAPTER)
544 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
546 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
550 TEST(CASetPortNumberTest, CASetPortNumberToAssign)
552 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV4, 5683));
553 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV6, 5683));
554 EXPECT_EQ(CA_STATUS_OK,
555 CASetPortNumberToAssign(CA_ADAPTER_IP,
556 static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE), 5683));
557 EXPECT_EQ(CA_STATUS_OK,
558 CASetPortNumberToAssign(CA_ADAPTER_IP,
559 static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE), 5683));
562 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV4, 5683));
563 EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV6, 5683));
567 TEST(CAGetPortNumberTest, CAGetPortNumberToAssign)
569 ASSERT_EQ(static_cast<uint16_t>(0),
570 CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV4));
571 ASSERT_EQ(static_cast<uint16_t>(0),
572 CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV6));
573 ASSERT_EQ(static_cast<uint16_t>(0),
574 CAGetAssignedPortNumber(CA_ADAPTER_IP,
575 static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE)));
576 ASSERT_EQ(static_cast<uint16_t>(0),
577 CAGetAssignedPortNumber(CA_ADAPTER_IP,
578 static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE)));
580 ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4));
581 ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV6));