1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "gtest/gtest.h"
22 #include "cainterface.h"
26 class CATests : public testing::Test {
28 virtual void SetUp() {
32 virtual void TearDown()
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();
44 void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
49 void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
54 void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo)
56 if(!object || !errorInfo)
61 //error handling shall be added
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;
76 static const char NORMAL_INFO_DATA[] =
77 "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
78 "\"if\":[\"oc.mi.def\"],\"obs\":1}}]}";
86 #define IDENTITY ("1111111111111111")
90 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
92 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
94 void clearDtlsCredentialInfo()
96 printf("clearDtlsCredentialInfo IN\n");
99 // Initialize sensitive data to zeroes before freeing.
100 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
101 free(pskCredsBlob->creds);
103 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
107 printf("clearDtlsCredentialInfo OUT\n");
110 // Internal API. Invoked by OC stack to retrieve credentials from this module
111 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
113 printf("CAGetDtlsPskCredentials IN\n");
115 if (pskCredsBlob != NULL)
117 *credInfo = pskCredsBlob;
120 printf("CAGetDtlsPskCredentials OUT\n");
123 int32_t SetCredentials()
125 printf("SetCredentials IN\n");
126 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
128 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
129 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
131 pskCredsBlob->num = 1;
133 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
135 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
136 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
138 printf("SetCredentials OUT\n");
143 int main(int argc, char **argv)
145 testing::InitGoogleTest(&argc, argv);
146 return RUN_ALL_TESTS();
150 // check return value
151 TEST(InitializeTest, TC_01_Positive_01)
153 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
158 TEST_F(CATests, TerminateTest)
162 char* check = (char *) "terminate success";
163 EXPECT_STREQ(check, "terminate success");
167 // CAStartListeningServer TC
168 // check return value
169 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
171 CASelectNetwork(CA_IPV4);
172 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
175 // CAStartDiscoveryServer TC
176 // check return value
177 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
179 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
182 // CARegisterHandlerTest TC
183 // check return value
184 TEST_F(CATests, RegisterHandlerTest)
186 CARegisterHandler(request_handler, response_handler, error_handler);
187 char* check = (char *) "registerHandler success";
188 EXPECT_STREQ(check, "registerHandler success");
191 // CACreateRemoteEndpoint TC
192 // check return value
193 TEST_F(CATests, CreateRemoteEndpointTestGood)
195 addr = (char *) ADDRESS;
197 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
202 CADestroyEndpoint(tempRep);
207 // check remoteEndpoint and values of remoteEndpoint
208 TEST_F(CATests, CreateRemoteEndpointTestValues)
210 addr = (char *) ADDRESS;
212 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
214 EXPECT_TRUE(tempRep != NULL);
218 CADestroyEndpoint(tempRep);
224 // check return value
225 TEST_F(CATests, GenerateTokenTestGood)
227 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
229 CADestroyToken(tempToken);
232 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
233 TEST_F(CATests, GenerateTokenTestBad)
235 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
239 // check destroyed token
240 TEST_F(CATests, DestroyTokenTest)
242 CAGenerateToken(&tempToken, tokenLength);
243 CADestroyToken(tempToken);
245 char * check = (char *) "destroy success";
246 EXPECT_STREQ(check, "destroy success");
250 // check return value
251 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
253 addr = (char *) ADDRESS;
254 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
256 memset(&requestData, 0, sizeof(CAInfo_t));
257 CAGenerateToken(&tempToken, tokenLength);
258 requestData.token = tempToken;
259 requestData.tokenLength = tokenLength;
261 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
262 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
263 if(!requestData.payload)
265 CADestroyToken(tempToken);
266 FAIL() << "requestData.payload allocation failed";
268 snprintf((char*)requestData.payload, length, NORMAL_INFO_DATA, "a/light");
269 requestData.payloadSize = length + 1;
270 requestData.type = CA_MSG_NONCONFIRM;
272 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
273 requestInfo.method = CA_GET;
274 requestInfo.info = requestData;
276 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
278 CADestroyToken(tempToken);
280 free(requestData.payload);
282 CADestroyEndpoint(tempRep);
287 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
288 TEST_F(CATests, SendRequestTestWithNullAddr)
290 addr = (char *) ADDRESS;
291 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
293 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
297 CADestroyEndpoint(tempRep);
303 // check return value
304 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
306 addr = (char *) ADDRESS;
307 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
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");
316 CAGenerateToken(&tempToken, tokenLength);
317 requestData.token = tempToken;
318 requestData.tokenLength = tokenLength;
320 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
321 responseInfo.result = CA_CONTENT;
322 responseInfo.info = responseData;
324 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
326 CADestroyToken(tempToken);
327 CADestroyEndpoint(tempRep);
328 free(responseData.payload);
332 // check return value when address is NULL as multicast
333 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
336 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
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");
345 CAGenerateToken(&tempToken, tokenLength);
346 requestData.token = tempToken;
347 requestData.tokenLength = tokenLength;
349 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
350 responseInfo.result = CA_CONTENT;
351 responseInfo.info = responseData;
353 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
355 CADestroyToken(tempToken);
358 CADestroyEndpoint(tempRep);
361 free (responseData.payload);
364 // check return value NULL is passed instead of a valid CAResponseInfo_t address
365 TEST_F(CATests, SendResponseTest)
367 addr = (char *) ADDRESS;
368 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
370 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
374 CADestroyEndpoint(tempRep);
379 // CASendNotification TC
380 // check return value
381 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
383 addr = (char *) ADDRESS;
384 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
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");
392 CAGenerateToken(&tempToken, tokenLength);
393 requestData.token = tempToken;
394 requestData.tokenLength = tokenLength;
396 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
397 responseInfo.result = CA_CONTENT;
398 responseInfo.info = responseData;
400 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
402 CADestroyToken(tempToken);
405 CADestroyEndpoint(tempRep);
408 free(responseData.payload);
411 // CASelectNewwork TC
412 // check return value
413 TEST_F(CATests, SelectNetworkTestGood)
415 CAResult_t res = checkSelectNetwork();
416 EXPECT_EQ(CA_STATUS_OK, res);
419 CAResult_t checkSelectNetwork()
421 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
423 if (CA_STATUS_OK == res)
425 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
428 if (CA_NOT_SUPPORTED == res)
430 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
437 // check return value when selected network is disable
438 TEST_F(CATests, SelectNetworkTestBad)
440 //Select disable network
441 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork(1000));
444 // check return value when selected network is disable
445 TEST_F(CATests, UnSelectNetworkTest)
447 //UnSelect disable network
448 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(1000));
451 // CAHandlerRequestResponse TC
452 // check return value
453 TEST_F(CATests, HandlerRequestResponseTest)
455 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
458 // CAGetNetworkInformation TC
459 // check return value
460 TEST_F (CATests, GetNetworkInformationTestGood)
462 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
465 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
468 if (SetCredentials() == 0)
470 printf("SetCredentials failed\n");
473 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
477 CAResult_t checkGetNetworkInfo()
479 CAEndpoint_t *tempInfo = NULL;
480 uint32_t tempSize = 0;
482 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
486 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
487 CA_NOT_SUPPORTED == res)
493 return CA_STATUS_FAILED;