replace : iotivity -> iotivity-sec
[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 "platform_features.h"
22 #include "gtest/gtest.h"
23 #include "cainterface.h"
24 #include "cautilinterface.h"
25 #include "cacommon.h"
26 #include "oic_string.h"
27 #include "oic_malloc.h"
28
29 #define CA_TRANSPORT_ADAPTER_SCOPE  1000
30
31 class CATests : public testing::Test {
32     protected:
33     virtual void SetUp()
34     {
35         CAInitialize(CA_ADAPTER_IP);
36     }
37
38     virtual void TearDown()
39     {
40         CATerminate();
41     }
42 };
43
44 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
45 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
46 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
47 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
48 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
49 CAResult_t checkSelectNetwork();
50
51 void request_handler(const CAEndpoint_t * /*object*/,
52                      const CARequestInfo_t * /*requestInfo*/)
53 {
54
55 }
56
57 void response_handler(const CAEndpoint_t * /*object*/,
58                       const CAResponseInfo_t * /*responseInfo*/)
59 {
60
61 }
62
63 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
64 {
65     if(!object || !errorInfo)
66     {
67         return;
68     }
69
70     //error handling shall be added
71     return;
72 }
73
74 void adapter_handler(CATransportAdapter_t /*adapter*/,
75                      bool /*enabled*/)
76 {
77 }
78
79 void connection_handler(const CAEndpoint_t * /*endpoint*/, 
80                         bool /*connected*/)
81 {
82 }
83
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;
94
95 static const char NORMAL_INFO_DATA[] =
96                                     "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
97                                      "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
98
99 #ifdef __WITH_DTLS__
100
101 // Iotivity Device Identity.
102 const unsigned char IDENTITY[] = ("1111111111111111");
103
104 // PSK between this device and peer device.
105 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
106
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)
111 {
112     printf("CAGetDtlsPskCredentials IN\n");
113
114     int32_t ret = -1;
115
116     if (NULL == result)
117     {
118         return ret;
119     }
120
121     switch (type)
122     {
123         case CA_DTLS_PSK_HINT:
124         case CA_DTLS_PSK_IDENTITY:
125
126             if (result_length < sizeof(IDENTITY))
127             {
128                 printf("ERROR : Wrong value for result for storing IDENTITY");
129                 return ret;
130             }
131
132             memcpy(result, IDENTITY, sizeof(IDENTITY));
133             ret = sizeof(IDENTITY);
134             break;
135
136         case CA_DTLS_PSK_KEY:
137
138             if ((desc_len == sizeof(IDENTITY)) &&
139                 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
140             {
141                 if (result_length < sizeof(RS_CLIENT_PSK))
142                 {
143                     printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
144                     return ret;
145                 }
146
147                 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
148                 ret = sizeof(RS_CLIENT_PSK);
149             }
150             break;
151
152         default:
153
154             printf("Wrong value passed for PSK_CRED_TYPE.");
155             ret = -1;
156     }
157
158
159     printf("CAGetDtlsPskCredentials OUT\n");
160     return ret;
161 }
162 #endif  //__WITH_DTLS__
163
164 // CAInitialize TC
165 TEST(InitializeTest, CAInitializeTest)
166 {
167     EXPECT_EQ(CA_STATUS_OK, CAInitialize(CA_ADAPTER_IP));
168     CATerminate();
169 }
170
171 //CATerminate TC
172 TEST_F(CATests, TerminateTest)
173 {
174     CATerminate();
175
176     char* check = (char *) "terminate success";
177     EXPECT_STREQ(check, "terminate success");
178
179     CAInitialize(CA_ADAPTER_IP);
180 }
181
182 // CAStartListeningServer TC
183 TEST_F(CATests, StartListeningServerTestWithNonSelect)
184 {
185     EXPECT_EQ(CA_STATUS_FAILED, CAStartListeningServer());
186 }
187
188 // CAStartListeningServer TC
189 TEST_F(CATests, StartListeningServerTest)
190 {
191     EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
192     EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
193 }
194
195 // CAStopListeningServer TC
196 TEST_F(CATests, CAStopListeningServerTestWithNonSelect)
197 {
198     EXPECT_EQ(CA_STATUS_FAILED, CAStopListeningServer());
199 }
200
201 // CAStopListeningServer TC
202 TEST_F(CATests, CAStopListeningServerTest)
203 {
204     EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
205     EXPECT_EQ(CA_STATUS_OK, CAStopListeningServer());
206 }
207
208 // CARegisterHandlerTest TC
209 TEST_F(CATests, RegisterHandlerTest)
210 {
211     CARegisterHandler(request_handler, response_handler, error_handler);
212     char* check = (char *) "registerHandler success";
213     EXPECT_STREQ(check, "registerHandler success");
214 }
215
216 // CACreateRemoteEndpoint TC
217 TEST_F(CATests, CreateRemoteEndpointTestGood)
218 {
219     addr = (char *) ADDRESS;
220
221     EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
222                                              PORT, &tempRep));
223
224     CADestroyEndpoint(tempRep);
225     tempRep = NULL;
226 }
227
228 // check remoteEndpoint and values of remoteEndpoint
229 TEST_F(CATests, CreateRemoteEndpointTestValues)
230 {
231     addr = (char *) ADDRESS;
232
233     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
234
235     EXPECT_TRUE(tempRep != NULL);
236
237     CADestroyEndpoint(tempRep);
238     tempRep = NULL;
239 }
240
241 // CAGerateToken TC
242 TEST_F(CATests, GenerateTokenTestGood)
243 {
244     EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
245
246     CADestroyToken(tempToken);
247 }
248
249 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
250 TEST_F(CATests, GenerateTokenTestBad)
251 {
252     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
253 }
254
255 // CADestroyToken TC
256 // check destroyed token
257 TEST_F(CATests, DestroyTokenTest)
258 {
259     CAGenerateToken(&tempToken, tokenLength);
260     CADestroyToken(tempToken);
261
262     char * check = (char *) "destroy success";
263     EXPECT_STREQ(check, "destroy success");
264 }
265
266 TEST_F(CATests, SendRequestTestWithInvalidAddress)
267 {
268     CARegisterHandler(request_handler, response_handler, error_handler);
269
270     EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
271     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "10.11.13.13.14", PORT, &tempRep);
272
273     memset(&requestData, 0, sizeof(CAInfo_t));
274     CAGenerateToken(&tempToken, tokenLength);
275     requestData.token = tempToken;
276     requestData.tokenLength = tokenLength;
277     requestData.type = CA_MSG_CONFIRM;
278
279     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
280     requestInfo.method = CA_GET;
281     requestInfo.info = requestData;
282
283     EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
284
285     CADestroyToken(tempToken);
286     CADestroyEndpoint(tempRep);
287     free(requestData.payload);
288     tempRep = NULL;
289 }
290
291 // CASendRequest TC
292 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
293 {
294     addr = (char *) ADDRESS;
295     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
296
297     memset(&requestData, 0, sizeof(CAInfo_t));
298     CAGenerateToken(&tempToken, tokenLength);
299     requestData.token = tempToken;
300     requestData.tokenLength = tokenLength;
301
302     size_t length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
303     requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
304     if(!requestData.payload)
305     {
306         CADestroyToken(tempToken);
307         FAIL() << "requestData.payload allocation failed";
308     }
309     snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
310     requestData.payloadSize = length + 1;
311     requestData.type = CA_MSG_NONCONFIRM;
312
313     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
314     requestInfo.method = CA_GET;
315     requestInfo.info = requestData;
316
317     EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
318
319     CADestroyToken(tempToken);
320     CADestroyEndpoint(tempRep);
321     free(requestData.payload);
322     tempRep = NULL;
323 }
324
325 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
326 TEST_F(CATests, SendRequestTestWithNullAddr)
327 {
328     addr = (char *) ADDRESS;
329     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
330
331     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
332
333     CADestroyEndpoint(tempRep);
334     tempRep = NULL;
335 }
336
337 TEST_F(CATests, SendResponseTestWithInvalidCode)
338 {
339     EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
340
341     addr = (char *) ADDRESS;
342     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
343
344     memset(&responseData, 0, sizeof(CAInfo_t));
345     responseData.type = CA_MSG_RESET;
346     responseData.messageId = 1;
347     responseData.payload = (CAPayload_t)OICMalloc(sizeof("response payload"));
348     responseData.dataType = CA_RESPONSE_DATA;
349
350     EXPECT_TRUE(responseData.payload != NULL);
351
352     if (responseData.payload)
353     {
354         CAGenerateToken(&tempToken, tokenLength);
355         requestData.token = tempToken;
356         requestData.tokenLength = tokenLength;
357
358         memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
359         responseInfo.result = CA_CONTENT;
360         responseInfo.info = responseData;
361
362         EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
363
364         CADestroyToken(tempToken);
365         CADestroyEndpoint(tempRep);
366         OICFree(responseData.payload);
367         tempRep = NULL;
368     }
369 }
370
371 // CASendResponse TC
372 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
373 {
374     addr = (char *) ADDRESS;
375     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
376
377     memset(&responseData, 0, sizeof(CAInfo_t));
378     responseData.type = CA_MSG_NONCONFIRM;
379     responseData.messageId = 1;
380     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
381     responseData.dataType = CA_RESPONSE_DATA;
382
383     EXPECT_TRUE(responseData.payload != NULL);
384     if(!responseData.payload)
385     {
386         CADestroyEndpoint(tempRep);
387         return;
388     }
389
390     memcpy(responseData.payload, "response payload", sizeof("response payload"));
391     responseData.payloadSize = sizeof("response payload");
392
393     CAGenerateToken(&tempToken, tokenLength);
394     requestData.token = tempToken;
395     requestData.tokenLength = tokenLength;
396
397     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
398     responseInfo.result = CA_CONTENT;
399     responseInfo.info = responseData;
400
401     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
402
403     CADestroyToken(tempToken);
404     CADestroyEndpoint(tempRep);
405     free(responseData.payload);
406     tempRep = NULL;
407 }
408
409 // check return value when address is NULL as multicast
410 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
411 {
412     addr = NULL;
413     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
414
415     memset(&responseData, 0, sizeof(CAInfo_t));
416     responseData.type = CA_MSG_NONCONFIRM;
417     responseData.messageId = 1;
418     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
419     responseData.dataType = CA_RESPONSE_DATA;
420     EXPECT_TRUE(responseData.payload != NULL);
421
422     if(!responseData.payload)
423     {
424         CADestroyEndpoint(tempRep);
425         return;
426     }
427
428     memcpy(responseData.payload, "response payload", sizeof("response payload"));
429     responseData.payloadSize = sizeof("response payload");
430
431     CAGenerateToken(&tempToken, tokenLength);
432     requestData.token = tempToken;
433     requestData.tokenLength = tokenLength;
434
435     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
436     responseInfo.result = CA_CONTENT;
437     responseInfo.info = responseData;
438
439     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
440
441     CADestroyToken(tempToken);
442     CADestroyEndpoint(tempRep);
443     free (responseData.payload);
444     tempRep = NULL;
445 }
446
447 // check return value NULL is passed instead of a valid CAResponseInfo_t address
448 TEST_F(CATests, SendResponseTest)
449 {
450     addr = (char *) ADDRESS;
451     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
452
453     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
454
455     CADestroyEndpoint(tempRep);
456     tempRep = NULL;
457 }
458
459 // CASelectNewwork TC
460 TEST_F(CATests, SelectNetworkTestGood)
461 {
462     EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
463 }
464
465 CAResult_t checkSelectNetwork()
466 {
467     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
468
469     if (CA_STATUS_OK == res)
470     {
471         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
472         return CA_STATUS_OK;
473     }
474     if (CA_NOT_SUPPORTED == res)
475     {
476         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
477         return CA_STATUS_OK;
478     }
479
480     return res;
481 }
482
483 // check return value when selected network is disable
484 TEST_F(CATests, SelectNetworkTestBad)
485 {
486     //Select disable network
487     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
488                                                 CA_TRANSPORT_ADAPTER_SCOPE));
489 }
490
491 // check return value when selected network is disable
492 TEST_F(CATests, UnSelectNetworkTest)
493 {
494     //UnSelect disable network
495     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
496                                                   CA_TRANSPORT_ADAPTER_SCOPE));
497 }
498
499 // CAHandlerRequestResponse TC
500 TEST_F(CATests, HandlerRequestResponseTest)
501 {
502     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
503 }
504
505 // CAGetNetworkInformation TC
506 TEST_F (CATests, GetNetworkInformationTest)
507 {
508     uint32_t tempSize = 0;
509     CAEndpoint_t *tempInfo = NULL;
510
511     EXPECT_EQ(CA_STATUS_OK, CASelectNetwork(CA_ADAPTER_IP));
512     EXPECT_EQ(CA_STATUS_OK, CAGetNetworkInformation(&tempInfo, &tempSize));
513
514 // @todo: if this api is supported on windows platform, it should be changed.
515 #if !defined(_WIN32)
516     for (uint32_t index = 0; index < tempSize; index++)
517     {
518         EXPECT_TRUE(tempInfo[index].adapter != 0);
519         EXPECT_TRUE(strlen(tempInfo[index].addr) != 0);
520     }
521 #endif
522
523     free(tempInfo);
524 }
525
526 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
527 {
528 #ifdef __WITH_DTLS__
529     EXPECT_EQ(CA_STATUS_OK, CAregisterPskCredentialsHandler(CAGetDtlsPskCredentials));
530 #endif
531 }
532
533 // CARegisterNetworkMonitorHandler TC
534 TEST_F(CATests, RegisterNetworkMonitorHandler)
535 {
536     EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
537                                                             connection_handler));
538 }
539
540 // CASetAutoConnectionDeviceInfo TC
541 TEST_F(CATests, SetAutoConnectionDeviceInfo)
542 {
543     addr = (char *) ADDRESS;
544
545 #if defined(__ANDROID__) && defined(LE_ADAPTER)
546     EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
547 #else
548     EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
549 #endif
550 }
551
552 // CAUnsetAutoConnectionDeviceInfo TC
553 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
554 {
555     addr = (char *) ADDRESS;
556
557 #if defined(__ANDROID__) && defined(LE_ADAPTER)
558     EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
559 #else
560     EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
561 #endif
562 }
563
564 TEST(CASetPortNumberTest, CASetPortNumberToAssign)
565 {
566     EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV4, 5683));
567     EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_IP, CA_IPV6, 5683));
568     EXPECT_EQ(CA_STATUS_OK,
569               CASetPortNumberToAssign(CA_ADAPTER_IP,
570                                       static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE), 5683));
571     EXPECT_EQ(CA_STATUS_OK,
572               CASetPortNumberToAssign(CA_ADAPTER_IP,
573                                       static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE), 5683));
574
575 #ifdef TCP_ADAPTER
576     EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV4, 5683));
577     EXPECT_EQ(CA_STATUS_OK, CASetPortNumberToAssign(CA_ADAPTER_TCP, CA_IPV6, 5683));
578 #endif
579 }
580
581 TEST(CAGetPortNumberTest, CAGetPortNumberToAssign)
582 {
583     ASSERT_EQ(static_cast<uint16_t>(0),
584               CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV4));
585     ASSERT_EQ(static_cast<uint16_t>(0),
586               CAGetAssignedPortNumber(CA_ADAPTER_IP, CA_IPV6));
587     ASSERT_EQ(static_cast<uint16_t>(0),
588               CAGetAssignedPortNumber(CA_ADAPTER_IP,
589                                       static_cast<CATransportFlags_t>(CA_IPV4|CA_SECURE)));
590     ASSERT_EQ(static_cast<uint16_t>(0),
591               CAGetAssignedPortNumber(CA_ADAPTER_IP,
592                                       static_cast<CATransportFlags_t>(CA_IPV6|CA_SECURE)));
593 #ifdef TCP_ADAPTER
594     ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4));
595     ASSERT_EQ(static_cast<uint16_t>(0), CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV6));
596 #endif
597 }