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 CAResult_t checkGetNetworkInfo();
41 CAResult_t checkSelectNetwork();
45 void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
50 void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
55 static char* uri = NULL;
56 static CARemoteEndpoint_t* tempRep = NULL;
57 static CARequestInfo_t requestInfo;
58 static CAInfo_t requestData;
59 static CAInfo_t responseData;
60 static CAResponseInfo_t responseInfo;
61 static CAToken_t tempToken = NULL;
62 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
63 static const char URI[] = "coap://10.11.12.13:4545/a/light";
64 static const char RESOURCE_URI[] = "/a/light";
66 static const char SECURE_INFO_DATA[] =
67 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
68 "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
69 static const char NORMAL_INFO_DATA[] =
70 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
71 "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
79 #define IDENTITY ("1111111111111111")
83 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
85 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
87 void clearDtlsCredentialInfo()
89 printf("clearDtlsCredentialInfo IN\n");
92 // Initialize sensitive data to zeroes before freeing.
93 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
94 free(pskCredsBlob->creds);
96 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
100 printf("clearDtlsCredentialInfo OUT\n");
103 // Internal API. Invoked by OC stack to retrieve credentials from this module
104 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
106 printf("CAGetDtlsPskCredentials IN\n");
108 if (pskCredsBlob != NULL)
110 *credInfo = pskCredsBlob;
113 printf("CAGetDtlsPskCredentials OUT\n");
116 int32_t SetCredentials()
118 printf("SetCredentials IN\n");
119 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
121 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
122 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
124 pskCredsBlob->num = 1;
126 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
128 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
129 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
131 printf("SetCredentials OUT\n");
136 int main(int argc, char **argv)
138 testing::InitGoogleTest(&argc, argv);
139 return RUN_ALL_TESTS();
143 // check return value
144 TEST(InitializeTest, TC_01_Positive_01)
146 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
151 TEST_F(CATests, TerminateTest)
155 char* check = (char *) "terminate success";
156 EXPECT_STREQ(check, "terminate success");
160 // CAStartListeningServer TC
161 // check return value
162 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
164 CASelectNetwork(CA_IPV4);
165 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
168 // CAStartDiscoveryServer TC
169 // check return value
170 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
172 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
175 // CARegisterHandlerTest TC
176 // check return value
177 TEST_F(CATests, RegisterHandlerTest)
179 CARegisterHandler(request_handler, response_handler);
180 char* check = (char *) "registerHandler success";
181 EXPECT_STREQ(check, "registerHandler success");
184 // CACreateRemoteEndpoint TC
185 // check return value
186 TEST_F(CATests, CreateRemoteEndpointTestGood)
190 EXPECT_EQ(CA_STATUS_OK, CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep));
194 CADestroyRemoteEndpoint(tempRep);
199 // check remoteEndpoint and values of remoteEndpoint
200 TEST_F(CATests, CreateRemoteEndpointTestValues)
204 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
206 EXPECT_TRUE(tempRep != NULL);
210 EXPECT_STRNE(NULL, tempRep->resourceUri);
215 CADestroyRemoteEndpoint(tempRep);
220 // check return value when uri is NULL
221 TEST_F(CATests, CreateRemoteEndpointTestBad)
225 EXPECT_EQ(CA_STATUS_FAILED, CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep));
229 CADestroyRemoteEndpoint(tempRep);
234 // check values of remoteEndpoint when uri is NULL
235 TEST_F(CATests, CreateRemoteEndpointTestWithNullUri)
238 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
242 EXPECT_STREQ(NULL, tempRep->resourceUri);
248 CADestroyRemoteEndpoint(tempRep);
253 // CADestroyRemoteEndpoint TC
254 // check destroyed remoteEndpoint
255 TEST_F(CATests, DestroyRemoteEndpointTest)
258 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
260 CADestroyRemoteEndpoint(tempRep);
263 char * check = (char *) "destroy success";
264 EXPECT_STREQ(check, "destroy success");
268 // check return value
269 TEST_F(CATests, GenerateTokenTestGood)
271 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
273 CADestroyToken(tempToken);
276 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
277 TEST_F(CATests, GenerateTokenTestBad)
279 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
283 // check destroyed token
284 TEST_F(CATests, DestroyTokenTest)
286 CAGenerateToken(&tempToken, tokenLength);
287 CADestroyToken(tempToken);
289 char * check = (char *) "destroy success";
290 EXPECT_STREQ(check, "destroy success");
294 // check return value
295 TEST(FindResourceTest, DISABLED_TC_14_Positive_01)
297 uri = (char *) RESOURCE_URI;
299 CAGenerateToken(&tempToken, tokenLength);
300 EXPECT_EQ(CA_STATUS_OK, CAFindResource(uri, tempToken, tokenLength));
301 CADestroyToken(tempToken);
304 // check return value when uri is NULL
305 TEST_F(CATests, FindResourceTest)
308 CAGenerateToken(&tempToken, tokenLength);
309 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAFindResource(uri, tempToken, tokenLength));
310 CADestroyToken(tempToken);
314 // check return value
315 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
318 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
320 memset(&requestData, 0, sizeof(CAInfo_t));
321 CAGenerateToken(&tempToken, tokenLength);
322 requestData.token = tempToken;
323 requestData.tokenLength = tokenLength;
325 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
326 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
328 if(!requestData.payload)
330 CADestroyToken(tempToken);
331 FAIL() << "requestData.payload allocation failed";
334 snprintf(requestData.payload, length, NORMAL_INFO_DATA, "a/light");
335 requestData.type = CA_MSG_NONCONFIRM;
337 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
338 requestInfo.method = CA_GET;
339 requestInfo.info = requestData;
341 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
343 CADestroyToken(tempToken);
345 free(requestData.payload);
347 CADestroyRemoteEndpoint(tempRep);
352 // check return value when uri is NULL
353 TEST_F(CATests, SendRequestTestWithNullURI)
356 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
358 memset(&requestData, 0, sizeof(CAInfo_t));
359 CAGenerateToken(&tempToken, tokenLength);
360 requestData.token = tempToken;
361 requestData.tokenLength = tokenLength;
363 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
364 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
366 if(!requestData.payload)
368 CADestroyToken(tempToken);
369 FAIL() << "requestData.payload allocation failed";
372 snprintf(requestData.payload, length, NORMAL_INFO_DATA, "a/light");
373 requestData.type = CA_MSG_NONCONFIRM;
375 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
376 requestInfo.method = CA_GET;
377 requestInfo.info = requestData;
379 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, &requestInfo));
381 CADestroyToken(tempToken);
383 free(requestData.payload);
387 CADestroyRemoteEndpoint(tempRep);
392 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
393 TEST_F(CATests, SendRequestTestWithNullAddr)
396 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
398 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
402 CADestroyRemoteEndpoint(tempRep);
408 // check return value
409 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
412 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
414 memset(&responseData, 0, sizeof(CAInfo_t));
415 responseData.type = CA_MSG_NONCONFIRM;
416 responseData.messageId = 1;
417 responseData.payload = (char *) "response payload";
419 CAGenerateToken(&tempToken, tokenLength);
420 requestData.token = tempToken;
421 requestData.tokenLength = tokenLength;
423 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
424 responseInfo.result = CA_SUCCESS;
425 responseInfo.info = responseData;
427 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
429 CADestroyToken(tempToken);
430 CADestroyRemoteEndpoint(tempRep);
434 // check return value when uri is NULL
435 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
438 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
440 memset(&responseData, 0, sizeof(CAInfo_t));
441 responseData.type = CA_MSG_NONCONFIRM;
442 responseData.messageId = 1;
443 responseData.payload = (char *) "response payload";
445 CAGenerateToken(&tempToken, tokenLength);
446 requestData.token = tempToken;
447 requestData.tokenLength = tokenLength;
449 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
450 responseInfo.result = CA_SUCCESS;
451 responseInfo.info = responseData;
453 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, &responseInfo));
455 CADestroyToken(tempToken);
458 CADestroyRemoteEndpoint(tempRep);
463 // check return value NULL is passed instead of a valid CAResponseInfo_t address
464 TEST_F(CATests, SendResponseTest)
467 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
469 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
473 CADestroyRemoteEndpoint(tempRep);
478 // CASendNotification TC
479 // check return value
480 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
483 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
485 memset(&responseData, 0, sizeof(CAInfo_t));
486 responseData.type = CA_MSG_NONCONFIRM;
487 responseData.payload = (char *) "Temp Notification Data";
489 CAGenerateToken(&tempToken, tokenLength);
490 requestData.token = tempToken;
491 requestData.tokenLength = tokenLength;
493 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
494 responseInfo.result = CA_SUCCESS;
495 responseInfo.info = responseData;
497 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
499 CADestroyToken(tempToken);
502 CADestroyRemoteEndpoint(tempRep);
507 // check return value when uri is NULL
508 TEST_F(CATests, SendNotificationTest)
511 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
513 memset(&responseData, 0, sizeof(CAInfo_t));
514 responseData.type = CA_MSG_NONCONFIRM;
515 responseData.payload = (char *) "Temp Notification Data";
517 CAGenerateToken(&tempToken, tokenLength);
518 requestData.token = tempToken;
519 requestData.tokenLength = tokenLength;
521 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
522 responseInfo.result = CA_SUCCESS;
523 responseInfo.info = responseData;
525 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendNotification(tempRep, &responseInfo));
527 CADestroyToken(tempToken);
530 CADestroyRemoteEndpoint(tempRep);
535 // CAAdvertiseResource TC
536 // check return value
537 TEST(AdvertiseResourceTest, DISABLED_TC_24_Positive_01)
539 uri = (char *) RESOURCE_URI;
542 CAHeaderOption_t* headerOpt;
543 headerOpt = (CAHeaderOption_t *) calloc(1, optionNum * sizeof(CAHeaderOption_t));
547 FAIL() <<"Allocation for headerOpt failed";
550 char* tmpOptionData1 = (char *) "Hello";
551 size_t tmpOptionDataLen = (strlen(tmpOptionData1) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
552 strlen(tmpOptionData1) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
553 headerOpt[0].optionID = 3000;
554 memcpy(headerOpt[0].optionData, tmpOptionData1, tmpOptionDataLen);
555 headerOpt[0].optionLength = (uint16_t) tmpOptionDataLen;
557 char* tmpOptionData2 = (char *) "World";
558 tmpOptionDataLen = (strlen(tmpOptionData2) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
559 strlen(tmpOptionData2) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
560 headerOpt[1].optionID = 3001;
561 memcpy(headerOpt[1].optionData, tmpOptionData2, tmpOptionDataLen);
562 headerOpt[1].optionLength = (uint16_t) tmpOptionDataLen;
564 CAGenerateToken(&tempToken, tokenLength);
566 EXPECT_EQ(CA_STATUS_OK, CAAdvertiseResource(uri, tempToken, tokenLength,
567 headerOpt, (uint8_t )optionNum));
569 CADestroyToken(tempToken);
574 // check return value when uri is NULL
575 TEST_F(CATests, AdvertiseResourceTest)
580 CAHeaderOption_t* headerOpt;
581 headerOpt = (CAHeaderOption_t *) calloc(1, optionNum * sizeof(CAHeaderOption_t));
585 FAIL() << "Allocation for headerOpt failed";
588 char* tmpOptionData1 = (char *) "Hello";
589 size_t tmpOptionDataLen = (strlen(tmpOptionData1) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
590 strlen(tmpOptionData1) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
591 headerOpt[0].optionID = 3000;
592 memcpy(headerOpt[0].optionData, tmpOptionData1, tmpOptionDataLen);
593 headerOpt[0].optionLength = (uint16_t) tmpOptionDataLen;
595 char* tmpOptionData2 = (char *) "World";
596 tmpOptionDataLen = (strlen(tmpOptionData2) < CA_MAX_HEADER_OPTION_DATA_LENGTH) ?
597 strlen(tmpOptionData2) : CA_MAX_HEADER_OPTION_DATA_LENGTH - 1;
598 headerOpt[1].optionID = 3001;
599 memcpy(headerOpt[1].optionData, tmpOptionData2, tmpOptionDataLen);
600 headerOpt[1].optionLength = (uint16_t) tmpOptionDataLen;
602 CAGenerateToken(&tempToken, tokenLength);
604 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAAdvertiseResource(uri, tempToken, tokenLength,
605 headerOpt, (uint8_t )optionNum));
607 CADestroyToken(tempToken);
612 // CASelectNewwork TC
613 // check return value
614 TEST_F(CATests, SelectNetworkTestGood)
616 CAResult_t res = checkSelectNetwork();
617 EXPECT_EQ(CA_STATUS_OK, res);
620 CAResult_t checkSelectNetwork()
622 CAResult_t res = CASelectNetwork(CA_IPV4);
624 if (CA_STATUS_OK == res)
626 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_IPV4));
629 if (CA_NOT_SUPPORTED == res)
631 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_IPV4));
638 // check return value when selected network is disable
639 TEST_F(CATests, SelectNetworkTestBad)
641 //Select disable network
642 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork(1000));
645 // check return value when selected network is disable
646 TEST_F(CATests, UnSelectNetworkTest)
648 //UnSelect disable network
649 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(1000));
652 // CAHandlerRequestResponse TC
653 // check return value
654 TEST_F(CATests, HandlerRequestResponseTest)
656 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
659 // CASendRequestToAll TC
660 // check return value
661 TEST(SendRequestToAllTest, DISABLED_TC_31_Positive_01)
663 CASelectNetwork(CA_IPV4);
665 uri = (char *) RESOURCE_URI;
666 CACreateRemoteEndpoint(uri, CA_IPV4, &tempRep);
667 CAGroupEndpoint_t *group = NULL;
668 group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
671 FAIL() << "Allocation for group failed";
674 group->transportType = tempRep->transportType;
675 group->resourceUri = tempRep->resourceUri;
677 memset(&requestData, 0, sizeof(CAInfo_t));
678 CAGenerateToken(&tempToken, tokenLength);
679 requestData.token = tempToken;
680 requestData.tokenLength = tokenLength;
682 requestData.payload = (char *) "Temp Json Payload";
683 requestData.type = CA_MSG_NONCONFIRM;
684 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
685 requestInfo.method = CA_GET;
686 requestInfo.info = requestData;
688 EXPECT_EQ(CA_STATUS_OK, CASendRequestToAll(group, &requestInfo));
690 CADestroyToken(tempToken);
692 CADestroyRemoteEndpoint(tempRep);
698 // check return value when group->resourceUri is NULL
699 TEST(SendRequestToAllTest, DISABLED_TC_32_Negative_01)
701 uri = (char *) RESOURCE_URI;
702 CAGroupEndpoint_t *group = NULL;
704 memset(&requestData, 0, sizeof(CAInfo_t));
705 CAGenerateToken(&tempToken, tokenLength);
706 requestData.token = tempToken;
707 requestData.tokenLength = tokenLength;
709 requestData.payload = (char *) "Temp Json Payload";
710 requestData.type = CA_MSG_NONCONFIRM;
711 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
712 requestInfo.method = CA_GET;
713 requestInfo.info = requestData;
715 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequestToAll(group, &requestInfo));
717 CADestroyToken(tempToken);
720 // CAGetNetworkInformation TC
721 // check return value
722 TEST_F (CATests, GetNetworkInformationTestGood)
724 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
727 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
730 if (SetCredentials() == 0)
732 printf("SetCredentials failed\n");
735 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
739 CAResult_t checkGetNetworkInfo()
741 CALocalConnectivity_t *tempInfo = NULL;
742 uint32_t tempSize = 0;
744 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
748 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
749 CA_NOT_SUPPORTED == res)
755 return CA_STATUS_FAILED;