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 "iotivity_config.h"
21 #include "gtest/gtest.h"
22 #include "provisioningdatabasemanager.h"
28 #define access _access_s
31 #define DB_FILE "PDM.db"
32 const char ID_1 [] = "1111111111111111";
33 const char ID_2 [] = "2111111111111111";
34 const char ID_3 [] = "3111111111111111";
35 const char ID_4 [] = "4111111111111111";
36 const char ID_5 [] = "5111111111111111";
37 const char ID_6 [] = "6111111111111111";
38 const char ID_7 [] = "7111111111111111";
39 const char ID_8 [] = "8111111111111111";
40 const char ID_9 [] = "9111111111111111";
41 const char ID_10[] = "1222222222222222";
42 const char ID_11[] = "2222222222222222";
43 const char ID_12[] = "3222222222222222";
44 const char ID_13[] = "4222222222222222";
47 TEST(CallPDMAPIbeforeInit, BeforeInit)
49 if (0 == access(DB_FILE, F_OK))
51 EXPECT_EQ(0, remove(DB_FILE));
53 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMAddDevice(NULL));
54 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsDuplicateDevice(NULL,NULL));
55 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMLinkDevices(NULL, NULL));
56 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMUnlinkDevices(NULL, NULL));
57 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMDeleteDevice(NULL));
58 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetOwnedDevices(NULL, NULL));
59 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetLinkedDevices(NULL, NULL, NULL));
60 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMSetLinkStale(NULL, NULL));
61 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetToBeUnlinkedDevices(NULL, NULL));
62 EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsLinkExists(NULL, NULL, NULL));
65 TEST(PDMInitTest, PDMInitWithNULL)
67 EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
70 TEST(PDMAddDeviceTest, NullUUID)
72 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMAddDevice(NULL));
75 TEST(PDMIsDuplicateDeviceTest, NullUUID)
77 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMIsDuplicateDevice(NULL,NULL));
81 TEST(PDMIsDuplicateDeviceTest, ValidUUID)
83 OicUuid_t uid1 = {{0,}};
84 memcpy(&uid1.id, ID_1, sizeof(uid1.id));
85 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
86 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
87 OicUuid_t uid2 = {{0,}};
88 memcpy(&uid2.id, ID_2, sizeof(uid2.id));
89 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
90 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
91 OicUuid_t uid3 = {{0,}};
92 memcpy(&uid3.id, ID_3, sizeof(uid3.id));
93 bool isDuplicate = true;
94 EXPECT_EQ(OC_STACK_OK, PDMIsDuplicateDevice(&uid1,&isDuplicate));
95 EXPECT_TRUE(isDuplicate);
97 EXPECT_EQ(OC_STACK_OK, PDMIsDuplicateDevice(&uid3, &isDuplicate));
98 EXPECT_FALSE(isDuplicate);
101 TEST(PDMAddDeviceTest, ValidUUID)
103 OicUuid_t uid = {{0,}};
105 uint8_t id[UUID_LENGTH] = {0,};
106 for (size_t i = 0 ; i < sizeof(id) ; i++)
108 int tem = rand() % 25;
110 id[i] = (uint8_t)(tem + 65);
113 memcpy(&uid.id, &id, UUID_LENGTH);
115 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid));
116 EXPECT_EQ(OC_STACK_OK, PDMDeleteDevice(&uid));
119 TEST(PDMLinkDevicesTest, NULLDevice1)
121 OicUuid_t uid = {{0,}};
122 memcpy(&uid.id, ID_2, sizeof(uid.id));
123 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMLinkDevices(NULL, &uid));
126 TEST(PDMLinkDevicesTest, NULLDevice2)
128 OicUuid_t uid = {{0,}};
129 memcpy(&uid.id, ID_3, sizeof(uid.id));
130 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMLinkDevices(&uid, NULL));
133 TEST(PDMLinkDevicesTest, ValidCase)
135 OicUuid_t uid1 = {{0,}};
136 memcpy(&uid1.id, ID_4, sizeof(uid1.id));
137 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
138 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
140 OicUuid_t uid2 = {{0,}};
141 memcpy(&uid2.id, ID_5, sizeof(uid2.id));
142 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
143 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
145 EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
148 TEST(PDMUnlinkDevicesTest, NULLDevice1)
150 OicUuid_t uid = {{0,}};
151 memcpy(&uid.id, ID_3, sizeof(uid.id));
152 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMUnlinkDevices(NULL, &uid));
155 TEST(PDMUnlinkDevicesTest, NULLDevice2)
157 OicUuid_t uid = {{0,}};
158 memcpy(&uid.id, ID_3, sizeof(uid.id));
159 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMUnlinkDevices(&uid, NULL));
162 TEST(PDMUnlinkDevices, ValidCase)
164 OicUuid_t uid1 = {{0,}};
165 memcpy(&uid1.id, ID_4, sizeof(uid1.id));
166 OicUuid_t uid2 = {{0,}};
167 memcpy(&uid2.id, ID_5, sizeof(uid2.id));
168 EXPECT_EQ(OC_STACK_OK, PDMUnlinkDevices(&uid1, &uid2));
172 TEST (PDMDeleteDevice, NULLDeviceID)
174 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMDeleteDevice(NULL));
177 TEST (PDMDeleteDevice, ValidButNonExistDeviceID)
180 OicUuid_t uid = {{0,}};
181 uint8_t id[UUID_LENGTH] = {0,};
182 for (size_t i = 0 ; i < sizeof(id) ; i++)
184 int tem = rand() % 25;
186 id[i] = (uint8_t)(tem + 65);
189 memcpy(&uid.id, &id, sizeof(uid.id));
190 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMDeleteDevice(&uid));
193 TEST(PDMGetOwnedDevices, ValidCase)
195 OCUuidList_t *list = NULL;
196 size_t noOfDevcies = 0;
197 EXPECT_EQ(OC_STACK_OK, PDMGetOwnedDevices(&list, &noOfDevcies));
200 TEST(PDMGetLinkedDevices, NULLDeviceID)
202 OCUuidList_t *list = NULL;
203 size_t noOfDevices = 0;
204 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(NULL, &list, &noOfDevices));
207 TEST(PDMGetLinkedDevices, ValidCase)
209 OicUuid_t uid = {{0,}};
210 memcpy(&uid.id, ID_1, sizeof(uid.id));
211 OCUuidList_t *list = NULL;
212 size_t noOfDevices = 0;
213 EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid, &list, &noOfDevices));
216 TEST(PDMGetLinkedDevices, InvalidCase)
218 OicUuid_t uid = {{0,}};
219 memcpy(&uid.id, ID_6, sizeof(uid.id));
220 OCUuidList_t *list = NULL;
221 size_t noOfDevices = 0;
222 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(&uid, &list, &noOfDevices));
225 TEST(PDMSetLinkStale, NULLDeviceID1)
227 OicUuid_t uid = {{0,}};
228 memcpy(&uid.id, ID_1, sizeof(uid.id));
229 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(NULL, &uid));
232 TEST(PDMSetLinkStale, NULLDeviceID2)
234 OicUuid_t uid = {{0,}};
235 memcpy(&uid.id, ID_1, sizeof(uid.id));
236 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(&uid, NULL));
239 TEST(PDMSetLinkStale, ValidCase)
241 OicUuid_t uid1 = {{0,}};
242 memcpy(&uid1.id, ID_6, sizeof(uid1.id));
243 OicUuid_t uid2 = {{0,}};
244 memcpy(&uid2.id, ID_1, sizeof(uid2.id));
246 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(&uid1, &uid2));
248 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
250 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
252 EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
254 EXPECT_EQ(OC_STACK_OK, PDMSetLinkStale(&uid1, &uid2));
257 TEST(PDMGetToBeUnlinkedDevices, ValidCase)
259 OCPairList_t *list = NULL;
260 size_t noOfDevices = 0;
261 EXPECT_EQ(OC_STACK_OK, PDMGetToBeUnlinkedDevices(&list, &noOfDevices));
264 TEST(PDMClose, ValidCase)
266 EXPECT_EQ(OC_STACK_OK, PDMClose());
269 TEST(PDMDestoryOicUuidLinkList, NULLParam)
271 PDMDestoryOicUuidLinkList(NULL);
274 TEST(PDMDestoryStaleLinkList, NULLParam)
276 PDMDestoryStaleLinkList(NULL);
279 TEST(PDMIsLinkExistsTest, DuplicateID)
281 EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
282 OicUuid_t uid1 = {{0,}};
283 memcpy(&uid1.id, ID_7, sizeof(uid1.id));
284 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
285 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
287 OicUuid_t uid2 = {{0,}};
288 memcpy(&uid2.id, ID_8, sizeof(uid2.id));
289 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
290 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
292 bool linkExisits = true;
293 OCStackResult res = PDMIsLinkExists(&uid1, &uid2, &linkExisits);
294 EXPECT_EQ(OC_STACK_OK, res);
295 EXPECT_FALSE(linkExisits);
298 TEST(PDMSetDeviceStaleTest, NULLUUID)
300 EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
301 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetDeviceState(NULL, PDM_DEVICE_STALE));
304 TEST(PDMSetDeviceStaleTest, VALIDUUID)
306 EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
307 OicUuid_t uid1 = {{0,}};
308 memcpy(&uid1.id, ID_9, sizeof(uid1.id));
309 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
310 EXPECT_EQ(OC_STACK_OK,PDMSetDeviceState(&uid1, PDM_DEVICE_STALE));
313 TEST(PDMSetDeviceStaleTest, StaleDeviceNotinDeviceList)
315 EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
316 OicUuid_t uid1 = {{0,}};
317 memcpy(&uid1.id, ID_10, sizeof(uid1.id));
318 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
319 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_STALE));
321 OCUuidList_t *list = NULL;
322 size_t noOfDevcies = 0;
323 EXPECT_EQ(OC_STACK_OK, PDMGetOwnedDevices(&list, &noOfDevcies));
327 EXPECT_FALSE(0 == memcmp(list->dev.id, uid1.id,sizeof(uid1.id)));
332 TEST(PDMSetDeviceStaleTest, StaleDeviceNotinLinkedDevice)
334 EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
335 OicUuid_t uid1 = {{0,}};
336 memcpy(&uid1.id, ID_11, sizeof(uid1.id));
338 OicUuid_t uid2 = {{0,}};
339 memcpy(&uid2.id, ID_12, sizeof(uid2.id));
341 OicUuid_t uid3 = {{0,}};
342 memcpy(&uid3.id, ID_13, sizeof(uid3.id));
344 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
345 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid1, PDM_DEVICE_ACTIVE));
346 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
347 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid2, PDM_DEVICE_ACTIVE));
348 EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid3));
349 EXPECT_EQ(OC_STACK_OK, PDMSetDeviceState(&uid3, PDM_DEVICE_ACTIVE));
351 EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
352 EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid2, &uid3));
353 EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid3));
355 EXPECT_EQ(OC_STACK_OK,PDMSetDeviceState(&uid1, PDM_DEVICE_STALE));
357 OCUuidList_t *list1 = NULL;
358 size_t noOfDevices1 = 0;
359 EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(&uid1, &list1, &noOfDevices1));
361 OCUuidList_t *list2 = NULL;
362 size_t noOfDevices2 = 0;
363 EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid2, &list2, &noOfDevices2));
364 OCUuidList_t *ptr = list2;
367 EXPECT_FALSE(0 == memcmp(ptr->dev.id, uid1.id,sizeof(uid1.id)));
373 EXPECT_TRUE(0 == memcmp(ptr->dev.id, uid3.id,sizeof(uid3.id)));
377 OCUuidList_t *list3 = NULL;
378 size_t noOfDevices3 = 0;
379 EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid3, &list3, &noOfDevices3));
383 EXPECT_FALSE(0 == memcmp(ptr->dev.id, uid1.id,sizeof(uid1.id)));
390 EXPECT_TRUE(0 == memcmp(ptr->dev.id, uid2.id,sizeof(uid2.id)));