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"
25 #define CA_TRANSPORT_ADAPTER_SCOPE 1000
27 class CATests : public testing::Test {
29 virtual void SetUp() {
33 virtual void TearDown()
39 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
40 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
41 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
42 CAResult_t checkGetNetworkInfo();
43 CAResult_t checkSelectNetwork();
45 void request_handler(const CAEndpoint_t * /*object*/,
46 const CARequestInfo_t * /*requestInfo*/)
51 void response_handler(const CAEndpoint_t * /*object*/,
52 const CAResponseInfo_t * /*responseInfo*/)
57 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
59 if(!object || !errorInfo)
64 //error handling shall be added
68 static char* addr = NULL;
69 static CAEndpoint_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 ADDRESS[] = "10.11.12.13";
77 static const uint16_t PORT = 4545;
79 static const char NORMAL_INFO_DATA[] =
80 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
81 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
89 #define IDENTITY ("1111111111111111")
93 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
95 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
97 void clearDtlsCredentialInfo()
99 printf("clearDtlsCredentialInfo IN\n");
102 // Initialize sensitive data to zeroes before freeing.
103 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
104 free(pskCredsBlob->creds);
106 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
110 printf("clearDtlsCredentialInfo OUT\n");
113 // Internal API. Invoked by OC stack to retrieve credentials from this module
114 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
116 printf("CAGetDtlsPskCredentials IN\n");
118 if (pskCredsBlob != NULL)
120 *credInfo = pskCredsBlob;
123 printf("CAGetDtlsPskCredentials OUT\n");
126 int32_t SetCredentials()
128 printf("SetCredentials IN\n");
129 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
131 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
132 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
134 pskCredsBlob->num = 1;
136 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
138 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
139 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
141 printf("SetCredentials OUT\n");
146 int main(int argc, char **argv)
148 testing::InitGoogleTest(&argc, argv);
149 return RUN_ALL_TESTS();
153 // check return value
154 TEST(InitializeTest, TC_01_Positive_01)
156 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
161 TEST_F(CATests, TerminateTest)
165 char* check = (char *) "terminate success";
166 EXPECT_STREQ(check, "terminate success");
170 // CAStartListeningServer TC
171 // check return value
172 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
174 CASelectNetwork(CA_ADAPTER_IP);
175 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
178 // CAStartDiscoveryServer TC
179 // check return value
180 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
182 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
185 // CARegisterHandlerTest TC
186 // check return value
187 TEST_F(CATests, RegisterHandlerTest)
189 CARegisterHandler(request_handler, response_handler, error_handler);
190 char* check = (char *) "registerHandler success";
191 EXPECT_STREQ(check, "registerHandler success");
194 // CACreateRemoteEndpoint TC
195 // check return value
196 TEST_F(CATests, CreateRemoteEndpointTestGood)
198 addr = (char *) ADDRESS;
200 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
205 CADestroyEndpoint(tempRep);
210 // check remoteEndpoint and values of remoteEndpoint
211 TEST_F(CATests, CreateRemoteEndpointTestValues)
213 addr = (char *) ADDRESS;
215 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
217 EXPECT_TRUE(tempRep != NULL);
221 CADestroyEndpoint(tempRep);
227 // check return value
228 TEST_F(CATests, GenerateTokenTestGood)
230 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
232 CADestroyToken(tempToken);
235 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
236 TEST_F(CATests, GenerateTokenTestBad)
238 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
242 // check destroyed token
243 TEST_F(CATests, DestroyTokenTest)
245 CAGenerateToken(&tempToken, tokenLength);
246 CADestroyToken(tempToken);
248 char * check = (char *) "destroy success";
249 EXPECT_STREQ(check, "destroy success");
253 // check return value
254 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
256 addr = (char *) ADDRESS;
257 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
259 memset(&requestData, 0, sizeof(CAInfo_t));
260 CAGenerateToken(&tempToken, tokenLength);
261 requestData.token = tempToken;
262 requestData.tokenLength = tokenLength;
264 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
265 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
266 if(!requestData.payload)
268 CADestroyToken(tempToken);
269 FAIL() << "requestData.payload allocation failed";
271 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
272 requestData.payloadSize = length + 1;
273 requestData.type = CA_MSG_NONCONFIRM;
275 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
276 requestInfo.method = CA_GET;
277 requestInfo.info = requestData;
279 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
281 CADestroyToken(tempToken);
283 free(requestData.payload);
285 CADestroyEndpoint(tempRep);
290 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
291 TEST_F(CATests, SendRequestTestWithNullAddr)
293 addr = (char *) ADDRESS;
294 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
296 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
300 CADestroyEndpoint(tempRep);
306 // check return value
307 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
309 addr = (char *) ADDRESS;
310 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
312 memset(&responseData, 0, sizeof(CAInfo_t));
313 responseData.type = CA_MSG_NONCONFIRM;
314 responseData.messageId = 1;
315 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
317 EXPECT_TRUE(responseData.payload != NULL);
318 if(!responseData.payload)
320 CADestroyEndpoint(tempRep);
324 memcpy(responseData.payload, "response payload", sizeof("response payload"));
325 responseData.payloadSize = sizeof("response payload");
327 CAGenerateToken(&tempToken, tokenLength);
328 requestData.token = tempToken;
329 requestData.tokenLength = tokenLength;
331 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
332 responseInfo.result = CA_CONTENT;
333 responseInfo.info = responseData;
335 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
337 CADestroyToken(tempToken);
338 CADestroyEndpoint(tempRep);
339 free(responseData.payload);
343 // check return value when address is NULL as multicast
344 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
347 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
349 memset(&responseData, 0, sizeof(CAInfo_t));
350 responseData.type = CA_MSG_NONCONFIRM;
351 responseData.messageId = 1;
352 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
353 EXPECT_TRUE(responseData.payload != NULL);
355 if(!responseData.payload)
357 CADestroyEndpoint(tempRep);
361 memcpy(responseData.payload, "response payload", sizeof("response payload"));
362 responseData.payloadSize = sizeof("response payload");
364 CAGenerateToken(&tempToken, tokenLength);
365 requestData.token = tempToken;
366 requestData.tokenLength = tokenLength;
368 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
369 responseInfo.result = CA_CONTENT;
370 responseInfo.info = responseData;
372 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
374 CADestroyToken(tempToken);
377 CADestroyEndpoint(tempRep);
380 free (responseData.payload);
383 // check return value NULL is passed instead of a valid CAResponseInfo_t address
384 TEST_F(CATests, SendResponseTest)
386 addr = (char *) ADDRESS;
387 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
389 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
393 CADestroyEndpoint(tempRep);
398 // CASendNotification TC
399 // check return value
400 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
402 addr = (char *) ADDRESS;
403 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
405 memset(&responseData, 0, sizeof(CAInfo_t));
406 responseData.type = CA_MSG_NONCONFIRM;
407 responseData.payload = (CAPayload_t)malloc(sizeof("Temp Notification Data"));
409 EXPECT_TRUE(responseData.payload != NULL);
410 if(!responseData.payload)
412 CADestroyEndpoint(tempRep);
416 memcpy(responseData.payload, "Temp Notification Data", sizeof("Temp Notification Data"));
417 responseData.payloadSize = sizeof("Temp Notification Data");
419 CAGenerateToken(&tempToken, tokenLength);
420 requestData.token = tempToken;
421 requestData.tokenLength = tokenLength;
423 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
424 responseInfo.result = CA_CONTENT;
425 responseInfo.info = responseData;
427 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
429 CADestroyToken(tempToken);
432 CADestroyEndpoint(tempRep);
435 free(responseData.payload);
438 // CASelectNewwork TC
439 // check return value
440 TEST_F(CATests, SelectNetworkTestGood)
442 CAResult_t res = checkSelectNetwork();
443 EXPECT_EQ(CA_STATUS_OK, res);
446 CAResult_t checkSelectNetwork()
448 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
450 if (CA_STATUS_OK == res)
452 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
455 if (CA_NOT_SUPPORTED == res)
457 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
464 // check return value when selected network is disable
465 TEST_F(CATests, SelectNetworkTestBad)
467 //Select disable network
468 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
469 CA_TRANSPORT_ADAPTER_SCOPE));
472 // check return value when selected network is disable
473 TEST_F(CATests, UnSelectNetworkTest)
475 //UnSelect disable network
476 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
477 CA_TRANSPORT_ADAPTER_SCOPE));
480 // CAHandlerRequestResponse TC
481 // check return value
482 TEST_F(CATests, HandlerRequestResponseTest)
484 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
487 // CAGetNetworkInformation TC
488 // check return value
489 TEST_F (CATests, GetNetworkInformationTestGood)
491 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
494 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
497 if (SetCredentials() == 0)
499 printf("SetCredentials failed\n");
502 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
506 CAResult_t checkGetNetworkInfo()
508 CAEndpoint_t *tempInfo = NULL;
509 uint32_t tempSize = 0;
511 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
515 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
516 CA_NOT_SUPPORTED == res)
522 return CA_STATUS_FAILED;