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,
77 void connection_handler(CATransportAdapter_t adapter,
78 const char *remote_address,
84 static char* addr = NULL;
85 static CAEndpoint_t* tempRep = NULL;
86 static CARequestInfo_t requestInfo;
87 static CAInfo_t requestData;
88 static CAInfo_t responseData;
89 static CAResponseInfo_t responseInfo;
90 static CAToken_t tempToken = NULL;
91 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
92 static const char ADDRESS[] = "10.11.12.13";
93 static const uint16_t PORT = 4545;
95 static const char NORMAL_INFO_DATA[] =
96 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
97 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
101 // Iotivity Device Identity.
102 const unsigned char IDENTITY[] = ("1111111111111111");
104 // PSK between this device and peer device.
105 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
107 // Internal API. Invoked by CA stack to retrieve credentials from this module
108 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
109 const unsigned char *desc, size_t desc_len,
110 unsigned char *result, size_t result_length)
112 printf("CAGetDtlsPskCredentials IN\n");
123 case CA_DTLS_PSK_HINT:
124 case CA_DTLS_PSK_IDENTITY:
126 if (result_length < sizeof(IDENTITY))
128 printf("ERROR : Wrong value for result for storing IDENTITY");
132 memcpy(result, IDENTITY, sizeof(IDENTITY));
133 ret = sizeof(IDENTITY);
136 case CA_DTLS_PSK_KEY:
138 if ((desc_len == sizeof(IDENTITY)) &&
139 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
141 if (result_length < sizeof(RS_CLIENT_PSK))
143 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
147 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
148 ret = sizeof(RS_CLIENT_PSK);
154 printf("Wrong value passed for PSK_CRED_TYPE.");
159 printf("CAGetDtlsPskCredentials OUT\n");
162 #endif //__WITH_DTLS__
164 int main(int argc, char **argv)
166 testing::InitGoogleTest(&argc, argv);
167 return RUN_ALL_TESTS();
171 // check return value
172 TEST(InitializeTest, TC_01_Positive_01)
174 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
179 TEST_F(CATests, TerminateTest)
183 char* check = (char *) "terminate success";
184 EXPECT_STREQ(check, "terminate success");
188 // CAStartListeningServer TC
189 // check return value
190 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
192 CASelectNetwork(CA_ADAPTER_IP);
193 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
196 // CAStartDiscoveryServer TC
197 // check return value
198 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
200 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
203 // CARegisterHandlerTest TC
204 // check return value
205 TEST_F(CATests, RegisterHandlerTest)
207 CARegisterHandler(request_handler, response_handler, error_handler);
208 char* check = (char *) "registerHandler success";
209 EXPECT_STREQ(check, "registerHandler success");
212 // CACreateRemoteEndpoint TC
213 // check return value
214 TEST_F(CATests, CreateRemoteEndpointTestGood)
216 addr = (char *) ADDRESS;
218 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
221 CADestroyEndpoint(tempRep);
225 // check remoteEndpoint and values of remoteEndpoint
226 TEST_F(CATests, CreateRemoteEndpointTestValues)
228 addr = (char *) ADDRESS;
230 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
232 EXPECT_TRUE(tempRep != NULL);
234 CADestroyEndpoint(tempRep);
239 // check return value
240 TEST_F(CATests, GenerateTokenTestGood)
242 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
244 CADestroyToken(tempToken);
247 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
248 TEST_F(CATests, GenerateTokenTestBad)
250 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
254 // check destroyed token
255 TEST_F(CATests, DestroyTokenTest)
257 CAGenerateToken(&tempToken, tokenLength);
258 CADestroyToken(tempToken);
260 char * check = (char *) "destroy success";
261 EXPECT_STREQ(check, "destroy success");
265 // check return value
266 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
268 addr = (char *) ADDRESS;
269 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
271 memset(&requestData, 0, sizeof(CAInfo_t));
272 CAGenerateToken(&tempToken, tokenLength);
273 requestData.token = tempToken;
274 requestData.tokenLength = tokenLength;
276 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
277 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
278 if(!requestData.payload)
280 CADestroyToken(tempToken);
281 FAIL() << "requestData.payload allocation failed";
283 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
284 requestData.payloadSize = length + 1;
285 requestData.type = CA_MSG_NONCONFIRM;
287 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
288 requestInfo.method = CA_GET;
289 requestInfo.info = requestData;
291 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
293 CADestroyToken(tempToken);
294 CADestroyEndpoint(tempRep);
295 free(requestData.payload);
299 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
300 TEST_F(CATests, SendRequestTestWithNullAddr)
302 addr = (char *) ADDRESS;
303 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
305 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
307 CADestroyEndpoint(tempRep);
312 // check return value
313 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
315 addr = (char *) ADDRESS;
316 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
318 memset(&responseData, 0, sizeof(CAInfo_t));
319 responseData.type = CA_MSG_NONCONFIRM;
320 responseData.messageId = 1;
321 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
323 EXPECT_TRUE(responseData.payload != NULL);
324 if(!responseData.payload)
326 CADestroyEndpoint(tempRep);
330 memcpy(responseData.payload, "response payload", sizeof("response payload"));
331 responseData.payloadSize = sizeof("response payload");
333 CAGenerateToken(&tempToken, tokenLength);
334 requestData.token = tempToken;
335 requestData.tokenLength = tokenLength;
337 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
338 responseInfo.result = CA_CONTENT;
339 responseInfo.info = responseData;
341 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
343 CADestroyToken(tempToken);
344 CADestroyEndpoint(tempRep);
345 free(responseData.payload);
349 // check return value when address is NULL as multicast
350 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
353 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
355 memset(&responseData, 0, sizeof(CAInfo_t));
356 responseData.type = CA_MSG_NONCONFIRM;
357 responseData.messageId = 1;
358 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
359 EXPECT_TRUE(responseData.payload != NULL);
361 if(!responseData.payload)
363 CADestroyEndpoint(tempRep);
367 memcpy(responseData.payload, "response payload", sizeof("response payload"));
368 responseData.payloadSize = sizeof("response payload");
370 CAGenerateToken(&tempToken, tokenLength);
371 requestData.token = tempToken;
372 requestData.tokenLength = tokenLength;
374 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
375 responseInfo.result = CA_CONTENT;
376 responseInfo.info = responseData;
378 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
380 CADestroyToken(tempToken);
381 CADestroyEndpoint(tempRep);
382 free (responseData.payload);
386 // check return value NULL is passed instead of a valid CAResponseInfo_t address
387 TEST_F(CATests, SendResponseTest)
389 addr = (char *) ADDRESS;
390 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
392 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
394 CADestroyEndpoint(tempRep);
398 // CASelectNewwork TC
399 // check return value
400 TEST_F(CATests, SelectNetworkTestGood)
402 EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
405 CAResult_t checkSelectNetwork()
407 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
409 if (CA_STATUS_OK == res)
411 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
414 if (CA_NOT_SUPPORTED == res)
416 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
423 // check return value when selected network is disable
424 TEST_F(CATests, SelectNetworkTestBad)
426 //Select disable network
427 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
428 CA_TRANSPORT_ADAPTER_SCOPE));
431 // check return value when selected network is disable
432 TEST_F(CATests, UnSelectNetworkTest)
434 //UnSelect disable network
435 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
436 CA_TRANSPORT_ADAPTER_SCOPE));
439 // CAHandlerRequestResponse TC
440 // check return value
441 TEST_F(CATests, HandlerRequestResponseTest)
443 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
446 // CAGetNetworkInformation TC
447 // check return value
448 TEST_F (CATests, GetNetworkInformationTestGood)
450 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
453 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
456 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
460 // CARegisterNetworkMonitorHandler TC
461 // check return value
462 TEST_F(CATests, RegisterNetworkMonitorHandler)
465 EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
466 connection_handler));
468 EXPECT_EQ(CA_NOT_SUPPORTED, CARegisterNetworkMonitorHandler(adapter_handler,
469 connection_handler));
473 // CASetAutoConnectionDeviceInfo TC
474 // check return value
475 TEST_F(CATests, SetAutoConnectionDeviceInfo)
477 addr = (char *) ADDRESS;
479 #if defined(__ANDROID__) && defined(LE_ADAPTER)
480 EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
482 EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
486 // CAUnsetAutoConnectionDeviceInfo TC
487 // check return value
488 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
490 addr = (char *) ADDRESS;
492 #if defined(__ANDROID__) && defined(LE_ADAPTER)
493 EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
495 EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
499 CAResult_t checkGetNetworkInfo()
501 CAEndpoint_t *tempInfo = NULL;
502 uint32_t tempSize = 0;
504 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
508 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
509 CA_NOT_SUPPORTED == res)
515 return CA_STATUS_FAILED;