Merge branch 'master' into windows-port
[platform/upstream/iotivity.git] / resource / csdk / connectivity / test / ca_api_unittest.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "gtest/gtest.h"
22 #include "cainterface.h"
23 #include "cautilinterface.h"
24 #include "cacommon.h"
25
26 #define CA_TRANSPORT_ADAPTER_SCOPE  1000
27
28 class CATests : public testing::Test {
29     protected:
30     virtual void SetUp() {
31         CAInitialize();
32     }
33
34     virtual void TearDown()
35     {
36         CATerminate();
37     }
38 };
39
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();
47
48 void request_handler(const CAEndpoint_t * /*object*/,
49                      const CARequestInfo_t * /*requestInfo*/)
50 {
51
52 }
53
54 void response_handler(const CAEndpoint_t * /*object*/,
55                       const CAResponseInfo_t * /*responseInfo*/)
56 {
57
58 }
59
60 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
61 {
62     if(!object || !errorInfo)
63     {
64         return;
65     }
66
67     //error handling shall be added
68     return;
69 }
70
71 void adapter_handler(CATransportAdapter_t /*adapter*/,
72                      bool /*enabled*/)
73 {
74 }
75
76 void connection_handler(CATransportAdapter_t /*adapter*/,
77                         const char * /*remote_address*/,
78                         bool /*connected*/)
79 {
80 }
81
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;
92
93 static const char NORMAL_INFO_DATA[] =
94                                     "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
95                                      "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
96
97 #ifdef __WITH_DTLS__
98
99 // Iotivity Device Identity.
100 const unsigned char IDENTITY[] = ("1111111111111111");
101
102 // PSK between this device and peer device.
103 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
104
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)
109 {
110     printf("CAGetDtlsPskCredentials IN\n");
111
112     int32_t ret = -1;
113
114     if (NULL == result)
115     {
116         return ret;
117     }
118
119     switch (type)
120     {
121         case CA_DTLS_PSK_HINT:
122         case CA_DTLS_PSK_IDENTITY:
123
124             if (result_length < sizeof(IDENTITY))
125             {
126                 printf("ERROR : Wrong value for result for storing IDENTITY");
127                 return ret;
128             }
129
130             memcpy(result, IDENTITY, sizeof(IDENTITY));
131             ret = sizeof(IDENTITY);
132             break;
133
134         case CA_DTLS_PSK_KEY:
135
136             if ((desc_len == sizeof(IDENTITY)) &&
137                 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
138             {
139                 if (result_length < sizeof(RS_CLIENT_PSK))
140                 {
141                     printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
142                     return ret;
143                 }
144
145                 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
146                 ret = sizeof(RS_CLIENT_PSK);
147             }
148             break;
149
150         default:
151
152             printf("Wrong value passed for PSK_CRED_TYPE.");
153             ret = -1;
154     }
155
156
157     printf("CAGetDtlsPskCredentials OUT\n");
158     return ret;
159 }
160 #endif  //__WITH_DTLS__
161
162 int main(int argc, char **argv)
163 {
164     testing::InitGoogleTest(&argc, argv);
165     return RUN_ALL_TESTS();
166 }
167
168 // CAInitialize TC
169 // check return value
170 TEST(InitializeTest, TC_01_Positive_01)
171 {
172     EXPECT_EQ(CA_STATUS_OK, CAInitialize());
173     CATerminate();
174 }
175
176 //CATerminate TC
177 TEST_F(CATests, TerminateTest)
178 {
179     CATerminate();
180
181     char* check = (char *) "terminate success";
182     EXPECT_STREQ(check, "terminate success");
183
184     CAInitialize();
185 }
186 // CAStartListeningServer TC
187 // check return value
188 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
189 {
190     CASelectNetwork(CA_ADAPTER_IP);
191     EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
192 }
193
194 // CAStartDiscoveryServer TC
195 // check return value
196 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
197 {
198     EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
199 }
200
201 // CARegisterHandlerTest TC
202 // check return value
203 TEST_F(CATests, RegisterHandlerTest)
204 {
205     CARegisterHandler(request_handler, response_handler, error_handler);
206     char* check = (char *) "registerHandler success";
207     EXPECT_STREQ(check, "registerHandler success");
208 }
209
210 // CACreateRemoteEndpoint TC
211 // check return value
212 TEST_F(CATests, CreateRemoteEndpointTestGood)
213 {
214     addr = (char *) ADDRESS;
215
216     EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
217                                              PORT, &tempRep));
218
219     CADestroyEndpoint(tempRep);
220     tempRep = NULL;
221 }
222
223 // check remoteEndpoint and values of remoteEndpoint
224 TEST_F(CATests, CreateRemoteEndpointTestValues)
225 {
226     addr = (char *) ADDRESS;
227
228     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
229
230     EXPECT_TRUE(tempRep != NULL);
231
232     CADestroyEndpoint(tempRep);
233     tempRep = NULL;
234 }
235
236 // CAGerateToken TC
237 // check return value
238 TEST_F(CATests, GenerateTokenTestGood)
239 {
240     EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
241
242     CADestroyToken(tempToken);
243 }
244
245 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
246 TEST_F(CATests, GenerateTokenTestBad)
247 {
248     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
249 }
250
251 // CADestroyToken TC
252 // check destroyed token
253 TEST_F(CATests, DestroyTokenTest)
254 {
255     CAGenerateToken(&tempToken, tokenLength);
256     CADestroyToken(tempToken);
257
258     char * check = (char *) "destroy success";
259     EXPECT_STREQ(check, "destroy success");
260 }
261
262 // CASendRequest TC
263 // check return value
264 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
265 {
266     addr = (char *) ADDRESS;
267     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
268
269     memset(&requestData, 0, sizeof(CAInfo_t));
270     CAGenerateToken(&tempToken, tokenLength);
271     requestData.token = tempToken;
272     requestData.tokenLength = tokenLength;
273
274     int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
275     requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
276     if(!requestData.payload)
277     {
278         CADestroyToken(tempToken);
279         FAIL() << "requestData.payload allocation failed";
280     }
281     snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
282     requestData.payloadSize = length + 1;
283     requestData.type = CA_MSG_NONCONFIRM;
284
285     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
286     requestInfo.method = CA_GET;
287     requestInfo.info = requestData;
288
289     EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
290
291     CADestroyToken(tempToken);
292     CADestroyEndpoint(tempRep);
293     free(requestData.payload);
294     tempRep = NULL;
295 }
296
297 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
298 TEST_F(CATests, SendRequestTestWithNullAddr)
299 {
300     addr = (char *) ADDRESS;
301     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
302
303     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
304
305     CADestroyEndpoint(tempRep);
306     tempRep = NULL;
307 }
308
309 // CASendResponse TC
310 // check return value
311 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
312 {
313     addr = (char *) ADDRESS;
314     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
315
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"));
320
321     EXPECT_TRUE(responseData.payload != NULL);
322     if(!responseData.payload)
323     {
324         CADestroyEndpoint(tempRep);
325         return;
326     }
327
328     memcpy(responseData.payload, "response payload", sizeof("response payload"));
329     responseData.payloadSize = sizeof("response payload");
330
331     CAGenerateToken(&tempToken, tokenLength);
332     requestData.token = tempToken;
333     requestData.tokenLength = tokenLength;
334
335     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
336     responseInfo.result = CA_CONTENT;
337     responseInfo.info = responseData;
338
339     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
340
341     CADestroyToken(tempToken);
342     CADestroyEndpoint(tempRep);
343     free(responseData.payload);
344     tempRep = NULL;
345 }
346
347 // check return value when address is NULL as multicast
348 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
349 {
350     addr = NULL;
351     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
352
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);
358
359     if(!responseData.payload)
360     {
361         CADestroyEndpoint(tempRep);
362         return;
363     }
364
365     memcpy(responseData.payload, "response payload", sizeof("response payload"));
366     responseData.payloadSize = sizeof("response payload");
367
368     CAGenerateToken(&tempToken, tokenLength);
369     requestData.token = tempToken;
370     requestData.tokenLength = tokenLength;
371
372     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
373     responseInfo.result = CA_CONTENT;
374     responseInfo.info = responseData;
375
376     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
377
378     CADestroyToken(tempToken);
379     CADestroyEndpoint(tempRep);
380     free (responseData.payload);
381     tempRep = NULL;
382 }
383
384 // check return value NULL is passed instead of a valid CAResponseInfo_t address
385 TEST_F(CATests, SendResponseTest)
386 {
387     addr = (char *) ADDRESS;
388     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
389
390     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
391
392     CADestroyEndpoint(tempRep);
393     tempRep = NULL;
394 }
395
396 // CASelectNewwork TC
397 // check return value
398 TEST_F(CATests, SelectNetworkTestGood)
399 {
400     EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
401 }
402
403 CAResult_t checkSelectNetwork()
404 {
405     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
406
407     if (CA_STATUS_OK == res)
408     {
409         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
410         return CA_STATUS_OK;
411     }
412     if (CA_NOT_SUPPORTED == res)
413     {
414         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
415         return CA_STATUS_OK;
416     }
417
418     return res;
419 }
420
421 // check return value when selected network is disable
422 TEST_F(CATests, SelectNetworkTestBad)
423 {
424     //Select disable network
425     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
426                                                 CA_TRANSPORT_ADAPTER_SCOPE));
427 }
428
429 // check return value when selected network is disable
430 TEST_F(CATests, UnSelectNetworkTest)
431 {
432     //UnSelect disable network
433     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
434                                                   CA_TRANSPORT_ADAPTER_SCOPE));
435 }
436
437 // CAHandlerRequestResponse TC
438 // check return value
439 TEST_F(CATests, HandlerRequestResponseTest)
440 {
441     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
442 }
443
444 // CAGetNetworkInformation TC
445 // check return value
446 TEST_F (CATests, GetNetworkInformationTestGood)
447 {
448     EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
449 }
450
451 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
452 {
453 #ifdef __WITH_DTLS__
454     EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
455 #endif
456 }
457
458 // CARegisterNetworkMonitorHandler TC
459 // check return value
460 TEST_F(CATests, RegisterNetworkMonitorHandler)
461 {
462 #ifdef LE_ADAPTER
463     EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
464                                                             connection_handler));
465 #else
466     EXPECT_EQ(CA_NOT_SUPPORTED, CARegisterNetworkMonitorHandler(adapter_handler,
467                                                                 connection_handler));
468 #endif
469 }
470
471 // CASetAutoConnectionDeviceInfo TC
472 // check return value
473 TEST_F(CATests, SetAutoConnectionDeviceInfo)
474 {
475     addr = (char *) ADDRESS;
476
477 #if defined(__ANDROID__) && defined(LE_ADAPTER)
478     EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
479 #else
480     EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
481 #endif
482 }
483
484 // CAUnsetAutoConnectionDeviceInfo TC
485 // check return value
486 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
487 {
488     addr = (char *) ADDRESS;
489
490 #if defined(__ANDROID__) && defined(LE_ADAPTER)
491     EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
492 #else
493     EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
494 #endif
495 }
496
497 CAResult_t checkGetNetworkInfo()
498 {
499     CAEndpoint_t *tempInfo = NULL;
500     uint32_t tempSize = 0;
501
502     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
503
504     free(tempInfo);
505
506     if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
507             CA_NOT_SUPPORTED == res)
508     {
509         return CA_STATUS_OK;
510     }
511     else
512     {
513         return CA_STATUS_FAILED;
514     }
515 }