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