Imported Upstream version 0.9.2
[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     memcpy(responseData.payload, "response payload", sizeof("response payload"));
314     responseData.payloadSize = sizeof("response payload");
315
316     CAGenerateToken(&tempToken, tokenLength);
317     requestData.token = tempToken;
318     requestData.tokenLength = tokenLength;
319
320     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
321     responseInfo.result = CA_CONTENT;
322     responseInfo.info = responseData;
323
324     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
325
326     CADestroyToken(tempToken);
327     CADestroyEndpoint(tempRep);
328     free(responseData.payload);
329     tempRep = NULL;
330 }
331
332 // check return value when address is NULL as multicast
333 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
334 {
335     addr = NULL;
336     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
337
338     memset(&responseData, 0, sizeof(CAInfo_t));
339     responseData.type = CA_MSG_NONCONFIRM;
340     responseData.messageId = 1;
341     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
342     memcpy(responseData.payload, "response payload", sizeof("response payload"));
343     responseData.payloadSize = sizeof("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     free (responseData.payload);
362 }
363
364 // check return value NULL is passed instead of a valid CAResponseInfo_t address
365 TEST_F(CATests, SendResponseTest)
366 {
367     addr = (char *) ADDRESS;
368     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
369
370     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
371
372     if (tempRep != NULL)
373     {
374         CADestroyEndpoint(tempRep);
375         tempRep = NULL;
376     }
377 }
378
379 // CASendNotification TC
380 // check return value
381 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
382 {
383     addr = (char *) ADDRESS;
384     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
385
386     memset(&responseData, 0, sizeof(CAInfo_t));
387     responseData.type = CA_MSG_NONCONFIRM;
388     responseData.payload = (CAPayload_t)malloc(sizeof("Temp Notification Data"));
389     memcpy(responseData.payload, "Temp Notification Data", sizeof("Temp Notification Data"));
390     responseData.payloadSize = sizeof("Temp Notification Data");
391
392     CAGenerateToken(&tempToken, tokenLength);
393     requestData.token = tempToken;
394     requestData.tokenLength = tokenLength;
395
396     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
397     responseInfo.result = CA_CONTENT;
398     responseInfo.info = responseData;
399
400     EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
401
402     CADestroyToken(tempToken);
403     if (tempRep != NULL)
404     {
405         CADestroyEndpoint(tempRep);
406         tempRep = NULL;
407     }
408     free(responseData.payload);
409 }
410
411 // CASelectNewwork TC
412 // check return value
413 TEST_F(CATests, SelectNetworkTestGood)
414 {
415     CAResult_t res = checkSelectNetwork();
416     EXPECT_EQ(CA_STATUS_OK, res);
417 }
418
419 CAResult_t checkSelectNetwork()
420 {
421     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
422
423     if (CA_STATUS_OK == res)
424     {
425         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
426         return CA_STATUS_OK;
427     }
428     if (CA_NOT_SUPPORTED == res)
429     {
430         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
431         return CA_STATUS_OK;
432     }
433
434     return res;
435 }
436
437 // check return value when selected network is disable
438 TEST_F(CATests, SelectNetworkTestBad)
439 {
440     //Select disable network
441     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)1000));
442 }
443
444 // check return value when selected network is disable
445 TEST_F(CATests, UnSelectNetworkTest)
446 {
447     //UnSelect disable network
448     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)1000));
449 }
450
451 // CAHandlerRequestResponse TC
452 // check return value
453 TEST_F(CATests, HandlerRequestResponseTest)
454 {
455     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
456 }
457
458 // CAGetNetworkInformation TC
459 // check return value
460 TEST_F (CATests, GetNetworkInformationTestGood)
461 {
462     EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
463 }
464
465 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
466 {
467 #ifdef __WITH_DTLS__
468     if (SetCredentials() == 0)
469     {
470         printf("SetCredentials failed\n");
471     }
472
473     EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
474 #endif
475 }
476
477 CAResult_t checkGetNetworkInfo()
478 {
479     CAEndpoint_t *tempInfo = NULL;
480     uint32_t tempSize = 0;
481
482     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
483
484     free(tempInfo);
485
486     if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
487             CA_NOT_SUPPORTED == res)
488     {
489         return CA_STATUS_OK;
490     }
491     else
492     {
493         return CA_STATUS_FAILED;
494     }
495 }