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 "gtest/gtest.h"
22 #include "cainterface.h"
26 class CATests : public testing::Test {
28 virtual void SetUp() {
32 virtual void TearDown()
38 void request_handler(CARemoteEndpoint_t* object, CARequestInfo_t* requestInfo);
39 void response_handler(CARemoteEndpoint_t* object, CAResponseInfo_t* responseInfo);
40 void error_handler(const CARemoteEndpoint_t *object, const CAErrorInfo_t* errorInfo);
42 CAResult_t checkGetNetworkInfo();
43 CAResult_t checkSelectNetwork();
47 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
52 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
57 void error_handler(const CARemoteEndpoint_t *object, const CAErrorInfo_t* errorInfo)
59 if(!object || !errorInfo)
64 //error handling shall be added
68 static char* uri = NULL;
69 static CARemoteEndpoint_t* tempRep = NULL;
70 static CARequestInfo_t requestInfo;
71 static CAInfo_t requestData;
72 static CAInfo_t responseData;
73 static CAResponseInfo_t responseInfo;
74 static CAToken_t tempToken = NULL;
75 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
76 static const char URI[] = "coap://10.11.12.13:4545/a/light";
77 static const char RESOURCE_URI[] = "/a/light";
79 static const char SECURE_INFO_DATA[] =
80 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
81 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
82 static const char NORMAL_INFO_DATA[] =
83 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
84 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
92 #define IDENTITY ("1111111111111111")
96 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
98 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
100 void clearDtlsCredentialInfo()
102 printf("clearDtlsCredentialInfo IN\n");
105 // Initialize sensitive data to zeroes before freeing.
106 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
107 free(pskCredsBlob->creds);
109 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
113 printf("clearDtlsCredentialInfo OUT\n");
116 // Internal API. Invoked by OC stack to retrieve credentials from this module
117 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
119 printf("CAGetDtlsPskCredentials IN\n");
121 if (pskCredsBlob != NULL)
123 *credInfo = pskCredsBlob;
126 printf("CAGetDtlsPskCredentials OUT\n");
129 int32_t SetCredentials()
131 printf("SetCredentials IN\n");
132 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
134 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
135 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
137 pskCredsBlob->num = 1;
139 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
141 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
142 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
144 printf("SetCredentials OUT\n");
149 int main(int argc, char **argv)
151 testing::InitGoogleTest(&argc, argv);
152 return RUN_ALL_TESTS();
156 // check return value
157 TEST(InitializeTest, TC_01_Positive_01)
159 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
164 TEST_F(CATests, TerminateTest)
168 char* check = (char *) "terminate success";
169 EXPECT_STREQ(check, "terminate success");
173 // CAStartListeningServer TC
174 // check return value
175 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
177 CASelectNetwork(CA_IPV4);
178 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
181 // CAStartDiscoveryServer TC
182 // check return value
183 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
185 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
188 // CARegisterHandlerTest TC
189 // check return value
190 TEST_F(CATests, RegisterHandlerTest)
192 CARegisterHandler(request_handler, response_handler, error_handler);
193 char* check = (char *) "registerHandler success";
194 EXPECT_STREQ(check, "registerHandler success");
197 // CACreateRemoteEndpoint TC
198 // check return value
199 TEST_F(CATests, CreateRemoteEndpointTestGood)
203 EXPECT_EQ(CA_STATUS_OK, CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep));
207 CADestroyRemoteEndpoint(tempRep);
212 // check remoteEndpoint and values of remoteEndpoint
213 TEST_F(CATests, CreateRemoteEndpointTestValues)
217 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
219 EXPECT_TRUE(tempRep != NULL);
223 EXPECT_STRNE(NULL, tempRep->resourceUri);
228 CADestroyRemoteEndpoint(tempRep);
233 // check return value when uri is NULL
234 TEST_F(CATests, CreateRemoteEndpointTestBad)
238 EXPECT_EQ(CA_STATUS_FAILED, CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep));
242 CADestroyRemoteEndpoint(tempRep);
247 // check values of remoteEndpoint when uri is NULL
248 TEST_F(CATests, CreateRemoteEndpointTestWithNullUri)
251 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
255 EXPECT_STREQ(NULL, tempRep->resourceUri);
261 CADestroyRemoteEndpoint(tempRep);
266 // CADestroyRemoteEndpoint TC
267 // check destroyed remoteEndpoint
268 TEST_F(CATests, DestroyRemoteEndpointTest)
271 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
273 CADestroyRemoteEndpoint(tempRep);
276 char * check = (char *) "destroy success";
277 EXPECT_STREQ(check, "destroy success");
281 // check return value
282 TEST_F(CATests, GenerateTokenTestGood)
284 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
286 CADestroyToken(tempToken);
289 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
290 TEST_F(CATests, GenerateTokenTestBad)
292 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
296 // check destroyed token
297 TEST_F(CATests, DestroyTokenTest)
299 CAGenerateToken(&tempToken, tokenLength);
300 CADestroyToken(tempToken);
302 char * check = (char *) "destroy success";
303 EXPECT_STREQ(check, "destroy success");
307 // check return value
308 TEST(FindResourceTest, DISABLED_TC_14_Positive_01)
310 uri = (char *) RESOURCE_URI;
312 CAGenerateToken(&tempToken, tokenLength);
313 EXPECT_EQ(CA_STATUS_OK, CAFindResource(uri, tempToken, tokenLength));
314 CADestroyToken(tempToken);
317 // check return value when uri is NULL
318 TEST_F(CATests, FindResourceTest)
321 CAGenerateToken(&tempToken, tokenLength);
322 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAFindResource(uri, tempToken, tokenLength));
323 CADestroyToken(tempToken);
327 // check return value
328 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
331 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
333 memset(&requestData, 0, sizeof(CAInfo_t));
334 CAGenerateToken(&tempToken, tokenLength);
335 requestData.token = tempToken;
336 requestData.tokenLength = tokenLength;
338 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
339 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
341 if(!requestData.payload)
343 CADestroyToken(tempToken);
344 FAIL() << "requestData.payload allocation failed";
347 snprintf(requestData.payload, length, NORMAL_INFO_DATA, "a/light");
348 requestData.type = CA_MSG_NONCONFIRM;
350 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
351 requestInfo.method = CA_GET;
352 requestInfo.info = requestData;
354 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
356 CADestroyToken(tempToken);
358 free(requestData.payload);
360 CADestroyRemoteEndpoint(tempRep);
365 // check return value when uri is NULL
366 TEST_F(CATests, SendRequestTestWithNullURI)
369 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
371 memset(&requestData, 0, sizeof(CAInfo_t));
372 CAGenerateToken(&tempToken, tokenLength);
373 requestData.token = tempToken;
374 requestData.tokenLength = tokenLength;
376 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
377 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
379 if(!requestData.payload)
381 CADestroyToken(tempToken);
382 FAIL() << "requestData.payload allocation failed";
385 snprintf(requestData.payload, length, NORMAL_INFO_DATA, "a/light");
386 requestData.type = CA_MSG_NONCONFIRM;
388 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
389 requestInfo.method = CA_GET;
390 requestInfo.info = requestData;
392 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, &requestInfo));
394 CADestroyToken(tempToken);
396 free(requestData.payload);
400 CADestroyRemoteEndpoint(tempRep);
405 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
406 TEST_F(CATests, SendRequestTestWithNullAddr)
409 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
411 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
415 CADestroyRemoteEndpoint(tempRep);
421 // check return value
422 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
425 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
427 memset(&responseData, 0, sizeof(CAInfo_t));
428 responseData.type = CA_MSG_NONCONFIRM;
429 responseData.messageId = 1;
430 responseData.payload = (char *) "response payload";
432 CAGenerateToken(&tempToken, tokenLength);
433 requestData.token = tempToken;
434 requestData.tokenLength = tokenLength;
436 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
437 responseInfo.result = CA_SUCCESS;
438 responseInfo.info = responseData;
440 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
442 CADestroyToken(tempToken);
443 CADestroyRemoteEndpoint(tempRep);
447 // check return value when uri is NULL
448 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
451 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
453 memset(&responseData, 0, sizeof(CAInfo_t));
454 responseData.type = CA_MSG_NONCONFIRM;
455 responseData.messageId = 1;
456 responseData.payload = (char *) "response payload";
458 CAGenerateToken(&tempToken, tokenLength);
459 requestData.token = tempToken;
460 requestData.tokenLength = tokenLength;
462 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
463 responseInfo.result = CA_SUCCESS;
464 responseInfo.info = responseData;
466 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, &responseInfo));
468 CADestroyToken(tempToken);
471 CADestroyRemoteEndpoint(tempRep);
476 // check return value NULL is passed instead of a valid CAResponseInfo_t address
477 TEST_F(CATests, SendResponseTest)
480 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
482 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
486 CADestroyRemoteEndpoint(tempRep);
491 // CASendNotification TC
492 // check return value
493 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
496 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
498 memset(&responseData, 0, sizeof(CAInfo_t));
499 responseData.type = CA_MSG_NONCONFIRM;
500 responseData.payload = (char *) "Temp Notification Data";
502 CAGenerateToken(&tempToken, tokenLength);
503 requestData.token = tempToken;
504 requestData.tokenLength = tokenLength;
506 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
507 responseInfo.result = CA_SUCCESS;
508 responseInfo.info = responseData;
510 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
512 CADestroyToken(tempToken);
515 CADestroyRemoteEndpoint(tempRep);
520 // check return value when uri is NULL
521 TEST_F(CATests, SendNotificationTest)
524 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
526 memset(&responseData, 0, sizeof(CAInfo_t));
527 responseData.type = CA_MSG_NONCONFIRM;
528 responseData.payload = (char *) "Temp Notification Data";
530 CAGenerateToken(&tempToken, tokenLength);
531 requestData.token = tempToken;
532 requestData.tokenLength = tokenLength;
534 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
535 responseInfo.result = CA_SUCCESS;
536 responseInfo.info = responseData;
538 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendNotification(tempRep, &responseInfo));
540 CADestroyToken(tempToken);
543 CADestroyRemoteEndpoint(tempRep);
548 // CAAdvertiseResource TC
549 // check return value
550 TEST(AdvertiseResourceTest, DISABLED_TC_24_Positive_01)
552 uri = (char *) RESOURCE_URI;
555 CAHeaderOption_t* headerOpt;
556 headerOpt = (CAHeaderOption_t *) calloc(1, optionNum * sizeof(CAHeaderOption_t));
560 FAIL() <<"Allocation for headerOpt failed";
563 char* tmpOptionData1 = (char *) "Hello";
564 size_t tmpOptionDataLen = (strlen(tmpOptionData1) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
565 strlen(tmpOptionData1) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
566 headerOpt[0].optionID = 3000;
567 memcpy(headerOpt[0].optionData, tmpOptionData1, tmpOptionDataLen);
568 headerOpt[0].optionLength = (uint16_t) tmpOptionDataLen;
570 char* tmpOptionData2 = (char *) "World";
571 tmpOptionDataLen = (strlen(tmpOptionData2) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
572 strlen(tmpOptionData2) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
573 headerOpt[1].optionID = 3001;
574 memcpy(headerOpt[1].optionData, tmpOptionData2, tmpOptionDataLen);
575 headerOpt[1].optionLength = (uint16_t) tmpOptionDataLen;
577 CAGenerateToken(&tempToken, tokenLength);
579 EXPECT_EQ(CA_STATUS_OK, CAAdvertiseResource(uri, tempToken, tokenLength,
580 headerOpt, (uint8_t )optionNum));
582 CADestroyToken(tempToken);
587 // check return value when uri is NULL
588 TEST_F(CATests, AdvertiseResourceTest)
593 CAHeaderOption_t* headerOpt;
594 headerOpt = (CAHeaderOption_t *) calloc(1, optionNum * sizeof(CAHeaderOption_t));
598 FAIL() << "Allocation for headerOpt failed";
601 char* tmpOptionData1 = (char *) "Hello";
602 size_t tmpOptionDataLen = (strlen(tmpOptionData1) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
603 strlen(tmpOptionData1) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
604 headerOpt[0].optionID = 3000;
605 memcpy(headerOpt[0].optionData, tmpOptionData1, tmpOptionDataLen);
606 headerOpt[0].optionLength = (uint16_t) tmpOptionDataLen;
608 char* tmpOptionData2 = (char *) "World";
609 tmpOptionDataLen = (strlen(tmpOptionData2) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
610 strlen(tmpOptionData2) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
611 headerOpt[1].optionID = 3001;
612 memcpy(headerOpt[1].optionData, tmpOptionData2, tmpOptionDataLen);
613 headerOpt[1].optionLength = (uint16_t) tmpOptionDataLen;
615 CAGenerateToken(&tempToken, tokenLength);
617 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAAdvertiseResource(uri, tempToken, tokenLength,
618 headerOpt, (uint8_t )optionNum));
620 CADestroyToken(tempToken);
625 // CASelectNewwork TC
626 // check return value
627 TEST_F(CATests, SelectNetworkTestGood)
629 CAResult_t res = checkSelectNetwork();
630 EXPECT_EQ(CA_STATUS_OK, res);
633 CAResult_t checkSelectNetwork()
635 CAResult_t res = CASelectNetwork(CA_IPV4);
637 if (CA_STATUS_OK == res)
639 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_IPV4));
642 if (CA_NOT_SUPPORTED == res)
644 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_IPV4));
651 // check return value when selected network is disable
652 TEST_F(CATests, SelectNetworkTestBad)
654 //Select disable network
655 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork(1000));
658 // check return value when selected network is disable
659 TEST_F(CATests, UnSelectNetworkTest)
661 //UnSelect disable network
662 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(1000));
665 // CAHandlerRequestResponse TC
666 // check return value
667 TEST_F(CATests, HandlerRequestResponseTest)
669 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
672 // CASendRequestToAll TC
673 // check return value
674 TEST(SendRequestToAllTest, DISABLED_TC_31_Positive_01)
676 CASelectNetwork(CA_IPV4);
678 uri = (char *) RESOURCE_URI;
679 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
680 CAGroupEndpoint_t *group = NULL;
681 group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
684 FAIL() << "Allocation for group failed";
687 group->transportType = tempRep->transportType;
688 group->resourceUri = tempRep->resourceUri;
690 memset(&requestData, 0, sizeof(CAInfo_t));
691 CAGenerateToken(&tempToken, tokenLength);
692 requestData.token = tempToken;
693 requestData.tokenLength = tokenLength;
695 requestData.payload = (char *) "Temp Json Payload";
696 requestData.type = CA_MSG_NONCONFIRM;
697 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
698 requestInfo.method = CA_GET;
699 requestInfo.info = requestData;
701 EXPECT_EQ(CA_STATUS_OK, CASendRequestToAll(group, &requestInfo));
703 CADestroyToken(tempToken);
705 CADestroyRemoteEndpoint(tempRep);
711 // check return value when group->resourceUri is NULL
712 TEST(SendRequestToAllTest, DISABLED_TC_32_Negative_01)
714 uri = (char *) RESOURCE_URI;
715 CAGroupEndpoint_t *group = NULL;
717 memset(&requestData, 0, sizeof(CAInfo_t));
718 CAGenerateToken(&tempToken, tokenLength);
719 requestData.token = tempToken;
720 requestData.tokenLength = tokenLength;
722 requestData.payload = (char *) "Temp Json Payload";
723 requestData.type = CA_MSG_NONCONFIRM;
724 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
725 requestInfo.method = CA_GET;
726 requestInfo.info = requestData;
728 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequestToAll(group, &requestInfo));
730 CADestroyToken(tempToken);
733 // CAGetNetworkInformation TC
734 // check return value
735 TEST_F (CATests, GetNetworkInformationTestGood)
737 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
740 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
743 if (SetCredentials() == 0)
745 printf("SetCredentials failed\n");
748 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
752 CAResult_t checkGetNetworkInfo()
754 CALocalConnectivity_t *tempInfo = NULL;
755 uint32_t tempSize = 0;
757 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
761 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
762 CA_NOT_SUPPORTED == res)
768 return CA_STATUS_FAILED;