Merge branch '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 "platform_features.h"
22 #include "gtest/gtest.h"
23 #include "cainterface.h"
24 #include "cautilinterface.h"
25 #include "cacommon.h"
26
27 #define CA_TRANSPORT_ADAPTER_SCOPE  1000
28
29 class CATests : public testing::Test {
30     protected:
31     virtual void SetUp() {
32         CAInitialize();
33     }
34
35     virtual void TearDown()
36     {
37         CATerminate();
38     }
39 };
40
41 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
42 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
43 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
44 void adapter_handler(CATransportAdapter_t adapter, bool enabled);
45 void connection_handler(CATransportAdapter_t adapter, const char *remote_address, bool connected);
46 CAResult_t checkGetNetworkInfo();
47 CAResult_t checkSelectNetwork();
48
49 void request_handler(const CAEndpoint_t * /*object*/,
50                      const CARequestInfo_t * /*requestInfo*/)
51 {
52
53 }
54
55 void response_handler(const CAEndpoint_t * /*object*/,
56                       const CAResponseInfo_t * /*responseInfo*/)
57 {
58
59 }
60
61 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
62 {
63     if(!object || !errorInfo)
64     {
65         return;
66     }
67
68     //error handling shall be added
69     return;
70 }
71
72 void adapter_handler(CATransportAdapter_t /*adapter*/,
73                      bool /*enabled*/)
74 {
75 }
76
77 void connection_handler(CATransportAdapter_t /*adapter*/,
78                         const char * /*remote_address*/,
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 // check return value
165 TEST(InitializeTest, TC_01_Positive_01)
166 {
167     EXPECT_EQ(CA_STATUS_OK, CAInitialize());
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();
180 }
181 // CAStartListeningServer TC
182 // check return value
183 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
184 {
185     CASelectNetwork(CA_ADAPTER_IP);
186     EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
187 }
188
189 // CAStartDiscoveryServer TC
190 // check return value
191 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
192 {
193     EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
194 }
195
196 // CARegisterHandlerTest TC
197 // check return value
198 TEST_F(CATests, RegisterHandlerTest)
199 {
200     CARegisterHandler(request_handler, response_handler, error_handler);
201     char* check = (char *) "registerHandler success";
202     EXPECT_STREQ(check, "registerHandler success");
203 }
204
205 // CACreateRemoteEndpoint TC
206 // check return value
207 TEST_F(CATests, CreateRemoteEndpointTestGood)
208 {
209     addr = (char *) ADDRESS;
210
211     EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
212                                              PORT, &tempRep));
213
214     CADestroyEndpoint(tempRep);
215     tempRep = NULL;
216 }
217
218 // check remoteEndpoint and values of remoteEndpoint
219 TEST_F(CATests, CreateRemoteEndpointTestValues)
220 {
221     addr = (char *) ADDRESS;
222
223     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
224
225     EXPECT_TRUE(tempRep != NULL);
226
227     CADestroyEndpoint(tempRep);
228     tempRep = NULL;
229 }
230
231 // CAGerateToken TC
232 // check return value
233 TEST_F(CATests, GenerateTokenTestGood)
234 {
235     EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
236
237     CADestroyToken(tempToken);
238 }
239
240 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
241 TEST_F(CATests, GenerateTokenTestBad)
242 {
243     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
244 }
245
246 // CADestroyToken TC
247 // check destroyed token
248 TEST_F(CATests, DestroyTokenTest)
249 {
250     CAGenerateToken(&tempToken, tokenLength);
251     CADestroyToken(tempToken);
252
253     char * check = (char *) "destroy success";
254     EXPECT_STREQ(check, "destroy success");
255 }
256
257 // CASendRequest TC
258 // check return value
259 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
260 {
261     addr = (char *) ADDRESS;
262     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
263
264     memset(&requestData, 0, sizeof(CAInfo_t));
265     CAGenerateToken(&tempToken, tokenLength);
266     requestData.token = tempToken;
267     requestData.tokenLength = tokenLength;
268
269     int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
270     requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
271     if(!requestData.payload)
272     {
273         CADestroyToken(tempToken);
274         FAIL() << "requestData.payload allocation failed";
275     }
276     snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
277     requestData.payloadSize = length + 1;
278     requestData.type = CA_MSG_NONCONFIRM;
279
280     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
281     requestInfo.method = CA_GET;
282     requestInfo.info = requestData;
283
284     EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
285
286     CADestroyToken(tempToken);
287     CADestroyEndpoint(tempRep);
288     free(requestData.payload);
289     tempRep = NULL;
290 }
291
292 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
293 TEST_F(CATests, SendRequestTestWithNullAddr)
294 {
295     addr = (char *) ADDRESS;
296     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
297
298     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
299
300     CADestroyEndpoint(tempRep);
301     tempRep = NULL;
302 }
303
304 // CASendResponse TC
305 // check return value
306 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
307 {
308     addr = (char *) ADDRESS;
309     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
310
311     memset(&responseData, 0, sizeof(CAInfo_t));
312     responseData.type = CA_MSG_NONCONFIRM;
313     responseData.messageId = 1;
314     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
315
316     EXPECT_TRUE(responseData.payload != NULL);
317     if(!responseData.payload)
318     {
319         CADestroyEndpoint(tempRep);
320         return;
321     }
322
323     memcpy(responseData.payload, "response payload", sizeof("response payload"));
324     responseData.payloadSize = sizeof("response payload");
325
326     CAGenerateToken(&tempToken, tokenLength);
327     requestData.token = tempToken;
328     requestData.tokenLength = tokenLength;
329
330     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
331     responseInfo.result = CA_CONTENT;
332     responseInfo.info = responseData;
333
334     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
335
336     CADestroyToken(tempToken);
337     CADestroyEndpoint(tempRep);
338     free(responseData.payload);
339     tempRep = NULL;
340 }
341
342 // check return value when address is NULL as multicast
343 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
344 {
345     addr = NULL;
346     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
347
348     memset(&responseData, 0, sizeof(CAInfo_t));
349     responseData.type = CA_MSG_NONCONFIRM;
350     responseData.messageId = 1;
351     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
352     EXPECT_TRUE(responseData.payload != NULL);
353
354     if(!responseData.payload)
355     {
356         CADestroyEndpoint(tempRep);
357         return;
358     }
359
360     memcpy(responseData.payload, "response payload", sizeof("response payload"));
361     responseData.payloadSize = sizeof("response payload");
362
363     CAGenerateToken(&tempToken, tokenLength);
364     requestData.token = tempToken;
365     requestData.tokenLength = tokenLength;
366
367     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
368     responseInfo.result = CA_CONTENT;
369     responseInfo.info = responseData;
370
371     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
372
373     CADestroyToken(tempToken);
374     CADestroyEndpoint(tempRep);
375     free (responseData.payload);
376     tempRep = NULL;
377 }
378
379 // check return value NULL is passed instead of a valid CAResponseInfo_t address
380 TEST_F(CATests, SendResponseTest)
381 {
382     addr = (char *) ADDRESS;
383     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
384
385     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
386
387     CADestroyEndpoint(tempRep);
388     tempRep = NULL;
389 }
390
391 // CASelectNewwork TC
392 // check return value
393 TEST_F(CATests, SelectNetworkTestGood)
394 {
395     EXPECT_EQ(CA_STATUS_OK, checkSelectNetwork());
396 }
397
398 CAResult_t checkSelectNetwork()
399 {
400     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
401
402     if (CA_STATUS_OK == res)
403     {
404         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
405         return CA_STATUS_OK;
406     }
407     if (CA_NOT_SUPPORTED == res)
408     {
409         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
410         return CA_STATUS_OK;
411     }
412
413     return res;
414 }
415
416 // check return value when selected network is disable
417 TEST_F(CATests, SelectNetworkTestBad)
418 {
419     //Select disable network
420     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
421                                                 CA_TRANSPORT_ADAPTER_SCOPE));
422 }
423
424 // check return value when selected network is disable
425 TEST_F(CATests, UnSelectNetworkTest)
426 {
427     //UnSelect disable network
428     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
429                                                   CA_TRANSPORT_ADAPTER_SCOPE));
430 }
431
432 // CAHandlerRequestResponse TC
433 // check return value
434 TEST_F(CATests, HandlerRequestResponseTest)
435 {
436     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
437 }
438
439 // CAGetNetworkInformation TC
440 // check return value
441 TEST_F (CATests, GetNetworkInformationTestGood)
442 {
443     EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
444 }
445
446 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
447 {
448 #ifdef __WITH_DTLS__
449     EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
450 #endif
451 }
452
453 // CARegisterNetworkMonitorHandler TC
454 // check return value
455 TEST_F(CATests, RegisterNetworkMonitorHandler)
456 {
457     EXPECT_EQ(CA_STATUS_OK, CARegisterNetworkMonitorHandler(adapter_handler,
458                                                             connection_handler));
459 }
460
461 // CASetAutoConnectionDeviceInfo TC
462 // check return value
463 TEST_F(CATests, SetAutoConnectionDeviceInfo)
464 {
465     addr = (char *) ADDRESS;
466
467 #if defined(__ANDROID__) && defined(LE_ADAPTER)
468     EXPECT_EQ(CA_STATUS_OK, CASetAutoConnectionDeviceInfo(addr));
469 #else
470     EXPECT_EQ(CA_NOT_SUPPORTED, CASetAutoConnectionDeviceInfo(addr));
471 #endif
472 }
473
474 // CAUnsetAutoConnectionDeviceInfo TC
475 // check return value
476 TEST_F(CATests, UnsetAutoConnectionDeviceInfo)
477 {
478     addr = (char *) ADDRESS;
479
480 #if defined(__ANDROID__) && defined(LE_ADAPTER)
481     EXPECT_EQ(CA_STATUS_OK, CAUnsetAutoConnectionDeviceInfo(addr));
482 #else
483     EXPECT_EQ(CA_NOT_SUPPORTED, CAUnsetAutoConnectionDeviceInfo(addr));
484 #endif
485 }
486
487 CAResult_t checkGetNetworkInfo()
488 {
489     CAEndpoint_t *tempInfo = NULL;
490     uint32_t tempSize = 0;
491
492     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
493
494     free(tempInfo);
495
496     if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
497             CA_NOT_SUPPORTED == res)
498     {
499         return CA_STATUS_OK;
500     }
501     else
502     {
503         return CA_STATUS_FAILED;
504     }
505 }