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(const CAEndpoint_t * /*endpoint*/,
82 static char* addr = NULL;
83 static CAEndpoint_t* tempRep = NULL;
84 static CARequestInfo_t requestInfo;
85 static CAInfo_t requestData;
86 static CAInfo_t responseData;
87 static CAResponseInfo_t responseInfo;
88 static CAToken_t tempToken = NULL;
89 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
90 static const char ADDRESS[] = "10.11.12.13";
91 static const uint16_t PORT = 4545;
93 static const char NORMAL_INFO_DATA[] =
94 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
95 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
99 // Iotivity Device Identity.
100 const unsigned char IDENTITY[] = ("1111111111111111");
102 // PSK between this device and peer device.
103 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
105 // Internal API. Invoked by CA stack to retrieve credentials from this module
106 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
107 const unsigned char *desc, size_t desc_len,
108 unsigned char *result, size_t result_length)
110 printf("CAGetDtlsPskCredentials IN\n");
121 case CA_DTLS_PSK_HINT:
122 case CA_DTLS_PSK_IDENTITY:
124 if (result_length < sizeof(IDENTITY))
126 printf("ERROR : Wrong value for result for storing IDENTITY");
130 memcpy(result, IDENTITY, sizeof(IDENTITY));
131 ret = sizeof(IDENTITY);
134 case CA_DTLS_PSK_KEY:
136 if ((desc_len == sizeof(IDENTITY)) &&
137 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
139 if (result_length < sizeof(RS_CLIENT_PSK))
141 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
145 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
146 ret = sizeof(RS_CLIENT_PSK);
152 printf("Wrong value passed for PSK_CRED_TYPE.");
157 printf("CAGetDtlsPskCredentials OUT\n");
160 #endif //__WITH_DTLS__
163 // check return value
164 TEST(InitializeTest, TC_01_Positive_01)
166 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
171 TEST_F(CATests, TerminateTest)
175 char* check = (char *) "terminate success";
176 EXPECT_STREQ(check, "terminate success");
180 // CAStartListeningServer TC
181 // check return value
182 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
184 CASelectNetwork(CA_ADAPTER_IP);
185 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
188 // CAStartDiscoveryServer TC
189 // check return value
190 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
192 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
195 // CARegisterHandlerTest TC
196 // check return value
197 TEST_F(CATests, RegisterHandlerTest)
199 CARegisterHandler(request_handler, response_handler, error_handler);
200 char* check = (char *) "registerHandler success";
201 EXPECT_STREQ(check, "registerHandler success");
204 // CACreateRemoteEndpoint TC
205 // check return value
206 TEST_F(CATests, CreateRemoteEndpointTestGood)
208 addr = (char *) ADDRESS;
210 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
213 CADestroyEndpoint(tempRep);
217 // check remoteEndpoint and values of remoteEndpoint
218 TEST_F(CATests, CreateRemoteEndpointTestValues)
220 addr = (char *) ADDRESS;
222 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
224 EXPECT_TRUE(tempRep != NULL);
226 CADestroyEndpoint(tempRep);
231 // check return value
232 TEST_F(CATests, GenerateTokenTestGood)
234 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
236 CADestroyToken(tempToken);
239 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
240 TEST_F(CATests, GenerateTokenTestBad)
242 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
246 // check destroyed token
247 TEST_F(CATests, DestroyTokenTest)
249 CAGenerateToken(&tempToken, tokenLength);
250 CADestroyToken(tempToken);
252 char * check = (char *) "destroy success";
253 EXPECT_STREQ(check, "destroy success");
257 // check return value
258 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
260 addr = (char *) ADDRESS;
261 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
263 memset(&requestData, 0, sizeof(CAInfo_t));
264 CAGenerateToken(&tempToken, tokenLength);
265 requestData.token = tempToken;
266 requestData.tokenLength = tokenLength;
268 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
269 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
270 if(!requestData.payload)
272 CADestroyToken(tempToken);
273 FAIL() << "requestData.payload allocation failed";
275 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
276 requestData.payloadSize = length + 1;
277 requestData.type = CA_MSG_NONCONFIRM;
279 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
280 requestInfo.method = CA_GET;
281 requestInfo.info = requestData;
283 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
285 CADestroyToken(tempToken);
286 CADestroyEndpoint(tempRep);
287 free(requestData.payload);
291 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
292 TEST_F(CATests, SendRequestTestWithNullAddr)
294 addr = (char *) ADDRESS;
295 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
297 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
299 CADestroyEndpoint(tempRep);
304 // check return value
305 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
307 addr = (char *) ADDRESS;
308 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
310 memset(&responseData, 0, sizeof(CAInfo_t));
311 responseData.type = CA_MSG_NONCONFIRM;
312 responseData.messageId = 1;
313 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
315 EXPECT_TRUE(responseData.payload != NULL);
316 if(!responseData.payload)
318 CADestroyEndpoint(tempRep);
322 memcpy(responseData.payload, "response payload", sizeof("response payload"));
323 responseData.payloadSize = sizeof("response payload");
325 CAGenerateToken(&tempToken, tokenLength);
326 requestData.token = tempToken;
327 requestData.tokenLength = tokenLength;
329 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
330 responseInfo.result = CA_CONTENT;
331 responseInfo.info = responseData;
333 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
335 CADestroyToken(tempToken);
336 CADestroyEndpoint(tempRep);
337 free(responseData.payload);
341 // check return value when address is NULL as multicast
342 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
345 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
347 memset(&responseData, 0, sizeof(CAInfo_t));
348 responseData.type = CA_MSG_NONCONFIRM;
349 responseData.messageId = 1;
350 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
351 EXPECT_TRUE(responseData.payload != NULL);
353 if(!responseData.payload)
355 CADestroyEndpoint(tempRep);
359 memcpy(responseData.payload, "response payload", sizeof("response payload"));
360 responseData.payloadSize = sizeof("response payload");
362 CAGenerateToken(&tempToken, tokenLength);
363 requestData.token = tempToken;
364 requestData.tokenLength = tokenLength;
366 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
367 responseInfo.result = CA_CONTENT;
368 responseInfo.info = responseData;
370 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
372 CADestroyToken(tempToken);
373 CADestroyEndpoint(tempRep);
374 free (responseData.payload);
378 // check return value NULL is passed instead of a valid CAResponseInfo_t address
379 TEST_F(CATests, SendResponseTest)
381 addr = (char *) ADDRESS;
382 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
384 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
386 CADestroyEndpoint(tempRep);
390 // CASelectNewwork TC
391 // check return value
392 TEST_F(CATests, SelectNetworkTestGood)
394 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
397 CAResult_t checkSelectNetwork()
399 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
401 if (CA_STATUS_OK == res)
403 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
406 if (CA_NOT_SUPPORTED == res)
408 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
415 // check return value when selected network is disable
416 TEST_F(CATests, SelectNetworkTestBad)
418 //Select disable network
419 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
420 CA_TRANSPORT_ADAPTER_SCOPE));
423 // check return value when selected network is disable
424 TEST_F(CATests, UnSelectNetworkTest)
426 //UnSelect disable network
427 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
428 CA_TRANSPORT_ADAPTER_SCOPE));
431 // CAHandlerRequestResponse TC
432 // check return value
433 TEST_F(CATests, HandlerRequestResponseTest)
435 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
438 // CAGetNetworkInformation TC
439 // check return value
440 TEST_F (CATests, GetNetworkInformationTestGood)
442 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
445 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
448 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
452 // CARegisterNetworkMonitorHandler TC
453 // check return value
454 TEST_F(CATests, RegisterNetworkMonitorHandler)
456 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
457 connection_handler));
460 // CASetAutoConnectionDeviceInfo TC
461 // check return value
462 TEST_F(CATests, SetAutoConnectionDeviceInfo)
464 addr = (char *) ADDRESS;
466 #if defined(__ANDROID__) && defined(LE_ADAPTER)
467 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
469 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
473 // CAUnsetAutoConnectionDeviceInfo TC
474 // check return value
475 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
477 addr = (char *) ADDRESS;
479 #if defined(__ANDROID__) && defined(LE_ADAPTER)
480 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
482 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
486 CAResult_t checkGetNetworkInfo()
488 CAEndpoint_t *tempInfo = NULL;
489 uint32_t tempSize = 0;
491 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
495 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
496 CA_NOT_SUPPORTED == res)
502 return CA_STATUS_FAILED;