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(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
39 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
40 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
41 CAResult_t checkGetNetworkInfo();
42 CAResult_t checkSelectNetwork();
44 void request_handler(const CAEndpoint_t * /*object*/,
45 const CARequestInfo_t * /*requestInfo*/)
50 void response_handler(const CAEndpoint_t * /*object*/,
51 const CAResponseInfo_t * /*responseInfo*/)
56 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
58 if(!object || !errorInfo)
63 //error handling shall be added
67 static char* addr = NULL;
68 static CAEndpoint_t* tempRep = NULL;
69 static CARequestInfo_t requestInfo;
70 static CAInfo_t requestData;
71 static CAInfo_t responseData;
72 static CAResponseInfo_t responseInfo;
73 static CAToken_t tempToken = NULL;
74 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
75 static const char ADDRESS[] = "10.11.12.13";
76 static const uint16_t PORT = 4545;
78 static const char NORMAL_INFO_DATA[] =
79 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
80 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
88 #define IDENTITY ("1111111111111111")
92 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
94 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
96 void clearDtlsCredentialInfo()
98 printf("clearDtlsCredentialInfo IN\n");
101 // Initialize sensitive data to zeroes before freeing.
102 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
103 free(pskCredsBlob->creds);
105 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
109 printf("clearDtlsCredentialInfo OUT\n");
112 // Internal API. Invoked by OC stack to retrieve credentials from this module
113 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
115 printf("CAGetDtlsPskCredentials IN\n");
117 if (pskCredsBlob != NULL)
119 *credInfo = pskCredsBlob;
122 printf("CAGetDtlsPskCredentials OUT\n");
125 int32_t SetCredentials()
127 printf("SetCredentials IN\n");
128 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
130 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
131 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
133 pskCredsBlob->num = 1;
135 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
137 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
138 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
140 printf("SetCredentials OUT\n");
145 int main(int argc, char **argv)
147 testing::InitGoogleTest(&argc, argv);
148 return RUN_ALL_TESTS();
152 // check return value
153 TEST(InitializeTest, TC_01_Positive_01)
155 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
160 TEST_F(CATests, TerminateTest)
164 char* check = (char *) "terminate success";
165 EXPECT_STREQ(check, "terminate success");
169 // CAStartListeningServer TC
170 // check return value
171 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
173 CASelectNetwork(CA_ADAPTER_IP);
174 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
177 // CAStartDiscoveryServer TC
178 // check return value
179 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
181 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
184 // CARegisterHandlerTest TC
185 // check return value
186 TEST_F(CATests, RegisterHandlerTest)
188 CARegisterHandler(request_handler, response_handler, error_handler);
189 char* check = (char *) "registerHandler success";
190 EXPECT_STREQ(check, "registerHandler success");
193 // CACreateRemoteEndpoint TC
194 // check return value
195 TEST_F(CATests, CreateRemoteEndpointTestGood)
197 addr = (char *) ADDRESS;
199 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
204 CADestroyEndpoint(tempRep);
209 // check remoteEndpoint and values of remoteEndpoint
210 TEST_F(CATests, CreateRemoteEndpointTestValues)
212 addr = (char *) ADDRESS;
214 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
216 EXPECT_TRUE(tempRep != NULL);
220 CADestroyEndpoint(tempRep);
226 // check return value
227 TEST_F(CATests, GenerateTokenTestGood)
229 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
231 CADestroyToken(tempToken);
234 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
235 TEST_F(CATests, GenerateTokenTestBad)
237 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
241 // check destroyed token
242 TEST_F(CATests, DestroyTokenTest)
244 CAGenerateToken(&tempToken, tokenLength);
245 CADestroyToken(tempToken);
247 char * check = (char *) "destroy success";
248 EXPECT_STREQ(check, "destroy success");
252 // check return value
253 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
255 addr = (char *) ADDRESS;
256 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
258 memset(&requestData, 0, sizeof(CAInfo_t));
259 CAGenerateToken(&tempToken, tokenLength);
260 requestData.token = tempToken;
261 requestData.tokenLength = tokenLength;
263 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
264 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
265 if(!requestData.payload)
267 CADestroyToken(tempToken);
268 FAIL() << "requestData.payload allocation failed";
270 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
271 requestData.payloadSize = length + 1;
272 requestData.type = CA_MSG_NONCONFIRM;
274 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
275 requestInfo.method = CA_GET;
276 requestInfo.info = requestData;
278 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
280 CADestroyToken(tempToken);
282 free(requestData.payload);
284 CADestroyEndpoint(tempRep);
289 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
290 TEST_F(CATests, SendRequestTestWithNullAddr)
292 addr = (char *) ADDRESS;
293 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
295 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
299 CADestroyEndpoint(tempRep);
305 // check return value
306 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
308 addr = (char *) ADDRESS;
309 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
311 memset(&responseData, 0, sizeof(CAInfo_t));
312 responseData.type = CA_MSG_NONCONFIRM;
313 responseData.messageId = 1;
314 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
316 EXPECT_TRUE(responseData.payload != NULL);
317 if(!responseData.payload)
319 CADestroyEndpoint(tempRep);
323 memcpy(responseData.payload, "response payload", sizeof("response payload"));
324 responseData.payloadSize = sizeof("response payload");
326 CAGenerateToken(&tempToken, tokenLength);
327 requestData.token = tempToken;
328 requestData.tokenLength = tokenLength;
330 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
331 responseInfo.result = CA_CONTENT;
332 responseInfo.info = responseData;
334 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
336 CADestroyToken(tempToken);
337 CADestroyEndpoint(tempRep);
338 free(responseData.payload);
342 // check return value when address is NULL as multicast
343 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
346 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
348 memset(&responseData, 0, sizeof(CAInfo_t));
349 responseData.type = CA_MSG_NONCONFIRM;
350 responseData.messageId = 1;
351 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
352 EXPECT_TRUE(responseData.payload != NULL);
354 if(!responseData.payload)
356 CADestroyEndpoint(tempRep);
360 memcpy(responseData.payload, "response payload", sizeof("response payload"));
361 responseData.payloadSize = sizeof("response payload");
363 CAGenerateToken(&tempToken, tokenLength);
364 requestData.token = tempToken;
365 requestData.tokenLength = tokenLength;
367 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
368 responseInfo.result = CA_CONTENT;
369 responseInfo.info = responseData;
371 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
373 CADestroyToken(tempToken);
376 CADestroyEndpoint(tempRep);
379 free (responseData.payload);
382 // check return value NULL is passed instead of a valid CAResponseInfo_t address
383 TEST_F(CATests, SendResponseTest)
385 addr = (char *) ADDRESS;
386 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
388 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
392 CADestroyEndpoint(tempRep);
397 // CASendNotification TC
398 // check return value
399 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
401 addr = (char *) ADDRESS;
402 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
404 memset(&responseData, 0, sizeof(CAInfo_t));
405 responseData.type = CA_MSG_NONCONFIRM;
406 responseData.payload = (CAPayload_t)malloc(sizeof("Temp Notification Data"));
408 EXPECT_TRUE(responseData.payload != NULL);
409 if(!responseData.payload)
411 CADestroyEndpoint(tempRep);
415 memcpy(responseData.payload, "Temp Notification Data", sizeof("Temp Notification Data"));
416 responseData.payloadSize = sizeof("Temp Notification Data");
418 CAGenerateToken(&tempToken, tokenLength);
419 requestData.token = tempToken;
420 requestData.tokenLength = tokenLength;
422 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
423 responseInfo.result = CA_CONTENT;
424 responseInfo.info = responseData;
426 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
428 CADestroyToken(tempToken);
431 CADestroyEndpoint(tempRep);
434 free(responseData.payload);
437 // CASelectNewwork TC
438 // check return value
439 TEST_F(CATests, SelectNetworkTestGood)
441 CAResult_t res = checkSelectNetwork();
442 EXPECT_EQ(CA_STATUS_OK, res);
445 CAResult_t checkSelectNetwork()
447 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
449 if (CA_STATUS_OK == res)
451 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
454 if (CA_NOT_SUPPORTED == res)
456 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
463 // check return value when selected network is disable
464 TEST_F(CATests, SelectNetworkTestBad)
466 //Select disable network
467 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)10000));
470 // check return value when selected network is disable
471 TEST_F(CATests, UnSelectNetworkTest)
473 //UnSelect disable network
474 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)10000));
477 // CAHandlerRequestResponse TC
478 // check return value
479 TEST_F(CATests, HandlerRequestResponseTest)
481 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
484 // CAGetNetworkInformation TC
485 // check return value
486 TEST_F (CATests, GetNetworkInformationTestGood)
488 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
491 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
494 if (SetCredentials() == 0)
496 printf("SetCredentials failed\n");
499 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
503 CAResult_t checkGetNetworkInfo()
505 CAEndpoint_t *tempInfo = NULL;
506 uint32_t tempSize = 0;
508 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
512 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
513 CA_NOT_SUPPORTED == res)
519 return CA_STATUS_FAILED;