a55ff93d280e61b5c4e765d161f7c96dd2473925
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / unittest / otmunittest.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 <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include "gtest/gtest.h"
24 #include "ocstack.h"
25 #include "utlist.h"
26 #include "logger.h"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
29 #include "ocprovisioningmanager.h"
30 #include "oxmjustworks.h"
31 #include "oxmrandompin.h"
32 #include "securevirtualresourcetypes.h"
33 #include "provisioningdatabasemanager.h"
34 #include "srmutility.h"
35 #include "doxmresource.h"
36 #include "pmtypes.h"
37 #include "pmutility.h"
38
39 using namespace std;
40
41 TEST(JustWorksOxMTest, NullParam)
42 {
43     OTMContext_t* otmCtx = NULL;
44     OCStackResult res = OC_STACK_ERROR;
45     uint8_t *payloadRes = NULL;
46     size_t size = 0;
47
48     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
49     res = LoadSecretJustWorksCallback(otmCtx);
50     EXPECT_TRUE(OC_STACK_OK == res);
51
52     res = CreateSecureSessionJustWorksCallback(otmCtx);
53     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
54
55     res = CreateJustWorksSelectOxmPayload(otmCtx, &payloadRes, &size);
56     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
57
58     res = CreateJustWorksOwnerTransferPayload(otmCtx, &payloadRes, &size);
59     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
60
61     OTMContext_t otmCtx2;
62     otmCtx2.selectedDeviceInfo = NULL;
63
64     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
65     res = LoadSecretJustWorksCallback(&otmCtx2);
66     EXPECT_TRUE(OC_STACK_OK == res);
67
68     res = CreateSecureSessionJustWorksCallback(&otmCtx2);
69     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
70
71     res = CreateJustWorksSelectOxmPayload(&otmCtx2, &payloadRes, &size);
72     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
73
74     res = CreateJustWorksOwnerTransferPayload(&otmCtx2, &payloadRes, &size);
75     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
76 }
77
78 TEST(RandomPinOxMTest, NullParam)
79 {
80     OTMContext_t* otmCtx = NULL;
81     OCStackResult res = OC_STACK_ERROR;
82     uint8_t *payloadRes = NULL;
83     size_t size = 0;
84
85     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
86     res = InputPinCodeCallback(otmCtx);
87     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
88
89     res = CreateSecureSessionRandomPinCallback(otmCtx);
90     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
91
92     res = CreatePinBasedSelectOxmPayload(otmCtx, &payloadRes, &size);
93     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
94
95     res = CreatePinBasedOwnerTransferPayload(otmCtx, &payloadRes, &size);
96     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
97
98     OTMContext_t otmCtx2;
99     otmCtx2.selectedDeviceInfo = NULL;
100
101     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
102     res = InputPinCodeCallback(&otmCtx2);
103     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
104
105     res = CreateSecureSessionRandomPinCallback(&otmCtx2);
106     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
107
108     res = CreatePinBasedSelectOxmPayload(&otmCtx2, &payloadRes, &size);
109     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
110
111     res = CreatePinBasedOwnerTransferPayload(&otmCtx2, &payloadRes, &size);
112     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
113 }
114
115
116 /****************************************
117  * Test the OTM modules with sample server
118  ****************************************/
119 #define TAG "JUSTWORKS_UNITTEST"
120 #define OTM_TIMEOUT 5
121 #define DISCOVERY_TIMEOUT 3
122
123 #define SVR_DB_PATH "oic_svr_db_client.dat"
124 #define UT_PATH "resource/csdk/security/provisioning/unittest"
125 char pdb_path[1024];
126 char svr_path[1024];
127
128 static uint8_t DEFAULT_SVR_DB[] = {
129     0xBF, 0x63, 0x61, 0x63, 0x6C, 0x59, 0x01, 0x80, 0xA2, 0x66, 0x61, 0x63, 0x6C, 0x69, 0x73, 0x74,
130     0xA1, 0x64, 0x61, 0x63, 0x65, 0x73, 0x82, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74,
131     0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73,
132     0x86, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x68, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x72, 0x65, 0x73,
133     0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68,
134     0x72, 0x65, 0x66, 0x66, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62,
135     0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x66, 0x2F, 0x6F,
136     0x69, 0x63, 0x2F, 0x70, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66,
137     0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x70, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x72, 0x65, 0x73,
138     0x2F, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2F, 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74,
139     0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x67, 0x2F, 0x6F, 0x69, 0x63,
140     0x2F, 0x61, 0x64, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60,
141     0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6E, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F,
142     0x61, 0x6D, 0x61, 0x63, 0x6C, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69,
143     0x66, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0xA3, 0x6B,
144     0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65,
145     0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73, 0x82, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6D, 0x2F,
146     0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x64, 0x6F, 0x78, 0x6D, 0x63, 0x72, 0x65, 0x6C,
147     0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6E,
148     0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x70, 0x73, 0x74, 0x61, 0x74, 0x63, 0x72,
149     0x65, 0x6C, 0x60, 0x62, 0x72, 0x74, 0x60, 0x62, 0x69, 0x66, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D,
150     0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0x6A, 0x72, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75,
151     0x69, 0x64, 0x78, 0x24, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39,
152     0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39,
153     0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x65, 0x70, 0x73, 0x74, 0x61, 0x74, 0x58, 0x79,
154     0xA7, 0x64, 0x69, 0x73, 0x6F, 0x70, 0xF5, 0x6A, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x75, 0x75,
155     0x69, 0x64, 0x78, 0x24, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39,
156     0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39,
157     0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x62, 0x63, 0x6D, 0x00, 0x62, 0x74, 0x6D, 0x00,
158     0x62, 0x6F, 0x6D, 0x03, 0x62, 0x73, 0x6D, 0x03, 0x6A, 0x72, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75,
159     0x75, 0x69, 0x64, 0x78, 0x24, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39,
160     0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39,
161     0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x64, 0x64, 0x6F, 0x78, 0x6D, 0x58, 0xB6,
162     0xA8, 0x64, 0x6F, 0x78, 0x6D, 0x73, 0x81, 0x00, 0x66, 0x6F, 0x78, 0x6D, 0x73, 0x65, 0x6C, 0x00,
163     0x63, 0x73, 0x63, 0x74, 0x01, 0x65, 0x6F, 0x77, 0x6E, 0x65, 0x64, 0xF5, 0x6A, 0x64, 0x65, 0x76,
164     0x69, 0x63, 0x65, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
165     0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39,
166     0x2D, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x6C, 0x64, 0x65,
167     0x76, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x39, 0x39, 0x39, 0x39,
168     0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39,
169     0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
170     0x6A, 0x72, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x39, 0x39, 0x39,
171     0x39, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x2D,
172     0x39, 0x39, 0x39, 0x39, 0x2D, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
173     0x39, 0x63, 0x64, 0x70, 0x63, 0xF4, 0xFF
174 };
175
176 static bool g_doneCB;
177 static bool g_callbackResult;
178 static pid_t g_myPID1;
179 static pid_t g_myPID2;
180
181 static const char* g_otmCtx = "Test User Context";
182 static OCProvisionDev_t* g_unownedDevices = NULL;
183 static OCProvisionDev_t* g_ownedDevices = NULL;
184
185 static void GetCurrentWorkingDirectory(char* buf, size_t bufsize)
186 {
187     char cwd[1024] = {0};
188     const char* unittest_path = "resource/csdk/security/provisioning/unittest";
189     if(getcwd(cwd, sizeof(cwd)) != NULL)
190     {
191         if(strstr(cwd, unittest_path) == NULL)
192         {
193 #if defined __linux__
194 #if __x86_64__
195         snprintf(buf, bufsize, "%s/out/linux/x86_64/release/%s/", cwd, unittest_path);
196         snprintf(buf, bufsize, "%s/out/linux/x86_64/release/%s/", cwd, unittest_path);
197 #else
198         snprintf(buf, bufsize, "%s/out/linux/x86/release/%s/", cwd, unittest_path);
199         snprintf(buf, bufsize, "%s/out/linux/x86/release/%s/", cwd, unittest_path);
200 #endif //__x86_64__
201 #endif //defined __linux__
202         }
203         else
204         {
205             snprintf(buf, bufsize, "%s/", cwd);
206         }
207     }
208 }
209
210 static FILE* fopen_prvnMng(const char* path, const char* mode)
211 {
212     (void)path;  // unused |path| parameter
213
214     // input |g_svr_db_fname| internally by force, not using |path| parameter
215     // because |OCPersistentStorage::open| is called |OCPersistentStorage| internally
216     // with its own |SVR_DB_FILE_NAME|
217     char cwd[1024] = {0};
218     char svr_db_path[1024] = {0};
219     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
220     snprintf(svr_db_path, sizeof(svr_db_path), "%s%s", cwd, SVR_DB_PATH);
221     return fopen(svr_db_path, mode);
222 }
223
224 // callback function(s) for provisioning client using C-level provisioning API
225 static void ownershipTransferCB(void* ctx, int UNUSED1, OCProvisionResult_t* UNUSED2, bool hasError)
226 {
227     (void)UNUSED1;
228     (void)UNUSED2;
229     (void)ctx;
230
231     if(!hasError)
232     {
233         OIC_LOG_V(INFO, TAG, "Ownership Transfer SUCCEEDED - ctx: %s", (char*) ctx);
234     }
235     else
236     {
237         OIC_LOG_V(ERROR, TAG, "Ownership Transfer FAILED - ctx: %s", (char*) ctx);
238     }
239     g_callbackResult = !hasError;
240     g_doneCB = true;
241 }
242
243
244 // callback function(s) for provisioning client using C-level provisioning API
245 static void removeDeviceCB(void* ctx, int UNUSED1, OCProvisionResult_t* UNUSED2, bool hasError)
246 {
247     (void)UNUSED1;
248     (void)UNUSED2;
249     (void)ctx;
250
251     if(!hasError)
252     {
253         OIC_LOG_V(INFO, TAG, "Remove device request SUCCEEDED - ctx: %s", (char*) ctx);
254     }
255     else
256     {
257         OIC_LOG_V(ERROR, TAG, "Remove device request FAILED - ctx: %s", (char*) ctx);
258     }
259     g_callbackResult = !hasError;
260     g_doneCB = true;
261 }
262
263
264 static int waitCallbackRet(void)
265 {
266     struct timespec timeout;
267     timeout.tv_sec  = 0;
268     timeout.tv_nsec = 100000000L;
269
270     for(long long i=0; !g_doneCB && OTM_TIMEOUT * 100000000L * 1000L > i; ++i)
271     {
272         nanosleep(&timeout, NULL);
273         if(OC_STACK_OK != OCProcess())
274         {
275             OIC_LOG(ERROR, TAG, "OCStack process error");
276             return -1;
277         }
278     }
279
280     return 0;
281 }
282
283 TEST(InitForOTM, NullParam)
284 {
285     OCStackResult result = OC_STACK_ERROR;
286
287     OTMCallbackData_t otmcb;
288     otmcb.loadSecretCB = LoadSecretJustWorksCallback;
289     otmcb.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
290     otmcb.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
291     otmcb.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
292
293     static OCPersistentStorage pstStr;
294     pstStr.open = fopen_prvnMng;
295     pstStr.read = fread;
296     pstStr.write = fwrite;
297     pstStr.close = fclose;
298     pstStr.unlink = unlink;
299
300     //Get current path to execute the sample server.
301     char cwd[1024] = {0};
302     char server1_path[1024] = {0};
303     char server2_path[1024] = {0};
304     char pdb_path[1024] = {0};
305     char del_cmd[1024] = {0};
306     char svrdb_path[1024] = {0};
307     FILE* fp = NULL;
308
309     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
310     EXPECT_TRUE(0 < strlen(cwd));
311
312     //Delete previous PDB, if exist.
313     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
314     snprintf(del_cmd, sizeof(del_cmd), "rm -rf %stest.db", cwd);
315     system(del_cmd);
316
317     //Delete previous SVR DB, if exist.
318     snprintf(del_cmd, sizeof(del_cmd), "rm -rf %s%s", cwd, SVR_DB_PATH);
319     system(del_cmd);
320
321     //Generate default SVR DB.
322     snprintf(svrdb_path, sizeof(svrdb_path), "%s%s", cwd, SVR_DB_PATH);
323     fp = fopen(svrdb_path, "w");
324     if(NULL != fp)
325     {
326         size_t numberItems = fwrite(DEFAULT_SVR_DB, 1, sizeof(DEFAULT_SVR_DB), fp);
327         fclose(fp);
328         ASSERT_TRUE(sizeof(DEFAULT_SVR_DB) == numberItems);
329     }
330
331     //Execute sample server to perform ownership transfer
332     int status1 = 0;
333     int status2 = 0;
334     if(0 == (g_myPID1 = fork()))
335     {
336         snprintf(server1_path, sizeof(server1_path), "%ssample_server1", cwd);
337         status1 = system(server1_path);
338         (void)status1;
339     }
340     if(0 == (g_myPID2 = fork()))
341     {
342         snprintf(server2_path, sizeof(server2_path), "%ssample_server2", cwd);
343         status2= system(server2_path);
344         (void)status2;
345     }
346
347     // register the persistent storage handler for SVR
348     result = OCRegisterPersistentStorageHandler(&pstStr);
349     EXPECT_EQ(OC_STACK_OK, result);
350
351     // initialize OC stack and provisioning manager
352     result = OCInit(NULL, 0, OC_CLIENT_SERVER);
353     EXPECT_EQ(OC_STACK_OK, result);
354
355     //initialize Provisioning DB Manager
356
357     snprintf(pdb_path, sizeof(pdb_path), "%stest.db", cwd);
358     result = OCInitPM(pdb_path);
359     EXPECT_EQ(OC_STACK_OK, result);
360
361     // register callback function(s) for Justworks OxM
362     result = OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb);
363     EXPECT_EQ(OC_STACK_OK, result);
364
365     g_doneCB = false;
366     g_callbackResult = false;
367 }
368
369
370 TEST(PerformUnownedDeviceDiscovery, NullParam)
371 {
372     OCStackResult result = OC_STACK_ERROR;
373
374     OIC_LOG(INFO, TAG, "Discovering Only Unowned Devices on Network..\n");
375     result = OCDiscoverUnownedDevices(DISCOVERY_TIMEOUT, &g_unownedDevices);
376     EXPECT_EQ(OC_STACK_OK, result);
377
378     int NumOfUnownDevice = 0;
379     OCProvisionDev_t* tempDev = g_unownedDevices;
380     while(tempDev)
381     {
382         NumOfUnownDevice++;
383         tempDev = tempDev->next;
384     }
385     EXPECT_EQ(2, NumOfUnownDevice);
386 }
387
388 TEST(PerformJustWorksOxM, NullParam)
389 {
390     OCStackResult result = OC_STACK_ERROR;
391
392     OIC_LOG(INFO, TAG, "Try Ownership Transfer for Unowned Devices...\n");
393     result = OCDoOwnershipTransfer((void*)g_otmCtx, g_unownedDevices, ownershipTransferCB);
394     EXPECT_EQ(OC_STACK_OK, result);
395
396     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
397     {
398         OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
399         return;
400     }
401     OIC_LOG(INFO, TAG, "Registered Discovered Unowned Device...\n");
402
403     EXPECT_EQ(true, g_callbackResult);
404     EXPECT_EQ(true, g_doneCB);
405 }
406
407
408 TEST(PerformOwnedDeviceDiscovery, NullParam)
409 {
410     OCStackResult result = OC_STACK_ERROR;
411
412     OIC_LOG(INFO, TAG, "Discovering Only Owned Devices on Network..\n");
413     result = OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_ownedDevices);
414     EXPECT_EQ(OC_STACK_OK, result);
415
416     int NumOfOwnDevice = 0;
417     OCProvisionDev_t* tempDev = g_ownedDevices;
418     while(tempDev)
419     {
420         NumOfOwnDevice++;
421         tempDev = tempDev->next;
422     }
423
424     EXPECT_EQ(2/*Server*/ , NumOfOwnDevice);
425 }
426
427 TEST(PerformLinkDevices, NullParam)
428 {
429     OicUuid_t myUuid;
430     OCStackResult result = OC_STACK_ERROR;
431     result = GetDoxmDeviceID(&myUuid);
432     EXPECT_EQ(OC_STACK_OK, result);
433
434     //Extract target device except PT to perform link devices.
435     OCProvisionDev_t* dev1 = NULL;
436     OCProvisionDev_t* dev2 = NULL;
437     OCProvisionDev_t* tempDev = g_ownedDevices;
438
439     while(tempDev)
440     {
441         if(memcmp(tempDev->doxm->deviceID.id, myUuid.id, UUID_LENGTH) != 0)
442         {
443             if(NULL == dev1)
444             {
445                 dev1 = tempDev;
446             }
447             else if(NULL == dev2)
448             {
449                 dev2 = tempDev;
450                 break;
451             }
452             else
453             {
454                 break;
455             }
456         }
457         tempDev = tempDev->next;
458     }
459     EXPECT_TRUE(NULL != dev1);
460     EXPECT_TRUE(NULL != dev2);
461
462     // TODO: Pairwise provisioning (Cred & ACL)
463     // TODO: This part will be updated after ACL and credential data-structure is updated.
464
465     EXPECT_EQ(OC_STACK_OK, result);
466 }
467
468 TEST(PerformUnlinkDevices, NullParam)
469 {
470     OCStackResult result = OC_STACK_OK;
471
472     // TODO: Unlink devices
473     // TODO: This part will be updated after ACL and credential data-structure is updated.
474
475     EXPECT_EQ(OC_STACK_OK, result);
476 }
477
478 TEST(PerformRemoveDevice, NullParam)
479 {
480     OicUuid_t myUuid;
481     OCStackResult result = OC_STACK_ERROR;
482     result = GetDoxmDeviceID(&myUuid);
483     EXPECT_EQ(OC_STACK_OK, result);
484
485     //Extract target device except PT to perform remove device.
486     OCProvisionDev_t* removeDev = g_ownedDevices;
487     while(removeDev)
488     {
489         if(memcmp(removeDev->doxm->deviceID.id, myUuid.id, UUID_LENGTH) != 0)
490         {
491             break;
492         }
493         removeDev = removeDev->next;
494     }
495     EXPECT_TRUE(NULL != removeDev);
496
497     g_doneCB = false;
498     g_callbackResult = false;
499
500     result = OCRemoveDevice((void*)g_otmCtx, DISCOVERY_TIMEOUT, removeDev, removeDeviceCB);
501     EXPECT_EQ(OC_STACK_OK, result);
502     EXPECT_EQ(true, g_callbackResult);
503     EXPECT_EQ(true, g_doneCB);
504 }
505
506 TEST(FinalizeOTMTest, NullParam)
507 {
508     OCStackResult result = OCStop();
509     EXPECT_EQ(OC_STACK_OK, result);
510
511     PMDeleteDeviceList(g_unownedDevices);
512     PMDeleteDeviceList(g_ownedDevices);
513     result = PDMClose();
514     EXPECT_EQ(OC_STACK_OK, result);
515
516     kill(g_myPID2, SIGKILL);
517     kill(g_myPID1, SIGKILL);
518
519     int interpreter_res1 = system("pkill -f \"sample_server1\"");
520     EXPECT_TRUE(0 <= interpreter_res1);
521     int interpreter_res2 = system("pkill -f \"sample_server2\"");
522     EXPECT_TRUE(0 <= interpreter_res2);
523 }
524