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