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"
25 #define CA_TRANSPORT_ADAPTER_SCOPE 1000
27 class CATests : public testing::Test {
29 virtual void SetUp() {
33 virtual void TearDown()
39 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
40 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
41 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
42 CAResult_t checkGetNetworkInfo();
43 CAResult_t checkSelectNetwork();
45 void request_handler(const CAEndpoint_t * /*object*/,
46 const CARequestInfo_t * /*requestInfo*/)
51 void response_handler(const CAEndpoint_t * /*object*/,
52 const CAResponseInfo_t * /*responseInfo*/)
57 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
59 if(!object || !errorInfo)
64 //error handling shall be added
68 static char* addr = NULL;
69 static CAEndpoint_t* tempRep = NULL;
70 static CARequestInfo_t requestInfo;
71 static CAInfo_t requestData;
72 static CAInfo_t responseData;
73 static CAResponseInfo_t responseInfo;
74 static CAToken_t tempToken = NULL;
75 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
76 static const char ADDRESS[] = "10.11.12.13";
77 static const uint16_t PORT = 4545;
79 static const char NORMAL_INFO_DATA[] =
80 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
81 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
85 // Iotivity Device Identity.
86 const unsigned char IDENTITY[] = ("1111111111111111");
88 // PSK between this device and peer device.
89 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
91 // Internal API. Invoked by CA stack to retrieve credentials from this module
92 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
93 const unsigned char *desc, size_t desc_len,
94 unsigned char *result, size_t result_length)
96 printf("CAGetDtlsPskCredentials IN\n");
107 case CA_DTLS_PSK_HINT:
108 case CA_DTLS_PSK_IDENTITY:
110 if (result_length < sizeof(IDENTITY))
112 printf("ERROR : Wrong value for result for storing IDENTITY");
116 memcpy(result, IDENTITY, sizeof(IDENTITY));
117 ret = sizeof(IDENTITY);
120 case CA_DTLS_PSK_KEY:
122 if ((desc_len == sizeof(IDENTITY)) &&
123 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
125 if (result_length < sizeof(RS_CLIENT_PSK))
127 printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
131 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
132 ret = sizeof(RS_CLIENT_PSK);
138 printf("Wrong value passed for PSK_CRED_TYPE.");
143 printf("CAGetDtlsPskCredentials OUT\n");
146 #endif //__WITH_DTLS__
148 int main(int argc, char **argv)
150 testing::InitGoogleTest(&argc, argv);
151 return RUN_ALL_TESTS();
155 // check return value
156 TEST(InitializeTest, TC_01_Positive_01)
158 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
163 TEST_F(CATests, TerminateTest)
167 char* check = (char *) "terminate success";
168 EXPECT_STREQ(check, "terminate success");
172 // CAStartListeningServer TC
173 // check return value
174 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
176 CASelectNetwork(CA_ADAPTER_IP);
177 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
180 // CAStartDiscoveryServer TC
181 // check return value
182 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
184 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
187 // CARegisterHandlerTest TC
188 // check return value
189 TEST_F(CATests, RegisterHandlerTest)
191 CARegisterHandler(request_handler, response_handler, error_handler);
192 char* check = (char *) "registerHandler success";
193 EXPECT_STREQ(check, "registerHandler success");
196 // CACreateRemoteEndpoint TC
197 // check return value
198 TEST_F(CATests, CreateRemoteEndpointTestGood)
200 addr = (char *) ADDRESS;
202 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
207 CADestroyEndpoint(tempRep);
212 // check remoteEndpoint and values of remoteEndpoint
213 TEST_F(CATests, CreateRemoteEndpointTestValues)
215 addr = (char *) ADDRESS;
217 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
219 EXPECT_TRUE(tempRep != NULL);
223 CADestroyEndpoint(tempRep);
229 // check return value
230 TEST_F(CATests, GenerateTokenTestGood)
232 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
234 CADestroyToken(tempToken);
237 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
238 TEST_F(CATests, GenerateTokenTestBad)
240 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
244 // check destroyed token
245 TEST_F(CATests, DestroyTokenTest)
247 CAGenerateToken(&tempToken, tokenLength);
248 CADestroyToken(tempToken);
250 char * check = (char *) "destroy success";
251 EXPECT_STREQ(check, "destroy success");
255 // check return value
256 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
258 addr = (char *) ADDRESS;
259 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
261 memset(&requestData, 0, sizeof(CAInfo_t));
262 CAGenerateToken(&tempToken, tokenLength);
263 requestData.token = tempToken;
264 requestData.tokenLength = tokenLength;
266 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
267 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
268 if(!requestData.payload)
270 CADestroyToken(tempToken);
271 FAIL() << "requestData.payload allocation failed";
273 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
274 requestData.payloadSize = length + 1;
275 requestData.type = CA_MSG_NONCONFIRM;
277 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
278 requestInfo.method = CA_GET;
279 requestInfo.info = requestData;
281 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
283 CADestroyToken(tempToken);
285 free(requestData.payload);
287 CADestroyEndpoint(tempRep);
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));
302 CADestroyEndpoint(tempRep);
308 // check return value
309 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
311 addr = (char *) ADDRESS;
312 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
314 memset(&responseData, 0, sizeof(CAInfo_t));
315 responseData.type = CA_MSG_NONCONFIRM;
316 responseData.messageId = 1;
317 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
319 EXPECT_TRUE(responseData.payload != NULL);
320 if(!responseData.payload)
322 CADestroyEndpoint(tempRep);
326 memcpy(responseData.payload, "response payload", sizeof("response payload"));
327 responseData.payloadSize = sizeof("response payload");
329 CAGenerateToken(&tempToken, tokenLength);
330 requestData.token = tempToken;
331 requestData.tokenLength = tokenLength;
333 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
334 responseInfo.result = CA_CONTENT;
335 responseInfo.info = responseData;
337 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
339 CADestroyToken(tempToken);
340 CADestroyEndpoint(tempRep);
341 free(responseData.payload);
345 // check return value when address is NULL as multicast
346 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
349 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
351 memset(&responseData, 0, sizeof(CAInfo_t));
352 responseData.type = CA_MSG_NONCONFIRM;
353 responseData.messageId = 1;
354 responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
355 EXPECT_TRUE(responseData.payload != NULL);
357 if(!responseData.payload)
359 CADestroyEndpoint(tempRep);
363 memcpy(responseData.payload, "response payload", sizeof("response payload"));
364 responseData.payloadSize = sizeof("response payload");
366 CAGenerateToken(&tempToken, tokenLength);
367 requestData.token = tempToken;
368 requestData.tokenLength = tokenLength;
370 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
371 responseInfo.result = CA_CONTENT;
372 responseInfo.info = responseData;
374 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
376 CADestroyToken(tempToken);
379 CADestroyEndpoint(tempRep);
382 free (responseData.payload);
385 // check return value NULL is passed instead of a valid CAResponseInfo_t address
386 TEST_F(CATests, SendResponseTest)
388 addr = (char *) ADDRESS;
389 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
391 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
395 CADestroyEndpoint(tempRep);
400 // CASelectNewwork TC
401 // check return value
402 TEST_F(CATests, SelectNetworkTestGood)
404 CAResult_t res = checkSelectNetwork();
405 EXPECT_EQ(CA_STATUS_OK, res);
408 CAResult_t checkSelectNetwork()
410 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
412 if (CA_STATUS_OK == res)
414 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
417 if (CA_NOT_SUPPORTED == res)
419 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
426 // check return value when selected network is disable
427 TEST_F(CATests, SelectNetworkTestBad)
429 //Select disable network
430 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
431 CA_TRANSPORT_ADAPTER_SCOPE));
434 // check return value when selected network is disable
435 TEST_F(CATests, UnSelectNetworkTest)
437 //UnSelect disable network
438 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
439 CA_TRANSPORT_ADAPTER_SCOPE));
442 // CAHandlerRequestResponse TC
443 // check return value
444 TEST_F(CATests, HandlerRequestResponseTest)
446 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
449 // CAGetNetworkInformation TC
450 // check return value
451 TEST_F (CATests, GetNetworkInformationTestGood)
453 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
456 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
459 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
463 CAResult_t checkGetNetworkInfo()
465 CAEndpoint_t *tempInfo = NULL;
466 uint32_t tempSize = 0;
468 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
472 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
473 CA_NOT_SUPPORTED == res)
479 return CA_STATUS_FAILED;