5bbf8113e7cbc11cf78ca6109fbf37ed6232a4a8
[iotivity.git] / resource / csdk / security / provisioning / unittest / provisioningdatabasemanager.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 "iotivity_config.h"
21 #include "gtest/gtest.h"
22 #include "provisioningdatabasemanager.h"
23
24 #define DB_FILE "PDM.db"
25 const char ID_1 [] = "1111111111111111";
26 const char ID_2 [] = "2111111111111111";
27 const char ID_3 [] = "3111111111111111";
28 const char ID_4 [] = "4111111111111111";
29 const char ID_5 [] = "5111111111111111";
30 const char ID_6 [] = "6111111111111111";
31 const char ID_7 [] = "7111111111111111";
32 const char ID_8 [] = "8111111111111111";
33 const char ID_9 [] = "9111111111111111";
34 const char ID_10[] = "1222222222222222";
35 const char ID_11[] = "2222222222222222";
36 const char ID_12[] = "3222222222222222";
37 const char ID_13[] = "4222222222222222";
38
39
40 TEST(CallPDMAPIbeforeInit, BeforeInit)
41 {
42     if (0 == access(DB_FILE, F_OK))
43     {
44         EXPECT_EQ(0, unlink(DB_FILE));
45     }
46     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMAddDevice(NULL));
47     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsDuplicateDevice(NULL,NULL));
48     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMLinkDevices(NULL, NULL));
49     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMUnlinkDevices(NULL, NULL));
50     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMDeleteDevice(NULL));
51     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetOwnedDevices(NULL, NULL));
52     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetLinkedDevices(NULL, NULL, NULL));
53     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMSetLinkStale(NULL, NULL));
54     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetToBeUnlinkedDevices(NULL, NULL));
55     EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsLinkExists(NULL, NULL, NULL));
56 }
57
58 TEST(PDMInitTest, PDMInitWithNULL)
59 {
60     EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
61 }
62
63 TEST(PDMAddDeviceTest, NullUUID)
64 {
65     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMAddDevice(NULL));
66 }
67
68 TEST(PDMIsDuplicateDeviceTest, NullUUID)
69 {
70     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMIsDuplicateDevice(NULL,NULL));
71 }
72
73
74 TEST(PDMIsDuplicateDeviceTest, ValidUUID)
75 {
76     OicUuid_t uid1 = {{0,}};
77     memcpy(&uid1.id, ID_1, sizeof(uid1.id));
78     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
79     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
80     OicUuid_t uid2 = {{0,}};
81     memcpy(&uid2.id, ID_2, sizeof(uid2.id));
82     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
83     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
84     OicUuid_t uid3 = {{0,}};
85     memcpy(&uid3.id, ID_3, sizeof(uid3.id));
86     bool isDuplicate = true;
87     EXPECT_EQ(OC_STACK_OK, PDMIsDuplicateDevice(&uid1,&isDuplicate));
88     EXPECT_TRUE(isDuplicate);
89
90     EXPECT_EQ(OC_STACK_OK, PDMIsDuplicateDevice(&uid3, &isDuplicate));
91     EXPECT_FALSE(isDuplicate);
92 }
93
94 TEST(PDMAddDeviceTest, ValidUUID)
95 {
96     OicUuid_t uid = {{0,}};
97
98     uint8_t id[UUID_LENGTH] = {0,};
99     for (size_t i = 0 ; i < sizeof(id) ; i++)
100     {
101         int tem = rand() % 25;
102
103         id[i] = tem + 65;
104     }
105
106     memcpy(&uid.id, &id, UUID_LENGTH);
107
108     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid));
109     EXPECT_EQ(OC_STACK_OK, PDMDeleteDevice(&uid));
110 }
111
112 TEST(PDMLinkDevicesTest, NULLDevice1)
113 {
114     OicUuid_t uid = {{0,}};
115     memcpy(&uid.id, ID_2, sizeof(uid.id));
116     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMLinkDevices(NULL, &uid));
117 }
118
119 TEST(PDMLinkDevicesTest, NULLDevice2)
120 {
121     OicUuid_t uid = {{0,}};
122     memcpy(&uid.id, ID_3, sizeof(uid.id));
123     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMLinkDevices(&uid, NULL));
124 }
125
126 TEST(PDMLinkDevicesTest, ValidCase)
127 {
128     OicUuid_t uid1 = {{0,}};
129     memcpy(&uid1.id, ID_4, sizeof(uid1.id));
130     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
131     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
132
133     OicUuid_t uid2 = {{0,}};
134     memcpy(&uid2.id, ID_5, sizeof(uid2.id));
135     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
136     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
137
138     EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
139 }
140
141 TEST(PDMUnlinkDevicesTest, NULLDevice1)
142 {
143     OicUuid_t uid = {{0,}};
144     memcpy(&uid.id, ID_3, sizeof(uid.id));
145     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMUnlinkDevices(NULL, &uid));
146 }
147
148 TEST(PDMUnlinkDevicesTest, NULLDevice2)
149 {
150     OicUuid_t uid = {{0,}};
151     memcpy(&uid.id, ID_3, sizeof(uid.id));
152     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMUnlinkDevices(&uid, NULL));
153 }
154
155 TEST(PDMUnlinkDevices, ValidCase)
156 {
157     OicUuid_t uid1 = {{0,}};
158     memcpy(&uid1.id, ID_4, sizeof(uid1.id));
159     OicUuid_t uid2 = {{0,}};
160     memcpy(&uid2.id, ID_5, sizeof(uid2.id));
161     EXPECT_EQ(OC_STACK_OK, PDMUnlinkDevices(&uid1, &uid2));
162 }
163
164
165 TEST (PDMDeleteDevice, NULLDeviceID)
166 {
167     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMDeleteDevice(NULL));
168 }
169
170 TEST (PDMDeleteDevice, ValidButNonExistDeviceID)
171 {
172
173     OicUuid_t uid = {{0,}};
174     uint8_t id[UUID_LENGTH] = {0,};
175     for (size_t i = 0 ; i < sizeof(id) ; i++)
176     {
177         int tem = rand() % 25;
178
179         id[i] = tem + 65;
180     }
181
182     memcpy(&uid.id, &id, sizeof(uid.id));
183     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMDeleteDevice(&uid));
184 }
185
186 TEST(PDMGetOwnedDevices, ValidCase)
187 {
188     OCUuidList_t *list = NULL;
189     size_t noOfDevcies = 0;
190     EXPECT_EQ(OC_STACK_OK, PDMGetOwnedDevices(&list, &noOfDevcies));
191 }
192
193 TEST(PDMGetLinkedDevices, NULLDeviceID)
194 {
195     OCUuidList_t *list = NULL;
196     size_t noOfDevices = 0;
197     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(NULL, &list, &noOfDevices));
198 }
199
200 TEST(PDMGetLinkedDevices, ValidCase)
201 {
202     OicUuid_t uid = {{0,}};
203     memcpy(&uid.id, ID_1, sizeof(uid.id));
204     OCUuidList_t *list = NULL;
205     size_t noOfDevices = 0;
206     EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid, &list, &noOfDevices));
207 }
208
209 TEST(PDMGetLinkedDevices, InvalidCase)
210 {
211     OicUuid_t uid = {{0,}};
212     memcpy(&uid.id, ID_6, sizeof(uid.id));
213     OCUuidList_t *list = NULL;
214     size_t noOfDevices = 0;
215     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(&uid, &list, &noOfDevices));
216 }
217
218 TEST(PDMSetLinkStale, NULLDeviceID1)
219 {
220     OicUuid_t uid = {{0,}};
221     memcpy(&uid.id, ID_1, sizeof(uid.id));
222     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(NULL, &uid));
223 }
224
225 TEST(PDMSetLinkStale, NULLDeviceID2)
226 {
227     OicUuid_t uid = {{0,}};
228     memcpy(&uid.id, ID_1, sizeof(uid.id));
229     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(&uid, NULL));
230 }
231
232 TEST(PDMSetLinkStale, ValidCase)
233 {
234     OicUuid_t uid1 = {{0,}};
235     memcpy(&uid1.id, ID_6, sizeof(uid1.id));
236     OicUuid_t uid2 = {{0,}};
237     memcpy(&uid2.id, ID_1, sizeof(uid2.id));
238
239     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(&uid1, &uid2));
240
241     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
242
243     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
244
245     EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
246
247     EXPECT_EQ(OC_STACK_OK, PDMSetLinkStale(&uid1, &uid2));
248 }
249
250 TEST(PDMGetToBeUnlinkedDevices, ValidCase)
251 {
252     OCPairList_t *list = NULL;
253     size_t noOfDevices = 0;
254     EXPECT_EQ(OC_STACK_OK, PDMGetToBeUnlinkedDevices(&list, &noOfDevices));
255 }
256
257 TEST(PDMClose, ValidCase)
258 {
259    EXPECT_EQ(OC_STACK_OK, PDMClose());
260 }
261
262 TEST(PDMDestoryOicUuidLinkList, NULLParam)
263 {
264     PDMDestoryOicUuidLinkList(NULL);
265 }
266
267 TEST(PDMDestoryStaleLinkList, NULLParam)
268 {
269     PDMDestoryStaleLinkList(NULL);
270 }
271
272 TEST(PDMIsLinkExistsTest, DuplicateID)
273 {
274     EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
275     OicUuid_t uid1 = {{0,}};
276     memcpy(&uid1.id, ID_7, sizeof(uid1.id));
277     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
278     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
279
280     OicUuid_t uid2 = {{0,}};
281     memcpy(&uid2.id, ID_8, sizeof(uid2.id));
282     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
283     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
284
285     bool linkExisits = true;
286     OCStackResult res = PDMIsLinkExists(&uid1, &uid2, &linkExisits);
287     EXPECT_EQ(OC_STACK_OK, res);
288     EXPECT_FALSE(linkExisits);
289 }
290
291 TEST(PDMSetDeviceStaleTest, NULLUUID)
292 {
293     EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
294     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetDeviceState(NULL, PDM_DEVICE_STALE));
295 }
296
297 TEST(PDMSetDeviceStaleTest, VALIDUUID)
298 {
299     EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
300     OicUuid_t uid1 = {{0,}};
301     memcpy(&uid1.id, ID_9, sizeof(uid1.id));
302     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
303     EXPECT_EQ(OC_STACK_OK,PDMSetDeviceState(&uid1, PDM_DEVICE_STALE));
304 }
305
306 TEST(PDMSetDeviceStaleTest, StaleDeviceNotinDeviceList)
307 {
308     EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
309     OicUuid_t uid1 = {{0,}};
310     memcpy(&uid1.id, ID_10, sizeof(uid1.id));
311     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
312     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_STALE));
313
314     OCUuidList_t *list = NULL;
315     size_t noOfDevcies = 0;
316     EXPECT_EQ(OC_STACK_OK, PDMGetOwnedDevices(&list, &noOfDevcies));
317
318     while (list)
319     {
320         EXPECT_FALSE(0 == memcmp(list->dev.id, uid1.id,sizeof(uid1.id)));
321         list = list->next;
322     }
323 }
324
325 TEST(PDMSetDeviceStaleTest, StaleDeviceNotinLinkedDevice)
326 {
327     EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
328     OicUuid_t uid1 = {{0,}};
329     memcpy(&uid1.id, ID_11, sizeof(uid1.id));
330
331     OicUuid_t uid2 = {{0,}};
332     memcpy(&uid2.id, ID_12, sizeof(uid2.id));
333
334     OicUuid_t uid3 = {{0,}};
335     memcpy(&uid3.id, ID_13, sizeof(uid3.id));
336
337     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
338     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
339     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
340     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
341     EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid3));
342     EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid3, PDM_DEVICE_ACTIVE));
343
344     EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
345     EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid2, &uid3));
346     EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid3));
347
348     EXPECT_EQ(OC_STACK_OK,PDMSetDeviceState(&uid1, PDM_DEVICE_STALE));
349
350     OCUuidList_t *list1 = NULL;
351     size_t noOfDevices1 = 0;
352     EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(&uid1, &list1, &noOfDevices1));
353
354     OCUuidList_t *list2 = NULL;
355     size_t noOfDevices2 = 0;
356     EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid2, &list2, &noOfDevices2));
357     OCUuidList_t *ptr = list2;
358     while(ptr)
359     {
360         EXPECT_FALSE(0 == memcmp(ptr->dev.id, uid1.id,sizeof(uid1.id)));
361         ptr = ptr->next;
362     }
363     ptr = list2;
364     while(ptr)
365     {
366         EXPECT_TRUE(0 == memcmp(ptr->dev.id, uid3.id,sizeof(uid3.id)));
367         ptr = ptr->next;
368     }
369
370     OCUuidList_t *list3 = NULL;
371     size_t noOfDevices3 = 0;
372     EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid3, &list3, &noOfDevices3));
373     ptr = list3;
374     while(ptr)
375     {
376         EXPECT_FALSE(0 == memcmp(ptr->dev.id, uid1.id,sizeof(uid1.id)));
377         ptr = ptr->next;
378     }
379
380     ptr = list3;
381     while(ptr)
382     {
383         EXPECT_TRUE(0 == memcmp(ptr->dev.id, uid2.id,sizeof(uid2.id)));
384         ptr = ptr->next;
385     }
386 }