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* 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";
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}}]}";
92 #define IDENTITY ("1111111111111111")
96 #define RS_CLIENT_PSK ("AAAAAAAAAAAAAAAA")
98 static CADtlsPskCredsBlob_t *pskCredsBlob = NULL;
100 void clearDtlsCredentialInfo()
102 printf("clearDtlsCredentialInfo IN\n");
105 // Initialize sensitive data to zeroes before freeing.
106 memset(pskCredsBlob->creds, 0, sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
107 free(pskCredsBlob->creds);
109 memset(pskCredsBlob, 0, sizeof(CADtlsPskCredsBlob_t));
113 printf("clearDtlsCredentialInfo OUT\n");
116 // Internal API. Invoked by OC stack to retrieve credentials from this module
117 void CAGetDtlsPskCredentials(CADtlsPskCredsBlob_t **credInfo)
119 printf("CAGetDtlsPskCredentials IN\n");
121 if (pskCredsBlob != NULL)
123 *credInfo = pskCredsBlob;
126 printf("CAGetDtlsPskCredentials OUT\n");
129 int32_t SetCredentials()
131 printf("SetCredentials IN\n");
132 pskCredsBlob = (CADtlsPskCredsBlob_t *)malloc(sizeof(CADtlsPskCredsBlob_t));
134 memset(pskCredsBlob, 0x0, sizeof(CADtlsPskCredsBlob_t));
135 memcpy(pskCredsBlob->identity, IDENTITY, DTLS_PSK_ID_LEN);
137 pskCredsBlob->num = 1;
139 pskCredsBlob->creds = (OCDtlsPskCreds *)malloc(sizeof(OCDtlsPskCreds) * (pskCredsBlob->num));
141 memcpy(pskCredsBlob->creds[0].id, IDENTITY, DTLS_PSK_ID_LEN);
142 memcpy(pskCredsBlob->creds[0].psk, RS_CLIENT_PSK, DTLS_PSK_PSK_LEN);
144 printf("SetCredentials OUT\n");
149 int main(int argc, char **argv)
151 testing::InitGoogleTest(&argc, argv);
152 return RUN_ALL_TESTS();
156 // check return value
157 TEST(InitializeTest, TC_01_Positive_01)
159 EXPECT_EQ(CA_STATUS_OK, CAInitialize());
164 TEST_F(CATests, TerminateTest)
168 char* check = (char *) "terminate success";
169 EXPECT_STREQ(check, "terminate success");
173 // CAStartListeningServer TC
174 // check return value
175 TEST(StartListeningServerTest, DISABLED_TC_03_Positive_01)
177 CASelectNetwork(CA_IPV4);
178 EXPECT_EQ(CA_STATUS_OK, CAStartListeningServer());
181 // CAStartDiscoveryServer TC
182 // check return value
183 TEST(StartDiscoveryServerTest, DISABLED_TC_04_Positive_01)
185 EXPECT_EQ(CA_STATUS_OK, CAStartDiscoveryServer());
188 // CARegisterHandlerTest TC
189 // check return value
190 TEST_F(CATests, RegisterHandlerTest)
192 CARegisterHandler(request_handler, response_handler, error_handler);
193 char* check = (char *) "registerHandler success";
194 EXPECT_STREQ(check, "registerHandler success");
197 // CACreateRemoteEndpoint TC
198 // check return value
199 TEST_F(CATests, CreateRemoteEndpointTestGood)
201 addr = (char *) ADDRESS;
203 EXPECT_EQ(CA_STATUS_OK, CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr,
208 CADestroyEndpoint(tempRep);
213 // check remoteEndpoint and values of remoteEndpoint
214 TEST_F(CATests, CreateRemoteEndpointTestValues)
216 addr = (char *) ADDRESS;
218 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
220 EXPECT_TRUE(tempRep != NULL);
224 CADestroyEndpoint(tempRep);
230 // check return value
231 TEST_F(CATests, GenerateTokenTestGood)
233 EXPECT_EQ(CA_STATUS_OK, CAGenerateToken(&tempToken, tokenLength));
235 CADestroyToken(tempToken);
238 // check return value when CAGenerateToken is passed a NULL instead a valid pointer
239 TEST_F(CATests, GenerateTokenTestBad)
241 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAGenerateToken(NULL, tokenLength));
245 // check destroyed token
246 TEST_F(CATests, DestroyTokenTest)
248 CAGenerateToken(&tempToken, tokenLength);
249 CADestroyToken(tempToken);
251 char * check = (char *) "destroy success";
252 EXPECT_STREQ(check, "destroy success");
256 // check return value
257 TEST(SendRequestTest, DISABLED_TC_16_Positive_01)
259 addr = (char *) ADDRESS;
260 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
262 memset(&requestData, 0, sizeof(CAInfo_t));
263 CAGenerateToken(&tempToken, tokenLength);
264 requestData.token = tempToken;
265 requestData.tokenLength = tokenLength;
267 int length = strlen(NORMAL_INFO_DATA) + strlen("a/light");
268 requestData.payload = (CAPayload_t) calloc(length, sizeof(char));
269 if(!requestData.payload)
271 CADestroyToken(tempToken);
272 FAIL() << "requestData.payload allocation failed";
274 snprintf(requestData.payload, length, NORMAL_INFO_DATA, "a/light");
275 requestData.type = CA_MSG_NONCONFIRM;
277 memset(&requestInfo, 0, sizeof(CARequestInfo_t));
278 requestInfo.method = CA_GET;
279 requestInfo.info = requestData;
281 EXPECT_EQ(CA_STATUS_OK, CASendRequest(tempRep, &requestInfo));
283 CADestroyToken(tempToken);
285 free(requestData.payload);
287 CADestroyEndpoint(tempRep);
292 // check return value when a NULL is passed instead of a valid CARequestInfo_t address
293 TEST_F(CATests, SendRequestTestWithNullAddr)
295 addr = (char *) ADDRESS;
296 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
298 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendRequest(tempRep, NULL));
302 CADestroyEndpoint(tempRep);
308 // check return value
309 TEST(SendResponseTest, DISABLED_TC_19_Positive_01)
311 addr = (char *) ADDRESS;
312 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
314 memset(&responseData, 0, sizeof(CAInfo_t));
315 responseData.type = CA_MSG_NONCONFIRM;
316 responseData.messageId = 1;
317 responseData.payload = (char *) "response payload";
319 CAGenerateToken(&tempToken, tokenLength);
320 requestData.token = tempToken;
321 requestData.tokenLength = tokenLength;
323 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
324 responseInfo.result = CA_CONTENT;
325 responseInfo.info = responseData;
327 EXPECT_EQ(CA_STATUS_OK, CASendResponse(tempRep, &responseInfo));
329 CADestroyToken(tempToken);
330 CADestroyEndpoint(tempRep);
334 // check return value when address is NULL as multicast
335 TEST(SendResponseTest, DISABLED_TC_20_Negative_01)
338 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, 0, &tempRep);
340 memset(&responseData, 0, sizeof(CAInfo_t));
341 responseData.type = CA_MSG_NONCONFIRM;
342 responseData.messageId = 1;
343 responseData.payload = (char *) "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);
363 // check return value NULL is passed instead of a valid CAResponseInfo_t address
364 TEST_F(CATests, SendResponseTest)
366 addr = (char *) ADDRESS;
367 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
369 EXPECT_EQ(CA_STATUS_INVALID_PARAM, CASendResponse(tempRep, NULL));
373 CADestroyEndpoint(tempRep);
378 // CASendNotification TC
379 // check return value
380 TEST(SendNotificationTest, DISABLED_TC_22_Positive_01)
382 addr = (char *) ADDRESS;
383 CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, addr, PORT, &tempRep);
385 memset(&responseData, 0, sizeof(CAInfo_t));
386 responseData.type = CA_MSG_NONCONFIRM;
387 responseData.payload = (char *) "Temp Notification Data";
389 CAGenerateToken(&tempToken, tokenLength);
390 requestData.token = tempToken;
391 requestData.tokenLength = tokenLength;
393 memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
394 responseInfo.result = CA_CONTENT;
395 responseInfo.info = responseData;
397 EXPECT_EQ(CA_STATUS_OK, CASendNotification(tempRep, &responseInfo));
399 CADestroyToken(tempToken);
402 CADestroyEndpoint(tempRep);
407 // CASelectNewwork TC
408 // check return value
409 TEST_F(CATests, SelectNetworkTestGood)
411 CAResult_t res = checkSelectNetwork();
412 EXPECT_EQ(CA_STATUS_OK, res);
415 CAResult_t checkSelectNetwork()
417 CAResult_t res = CASelectNetwork(CA_ADAPTER_IP);
419 if (CA_STATUS_OK == res)
421 EXPECT_EQ(CA_STATUS_OK, CAUnSelectNetwork(CA_ADAPTER_IP));
424 if (CA_NOT_SUPPORTED == res)
426 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(CA_ADAPTER_IP));
433 // check return value when selected network is disable
434 TEST_F(CATests, SelectNetworkTestBad)
436 //Select disable network
437 EXPECT_EQ(CA_NOT_SUPPORTED, CASelectNetwork(1000));
440 // check return value when selected network is disable
441 TEST_F(CATests, UnSelectNetworkTest)
443 //UnSelect disable network
444 EXPECT_EQ(CA_STATUS_FAILED, CAUnSelectNetwork(1000));
447 // CAHandlerRequestResponse TC
448 // check return value
449 TEST_F(CATests, HandlerRequestResponseTest)
451 EXPECT_EQ(CA_STATUS_OK, CAHandleRequestResponse());
454 // CAGetNetworkInformation TC
455 // check return value
456 TEST_F (CATests, GetNetworkInformationTestGood)
458 EXPECT_EQ(CA_STATUS_OK, checkGetNetworkInfo());
461 TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
464 if (SetCredentials() == 0)
466 printf("SetCredentials failed\n");
469 EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
473 CAResult_t checkGetNetworkInfo()
475 CAEndpoint_t *tempInfo = NULL;
476 uint32_t tempSize = 0;
478 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
482 if (CA_STATUS_OK == res || CA_ADAPTER_NOT_ENABLED == res ||
483 CA_NOT_SUPPORTED == res)
489 return CA_STATUS_FAILED;