1a4a20be9302bd235e2221c2bc883cf547856609
[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_IPV4);
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(requestData.payload, length, NORMAL_INFO_DATA, "a/light");
269     requestData.type = CA_MSG_NONCONFIRM;
270
271     memset(&requestInfo, 0, sizeof(CARequestInfo_t));
272     requestInfo.method = CA_GET;
273     requestInfo.info = requestData;
274
275     EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
276
277     CADestroyToken(tempToken);
278
279     free(requestData.payload);
280
281     CADestroyEndpoint(tempRep);
282     tempRep = NULL;
283
284 }
285
286 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
287 TEST_F(CATests, SendRequestTestWithNullAddr)
288 {
289     addr = (char *) ADDRESS;
290     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
291
292     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
293
294     if (tempRep != NULL)
295     {
296         CADestroyEndpoint(tempRep);
297         tempRep = NULL;
298     }
299 }
300
301 // CASendResponse TC
302 // check return value
303 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
304 {
305     addr = (char *) ADDRESS;
306     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
307
308     memset(&responseData, 0, sizeof(CAInfo_t));
309     responseData.type = CA_MSG_NONCONFIRM;
310     responseData.messageId = 1;
311     responseData.payload = (char *) "response payload";
312
313     CAGenerateToken(&tempToken, tokenLength);
314     requestData.token = tempToken;
315     requestData.tokenLength = tokenLength;
316
317     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
318     responseInfo.result = CA_CONTENT;
319     responseInfo.info = responseData;
320
321     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
322
323     CADestroyToken(tempToken);
324     CADestroyEndpoint(tempRep);
325     tempRep = NULL;
326 }
327
328 // check return value when address is NULL as multicast
329 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
330 {
331     addr = NULL;
332     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
333
334     memset(&responseData, 0, sizeof(CAInfo_t));
335     responseData.type = CA_MSG_NONCONFIRM;
336     responseData.messageId = 1;
337     responseData.payload = (char *) "response payload";
338
339     CAGenerateToken(&tempToken, tokenLength);
340     requestData.token = tempToken;
341     requestData.tokenLength = tokenLength;
342
343     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
344     responseInfo.result = CA_CONTENT;
345     responseInfo.info = responseData;
346
347     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
348
349     CADestroyToken(tempToken);
350     if (tempRep != NULL)
351     {
352         CADestroyEndpoint(tempRep);
353         tempRep = NULL;
354     }
355 }
356
357 // check return value NULL is passed instead of a valid CAResponseInfo_t address
358 TEST_F(CATests, SendResponseTest)
359 {
360     addr = (char *) ADDRESS;
361     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
362
363     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
364
365     if (tempRep != NULL)
366     {
367         CADestroyEndpoint(tempRep);
368         tempRep = NULL;
369     }
370 }
371
372 // CASendNotification TC
373 // check return value
374 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
375 {
376     addr = (char *) ADDRESS;
377     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
378
379     memset(&responseData, 0, sizeof(CAInfo_t));
380     responseData.type = CA_MSG_NONCONFIRM;
381     responseData.payload = (char *) "Temp Notification Data";
382
383     CAGenerateToken(&tempToken, tokenLength);
384     requestData.token = tempToken;
385     requestData.tokenLength = tokenLength;
386
387     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
388     responseInfo.result = CA_CONTENT;
389     responseInfo.info = responseData;
390
391     EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
392
393     CADestroyToken(tempToken);
394     if (tempRep != NULL)
395     {
396         CADestroyEndpoint(tempRep);
397         tempRep = NULL;
398     }
399 }
400
401 // CASelectNewwork TC
402 // check return value
403 TEST_F(CATests, SelectNetworkTestGood)
404 {
405     CAResult_t res = checkSelectNetwork();
406     EXPECT_EQ(CA_STATUS_OK, res);
407 }
408
409 CAResult_t checkSelectNetwork()
410 {
411     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
412
413     if (CA_STATUS_OK == res)
414     {
415         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
416         return CA_STATUS_OK;
417     }
418     if (CA_NOT_SUPPORTED == res)
419     {
420         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
421         return CA_STATUS_OK;
422     }
423
424     return res;
425 }
426
427 // check return value when selected network is disable
428 TEST_F(CATests, SelectNetworkTestBad)
429 {
430     //Select disable network
431     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork(1000));
432 }
433
434 // check return value when selected network is disable
435 TEST_F(CATests, UnSelectNetworkTest)
436 {
437     //UnSelect disable network
438     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(1000));
439 }
440
441 // CAHandlerRequestResponse TC
442 // check return value
443 TEST_F(CATests, HandlerRequestResponseTest)
444 {
445     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
446 }
447
448 // CAGetNetworkInformation TC
449 // check return value
450 TEST_F (CATests, GetNetworkInformationTestGood)
451 {
452     EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
453 }
454
455 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
456 {
457 #ifdef __WITH_DTLS__
458     if (SetCredentials() == 0)
459     {
460         printf("SetCredentials failed\n");
461     }
462
463     EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
464 #endif
465 }
466
467 CAResult_t checkGetNetworkInfo()
468 {
469     CAEndpoint_t *tempInfo = NULL;
470     uint32_t tempSize = 0;
471
472     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
473
474     free(tempInfo);
475
476     if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
477             CA_NOT_SUPPORTED == res)
478     {
479         return CA_STATUS_OK;
480     }
481     else
482     {
483         return CA_STATUS_FAILED;
484     }
485 }