1 /* *****************************************************************
3 * Copyright 2015 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 * *****************************************************************/
20 #include "gtest/gtest.h"
21 #include "pmutility.h"
22 #include "pmutilityinternal.h"
23 #include "doxmresource.h"
25 #include "oic_malloc.h"
34 extern OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, const char* addr,
35 const uint16_t port, OCTransportAdapter adapter,
36 OCConnectivityType connType, OicSecDoxm_t *doxm);
41 OCProvisionDev_t* gList = NULL;
43 static bool UuidsIdentical(const OicUuid_t* first, const OicUuid_t* second)
45 return (0 == memcmp(first, second, sizeof(OicUuid_t)));
49 class CloneOicSecMomTest : public testing::Test
52 CloneOicSecMomTest() :
53 m_originalStruct({OIC_MULTIPLE_OWNER_DISABLE}),
59 OICFree(m_clonedStruct);
63 OicSecMom_t m_originalStruct;
64 OicSecMom_t* m_clonedStruct;
67 TEST(CloneOicSecMomSimpleTest, shouldReturnNullForNullInput)
69 EXPECT_TRUE(NULL == CloneOicSecMom(NULL));
72 TEST_F(CloneOicSecMomTest, shouldReturnValidPointerForValidInput)
74 m_clonedStruct = CloneOicSecMom(&m_originalStruct);
75 EXPECT_FALSE(NULL == m_clonedStruct);
78 TEST_F(CloneOicSecMomTest, copyShouldHaveTheSameValueAsOriginal)
80 m_clonedStruct = CloneOicSecMom(&m_originalStruct);
81 EXPECT_EQ(m_originalStruct.mode, m_clonedStruct->mode);
84 TEST_F(CloneOicSecMomTest, shouldReturnDifferentAddressThanOriginalForValidInput)
86 m_clonedStruct = CloneOicSecMom(&m_originalStruct);
87 EXPECT_NE(&m_originalStruct, m_clonedStruct);
90 static OicSecSubOwner_t* BuildSampleOicSecSubOwner(MotStatus_t status,
91 const OicUuid_t* uuid)
93 OicSecSubOwner_t* result =
94 (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
100 result->status = status;
101 memcpy(&result->uuid, uuid, sizeof(OicUuid_t));
107 static void FreeOicSecSubOwner(OicSecSubOwner_t* input)
109 OicSecSubOwner_t* current = input;
110 OicSecSubOwner_t* temp = input;
111 while (NULL != current)
114 current = temp->next;
119 class CloneOicSecSubOwnerTest : public testing::Test
122 CloneOicSecSubOwnerTest() :
123 m_originalStruct(NULL),
130 BuildSampleOicSecSubOwner(MOT_STATUS_READY, &s_sampleUuid);
131 ASSERT_FALSE(NULL == m_originalStruct);
133 m_originalStruct->next =
134 BuildSampleOicSecSubOwner(MOT_STATUS_IN_PROGRESS, &s_sampleNextUuid);
135 ASSERT_FALSE(NULL == m_originalStruct->next);
140 FreeOicSecSubOwner(m_originalStruct);
141 FreeOicSecSubOwner(m_clonedStruct);
145 static const OicUuid_t s_sampleUuid;
146 static const OicUuid_t s_sampleNextUuid;
148 OicSecSubOwner_t* m_originalStruct;
149 OicSecSubOwner_t* m_clonedStruct;
152 const OicUuid_t CloneOicSecSubOwnerTest::s_sampleUuid = {
153 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
157 const OicUuid_t CloneOicSecSubOwnerTest::s_sampleNextUuid = {
158 { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF }
162 TEST(CloneOicSecSubOwnerSimpleTest, shouldReturnNullForNullInput)
164 EXPECT_TRUE(NULL == CloneOicSecSubOwner(NULL));
167 TEST_F(CloneOicSecSubOwnerTest, shouldReturnValidPointerForValidInput)
169 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
170 EXPECT_FALSE(NULL == m_clonedStruct);
173 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveTheSameStatusAsOriginal)
175 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
176 EXPECT_EQ(m_originalStruct->status, m_clonedStruct->status);
179 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveTheSameUuidAsOriginal)
181 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
182 EXPECT_TRUE(UuidsIdentical(&m_originalStruct->uuid, &m_clonedStruct->uuid));
185 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveNext)
187 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
188 EXPECT_FALSE(NULL == m_clonedStruct->next);
191 TEST_F(CloneOicSecSubOwnerTest, copyNextShouldHaveTheSameStatusAsOriginal)
193 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
194 EXPECT_EQ(m_originalStruct->next->status, m_clonedStruct->next->status);
197 TEST_F(CloneOicSecSubOwnerTest, copyNextShouldHaveTheSameUuidAsOriginal)
199 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
200 EXPECT_TRUE(UuidsIdentical(&m_originalStruct->next->uuid,
201 &m_clonedStruct->next->uuid));
204 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveDifferentAddressThanOriginal)
206 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
207 EXPECT_NE(m_originalStruct, m_clonedStruct);
210 TEST_F(CloneOicSecSubOwnerTest, copyNextShouldHaveDifferentAddressThanOriginal)
212 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
213 EXPECT_NE(m_originalStruct->next, m_clonedStruct->next);
216 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveNullLastNext)
218 m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
219 EXPECT_TRUE(NULL == m_clonedStruct->next->next);
223 class CloneOicSecDoxmTest : public testing::Test
226 CloneOicSecDoxmTest() :
227 m_originalStruct(NULL),
233 m_originalStruct = BuildSampleOicSecDoxm();
234 ASSERT_FALSE(NULL == m_originalStruct);
239 DeleteDoxmBinData(m_originalStruct);
240 DeleteDoxmBinData(m_clonedStruct);
245 static const OicUuid_t s_sampleDeviceId;
246 static const OicUuid_t s_sampleOwner;
247 static const OicUuid_t s_sampleOwnerId;
248 static const OicUuid_t s_sampleSubOwner;
250 OicSecDoxm_t* m_originalStruct;
251 OicSecDoxm_t* m_clonedStruct;
255 OicSecDoxm_t* BuildSampleOicSecDoxm()
257 OicSecDoxm_t* result =
258 (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
264 memcpy(&result->deviceID, &s_sampleDeviceId, sizeof(result->deviceID));
267 #ifdef MULTIPLE_OWNER
268 result->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
269 if (NULL == result->mom)
273 result->mom->mode = OIC_MULTIPLE_OWNER_DISABLE;
275 BuildSampleOicSecSubOwner(MOT_STATUS_IN_PROGRESS, &s_sampleSubOwner);
276 if (NULL == result->subOwners)
282 result->owned = true;
283 memcpy(&result->owner, &s_sampleOwner, sizeof(result->owner));
284 result->oxm = (OicSecOxm_t*)OICCalloc(1, sizeof(OicSecOxm_t));
285 if (NULL == result->oxm)
289 result->oxm[0] = OIC_JUST_WORKS;
291 result->oxmSel = OIC_JUST_WORKS;
292 memcpy(&result->rownerID, &s_sampleOwnerId, sizeof(result->rownerID));
293 result->sct = NO_SECURITY_MODE;
299 const OicUuid_t CloneOicSecDoxmTest::s_sampleDeviceId = {
300 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
304 const OicUuid_t CloneOicSecDoxmTest::s_sampleOwner = {
305 { 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA }
309 const OicUuid_t CloneOicSecDoxmTest::s_sampleOwnerId = {
310 { 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF }
314 const OicUuid_t CloneOicSecDoxmTest::s_sampleSubOwner = {
315 { 0xCC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCC }
319 TEST(CloneOicSecDoxmSimpleTest, shouldReturnNullForNullInput)
321 EXPECT_TRUE(NULL == CloneOicSecDoxm(NULL));
324 TEST_F(CloneOicSecDoxmTest, shouldReturnValidPointerForValidInput)
326 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
327 EXPECT_FALSE(NULL == m_clonedStruct);
330 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNulledOxm)
332 ASSERT_FALSE(NULL == m_originalStruct->oxm);
334 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
335 EXPECT_TRUE(NULL == m_clonedStruct->oxm);
338 TEST_F(CloneOicSecDoxmTest, copyShouldHaveZeroedOxmLen)
340 ASSERT_NE(0U, m_originalStruct->oxmLen);
342 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
343 EXPECT_EQ(0U, m_clonedStruct->oxmLen);
346 #ifdef MULTIPLE_OWNER
347 TEST_F(CloneOicSecDoxmTest, shouldReturnValidPointerForInputWithNullMom)
349 OICFree(m_originalStruct->mom);
350 m_originalStruct->mom = NULL;
352 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
353 EXPECT_FALSE(NULL == m_clonedStruct);
356 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNonNullMom)
358 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
359 EXPECT_FALSE(NULL == m_clonedStruct->mom);
362 TEST_F(CloneOicSecDoxmTest, copyShouldHaveDifferentMomAddress)
364 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
365 EXPECT_NE(m_originalStruct->mom, m_clonedStruct->mom);
368 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNullMomForNullOriginal)
370 OICFree(m_originalStruct->mom);
371 m_originalStruct->mom = NULL;
373 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
374 EXPECT_TRUE(NULL == m_clonedStruct->mom);
377 TEST_F(CloneOicSecDoxmTest, shouldReturnValidPointerForInputWithNullSubOwners)
379 FreeOicSecSubOwner(m_originalStruct->subOwners);
380 m_originalStruct->subOwners = NULL;
382 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
383 EXPECT_FALSE(NULL == m_clonedStruct);
386 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNonNullSubOwners)
388 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
389 EXPECT_FALSE(NULL == m_clonedStruct->subOwners);
392 TEST_F(CloneOicSecDoxmTest, copyShouldHaveDifferentSubOwnersAddress)
394 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
395 EXPECT_NE(m_originalStruct->subOwners, m_clonedStruct->subOwners);
398 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNullSubOwnersForNullOriginal)
400 FreeOicSecSubOwner(m_originalStruct->subOwners);
401 m_originalStruct->subOwners = NULL;
403 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
404 EXPECT_TRUE(NULL == m_clonedStruct->subOwners);
408 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameDeviceIdAsOriginal)
410 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
411 EXPECT_TRUE(UuidsIdentical(&m_originalStruct->deviceID,
412 &m_clonedStruct->deviceID));
415 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameOwnerIdAsOriginal)
417 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
418 EXPECT_TRUE(UuidsIdentical(&m_originalStruct->owner,
419 &m_clonedStruct->owner));
422 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameRownerIdAsOriginal)
424 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
425 EXPECT_TRUE(UuidsIdentical(&m_originalStruct->rownerID,
426 &m_clonedStruct->rownerID));
429 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameSctAsOriginal)
431 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
432 EXPECT_EQ(m_originalStruct->sct, m_clonedStruct->sct);
435 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameOwnedAsOriginal)
437 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
438 EXPECT_EQ(m_originalStruct->owned, m_clonedStruct->owned);
441 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameDpcAsOriginal)
443 m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
444 EXPECT_EQ(m_originalStruct->dpc, m_clonedStruct->dpc);
447 class CloneOCProvisionDevListTest : public ::testing::Test
450 CloneOCProvisionDevListTest() :
451 m_originalStruct(NULL),
457 m_originalStruct = BuildSampleOCProvisionDev();
458 ASSERT_FALSE(NULL == m_originalStruct);
460 m_originalStruct->next = BuildSampleOCProvisionDev();
461 ASSERT_FALSE(NULL == m_originalStruct->next);
466 PMDeleteDeviceList(m_originalStruct);
467 PMDeleteDeviceList(m_clonedStruct);
471 OCProvisionDev_t* m_originalStruct;
472 OCProvisionDev_t* m_clonedStruct;
476 OCProvisionDev_t* BuildSampleOCProvisionDev()
478 return (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
482 TEST(CloneOCProvisionDevListSimpleTest, shouldReturnNullForNullInput)
484 EXPECT_TRUE(NULL == PMCloneOCProvisionDevList(NULL));
487 TEST_F(CloneOCProvisionDevListTest, shouldReturnValidPointerForNonNullInput)
489 m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
491 EXPECT_FALSE(NULL == m_clonedStruct);
494 TEST_F(CloneOCProvisionDevListTest, shouldReturnValidPointerForNonNext)
496 m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
498 EXPECT_FALSE(NULL == m_clonedStruct->next);
501 TEST_F(CloneOCProvisionDevListTest, copyShouldHaveDifferentAddress)
503 m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
505 EXPECT_NE(m_originalStruct, m_clonedStruct);
508 TEST_F(CloneOCProvisionDevListTest, copyShouldHaveDifferentAddressForNext)
510 m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
512 EXPECT_NE(m_originalStruct->next, m_clonedStruct->next);
517 TEST(ProvisionListTest, Addition)
519 OCProvisionDev_t* el = NULL;
520 OCStackResult res = OC_STACK_ERROR;
521 OicSecDoxm_t* pDoxm = NULL;
524 // The first addition
525 res = AddDevice(&gList, "10.20.30.40", 5684, OC_DEFAULT_ADAPTER, CT_IP_USE_V4, pDoxm);
526 EXPECT_TRUE(OC_STACK_OK == res);
527 EXPECT_TRUE(NULL != gList);
529 LL_FOREACH(gList,el){ ++cnt; };
530 EXPECT_TRUE(1 == cnt);
532 // Same node must not be inserted
533 res = AddDevice(&gList, "10.20.30.40", 5684, OC_ADAPTER_IP, CT_IP_USE_V4, pDoxm);
534 EXPECT_TRUE(OC_STACK_OK == res);
535 EXPECT_TRUE(NULL != gList);
538 LL_FOREACH(gList,el){ ++cnt; };
539 EXPECT_TRUE(1 == cnt);
541 // Differnet node must be inserted
542 res = AddDevice(&gList, "110.120.130.140", 6789, OC_DEFAULT_ADAPTER, CT_IP_USE_V4, pDoxm);
543 EXPECT_TRUE(OC_STACK_OK == res);
544 EXPECT_TRUE(NULL != gList);
547 LL_FOREACH(gList,el){ ++cnt; };
548 EXPECT_TRUE(2 == cnt);
552 TEST(ProvisionListTest, Deletion)
554 OCProvisionDev_t* el = NULL;
558 PMDeleteDeviceList(gList);
561 LL_FOREACH(gList,el){ ++cnt; };
562 EXPECT_TRUE(0 == cnt);