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, const CARequestInfo_t *requestInfo)
49 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
54 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
56 if(!object || !errorInfo)
61 //error handling shall be added
65 static char* addr = NULL;
66 static CAEndpoint_t* tempRep = NULL;
67 static CARequestInfo_t requestInfo;
68 static CAInfo_t requestData;
69 static CAInfo_t responseData;
70 static CAResponseInfo_t responseInfo;
71 static CAToken_t tempToken = NULL;
72 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
73 static const char ADDRESS[] = "10.11.12.13";
74 static const uint16_t PORT = 4545;
76 static const char NORMAL_INFO_DATA[] =
77 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
78 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
86 #define IDENTITY ("1111111111111111")
90 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
92 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
94 void clearDtlsCredentialInfo()
96 printf("clearDtlsCredentialInfo IN\n");
99 // Initialize sensitive data to zeroes before freeing.
100 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
101 free(pskCredsBlob->creds);
103 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
107 printf("clearDtlsCredentialInfo OUT\n");
110 // Internal API. Invoked by OC stack to retrieve credentials from this module
111 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
113 printf("CAGetDtlsPskCredentials IN\n");
115 if (pskCredsBlob != NULL)
117 *credInfo = pskCredsBlob;
120 printf("CAGetDtlsPskCredentials OUT\n");
123 int32_t SetCredentials()
125 printf("SetCredentials IN\n");
126 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
128 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
129 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
131 pskCredsBlob->num = 1;
133 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
135 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
136 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
138 printf("SetCredentials OUT\n");
143 int main(int argc, char **argv)
145 testing::InitGoogleTest(&argc, argv);
146 return RUN_ALL_TESTS();
150 // check return value
151 TEST(InitializeTest, TC_01_Positive_01)
153 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
158 TEST_F(CATests, TerminateTest)
162 char* check = (char *) "terminate success";
163 EXPECT_STREQ(check, "terminate success");
167 // CAStartListeningServer TC
168 // check return value
169 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
171 CASelectNetwork(CA_ADAPTER_IP);
172 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
175 // CAStartDiscoveryServer TC
176 // check return value
177 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
179 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
182 // CARegisterHandlerTest TC
183 // check return value
184 TEST_F(CATests, RegisterHandlerTest)
186 CARegisterHandler(request_handler, response_handler, error_handler);
187 char* check = (char *) "registerHandler success";
188 EXPECT_STREQ(check, "registerHandler success");
191 // CACreateRemoteEndpoint TC
192 // check return value
193 TEST_F(CATests, CreateRemoteEndpointTestGood)
195 addr = (char *) ADDRESS;
197 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
202 CADestroyEndpoint(tempRep);
207 // check remoteEndpoint and values of remoteEndpoint
208 TEST_F(CATests, CreateRemoteEndpointTestValues)
210 addr = (char *) ADDRESS;
212 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
214 EXPECT_TRUE(tempRep != NULL);
218 CADestroyEndpoint(tempRep);
224 // check return value
225 TEST_F(CATests, GenerateTokenTestGood)
227 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
229 CADestroyToken(tempToken);
232 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
233 TEST_F(CATests, GenerateTokenTestBad)
235 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
239 // check destroyed token
240 TEST_F(CATests, DestroyTokenTest)
242 CAGenerateToken(&tempToken, tokenLength);
243 CADestroyToken(tempToken);
245 char * check = (char *) "destroy success";
246 EXPECT_STREQ(check, "destroy success");
250 // check return value
251 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
253 addr = (char *) ADDRESS;
254 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
256 memset(&requestData, 0, sizeof(CAInfo_t));
257 CAGenerateToken(&tempToken, tokenLength);
258 requestData.token = tempToken;
259 requestData.tokenLength = tokenLength;
261 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
262 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
263 if(!requestData.payload)
265 CADestroyToken(tempToken);
266 FAIL() << "requestData.payload allocation failed";
268 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
269 requestData.payloadSize = length + 1;
270 requestData.type = CA_MSG_NONCONFIRM;
272 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
273 requestInfo.method = CA_GET;
274 requestInfo.info = requestData;
276 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
278 CADestroyToken(tempToken);
280 free(requestData.payload);
282 CADestroyEndpoint(tempRep);
287 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
288 TEST_F(CATests, SendRequestTestWithNullAddr)
290 addr = (char *) ADDRESS;
291 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
293 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
297 CADestroyEndpoint(tempRep);
303 // check return value
304 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
306 addr = (char *) ADDRESS;
307 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
309 memset(&responseData, 0, sizeof(CAInfo_t));
310 responseData.type = CA_MSG_NONCONFIRM;
311 responseData.messageId = 1;
312 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
314 EXPECT_TRUE(responseData.payload != NULL);
315 if(!responseData.payload)
317 CADestroyEndpoint(tempRep);
321 memcpy(responseData.payload, "response payload", sizeof("response payload"));
322 responseData.payloadSize = sizeof("response payload");
324 CAGenerateToken(&tempToken, tokenLength);
325 requestData.token = tempToken;
326 requestData.tokenLength = tokenLength;
328 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
329 responseInfo.result = CA_CONTENT;
330 responseInfo.info = responseData;
332 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
334 CADestroyToken(tempToken);
335 CADestroyEndpoint(tempRep);
336 free(responseData.payload);
340 // check return value when address is NULL as multicast
341 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
344 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
346 memset(&responseData, 0, sizeof(CAInfo_t));
347 responseData.type = CA_MSG_NONCONFIRM;
348 responseData.messageId = 1;
349 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
350 EXPECT_TRUE(responseData.payload != NULL);
352 if(!responseData.payload)
354 CADestroyEndpoint(tempRep);
358 memcpy(responseData.payload, "response payload", sizeof("response payload"));
359 responseData.payloadSize = sizeof("response payload");
361 CAGenerateToken(&tempToken, tokenLength);
362 requestData.token = tempToken;
363 requestData.tokenLength = tokenLength;
365 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
366 responseInfo.result = CA_CONTENT;
367 responseInfo.info = responseData;
369 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
371 CADestroyToken(tempToken);
374 CADestroyEndpoint(tempRep);
377 free (responseData.payload);
380 // check return value NULL is passed instead of a valid CAResponseInfo_t address
381 TEST_F(CATests, SendResponseTest)
383 addr = (char *) ADDRESS;
384 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
386 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
390 CADestroyEndpoint(tempRep);
395 // CASendNotification TC
396 // check return value
397 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
399 addr = (char *) ADDRESS;
400 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
402 memset(&responseData, 0, sizeof(CAInfo_t));
403 responseData.type = CA_MSG_NONCONFIRM;
404 responseData.payload = (CAPayload_t)malloc(sizeof("Temp Notification Data"));
406 EXPECT_TRUE(responseData.payload != NULL);
407 if(!responseData.payload)
409 CADestroyEndpoint(tempRep);
413 memcpy(responseData.payload, "Temp Notification Data", sizeof("Temp Notification Data"));
414 responseData.payloadSize = sizeof("Temp Notification Data");
416 CAGenerateToken(&tempToken, tokenLength);
417 requestData.token = tempToken;
418 requestData.tokenLength = tokenLength;
420 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
421 responseInfo.result = CA_CONTENT;
422 responseInfo.info = responseData;
424 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
426 CADestroyToken(tempToken);
429 CADestroyEndpoint(tempRep);
432 free(responseData.payload);
435 // CASelectNewwork TC
436 // check return value
437 TEST_F(CATests, SelectNetworkTestGood)
439 CAResult_t res = checkSelectNetwork();
440 EXPECT_EQ(CA_STATUS_OK, res);
443 CAResult_t checkSelectNetwork()
445 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
447 if (CA_STATUS_OK == res)
449 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
452 if (CA_NOT_SUPPORTED == res)
454 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
461 // check return value when selected network is disable
462 TEST_F(CATests, SelectNetworkTestBad)
464 //Select disable network
465 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)1000));
468 // check return value when selected network is disable
469 TEST_F(CATests, UnSelectNetworkTest)
471 //UnSelect disable network
472 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)1000));
475 // CAHandlerRequestResponse TC
476 // check return value
477 TEST_F(CATests, HandlerRequestResponseTest)
479 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
482 // CAGetNetworkInformation TC
483 // check return value
484 TEST_F (CATests, GetNetworkInformationTestGood)
486 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
489 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
492 if (SetCredentials() == 0)
494 printf("SetCredentials failed\n");
497 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
501 CAResult_t checkGetNetworkInfo()
503 CAEndpoint_t *tempInfo = NULL;
504 uint32_t tempSize = 0;
506 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
510 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
511 CA_NOT_SUPPORTED == res)
517 return CA_STATUS_FAILED;