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