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"
23 #include "cautilinterface.h"
26 #define CA_TRANSPORT_ADAPTER_SCOPE 1000
28 class CATests : public testing::Test {
30 virtual void SetUp() {
34 virtual void TearDown()
40 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
41 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
42 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
43 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
44 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
45 CAResult_t checkGetNetworkInfo();
46 CAResult_t checkSelectNetwork();
48 void request_handler(const CAEndpoint_t * /*object*/,
49 const CARequestInfo_t * /*requestInfo*/)
54 void response_handler(const CAEndpoint_t * /*object*/,
55 const CAResponseInfo_t * /*responseInfo*/)
60 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
62 if(!object || !errorInfo)
67 //error handling shall be added
71 void adapter_handler(CATransportAdapter_t /*adapter*/,
76 void connection_handler(CATransportAdapter_t /*adapter*/,
77 const char * /*remote_address*/,
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__
162 int main(int argc, char **argv)
164 testing::InitGoogleTest(&argc, argv);
165 return RUN_ALL_TESTS();
169 // check return value
170 TEST(InitializeTest, TC_01_Positive_01)
172 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
177 TEST_F(CATests, TerminateTest)
181 char* check = (char *) "terminate success";
182 EXPECT_STREQ(check, "terminate success");
186 // CAStartListeningServer TC
187 // check return value
188 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
190 CASelectNetwork(CA_ADAPTER_IP);
191 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
194 // CAStartDiscoveryServer TC
195 // check return value
196 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
198 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
201 // CARegisterHandlerTest TC
202 // check return value
203 TEST_F(CATests, RegisterHandlerTest)
205 CARegisterHandler(request_handler, response_handler, error_handler);
206 char* check = (char *) "registerHandler success";
207 EXPECT_STREQ(check, "registerHandler success");
210 // CACreateRemoteEndpoint TC
211 // check return value
212 TEST_F(CATests, CreateRemoteEndpointTestGood)
214 addr = (char *) ADDRESS;
216 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
219 CADestroyEndpoint(tempRep);
223 // check remoteEndpoint and values of remoteEndpoint
224 TEST_F(CATests, CreateRemoteEndpointTestValues)
226 addr = (char *) ADDRESS;
228 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
230 EXPECT_TRUE(tempRep != NULL);
232 CADestroyEndpoint(tempRep);
237 // check return value
238 TEST_F(CATests, GenerateTokenTestGood)
240 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
242 CADestroyToken(tempToken);
245 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
246 TEST_F(CATests, GenerateTokenTestBad)
248 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
252 // check destroyed token
253 TEST_F(CATests, DestroyTokenTest)
255 CAGenerateToken(&tempToken, tokenLength);
256 CADestroyToken(tempToken);
258 char * check = (char *) "destroy success";
259 EXPECT_STREQ(check, "destroy success");
263 // check return value
264 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
266 addr = (char *) ADDRESS;
267 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
269 memset(&requestData, 0, sizeof(CAInfo_t));
270 CAGenerateToken(&tempToken, tokenLength);
271 requestData.token = tempToken;
272 requestData.tokenLength = tokenLength;
274 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
275 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
276 if(!requestData.payload)
278 CADestroyToken(tempToken);
279 FAIL() << "requestData.payload allocation failed";
281 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
282 requestData.payloadSize = length + 1;
283 requestData.type = CA_MSG_NONCONFIRM;
285 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
286 requestInfo.method = CA_GET;
287 requestInfo.info = requestData;
289 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
291 CADestroyToken(tempToken);
292 CADestroyEndpoint(tempRep);
293 free(requestData.payload);
297 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
298 TEST_F(CATests, SendRequestTestWithNullAddr)
300 addr = (char *) ADDRESS;
301 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
303 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
305 CADestroyEndpoint(tempRep);
310 // check return value
311 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
313 addr = (char *) ADDRESS;
314 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
316 memset(&responseData, 0, sizeof(CAInfo_t));
317 responseData.type = CA_MSG_NONCONFIRM;
318 responseData.messageId = 1;
319 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
321 EXPECT_TRUE(responseData.payload != NULL);
322 if(!responseData.payload)
324 CADestroyEndpoint(tempRep);
328 memcpy(responseData.payload, "response payload", sizeof("response payload"));
329 responseData.payloadSize = sizeof("response payload");
331 CAGenerateToken(&tempToken, tokenLength);
332 requestData.token = tempToken;
333 requestData.tokenLength = tokenLength;
335 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
336 responseInfo.result = CA_CONTENT;
337 responseInfo.info = responseData;
339 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
341 CADestroyToken(tempToken);
342 CADestroyEndpoint(tempRep);
343 free(responseData.payload);
347 // check return value when address is NULL as multicast
348 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
351 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
353 memset(&responseData, 0, sizeof(CAInfo_t));
354 responseData.type = CA_MSG_NONCONFIRM;
355 responseData.messageId = 1;
356 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
357 EXPECT_TRUE(responseData.payload != NULL);
359 if(!responseData.payload)
361 CADestroyEndpoint(tempRep);
365 memcpy(responseData.payload, "response payload", sizeof("response payload"));
366 responseData.payloadSize = sizeof("response payload");
368 CAGenerateToken(&tempToken, tokenLength);
369 requestData.token = tempToken;
370 requestData.tokenLength = tokenLength;
372 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
373 responseInfo.result = CA_CONTENT;
374 responseInfo.info = responseData;
376 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
378 CADestroyToken(tempToken);
379 CADestroyEndpoint(tempRep);
380 free (responseData.payload);
384 // check return value NULL is passed instead of a valid CAResponseInfo_t address
385 TEST_F(CATests, SendResponseTest)
387 addr = (char *) ADDRESS;
388 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
390 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
392 CADestroyEndpoint(tempRep);
396 // CASelectNewwork TC
397 // check return value
398 TEST_F(CATests, SelectNetworkTestGood)
400 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
403 CAResult_t checkSelectNetwork()
405 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
407 if (CA_STATUS_OK == res)
409 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
412 if (CA_NOT_SUPPORTED == res)
414 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
421 // check return value when selected network is disable
422 TEST_F(CATests, SelectNetworkTestBad)
424 //Select disable network
425 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
426 CA_TRANSPORT_ADAPTER_SCOPE));
429 // check return value when selected network is disable
430 TEST_F(CATests, UnSelectNetworkTest)
432 //UnSelect disable network
433 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
434 CA_TRANSPORT_ADAPTER_SCOPE));
437 // CAHandlerRequestResponse TC
438 // check return value
439 TEST_F(CATests, HandlerRequestResponseTest)
441 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
444 // CAGetNetworkInformation TC
445 // check return value
446 TEST_F (CATests, GetNetworkInformationTestGood)
448 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
451 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
454 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
458 // CARegisterNetworkMonitorHandler TC
459 // check return value
460 TEST_F(CATests, RegisterNetworkMonitorHandler)
463 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
464 connection_handler));
466 EXPECT_EQ(CA_NOT_SUPPORTED, CARegisterNetworkMonitorHandler(adapter_handler,
467 connection_handler));
471 // CASetAutoConnectionDeviceInfo TC
472 // check return value
473 TEST_F(CATests, SetAutoConnectionDeviceInfo)
475 addr = (char *) ADDRESS;
477 #if defined(__ANDROID__) && defined(LE_ADAPTER)
478 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
480 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
484 // CAUnsetAutoConnectionDeviceInfo TC
485 // check return value
486 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
488 addr = (char *) ADDRESS;
490 #if defined(__ANDROID__) && defined(LE_ADAPTER)
491 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
493 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
497 CAResult_t checkGetNetworkInfo()
499 CAEndpoint_t *tempInfo = NULL;
500 uint32_t tempSize = 0;
502 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
506 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
507 CA_NOT_SUPPORTED == res)
513 return CA_STATUS_FAILED;