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 "platform_features.h"
22 #include "gtest/gtest.h"
23 #include "cainterface.h"
24 #include "cautilinterface.h"
27 #define CA_TRANSPORT_ADAPTER_SCOPE 1000
29 class CATests : public testing::Test {
31 virtual void SetUp() {
35 virtual void TearDown()
41 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
42 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
43 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
44 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
45 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
46 CAResult_t checkGetNetworkInfo();
47 CAResult_t checkSelectNetwork();
49 void request_handler(const CAEndpoint_t * /*object*/,
50 const CARequestInfo_t * /*requestInfo*/)
55 void response_handler(const CAEndpoint_t * /*object*/,
56 const CAResponseInfo_t * /*responseInfo*/)
61 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
63 if(!object || !errorInfo)
68 //error handling shall be added
72 void adapter_handler(CATransportAdapter_t /*adapter*/,
77 void connection_handler(CATransportAdapter_t /*adapter*/,
78 const char * /*remote_address*/,
83 static char* addr = NULL;
84 static CAEndpoint_t* tempRep = NULL;
85 static CARequestInfo_t requestInfo;
86 static CAInfo_t requestData;
87 static CAInfo_t responseData;
88 static CAResponseInfo_t responseInfo;
89 static CAToken_t tempToken = NULL;
90 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
91 static const char ADDRESS[] = "10.11.12.13";
92 static const uint16_t PORT = 4545;
94 static const char NORMAL_INFO_DATA[] =
95 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
96 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
100 // Iotivity Device Identity.
101 const unsigned char IDENTITY[] = ("1111111111111111");
103 // PSK between this device and peer device.
104 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
106 // Internal API. Invoked by CA stack to retrieve credentials from this module
107 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
108 const unsigned char *desc, size_t desc_len,
109 unsigned char *result, size_t result_length)
111 printf("CAGetDtlsPskCredentials IN\n");
122 case CA_DTLS_PSK_HINT:
123 case CA_DTLS_PSK_IDENTITY:
125 if (result_length < sizeof(IDENTITY))
127 printf("ERROR : Wrong value for result for storing IDENTITY");
131 memcpy(result, IDENTITY, sizeof(IDENTITY));
132 ret = sizeof(IDENTITY);
135 case CA_DTLS_PSK_KEY:
137 if ((desc_len == sizeof(IDENTITY)) &&
138 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
140 if (result_length < sizeof(RS_CLIENT_PSK))
142 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
146 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
147 ret = sizeof(RS_CLIENT_PSK);
153 printf("Wrong value passed for PSK_CRED_TYPE.");
158 printf("CAGetDtlsPskCredentials OUT\n");
161 #endif //__WITH_DTLS__
164 // check return value
165 TEST(InitializeTest, TC_01_Positive_01)
167 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
172 TEST_F(CATests, TerminateTest)
176 char* check = (char *) "terminate success";
177 EXPECT_STREQ(check, "terminate success");
181 // CAStartListeningServer TC
182 // check return value
183 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
185 CASelectNetwork(CA_ADAPTER_IP);
186 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
189 // CAStartDiscoveryServer TC
190 // check return value
191 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
193 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
196 // CARegisterHandlerTest TC
197 // check return value
198 TEST_F(CATests, RegisterHandlerTest)
200 CARegisterHandler(request_handler, response_handler, error_handler);
201 char* check = (char *) "registerHandler success";
202 EXPECT_STREQ(check, "registerHandler success");
205 // CACreateRemoteEndpoint TC
206 // check return value
207 TEST_F(CATests, CreateRemoteEndpointTestGood)
209 addr = (char *) ADDRESS;
211 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
214 CADestroyEndpoint(tempRep);
218 // check remoteEndpoint and values of remoteEndpoint
219 TEST_F(CATests, CreateRemoteEndpointTestValues)
221 addr = (char *) ADDRESS;
223 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
225 EXPECT_TRUE(tempRep != NULL);
227 CADestroyEndpoint(tempRep);
232 // check return value
233 TEST_F(CATests, GenerateTokenTestGood)
235 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
237 CADestroyToken(tempToken);
240 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
241 TEST_F(CATests, GenerateTokenTestBad)
243 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
247 // check destroyed token
248 TEST_F(CATests, DestroyTokenTest)
250 CAGenerateToken(&tempToken, tokenLength);
251 CADestroyToken(tempToken);
253 char * check = (char *) "destroy success";
254 EXPECT_STREQ(check, "destroy success");
258 // check return value
259 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
261 addr = (char *) ADDRESS;
262 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
264 memset(&requestData, 0, sizeof(CAInfo_t));
265 CAGenerateToken(&tempToken, tokenLength);
266 requestData.token = tempToken;
267 requestData.tokenLength = tokenLength;
269 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
270 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
271 if(!requestData.payload)
273 CADestroyToken(tempToken);
274 FAIL() << "requestData.payload allocation failed";
276 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
277 requestData.payloadSize = length + 1;
278 requestData.type = CA_MSG_NONCONFIRM;
280 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
281 requestInfo.method = CA_GET;
282 requestInfo.info = requestData;
284 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
286 CADestroyToken(tempToken);
287 CADestroyEndpoint(tempRep);
288 free(requestData.payload);
292 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
293 TEST_F(CATests, SendRequestTestWithNullAddr)
295 addr = (char *) ADDRESS;
296 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
298 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
300 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);
374 CADestroyEndpoint(tempRep);
375 free (responseData.payload);
379 // check return value NULL is passed instead of a valid CAResponseInfo_t address
380 TEST_F(CATests, SendResponseTest)
382 addr = (char *) ADDRESS;
383 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
385 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
387 CADestroyEndpoint(tempRep);
391 // CASelectNewwork TC
392 // check return value
393 TEST_F(CATests, SelectNetworkTestGood)
395 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
398 CAResult_t checkSelectNetwork()
400 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
402 if (CA_STATUS_OK == res)
404 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
407 if (CA_NOT_SUPPORTED == res)
409 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
416 // check return value when selected network is disable
417 TEST_F(CATests, SelectNetworkTestBad)
419 //Select disable network
420 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
421 CA_TRANSPORT_ADAPTER_SCOPE));
424 // check return value when selected network is disable
425 TEST_F(CATests, UnSelectNetworkTest)
427 //UnSelect disable network
428 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
429 CA_TRANSPORT_ADAPTER_SCOPE));
432 // CAHandlerRequestResponse TC
433 // check return value
434 TEST_F(CATests, HandlerRequestResponseTest)
436 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
439 // CAGetNetworkInformation TC
440 // check return value
441 TEST_F (CATests, GetNetworkInformationTestGood)
443 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
446 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
449 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
453 // CARegisterNetworkMonitorHandler TC
454 // check return value
455 TEST_F(CATests, RegisterNetworkMonitorHandler)
457 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
458 connection_handler));
461 // CASetAutoConnectionDeviceInfo TC
462 // check return value
463 TEST_F(CATests, SetAutoConnectionDeviceInfo)
465 addr = (char *) ADDRESS;
467 #if defined(__ANDROID__) && defined(LE_ADAPTER)
468 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
470 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
474 // CAUnsetAutoConnectionDeviceInfo TC
475 // check return value
476 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
478 addr = (char *) ADDRESS;
480 #if defined(__ANDROID__) && defined(LE_ADAPTER)
481 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
483 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
487 CAResult_t checkGetNetworkInfo()
489 CAEndpoint_t *tempInfo = NULL;
490 uint32_t tempSize = 0;
492 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
496 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
497 CA_NOT_SUPPORTED == res)
503 return CA_STATUS_FAILED;