Fix provisioning unit tests
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / unittest / pmutilitytest.cpp
1 /* *****************************************************************
2  *
3  * Copyright 2015 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 #include "gtest/gtest.h"
21 #include "pmutility.h"
22 #include "pmutilityinternal.h"
23 #include "doxmresource.h"
24 #include "ocstack.h"
25 #include "oic_malloc.h"
26 #include "utlist.h"
27
28 using namespace std;
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 extern OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, const char* addr,
35                                const uint16_t port, OCTransportAdapter adapter,
36                                OCConnectivityType connType, OicSecDoxm_t *doxm);
37 #ifdef __cplusplus
38 }
39 #endif
40
41 OCProvisionDev_t* gList = NULL;
42
43 static bool UuidsIdentical(const OicUuid_t* first, const OicUuid_t* second)
44 {
45     return (0 == memcmp(first, second, sizeof(OicUuid_t)));
46 }
47
48 #ifdef MULTIPLE_OWNER
49 class CloneOicSecMomTest : public testing::Test
50 {
51 public:
52     CloneOicSecMomTest() :
53         m_originalStruct({OIC_MULTIPLE_OWNER_DISABLE}),
54         m_clonedStruct(NULL)
55     {}
56
57     void TearDown()
58     {
59         OICFree(m_clonedStruct);
60     }
61 protected:
62
63     OicSecMom_t m_originalStruct;
64     OicSecMom_t* m_clonedStruct;
65 };
66
67 TEST(CloneOicSecMomSimpleTest, shouldReturnNullForNullInput)
68 {
69     EXPECT_TRUE(NULL == CloneOicSecMom(NULL));
70 }
71
72 TEST_F(CloneOicSecMomTest, shouldReturnValidPointerForValidInput)
73 {
74     m_clonedStruct = CloneOicSecMom(&m_originalStruct);
75     EXPECT_FALSE(NULL == m_clonedStruct);
76 }
77
78 TEST_F(CloneOicSecMomTest, copyShouldHaveTheSameValueAsOriginal)
79 {
80     m_clonedStruct = CloneOicSecMom(&m_originalStruct);
81     EXPECT_EQ(m_originalStruct.mode, m_clonedStruct->mode);
82 }
83
84 TEST_F(CloneOicSecMomTest, shouldReturnDifferentAddressThanOriginalForValidInput)
85 {
86     m_clonedStruct = CloneOicSecMom(&m_originalStruct);
87     EXPECT_NE(&m_originalStruct, m_clonedStruct);
88 }
89
90 static OicSecSubOwner_t* BuildSampleOicSecSubOwner(MotStatus_t status,
91                                                    const OicUuid_t* uuid)
92 {
93     OicSecSubOwner_t* result =
94         (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
95     if (NULL == result)
96     {
97         return NULL;
98     }
99
100     result->status = status;
101     memcpy(&result->uuid, uuid, sizeof(OicUuid_t));
102     result->next = NULL;
103
104     return result;
105 }
106
107 static void FreeOicSecSubOwner(OicSecSubOwner_t* input)
108 {
109     OicSecSubOwner_t* current = input;
110     OicSecSubOwner_t* temp = input;
111     while (NULL != current)
112     {
113         temp = current;
114         current = temp->next;
115         OICFree(temp);
116     }
117 }
118
119 class CloneOicSecSubOwnerTest : public testing::Test
120 {
121 public:
122     CloneOicSecSubOwnerTest() :
123         m_originalStruct(NULL),
124         m_clonedStruct(NULL)
125     {}
126
127     void SetUp()
128     {
129         m_originalStruct =
130             BuildSampleOicSecSubOwner(MOT_STATUS_READY, &s_sampleUuid);
131         ASSERT_FALSE(NULL == m_originalStruct);
132
133         m_originalStruct->next =
134             BuildSampleOicSecSubOwner(MOT_STATUS_IN_PROGRESS, &s_sampleNextUuid);
135         ASSERT_FALSE(NULL == m_originalStruct->next);
136     }
137
138     void TearDown()
139     {
140         FreeOicSecSubOwner(m_originalStruct);
141         FreeOicSecSubOwner(m_clonedStruct);
142     }
143 protected:
144
145     static const OicUuid_t s_sampleUuid;
146     static const OicUuid_t s_sampleNextUuid;
147
148     OicSecSubOwner_t* m_originalStruct;
149     OicSecSubOwner_t* m_clonedStruct;
150 };
151
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 }
155 };
156
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 }
160 };
161
162 TEST(CloneOicSecSubOwnerSimpleTest, shouldReturnNullForNullInput)
163 {
164     EXPECT_TRUE(NULL == CloneOicSecSubOwner(NULL));
165 }
166
167 TEST_F(CloneOicSecSubOwnerTest, shouldReturnValidPointerForValidInput)
168 {
169     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
170     EXPECT_FALSE(NULL == m_clonedStruct);
171 }
172
173 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveTheSameStatusAsOriginal)
174 {
175     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
176     EXPECT_EQ(m_originalStruct->status, m_clonedStruct->status);
177 }
178
179 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveTheSameUuidAsOriginal)
180 {
181     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
182     EXPECT_TRUE(UuidsIdentical(&m_originalStruct->uuid, &m_clonedStruct->uuid));
183 }
184
185 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveNext)
186 {
187     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
188     EXPECT_FALSE(NULL == m_clonedStruct->next);
189 }
190
191 TEST_F(CloneOicSecSubOwnerTest, copyNextShouldHaveTheSameStatusAsOriginal)
192 {
193     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
194     EXPECT_EQ(m_originalStruct->next->status, m_clonedStruct->next->status);
195 }
196
197 TEST_F(CloneOicSecSubOwnerTest, copyNextShouldHaveTheSameUuidAsOriginal)
198 {
199     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
200     EXPECT_TRUE(UuidsIdentical(&m_originalStruct->next->uuid,
201                                &m_clonedStruct->next->uuid));
202 }
203
204 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveDifferentAddressThanOriginal)
205 {
206     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
207     EXPECT_NE(m_originalStruct, m_clonedStruct);
208 }
209
210 TEST_F(CloneOicSecSubOwnerTest, copyNextShouldHaveDifferentAddressThanOriginal)
211 {
212     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
213     EXPECT_NE(m_originalStruct->next, m_clonedStruct->next);
214 }
215
216 TEST_F(CloneOicSecSubOwnerTest, copyShouldHaveNullLastNext)
217 {
218     m_clonedStruct = CloneOicSecSubOwner(m_originalStruct);
219     EXPECT_TRUE(NULL == m_clonedStruct->next->next);
220 }
221 #endif
222
223 class CloneOicSecDoxmTest : public testing::Test
224 {
225 public:
226     CloneOicSecDoxmTest() :
227         m_originalStruct(NULL),
228         m_clonedStruct(NULL)
229     {}
230
231     void SetUp()
232     {
233         m_originalStruct = BuildSampleOicSecDoxm();
234         ASSERT_FALSE(NULL == m_originalStruct);
235     }
236
237     void TearDown()
238     {
239         DeleteDoxmBinData(m_originalStruct);
240         DeleteDoxmBinData(m_clonedStruct);
241     }
242
243 protected:
244
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;
249
250     OicSecDoxm_t* m_originalStruct;
251     OicSecDoxm_t* m_clonedStruct;
252
253 private:
254
255     OicSecDoxm_t* BuildSampleOicSecDoxm()
256     {
257         OicSecDoxm_t* result =
258             (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
259         if (NULL == result)
260         {
261             return NULL;
262         }
263
264         memcpy(&result->deviceID, &s_sampleDeviceId, sizeof(result->deviceID));
265         result->dpc = true;
266
267 #ifdef MULTIPLE_OWNER
268         result->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
269         if (NULL == result->mom)
270         {
271             return NULL;
272         }
273         result->mom->mode = OIC_MULTIPLE_OWNER_DISABLE;
274         result->subOwners =
275             BuildSampleOicSecSubOwner(MOT_STATUS_IN_PROGRESS, &s_sampleSubOwner);
276         if (NULL == result->subOwners)
277         {
278             return NULL;
279         }
280 #endif
281
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)
286         {
287             return NULL;
288         }
289         result->oxm[0] = OIC_JUST_WORKS;
290         result->oxmLen = 1U;
291         result->oxmSel = OIC_JUST_WORKS;
292         memcpy(&result->rownerID, &s_sampleOwnerId, sizeof(result->rownerID));
293         result->sct = NO_SECURITY_MODE;
294
295         return result;
296     }
297 };
298
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 }
302 };
303
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 }
307 };
308
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 }
312 };
313
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 }
317 };
318
319 TEST(CloneOicSecDoxmSimpleTest, shouldReturnNullForNullInput)
320 {
321     EXPECT_TRUE(NULL == CloneOicSecDoxm(NULL));
322 }
323
324 TEST_F(CloneOicSecDoxmTest, shouldReturnValidPointerForValidInput)
325 {
326     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
327     EXPECT_FALSE(NULL == m_clonedStruct);
328 }
329
330 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNulledOxm)
331 {
332     ASSERT_FALSE(NULL == m_originalStruct->oxm);
333
334     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
335     EXPECT_TRUE(NULL == m_clonedStruct->oxm);
336 }
337
338 TEST_F(CloneOicSecDoxmTest, copyShouldHaveZeroedOxmLen)
339 {
340     ASSERT_NE(0U, m_originalStruct->oxmLen);
341
342     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
343     EXPECT_EQ(0U, m_clonedStruct->oxmLen);
344 }
345
346 #ifdef MULTIPLE_OWNER
347 TEST_F(CloneOicSecDoxmTest, shouldReturnValidPointerForInputWithNullMom)
348 {
349     OICFree(m_originalStruct->mom);
350     m_originalStruct->mom = NULL;
351
352     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
353     EXPECT_FALSE(NULL == m_clonedStruct);
354 }
355
356 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNonNullMom)
357 {
358     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
359     EXPECT_FALSE(NULL == m_clonedStruct->mom);
360 }
361
362 TEST_F(CloneOicSecDoxmTest, copyShouldHaveDifferentMomAddress)
363 {
364     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
365     EXPECT_NE(m_originalStruct->mom, m_clonedStruct->mom);
366 }
367
368 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNullMomForNullOriginal)
369 {
370     OICFree(m_originalStruct->mom);
371     m_originalStruct->mom = NULL;
372
373     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
374     EXPECT_TRUE(NULL == m_clonedStruct->mom);
375 }
376
377 TEST_F(CloneOicSecDoxmTest, shouldReturnValidPointerForInputWithNullSubOwners)
378 {
379     FreeOicSecSubOwner(m_originalStruct->subOwners);
380     m_originalStruct->subOwners = NULL;
381
382     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
383     EXPECT_FALSE(NULL == m_clonedStruct);
384 }
385
386 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNonNullSubOwners)
387 {
388     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
389     EXPECT_FALSE(NULL == m_clonedStruct->subOwners);
390 }
391
392 TEST_F(CloneOicSecDoxmTest, copyShouldHaveDifferentSubOwnersAddress)
393 {
394     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
395     EXPECT_NE(m_originalStruct->subOwners, m_clonedStruct->subOwners);
396 }
397
398 TEST_F(CloneOicSecDoxmTest, copyShouldHaveNullSubOwnersForNullOriginal)
399 {
400     FreeOicSecSubOwner(m_originalStruct->subOwners);
401     m_originalStruct->subOwners = NULL;
402
403     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
404     EXPECT_TRUE(NULL == m_clonedStruct->subOwners);
405 }
406 #endif
407
408 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameDeviceIdAsOriginal)
409 {
410     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
411     EXPECT_TRUE(UuidsIdentical(&m_originalStruct->deviceID,
412                                &m_clonedStruct->deviceID));
413 }
414
415 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameOwnerIdAsOriginal)
416 {
417     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
418     EXPECT_TRUE(UuidsIdentical(&m_originalStruct->owner,
419                                &m_clonedStruct->owner));
420 }
421
422 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameRownerIdAsOriginal)
423 {
424     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
425     EXPECT_TRUE(UuidsIdentical(&m_originalStruct->rownerID,
426                                &m_clonedStruct->rownerID));
427 }
428
429 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameSctAsOriginal)
430 {
431     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
432     EXPECT_EQ(m_originalStruct->sct, m_clonedStruct->sct);
433 }
434
435 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameOwnedAsOriginal)
436 {
437     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
438     EXPECT_EQ(m_originalStruct->owned, m_clonedStruct->owned);
439 }
440
441 TEST_F(CloneOicSecDoxmTest, copyShouldHaveTheSameDpcAsOriginal)
442 {
443     m_clonedStruct = CloneOicSecDoxm(m_originalStruct);
444     EXPECT_EQ(m_originalStruct->dpc, m_clonedStruct->dpc);
445 }
446
447 class CloneOCProvisionDevListTest : public ::testing::Test
448 {
449 public:
450     CloneOCProvisionDevListTest() :
451         m_originalStruct(NULL),
452         m_clonedStruct(NULL)
453     {}
454
455     void SetUp()
456     {
457         m_originalStruct = BuildSampleOCProvisionDev();
458         ASSERT_FALSE(NULL == m_originalStruct);
459
460         m_originalStruct->next = BuildSampleOCProvisionDev();
461         ASSERT_FALSE(NULL == m_originalStruct->next);
462     }
463
464     void TearDown()
465     {
466         PMDeleteDeviceList(m_originalStruct);
467         PMDeleteDeviceList(m_clonedStruct);
468     }
469 protected:
470
471     OCProvisionDev_t* m_originalStruct;
472     OCProvisionDev_t* m_clonedStruct;
473
474 private:
475
476     OCProvisionDev_t* BuildSampleOCProvisionDev()
477     {
478         return (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
479     }
480 };
481
482 TEST(CloneOCProvisionDevListSimpleTest, shouldReturnNullForNullInput)
483 {
484     EXPECT_TRUE(NULL == PMCloneOCProvisionDevList(NULL));
485 }
486
487 TEST_F(CloneOCProvisionDevListTest, shouldReturnValidPointerForNonNullInput)
488 {
489     m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
490
491     EXPECT_FALSE(NULL == m_clonedStruct);
492 }
493
494 TEST_F(CloneOCProvisionDevListTest, shouldReturnValidPointerForNonNext)
495 {
496     m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
497
498     EXPECT_FALSE(NULL == m_clonedStruct->next);
499 }
500
501 TEST_F(CloneOCProvisionDevListTest, copyShouldHaveDifferentAddress)
502 {
503     m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
504
505     EXPECT_NE(m_originalStruct, m_clonedStruct);
506 }
507
508 TEST_F(CloneOCProvisionDevListTest, copyShouldHaveDifferentAddressForNext)
509 {
510     m_clonedStruct = PMCloneOCProvisionDevList(m_originalStruct);
511
512     EXPECT_NE(m_originalStruct->next, m_clonedStruct->next);
513 }
514
515
516 // List add Tests
517 TEST(ProvisionListTest, Addition)
518 {
519     OCProvisionDev_t* el = NULL;
520     OCStackResult res = OC_STACK_ERROR;
521     OicSecDoxm_t* pDoxm = NULL;
522     int cnt =0;
523
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);
528
529     LL_FOREACH(gList,el){ ++cnt; };
530     EXPECT_TRUE(1 == cnt);
531
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);
536
537     cnt = 0;
538     LL_FOREACH(gList,el){ ++cnt; };
539     EXPECT_TRUE(1 == cnt);
540
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);
545
546     cnt = 0;
547     LL_FOREACH(gList,el){ ++cnt; };
548     EXPECT_TRUE(2 == cnt);
549 }
550
551 // List Delete Tests
552 TEST(ProvisionListTest, Deletion)
553 {
554     OCProvisionDev_t* el = NULL;
555     int cnt =0;
556
557     // Delete whole
558     PMDeleteDeviceList(gList);
559     gList = NULL;
560
561     LL_FOREACH(gList,el){ ++cnt; };
562     EXPECT_TRUE(0 == cnt);
563 }