Fixed a large amount of memory leaks/ Null pointer dereferences
[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 "cacommon.h"
24
25
26 class CATests : public testing::Test {
27     protected:
28     virtual void SetUp() {
29         CAInitialize();
30     }
31
32     virtual void TearDown()
33     {
34         CATerminate();
35     }
36 };
37
38 void request_handler(CAEndpoint_t* object, CARequestInfo_t* requestInfo);
39 void response_handler(CAEndpoint_t* object, CAResponseInfo_t* responseInfo);
40 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
41 CAResult_t checkGetNetworkInfo();
42 CAResult_t checkSelectNetwork();
43
44 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
45 {
46
47 }
48
49 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
50 {
51
52 }
53
54 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
55 {
56     if(!object || !errorInfo)
57     {
58         return;
59     }
60
61     //error handling shall be added
62     return;
63 }
64
65 static char* addr = NULL;
66 static CAEndpoint_t* tempRep = NULL;
67 static CARequestInfo_t requestInfo;
68 static CAInfo_t requestData;
69 static CAInfo_t responseData;
70 static CAResponseInfo_t responseInfo;
71 static CAToken_t tempToken = NULL;
72 static uint8_t tokenLength = CA_MAX_TOKEN_LEN;
73 static const char ADDRESS[] = "10.11.12.13";
74 static const uint16_t PORT = 4545;
75
76 static const char NORMAL_INFO_DATA[] =
77                                     "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
78                                      "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
79
80 #ifdef __WITH_DTLS__
81
82 /**
83  * @def RS_IDENTITY
84  * @brief
85  */
86 #define IDENTITY     ("1111111111111111")
87 /* @def RS_CLIENT_PSK
88  * @brief
89  */
90 #define RS_CLIENT_PSK   ("AAAAAAAAAAAAAAAA")
91
92 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
93
94 void clearDtlsCredentialInfo()
95 {
96     printf("clearDtlsCredentialInfo IN\n");
97     if (pskCredsBlob)
98     {
99         // Initialize sensitive data to zeroes before freeing.
100         memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
101         free(pskCredsBlob->creds);
102
103         memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
104         free(pskCredsBlob);
105         pskCredsBlob = NULL;
106     }
107     printf("clearDtlsCredentialInfo OUT\n");
108 }
109
110 // Internal API. Invoked by OC stack to retrieve credentials from this module
111 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
112 {
113     printf("CAGetDtlsPskCredentials IN\n");
114
115     if (pskCredsBlob != NULL)
116     {
117         *credInfo = pskCredsBlob;
118     }
119
120     printf("CAGetDtlsPskCredentials OUT\n");
121 }
122
123 int32_t SetCredentials()
124 {
125     printf("SetCredentials IN\n");
126     pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
127
128     memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
129     memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
130
131     pskCredsBlob->num = 1;
132
133     pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
134
135     memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
136     memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
137
138     printf("SetCredentials OUT\n");
139     return 1;
140 }
141 #endif
142
143 int main(int argc, char **argv)
144 {
145     testing::InitGoogleTest(&argc, argv);
146     return RUN_ALL_TESTS();
147 }
148
149 // CAInitialize TC
150 // check return value
151 TEST(InitializeTest, TC_01_Positive_01)
152 {
153     EXPECT_EQ(CA_STATUS_OK, CAInitialize());
154     CATerminate();
155 }
156
157 //CATerminate TC
158 TEST_F(CATests, TerminateTest)
159 {
160     CATerminate();
161
162     char* check = (char *) "terminate success";
163     EXPECT_STREQ(check, "terminate success");
164
165     CAInitialize();
166 }
167 // CAStartListeningServer TC
168 // check return value
169 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
170 {
171     CASelectNetwork(CA_ADAPTER_IP);
172     EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
173 }
174
175 // CAStartDiscoveryServer TC
176 // check return value
177 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
178 {
179     EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
180 }
181
182 // CARegisterHandlerTest TC
183 // check return value
184 TEST_F(CATests, RegisterHandlerTest)
185 {
186     CARegisterHandler(request_handler, response_handler, error_handler);
187     char* check = (char *) "registerHandler success";
188     EXPECT_STREQ(check, "registerHandler success");
189 }
190
191 // CACreateRemoteEndpoint TC
192 // check return value
193 TEST_F(CATests, CreateRemoteEndpointTestGood)
194 {
195     addr = (char *) ADDRESS;
196
197     EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
198                                              PORT, &tempRep));
199
200     if (tempRep != NULL)
201     {
202         CADestroyEndpoint(tempRep);
203         tempRep = NULL;
204     }
205 }
206
207 // check remoteEndpoint and values of remoteEndpoint
208 TEST_F(CATests, CreateRemoteEndpointTestValues)
209 {
210     addr = (char *) ADDRESS;
211
212     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
213
214     EXPECT_TRUE(tempRep != NULL);
215
216     if (tempRep != NULL)
217     {
218         CADestroyEndpoint(tempRep);
219         tempRep = NULL;
220     }
221 }
222
223 // CAGerateToken TC
224 // check return value
225 TEST_F(CATests, GenerateTokenTestGood)
226 {
227     EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
228
229     CADestroyToken(tempToken);
230 }
231
232 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
233 TEST_F(CATests, GenerateTokenTestBad)
234 {
235     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
236 }
237
238 // CADestroyToken TC
239 // check destroyed token
240 TEST_F(CATests, DestroyTokenTest)
241 {
242     CAGenerateToken(&tempToken, tokenLength);
243     CADestroyToken(tempToken);
244
245     char * check = (char *) "destroy success";
246     EXPECT_STREQ(check, "destroy success");
247 }
248
249 // CASendRequest TC
250 // check return value
251 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
252 {
253     addr = (char *) ADDRESS;
254     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
255
256     memset(&requestData, 0, sizeof(CAInfo_t));
257     CAGenerateToken(&tempToken, tokenLength);
258     requestData.token = tempToken;
259     requestData.tokenLength = tokenLength;
260
261     int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
262     requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
263     if(!requestData.payload)
264     {
265         CADestroyToken(tempToken);
266         FAIL() << "requestData.payload allocation failed";
267     }
268     snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
269     requestData.payloadSize = length + 1;
270     requestData.type = CA_MSG_NONCONFIRM;
271
272     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
273     requestInfo.method = CA_GET;
274     requestInfo.info = requestData;
275
276     EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
277
278     CADestroyToken(tempToken);
279
280     free(requestData.payload);
281
282     CADestroyEndpoint(tempRep);
283     tempRep = NULL;
284
285 }
286
287 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
288 TEST_F(CATests, SendRequestTestWithNullAddr)
289 {
290     addr = (char *) ADDRESS;
291     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
292
293     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
294
295     if (tempRep != NULL)
296     {
297         CADestroyEndpoint(tempRep);
298         tempRep = NULL;
299     }
300 }
301
302 // CASendResponse TC
303 // check return value
304 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
305 {
306     addr = (char *) ADDRESS;
307     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
308
309     memset(&responseData, 0, sizeof(CAInfo_t));
310     responseData.type = CA_MSG_NONCONFIRM;
311     responseData.messageId = 1;
312     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
313
314     EXPECT_TRUE(responseData.payload != NULL);
315     if(!responseData.payload)
316     {
317         CADestroyEndpoint(tempRep);
318         return;
319     }
320
321     memcpy(responseData.payload, "response payload", sizeof("response payload"));
322     responseData.payloadSize = sizeof("response payload");
323
324     CAGenerateToken(&tempToken, tokenLength);
325     requestData.token = tempToken;
326     requestData.tokenLength = tokenLength;
327
328     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
329     responseInfo.result = CA_CONTENT;
330     responseInfo.info = responseData;
331
332     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
333
334     CADestroyToken(tempToken);
335     CADestroyEndpoint(tempRep);
336     free(responseData.payload);
337     tempRep = NULL;
338 }
339
340 // check return value when address is NULL as multicast
341 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
342 {
343     addr = NULL;
344     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
345
346     memset(&responseData, 0, sizeof(CAInfo_t));
347     responseData.type = CA_MSG_NONCONFIRM;
348     responseData.messageId = 1;
349     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
350     EXPECT_TRUE(responseData.payload != NULL);
351
352     if(!responseData.payload)
353     {
354         CADestroyEndpoint(tempRep);
355         return;
356     }
357
358     memcpy(responseData.payload, "response payload", sizeof("response payload"));
359     responseData.payloadSize = sizeof("response payload");
360
361     CAGenerateToken(&tempToken, tokenLength);
362     requestData.token = tempToken;
363     requestData.tokenLength = tokenLength;
364
365     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
366     responseInfo.result = CA_CONTENT;
367     responseInfo.info = responseData;
368
369     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
370
371     CADestroyToken(tempToken);
372     if (tempRep != NULL)
373     {
374         CADestroyEndpoint(tempRep);
375         tempRep = NULL;
376     }
377     free (responseData.payload);
378 }
379
380 // check return value NULL is passed instead of a valid CAResponseInfo_t address
381 TEST_F(CATests, SendResponseTest)
382 {
383     addr = (char *) ADDRESS;
384     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
385
386     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
387
388     if (tempRep != NULL)
389     {
390         CADestroyEndpoint(tempRep);
391         tempRep = NULL;
392     }
393 }
394
395 // CASendNotification TC
396 // check return value
397 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
398 {
399     addr = (char *) ADDRESS;
400     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
401
402     memset(&responseData, 0, sizeof(CAInfo_t));
403     responseData.type = CA_MSG_NONCONFIRM;
404     responseData.payload = (CAPayload_t)malloc(sizeof("Temp Notification Data"));
405
406     EXPECT_TRUE(responseData.payload != NULL);
407     if(!responseData.payload)
408     {
409         CADestroyEndpoint(tempRep);
410         return;
411     }
412
413     memcpy(responseData.payload, "Temp Notification Data", sizeof("Temp Notification Data"));
414     responseData.payloadSize = sizeof("Temp Notification Data");
415
416     CAGenerateToken(&tempToken, tokenLength);
417     requestData.token = tempToken;
418     requestData.tokenLength = tokenLength;
419
420     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
421     responseInfo.result = CA_CONTENT;
422     responseInfo.info = responseData;
423
424     EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
425
426     CADestroyToken(tempToken);
427     if (tempRep != NULL)
428     {
429         CADestroyEndpoint(tempRep);
430         tempRep = NULL;
431     }
432     free(responseData.payload);
433 }
434
435 // CASelectNewwork TC
436 // check return value
437 TEST_F(CATests, SelectNetworkTestGood)
438 {
439     CAResult_t res = checkSelectNetwork();
440     EXPECT_EQ(CA_STATUS_OK, res);
441 }
442
443 CAResult_t checkSelectNetwork()
444 {
445     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
446
447     if (CA_STATUS_OK == res)
448     {
449         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
450         return CA_STATUS_OK;
451     }
452     if (CA_NOT_SUPPORTED == res)
453     {
454         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
455         return CA_STATUS_OK;
456     }
457
458     return res;
459 }
460
461 // check return value when selected network is disable
462 TEST_F(CATests, SelectNetworkTestBad)
463 {
464     //Select disable network
465     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)1000));
466 }
467
468 // check return value when selected network is disable
469 TEST_F(CATests, UnSelectNetworkTest)
470 {
471     //UnSelect disable network
472     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)1000));
473 }
474
475 // CAHandlerRequestResponse TC
476 // check return value
477 TEST_F(CATests, HandlerRequestResponseTest)
478 {
479     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
480 }
481
482 // CAGetNetworkInformation TC
483 // check return value
484 TEST_F (CATests, GetNetworkInformationTestGood)
485 {
486     EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
487 }
488
489 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
490 {
491 #ifdef __WITH_DTLS__
492     if (SetCredentials() == 0)
493     {
494         printf("SetCredentials failed\n");
495     }
496
497     EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
498 #endif
499 }
500
501 CAResult_t checkGetNetworkInfo()
502 {
503     CAEndpoint_t *tempInfo = NULL;
504     uint32_t tempSize = 0;
505
506     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
507
508     free(tempInfo);
509
510     if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
511             CA_NOT_SUPPORTED == res)
512     {
513         return CA_STATUS_OK;
514     }
515     else
516     {
517         return CA_STATUS_FAILED;
518     }
519 }