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