Refactor PSK Credential retrieval interface
[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 // Iotivity Device Identity.
86 const unsigned char IDENTITY[] = ("1111111111111111");
87
88 // PSK between this device and peer device.
89 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
90
91 // Internal API. Invoked by CA stack to retrieve credentials from this module
92 int32_t CAGetDtlsPskCredentials( CADtlsPskCredType_t type,
93               const unsigned char *desc, size_t desc_len,
94               unsigned char *result, size_t result_length)
95 {
96     printf("CAGetDtlsPskCredentials IN\n");
97
98     int32_t ret = -1;
99
100     if (NULL == result)
101     {
102         return ret;
103     }
104
105     switch (type)
106     {
107         case CA_DTLS_PSK_HINT:
108         case CA_DTLS_PSK_IDENTITY:
109
110             if (result_length < sizeof(IDENTITY))
111             {
112                 printf("ERROR : Wrong value for result for storing IDENTITY");
113                 return ret;
114             }
115
116             memcpy(result, IDENTITY, sizeof(IDENTITY));
117             ret = sizeof(IDENTITY);
118             break;
119
120         case CA_DTLS_PSK_KEY:
121
122             if ((desc_len == sizeof(IDENTITY)) &&
123                 memcmp(desc, IDENTITY, sizeof(IDENTITY)) == 0)
124             {
125                 if (result_length < sizeof(RS_CLIENT_PSK))
126                 {
127                     printf("ERROR : Wrong value for result for storing RS_CLIENT_PSK");
128                     return ret;
129                 }
130
131                 memcpy(result, RS_CLIENT_PSK, sizeof(RS_CLIENT_PSK));
132                 ret = sizeof(RS_CLIENT_PSK);
133             }
134             break;
135
136         default:
137
138             printf("Wrong value passed for PSK_CRED_TYPE.");
139             ret = -1;
140     }
141
142
143     printf("CAGetDtlsPskCredentials OUT\n");
144     return ret;
145 }
146 #endif  //__WITH_DTLS__
147
148 int main(int argc, char **argv)
149 {
150     testing::InitGoogleTest(&argc, argv);
151     return RUN_ALL_TESTS();
152 }
153
154 // CAInitialize TC
155 // check return value
156 TEST(InitializeTest, TC_01_Positive_01)
157 {
158     EXPECT_EQ(CA_STATUS_OK, CAInitialize());
159     CATerminate();
160 }
161
162 //CATerminate TC
163 TEST_F(CATests, TerminateTest)
164 {
165     CATerminate();
166
167     char* check = (char *) "terminate success";
168     EXPECT_STREQ(check, "terminate success");
169
170     CAInitialize();
171 }
172 // CAStartListeningServer TC
173 // check return value
174 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
175 {
176     CASelectNetwork(CA_ADAPTER_IP);
177     EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
178 }
179
180 // CAStartDiscoveryServer TC
181 // check return value
182 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
183 {
184     EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
185 }
186
187 // CARegisterHandlerTest TC
188 // check return value
189 TEST_F(CATests, RegisterHandlerTest)
190 {
191     CARegisterHandler(request_handler, response_handler, error_handler);
192     char* check = (char *) "registerHandler success";
193     EXPECT_STREQ(check, "registerHandler success");
194 }
195
196 // CACreateRemoteEndpoint TC
197 // check return value
198 TEST_F(CATests, CreateRemoteEndpointTestGood)
199 {
200     addr = (char *) ADDRESS;
201
202     EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
203                                              PORT, &tempRep));
204
205     if (tempRep != NULL)
206     {
207         CADestroyEndpoint(tempRep);
208         tempRep = NULL;
209     }
210 }
211
212 // check remoteEndpoint and values of remoteEndpoint
213 TEST_F(CATests, CreateRemoteEndpointTestValues)
214 {
215     addr = (char *) ADDRESS;
216
217     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
218
219     EXPECT_TRUE(tempRep != NULL);
220
221     if (tempRep != NULL)
222     {
223         CADestroyEndpoint(tempRep);
224         tempRep = NULL;
225     }
226 }
227
228 // CAGerateToken TC
229 // check return value
230 TEST_F(CATests, GenerateTokenTestGood)
231 {
232     EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
233
234     CADestroyToken(tempToken);
235 }
236
237 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
238 TEST_F(CATests, GenerateTokenTestBad)
239 {
240     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
241 }
242
243 // CADestroyToken TC
244 // check destroyed token
245 TEST_F(CATests, DestroyTokenTest)
246 {
247     CAGenerateToken(&tempToken, tokenLength);
248     CADestroyToken(tempToken);
249
250     char * check = (char *) "destroy success";
251     EXPECT_STREQ(check, "destroy success");
252 }
253
254 // CASendRequest TC
255 // check return value
256 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
257 {
258     addr = (char *) ADDRESS;
259     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
260
261     memset(&requestData, 0, sizeof(CAInfo_t));
262     CAGenerateToken(&tempToken, tokenLength);
263     requestData.token = tempToken;
264     requestData.tokenLength = tokenLength;
265
266     int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
267     requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
268     if(!requestData.payload)
269     {
270         CADestroyToken(tempToken);
271         FAIL() << "requestData.payload allocation failed";
272     }
273     snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
274     requestData.payloadSize = length + 1;
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 = (CAPayload_t)malloc(sizeof("response payload"));
318
319     EXPECT_TRUE(responseData.payload != NULL);
320     if(!responseData.payload)
321     {
322         CADestroyEndpoint(tempRep);
323         return;
324     }
325
326     memcpy(responseData.payload, "response payload", sizeof("response payload"));
327     responseData.payloadSize = sizeof("response payload");
328
329     CAGenerateToken(&tempToken, tokenLength);
330     requestData.token = tempToken;
331     requestData.tokenLength = tokenLength;
332
333     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
334     responseInfo.result = CA_CONTENT;
335     responseInfo.info = responseData;
336
337     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
338
339     CADestroyToken(tempToken);
340     CADestroyEndpoint(tempRep);
341     free(responseData.payload);
342     tempRep = NULL;
343 }
344
345 // check return value when address is NULL as multicast
346 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
347 {
348     addr = NULL;
349     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
350
351     memset(&responseData, 0, sizeof(CAInfo_t));
352     responseData.type = CA_MSG_NONCONFIRM;
353     responseData.messageId = 1;
354     responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
355     EXPECT_TRUE(responseData.payload != NULL);
356
357     if(!responseData.payload)
358     {
359         CADestroyEndpoint(tempRep);
360         return;
361     }
362
363     memcpy(responseData.payload, "response payload", sizeof("response payload"));
364     responseData.payloadSize = sizeof("response payload");
365
366     CAGenerateToken(&tempToken, tokenLength);
367     requestData.token = tempToken;
368     requestData.tokenLength = tokenLength;
369
370     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
371     responseInfo.result = CA_CONTENT;
372     responseInfo.info = responseData;
373
374     EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
375
376     CADestroyToken(tempToken);
377     if (tempRep != NULL)
378     {
379         CADestroyEndpoint(tempRep);
380         tempRep = NULL;
381     }
382     free (responseData.payload);
383 }
384
385 // check return value NULL is passed instead of a valid CAResponseInfo_t address
386 TEST_F(CATests, SendResponseTest)
387 {
388     addr = (char *) ADDRESS;
389     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
390
391     EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
392
393     if (tempRep != NULL)
394     {
395         CADestroyEndpoint(tempRep);
396         tempRep = NULL;
397     }
398 }
399
400 // CASendNotification TC
401 // check return value
402 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
403 {
404     addr = (char *) ADDRESS;
405     CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
406
407     memset(&responseData, 0, sizeof(CAInfo_t));
408     responseData.type = CA_MSG_NONCONFIRM;
409     responseData.payload = (CAPayload_t)malloc(sizeof("Temp Notification Data"));
410
411     EXPECT_TRUE(responseData.payload != NULL);
412     if(!responseData.payload)
413     {
414         CADestroyEndpoint(tempRep);
415         return;
416     }
417
418     memcpy(responseData.payload, "Temp Notification Data", sizeof("Temp Notification Data"));
419     responseData.payloadSize = sizeof("Temp Notification Data");
420
421     CAGenerateToken(&tempToken, tokenLength);
422     requestData.token = tempToken;
423     requestData.tokenLength = tokenLength;
424
425     memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
426     responseInfo.result = CA_CONTENT;
427     responseInfo.info = responseData;
428
429     EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
430
431     CADestroyToken(tempToken);
432     if (tempRep != NULL)
433     {
434         CADestroyEndpoint(tempRep);
435         tempRep = NULL;
436     }
437     free(responseData.payload);
438 }
439
440 // CASelectNewwork TC
441 // check return value
442 TEST_F(CATests, SelectNetworkTestGood)
443 {
444     CAResult_t res = checkSelectNetwork();
445     EXPECT_EQ(CA_STATUS_OK, res);
446 }
447
448 CAResult_t checkSelectNetwork()
449 {
450     CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
451
452     if (CA_STATUS_OK == res)
453     {
454         EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
455         return CA_STATUS_OK;
456     }
457     if (CA_NOT_SUPPORTED == res)
458     {
459         EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
460         return CA_STATUS_OK;
461     }
462
463     return res;
464 }
465
466 // check return value when selected network is disable
467 TEST_F(CATests, SelectNetworkTestBad)
468 {
469     //Select disable network
470     EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork((CATransportAdapter_t)
471                                                 CA_TRANSPORT_ADAPTER_SCOPE));
472 }
473
474 // check return value when selected network is disable
475 TEST_F(CATests, UnSelectNetworkTest)
476 {
477     //UnSelect disable network
478     EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork((CATransportAdapter_t)
479                                                   CA_TRANSPORT_ADAPTER_SCOPE));
480 }
481
482 // CAHandlerRequestResponse TC
483 // check return value
484 TEST_F(CATests, HandlerRequestResponseTest)
485 {
486     EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
487 }
488
489 // CAGetNetworkInformation TC
490 // check return value
491 TEST_F (CATests, GetNetworkInformationTestGood)
492 {
493     EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
494 }
495
496 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
497 {
498 #ifdef __WITH_DTLS__
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 }