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