IOT-1583: Removing /W3 warning from resource/csdk/security.
[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 "oxmmanufacturercert.h"
33 #include "securevirtualresourcetypes.h"
34 #include "provisioningdatabasemanager.h"
35 #ifdef MULTIPLE_OWNER
36 #include "multipleownershiptransfermanager.h"
37 #endif //MULTIPLE_OWNER
38 #include "srmutility.h"
39 #include "doxmresource.h"
40 #include "pmtypes.h"
41 #include "pmutility.h"
42
43 using namespace std;
44
45 TEST(JustWorksOxMTest, NullParam)
46 {
47     OTMContext_t* otmCtx = NULL;
48     OCStackResult res = OC_STACK_ERROR;
49     uint8_t *payloadRes = NULL;
50     size_t size = 0;
51
52     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
53     res = LoadSecretJustWorksCallback(otmCtx);
54     EXPECT_TRUE(OC_STACK_OK == res);
55
56     res = CreateSecureSessionJustWorksCallback(otmCtx);
57     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
58
59     res = CreateJustWorksSelectOxmPayload(otmCtx, &payloadRes, &size);
60     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
61
62     res = CreateJustWorksOwnerTransferPayload(otmCtx, &payloadRes, &size);
63     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
64
65     OTMContext_t otmCtx2;
66     otmCtx2.selectedDeviceInfo = NULL;
67
68     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
69     res = LoadSecretJustWorksCallback(&otmCtx2);
70     EXPECT_TRUE(OC_STACK_OK == res);
71
72     res = CreateSecureSessionJustWorksCallback(&otmCtx2);
73     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
74
75     res = CreateJustWorksSelectOxmPayload(&otmCtx2, &payloadRes, &size);
76     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
77
78     res = CreateJustWorksOwnerTransferPayload(&otmCtx2, &payloadRes, &size);
79     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
80 }
81
82 TEST(RandomPinOxMTest, NullParam)
83 {
84     OTMContext_t* otmCtx = NULL;
85     OCStackResult res = OC_STACK_ERROR;
86     uint8_t *payloadRes = NULL;
87     size_t size = 0;
88
89     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
90     res = InputPinCodeCallback(otmCtx);
91     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
92
93     res = CreateSecureSessionRandomPinCallback(otmCtx);
94     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
95
96     res = CreatePinBasedSelectOxmPayload(otmCtx, &payloadRes, &size);
97     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
98
99     res = CreatePinBasedOwnerTransferPayload(otmCtx, &payloadRes, &size);
100     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
101
102     OTMContext_t otmCtx2;
103     otmCtx2.selectedDeviceInfo = NULL;
104
105     //LoadSecretJustWorksCallback always returns OC_STACK_OK.
106     res = InputPinCodeCallback(&otmCtx2);
107     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
108
109     res = CreateSecureSessionRandomPinCallback(&otmCtx2);
110     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
111
112     res = CreatePinBasedSelectOxmPayload(&otmCtx2, &payloadRes, &size);
113     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
114
115     res = CreatePinBasedOwnerTransferPayload(&otmCtx2, &payloadRes, &size);
116     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
117 }
118
119 TEST(ManufacturerCertOxMTest, NullParam)
120 {
121     OTMContext_t* otmCtx = NULL;
122     OCStackResult res = OC_STACK_ERROR;
123     uint8_t *payloadRes = NULL;
124     size_t size = 0;
125
126     res = PrepareMCertificateCallback(otmCtx);
127     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
128
129     res = CreateSecureSessionMCertificateCallback(otmCtx);
130     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
131
132     res = CreateMCertificateBasedSelectOxmPayload(otmCtx, &payloadRes, &size);
133     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
134
135     res = CreateMCertificateBasedOwnerTransferPayload(otmCtx, &payloadRes, &size);
136     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
137
138     OTMContext_t otmCtx2;
139     otmCtx2.selectedDeviceInfo = NULL;
140
141     res = InputPinCodeCallback(&otmCtx2);
142     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
143
144     res = CreateSecureSessionMCertificateCallback(&otmCtx2);
145     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
146
147     res = CreateMCertificateBasedSelectOxmPayload(&otmCtx2, &payloadRes, &size);
148     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
149
150     res = CreateMCertificateBasedOwnerTransferPayload(&otmCtx2, &payloadRes, &size);
151     EXPECT_TRUE(OC_STACK_INVALID_PARAM == res);
152 }
153
154 /****************************************
155  * Test the OTM modules with sample server
156  ****************************************/
157 #define TAG "JUSTWORKS_UNITTEST"
158 #define OTM_TIMEOUT 5
159 #define DISCOVERY_TIMEOUT 5
160
161 #define SVR_DB_PATH "oic_svr_db_client.dat"
162 #define UT_PATH "resource/csdk/security/provisioning/unittest"
163 char pdb_path[1024];
164 char svr_path[1024];
165
166 static uint8_t DEFAULT_SVR_DB[] = {
167     0xBF, 0x63, 0x61, 0x63, 0x6C, 0x59, 0x02, 0x79, 0xA4, 0x66, 0x61, 0x63, 0x6C, 0x69, 0x73, 0x74,
168     0xA1, 0x64, 0x61, 0x63, 0x65, 0x73, 0x83, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74,
169     0x75, 0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73,
170     0x83, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x68, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x72, 0x65, 0x73,
171     0x62, 0x72, 0x74, 0x81, 0x6A, 0x6F, 0x69, 0x63, 0x2E, 0x77, 0x6B, 0x2E, 0x72, 0x65, 0x73, 0x62,
172     0x69, 0x66, 0x81, 0x69, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x6C, 0x6C, 0x63, 0x72, 0x65,
173     0x6C, 0x60, 0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x66, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x64, 0x62,
174     0x72, 0x74, 0x81, 0x68, 0x6F, 0x69, 0x63, 0x2E, 0x77, 0x6B, 0x2E, 0x64, 0x62, 0x69, 0x66, 0x82,
175     0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65,
176     0x68, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x72, 0x63, 0x72, 0x65, 0x6C, 0x60, 0xA4, 0x64,
177     0x68, 0x72, 0x65, 0x66, 0x66, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x70, 0x62, 0x72, 0x74, 0x81, 0x68,
178     0x6F, 0x69, 0x63, 0x2E, 0x77, 0x6B, 0x2E, 0x70, 0x62, 0x69, 0x66, 0x82, 0x6F, 0x6F, 0x69, 0x63,
179     0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x68, 0x6F, 0x69, 0x63,
180     0x2E, 0x69, 0x66, 0x2E, 0x72, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D, 0x69,
181     0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x75,
182     0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73, 0x82,
183     0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6D, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F,
184     0x64, 0x6F, 0x78, 0x6D, 0x62, 0x72, 0x74, 0x81, 0x6A, 0x6F, 0x69, 0x63, 0x2E, 0x72, 0x2E, 0x64,
185     0x6F, 0x78, 0x6D, 0x62, 0x69, 0x66, 0x81, 0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62,
186     0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x63, 0x72, 0x65, 0x6C, 0x60, 0xA4, 0x64, 0x68, 0x72,
187     0x65, 0x66, 0x6E, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x70, 0x73, 0x74, 0x61,
188     0x74, 0x62, 0x72, 0x74, 0x81, 0x6B, 0x6F, 0x69, 0x63, 0x2E, 0x72, 0x2E, 0x70, 0x73, 0x74, 0x61,
189     0x74, 0x62, 0x69, 0x66, 0x81, 0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61, 0x73,
190     0x65, 0x6C, 0x69, 0x6E, 0x65, 0x63, 0x72, 0x65, 0x6C, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D, 0x69,
191     0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0xA3, 0x6B, 0x73, 0x75, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x75,
192     0x75, 0x69, 0x64, 0x61, 0x2A, 0x69, 0x72, 0x65, 0x73, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x73, 0x82,
193     0xA4, 0x64, 0x68, 0x72, 0x65, 0x66, 0x6E, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F,
194     0x70, 0x63, 0x6F, 0x6E, 0x66, 0x62, 0x72, 0x74, 0x81, 0x6B, 0x6F, 0x69, 0x63, 0x2E, 0x72, 0x2E,
195     0x70, 0x63, 0x6F, 0x6E, 0x66, 0x62, 0x69, 0x66, 0x81, 0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66,
196     0x2E, 0x62, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x63, 0x72, 0x65, 0x6C, 0x60, 0xA4, 0x64,
197     0x68, 0x72, 0x65, 0x66, 0x71, 0x2F, 0x6F, 0x69, 0x63, 0x2F, 0x73, 0x65, 0x63, 0x2F, 0x64, 0x70,
198     0x61, 0x69, 0x72, 0x69, 0x6E, 0x67, 0x62, 0x72, 0x74, 0x81, 0x6E, 0x6F, 0x69, 0x63, 0x2E, 0x72,
199     0x2E, 0x64, 0x70, 0x61, 0x69, 0x72, 0x69, 0x6E, 0x67, 0x62, 0x69, 0x66, 0x81, 0x6F, 0x6F, 0x69,
200     0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x63, 0x72, 0x65,
201     0x6C, 0x60, 0x6A, 0x70, 0x65, 0x72, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0x6A, 0x72,
202     0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x36, 0x31, 0x36, 0x34, 0x36,
203     0x64, 0x36, 0x39, 0x2D, 0x36, 0x65, 0x34, 0x34, 0x2D, 0x36, 0x35, 0x37, 0x36, 0x2D, 0x36, 0x39,
204     0x36, 0x33, 0x2D, 0x36, 0x35, 0x35, 0x35, 0x37, 0x35, 0x36, 0x39, 0x36, 0x34, 0x33, 0x30, 0x62,
205     0x72, 0x74, 0x81, 0x69, 0x6F, 0x69, 0x63, 0x2E, 0x72, 0x2E, 0x61, 0x63, 0x6C, 0x62, 0x69, 0x66,
206     0x81, 0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61, 0x73, 0x65, 0x6C, 0x69, 0x6E,
207     0x65, 0x65, 0x70, 0x73, 0x74, 0x61, 0x74, 0x58, 0x9D, 0xA9, 0x64, 0x69, 0x73, 0x6F, 0x70, 0xF5,
208     0x6A, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x36, 0x31, 0x36,
209     0x34, 0x36, 0x64, 0x36, 0x39, 0x2D, 0x36, 0x65, 0x34, 0x34, 0x2D, 0x36, 0x35, 0x37, 0x36, 0x2D,
210     0x36, 0x39, 0x36, 0x33, 0x2D, 0x36, 0x35, 0x35, 0x35, 0x37, 0x35, 0x36, 0x39, 0x36, 0x34, 0x33,
211     0x30, 0x62, 0x63, 0x6D, 0x00, 0x62, 0x74, 0x6D, 0x00, 0x62, 0x6F, 0x6D, 0x03, 0x62, 0x73, 0x6D,
212     0x03, 0x6A, 0x72, 0x6F, 0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x36, 0x31,
213     0x36, 0x34, 0x36, 0x64, 0x36, 0x39, 0x2D, 0x36, 0x65, 0x34, 0x34, 0x2D, 0x36, 0x35, 0x37, 0x36,
214     0x2D, 0x36, 0x39, 0x36, 0x33, 0x2D, 0x36, 0x35, 0x35, 0x35, 0x37, 0x35, 0x36, 0x39, 0x36, 0x34,
215     0x33, 0x30, 0x62, 0x72, 0x74, 0x81, 0x6B, 0x6F, 0x69, 0x63, 0x2E, 0x72, 0x2E, 0x70, 0x73, 0x74,
216     0x61, 0x74, 0x62, 0x69, 0x66, 0x81, 0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61,
217     0x73, 0x65, 0x6C, 0x69, 0x6E, 0x65, 0x64, 0x64, 0x6F, 0x78, 0x6D, 0x58, 0xE8, 0xAA, 0x64, 0x6F,
218     0x78, 0x6D, 0x73, 0x81, 0x00, 0x66, 0x6F, 0x78, 0x6D, 0x73, 0x65, 0x6C, 0x00, 0x63, 0x73, 0x63,
219     0x74, 0x01, 0x65, 0x6F, 0x77, 0x6E, 0x65, 0x64, 0xF5, 0x6A, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65,
220     0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x36, 0x31, 0x36, 0x34, 0x36, 0x64, 0x36, 0x39, 0x2D, 0x36,
221     0x65, 0x34, 0x34, 0x2D, 0x36, 0x35, 0x37, 0x36, 0x2D, 0x36, 0x39, 0x36, 0x33, 0x2D, 0x36, 0x35,
222     0x35, 0x35, 0x37, 0x35, 0x36, 0x39, 0x36, 0x34, 0x33, 0x30, 0x6C, 0x64, 0x65, 0x76, 0x6F, 0x77,
223     0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x36, 0x31, 0x36, 0x34, 0x36, 0x64, 0x36,
224     0x39, 0x2D, 0x36, 0x65, 0x34, 0x34, 0x2D, 0x36, 0x35, 0x37, 0x36, 0x2D, 0x36, 0x39, 0x36, 0x33,
225     0x2D, 0x36, 0x35, 0x35, 0x35, 0x37, 0x35, 0x36, 0x39, 0x36, 0x34, 0x33, 0x30, 0x6A, 0x72, 0x6F,
226     0x77, 0x6E, 0x65, 0x72, 0x75, 0x75, 0x69, 0x64, 0x78, 0x24, 0x36, 0x31, 0x36, 0x34, 0x36, 0x64,
227     0x36, 0x39, 0x2D, 0x36, 0x65, 0x34, 0x34, 0x2D, 0x36, 0x35, 0x37, 0x36, 0x2D, 0x36, 0x39, 0x36,
228     0x33, 0x2D, 0x36, 0x35, 0x35, 0x35, 0x37, 0x35, 0x36, 0x39, 0x36, 0x34, 0x33, 0x30, 0x72, 0x78,
229     0x2E, 0x6F, 0x72, 0x67, 0x2E, 0x69, 0x6F, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x2E, 0x64, 0x70,
230     0x63, 0xF4, 0x62, 0x72, 0x74, 0x81, 0x6A, 0x6F, 0x69, 0x63, 0x2E, 0x72, 0x2E, 0x64, 0x6F, 0x78,
231     0x6D, 0x62, 0x69, 0x66, 0x81, 0x6F, 0x6F, 0x69, 0x63, 0x2E, 0x69, 0x66, 0x2E, 0x62, 0x61, 0x73,
232     0x65, 0x6C, 0x69, 0x6E, 0x65, 0xFF
233 };
234
235 static bool g_doneCB;
236 static bool g_callbackResult;
237 static pid_t g_myPID1;
238 static pid_t g_myPID2;
239
240 static const char* g_otmCtx = "Test User Context";
241 static OCProvisionDev_t* g_unownedDevices = NULL;
242 static OCProvisionDev_t* g_ownedDevices = NULL;
243 #ifdef MULTIPLE_OWNER
244 static OCProvisionDev_t* g_motEnabledDevices = NULL;
245 static OCProvisionDev_t* g_multiplOwnedDevices = NULL;
246 #endif //MULTIPLE_OWNER
247
248 static void GetCurrentWorkingDirectory(char* buf, size_t bufsize)
249 {
250     char cwd[1024] = {0};
251     const char* unittest_path = "resource/csdk/security/provisioning/unittest";
252     if(getcwd(cwd, sizeof(cwd)) != NULL)
253     {
254         if(strstr(cwd, unittest_path) == NULL)
255         {
256 #if defined __linux__
257 #if __x86_64__
258         snprintf(buf, bufsize, "%s/out/linux/x86_64/release/%s/", cwd, unittest_path);
259         snprintf(buf, bufsize, "%s/out/linux/x86_64/release/%s/", cwd, unittest_path);
260 #else
261         snprintf(buf, bufsize, "%s/out/linux/x86/release/%s/", cwd, unittest_path);
262         snprintf(buf, bufsize, "%s/out/linux/x86/release/%s/", cwd, unittest_path);
263 #endif //__x86_64__
264 #endif //defined __linux__
265         }
266         else
267         {
268             snprintf(buf, bufsize, "%s/", cwd);
269         }
270     }
271 }
272
273 static FILE* fopen_prvnMng(const char* path, const char* mode)
274 {
275     if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
276     {
277         // input |g_svr_db_fname| internally by force, not using |path| parameter
278         // because |OCPersistentStorage::open| is called |OCPersistentStorage| internally
279         // with its own |SVR_DB_FILE_NAME|
280         char cwd[1024] = { 0 };
281         char svr_db_path[1024] = { 0 };
282         GetCurrentWorkingDirectory(cwd, sizeof(cwd));
283         snprintf(svr_db_path, sizeof(svr_db_path), "%s%s", cwd, SVR_DB_PATH);
284         return fopen(svr_db_path, mode);
285     }
286     else
287     {
288         return fopen(path, mode);
289     }
290 }
291
292 // callback function(s) for provisioning client using C-level provisioning API
293 static void ownershipTransferCB(void* ctx, size_t UNUSED1, OCProvisionResult_t* UNUSED2, bool hasError)
294 {
295     OC_UNUSED(UNUSED1);
296     OC_UNUSED(UNUSED2);
297     OC_UNUSED(ctx);
298
299     if(!hasError)
300     {
301         OIC_LOG_V(INFO, TAG, "Ownership Transfer SUCCEEDED - ctx: %s", (char*) ctx);
302     }
303     else
304     {
305         OIC_LOG_V(ERROR, TAG, "Ownership Transfer FAILED - ctx: %s", (char*) ctx);
306     }
307     g_callbackResult = !hasError;
308     g_doneCB = true;
309 }
310
311 #ifdef MULTIPLE_OWNER
312 static void updateDoxmForMOTCB(void* ctx, size_t nOfRes, OCProvisionResult_t* arr, bool hasError)
313 {
314     OC_UNUSED(nOfRes);
315
316     if(!hasError)
317     {
318         OIC_LOG_V(INFO, TAG, "POST 'doxm' SUCCEEDED - ctx: %s", (char*) ctx);
319     }
320     else
321     {
322         OIC_LOG_V(ERROR, TAG, "POST 'doxm'  FAILED - ctx: %s", (char*) ctx);
323     }
324     g_callbackResult = !hasError;
325     g_doneCB = true;
326 }
327
328 static void provisionPreconfiguredPinCB(void* ctx, size_t nOfRes, OCProvisionResult_t* arr, bool hasError)
329 {
330     OC_UNUSED(nOfRes);
331
332     if(!hasError)
333     {
334         OIC_LOG_V(INFO, TAG, "Provision Preconfigured-PIN SUCCEEDED - ctx: %s", (char*) ctx);
335     }
336     else
337     {
338         OIC_LOG_V(ERROR, TAG, "Provision Preconfigured-PIN FAILED - ctx: %s", (char*) ctx);
339     }
340     g_callbackResult = !hasError;
341     g_doneCB = true;
342 }
343 #endif //MULTIPLE_OWNER
344
345 // callback function(s) for provisioning client using C-level provisioning API
346 static void removeDeviceCB(void* ctx, size_t UNUSED1, OCProvisionResult_t* UNUSED2, bool hasError)
347 {
348     OC_UNUSED(UNUSED1);
349     OC_UNUSED(UNUSED2);
350     OC_UNUSED(ctx);
351
352     if(!hasError)
353     {
354         OIC_LOG_V(INFO, TAG, "Remove device request SUCCEEDED - ctx: %s", (char*) ctx);
355     }
356     else
357     {
358         OIC_LOG_V(ERROR, TAG, "Remove device request FAILED - ctx: %s", (char*) ctx);
359     }
360     g_callbackResult = !hasError;
361     g_doneCB = true;
362 }
363
364
365 static int waitCallbackRet(void)
366 {
367     struct timespec timeout;
368     timeout.tv_sec  = 0;
369     timeout.tv_nsec = 100000000L;
370
371     for(long long i=0; !g_doneCB && OTM_TIMEOUT * 100000000L * 1000L > i; ++i)
372     {
373         nanosleep(&timeout, NULL);
374         if(OC_STACK_OK != OCProcess())
375         {
376             OIC_LOG(ERROR, TAG, "OCStack process error");
377             return -1;
378         }
379     }
380
381     return 0;
382 }
383
384 static void RemoveUnknownDeviceFromDevList(OCProvisionDev_t* devList)
385 {
386     OicUuid_t uuid1;
387     OicUuid_t uuid2;
388     memset(uuid1.id, 0x11, sizeof(uuid1.id));
389     memset(uuid2.id, 0x22, sizeof(uuid2.id));
390
391     OCProvisionDev_t* unknowDev = NULL;
392     OCProvisionDev_t* tempDev1 = NULL;
393     OCProvisionDev_t* tempDev2 =NULL;
394     LL_FOREACH_SAFE(devList, tempDev1, tempDev2)
395     {
396
397         if(!(memcmp(tempDev1->doxm->deviceID.id, uuid1.id, sizeof(uuid1.id)) == 0 ||
398              memcmp(tempDev1->doxm->deviceID.id, uuid2.id, sizeof(uuid2.id)) == 0))
399         {
400             LL_DELETE(devList, tempDev1);
401             LL_APPEND(unknowDev, tempDev1);
402         }
403     }
404     OCDeleteDiscoveredDevices(unknowDev);
405 }
406
407 TEST(InitForOTM, NullParam)
408 {
409     OCStackResult result = OC_STACK_ERROR;
410
411     OTMCallbackData_t otmcb;
412     otmcb.loadSecretCB = LoadSecretJustWorksCallback;
413     otmcb.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
414     otmcb.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
415     otmcb.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
416
417     static OCPersistentStorage pstStr;
418     pstStr.open = fopen_prvnMng;
419     pstStr.read = fread;
420     pstStr.write = fwrite;
421     pstStr.close = fclose;
422     pstStr.unlink = unlink;
423
424     //Get current path to execute the sample server.
425     char cwd[1024] = {0};
426     char server1_path[1024] = {0};
427     char server2_path[1024] = {0};
428     char pdb_path[1024] = {0};
429     char del_cmd[1024] = {0};
430     char svrdb_path[1024] = {0};
431     FILE* fp = NULL;
432
433     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
434     EXPECT_TRUE(0 < strlen(cwd));
435
436     //Delete previous PDB, if exist.
437     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
438     snprintf(del_cmd, sizeof(del_cmd), "rm -rf %stest.db", cwd);
439     system(del_cmd);
440
441     //Delete previous SVR DB, if exist.
442     snprintf(del_cmd, sizeof(del_cmd), "rm -rf %s%s", cwd, SVR_DB_PATH);
443     system(del_cmd);
444
445     //Generate default SVR DB.
446     snprintf(svrdb_path, sizeof(svrdb_path), "%s%s", cwd, SVR_DB_PATH);
447     fp = fopen(svrdb_path, "w");
448     if(NULL != fp)
449     {
450         size_t numberItems = fwrite(DEFAULT_SVR_DB, 1, sizeof(DEFAULT_SVR_DB), fp);
451         fclose(fp);
452         ASSERT_TRUE(sizeof(DEFAULT_SVR_DB) == numberItems);
453     }
454
455     //Execute sample server to perform ownership transfer
456     int status1 = 0;
457     int status2 = 0;
458     if(0 == (g_myPID1 = fork()))
459     {
460         snprintf(server1_path, sizeof(server1_path), "%ssample_server1", cwd);
461         status1 = system(server1_path);
462         OC_UNUSED(status1);
463     }
464     if(0 == (g_myPID2 = fork()))
465     {
466         snprintf(server2_path, sizeof(server2_path), "%ssample_server2", cwd);
467         status2 = system(server2_path);
468         OC_UNUSED(status2);
469     }
470
471     //Waiting for sample server initialization.
472     sleep(3);
473
474     // register the persistent storage handler for SVR
475     result = OCRegisterPersistentStorageHandler(&pstStr);
476     EXPECT_EQ(OC_STACK_OK, result);
477
478     // initialize OC stack and provisioning manager
479     result = OCInit(NULL, 0, OC_CLIENT_SERVER);
480     EXPECT_EQ(OC_STACK_OK, result);
481
482     //initialize Provisioning DB Manager
483
484     snprintf(pdb_path, sizeof(pdb_path), "%stest.db", cwd);
485     result = OCInitPM(pdb_path);
486     EXPECT_EQ(OC_STACK_OK, result);
487
488     // register callback function(s) for Justworks OxM
489     result = OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb);
490     EXPECT_EQ(OC_STACK_OK, result);
491
492     g_doneCB = false;
493     g_callbackResult = false;
494 }
495
496 TEST(PerformSecureResourceDiscovery, NullParam)
497 {
498     OCStackResult result = OC_STACK_ERROR;
499     OCProvisionDev_t* foundDevice = NULL;
500
501     OicUuid_t uuid;
502     ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
503
504     OIC_LOG(INFO, TAG, "Discovering Owned/Unowned Device using multicast\n");
505     result = OCDiscoverSingleDevice(DISCOVERY_TIMEOUT, &uuid, &foundDevice);
506     EXPECT_EQ(OC_STACK_OK, result);
507
508     int NumOfFoundDevice = 0;
509     OCProvisionDev_t* tempDev = foundDevice;
510     while(tempDev)
511     {
512         NumOfFoundDevice++;
513         tempDev = tempDev->next;
514     }
515     PMDeleteDeviceList(foundDevice);
516
517     EXPECT_EQ(true, NumOfFoundDevice > 0);
518 }
519
520 TEST(PerformUnownedDeviceDiscovery, NullParam)
521 {
522     OCStackResult result = OC_STACK_ERROR;
523
524     OIC_LOG(INFO, TAG, "Discovering Only Unowned Devices on Network..\n");
525     result = OCDiscoverUnownedDevices(DISCOVERY_TIMEOUT, &g_unownedDevices);
526     EXPECT_EQ(OC_STACK_OK, result);
527
528     RemoveUnknownDeviceFromDevList(g_unownedDevices);
529
530     int NumOfUnownDevice = 0;
531     OCProvisionDev_t* tempDev = g_unownedDevices;
532     while(tempDev)
533     {
534         NumOfUnownDevice++;
535         tempDev = tempDev->next;
536     }
537
538     //if two more devices are running on the subnet,
539     //stop the unittest process in order to prevent unittest failure.
540     if(2 < NumOfUnownDevice)
541     {
542         printf("%c[1;33;40m ******************************************************************\n", 0x1B);
543         printf("%c[1;33;40m   [%d] devices are running on the your sub-network.\n", 0x1B, NumOfUnownDevice);
544         printf("%c[1;33;40m   Please turn off the other devices, and then try once again.\n", 0x1B);
545         printf("%c[1;33;40m ******************************************************************", 0x1B);
546         printf("%c[1;0;40m \n", 0x1B);
547
548         OCStackResult result = OCStop();
549         EXPECT_EQ(OC_STACK_OK, result);
550
551         PMDeleteDeviceList(g_unownedDevices);
552         PMDeleteDeviceList(g_ownedDevices);
553         result = PDMClose();
554         EXPECT_EQ(OC_STACK_OK, result);
555
556         kill(g_myPID2, SIGKILL);
557         kill(g_myPID1, SIGKILL);
558
559         int interpreter_res1 = system("pkill -f \"sample_server1\"");
560         EXPECT_TRUE(0 <= interpreter_res1);
561         int interpreter_res2 = system("pkill -f \"sample_server2\"");
562         EXPECT_TRUE(0 <= interpreter_res2);
563
564         exit(0);
565     }
566
567     EXPECT_EQ(2, NumOfUnownDevice);
568 }
569
570 TEST(PerformJustWorksOxM, NullParam)
571 {
572     OCStackResult result = OC_STACK_ERROR;
573
574     OIC_LOG(INFO, TAG, "Try Ownership Transfer for Unowned Devices...\n");
575     result = OCDoOwnershipTransfer((void*)g_otmCtx, g_unownedDevices, ownershipTransferCB);
576     EXPECT_EQ(OC_STACK_OK, result);
577
578     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
579     {
580         OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
581         return;
582     }
583     OIC_LOG(INFO, TAG, "Registered Discovered Unowned Device...\n");
584
585     EXPECT_EQ(true, g_callbackResult);
586     EXPECT_EQ(true, g_doneCB);
587 }
588
589
590 TEST(PerformOwnedDeviceDiscovery, NullParam)
591 {
592     OCStackResult result = OC_STACK_ERROR;
593
594     OIC_LOG(INFO, TAG, "Discovering Only Owned Devices on Network..\n");
595     result = OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_ownedDevices);
596     EXPECT_EQ(OC_STACK_OK, result);
597
598     RemoveUnknownDeviceFromDevList(g_ownedDevices);
599
600     int NumOfOwnDevice = 0;
601     OCProvisionDev_t* tempDev = g_ownedDevices;
602     while(tempDev)
603     {
604         NumOfOwnDevice++;
605         tempDev = tempDev->next;
606     }
607
608     EXPECT_EQ(2 , NumOfOwnDevice);
609 }
610
611 TEST(PerformLinkDevices, NullParam)
612 {
613     OicUuid_t myUuid;
614     OCStackResult result = OC_STACK_ERROR;
615     result = GetDoxmDeviceID(&myUuid);
616     EXPECT_EQ(OC_STACK_OK, result);
617
618     //Extract target device except PT to perform link devices.
619     OCProvisionDev_t* dev1 = NULL;
620     OCProvisionDev_t* dev2 = NULL;
621     OCProvisionDev_t* tempDev = g_ownedDevices;
622
623     while(tempDev)
624     {
625         if(memcmp(tempDev->doxm->deviceID.id, myUuid.id, UUID_LENGTH) != 0)
626         {
627             if(NULL == dev1)
628             {
629                 dev1 = tempDev;
630             }
631             else if(NULL == dev2)
632             {
633                 dev2 = tempDev;
634                 break;
635             }
636         }
637         tempDev = tempDev->next;
638     }
639     EXPECT_TRUE(NULL != dev1);
640     EXPECT_TRUE(NULL != dev2);
641
642     // TODO: Pairwise provisioning (Cred & ACL)
643     // TODO: This part will be updated after ACL and credential data-structure is updated.
644
645     EXPECT_EQ(OC_STACK_OK, result);
646 }
647
648 TEST(PerformUnlinkDevices, NullParam)
649 {
650     OCStackResult result = OC_STACK_OK;
651
652     // TODO: Unlink devices
653     // TODO: This part will be updated after ACL and credential data-structure is updated.
654
655     EXPECT_EQ(OC_STACK_OK, result);
656 }
657
658 #ifdef MULTIPLE_OWNER
659 TEST(RegisterPreconfiguredPIN, NullParam)
660 {
661     OCStackResult result = SetPreconfigPin("12341234", strlen("12341234"));
662     EXPECT_EQ(OC_STACK_OK, result);
663 }
664
665 TEST(EnableMOT, NullParam)
666 {
667     OCStackResult result = OC_STACK_OK;
668
669     if(NULL == g_ownedDevices)
670     {
671         OIC_LOG(INFO, TAG, "Discovering Only Owned Devices on Network..\n");
672         result = OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_ownedDevices);
673         EXPECT_EQ(OC_STACK_OK, result);
674         RemoveUnknownDeviceFromDevList(g_ownedDevices);
675     }
676     EXPECT_NE((OCProvisionDev_t*)NULL, g_ownedDevices);
677
678     g_doneCB = false;
679     result = OCChangeMOTMode(NULL, g_ownedDevices, OIC_MULTIPLE_OWNER_ENABLE, updateDoxmForMOTCB);
680     EXPECT_EQ(OC_STACK_OK, result);
681     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
682     {
683         OIC_LOG(ERROR, TAG, "OCChangeMOTMode callback error");
684         return;
685     }
686
687     EXPECT_TRUE(g_callbackResult);
688 }
689
690 TEST(DiscoverMOTEnabledDevices, NullParam)
691 {
692     OCStackResult result = OC_STACK_OK;
693
694     if(g_motEnabledDevices)
695     {
696         PMDeleteDeviceList(g_motEnabledDevices);
697     }
698
699     OIC_LOG(INFO, TAG, "Discovering MOT Enabled Devices on Network..\n");
700     result = OCDiscoverMultipleOwnerEnabledDevices(DISCOVERY_TIMEOUT, &g_motEnabledDevices);
701     EXPECT_EQ(OC_STACK_OK, result);
702     RemoveUnknownDeviceFromDevList(g_motEnabledDevices);
703     EXPECT_NE((OCProvisionDev_t*)NULL, g_motEnabledDevices);
704 }
705
706 TEST(ProvisonPreconfiguredPIN, NullParam)
707 {
708     OCStackResult result = OC_STACK_OK;
709
710     g_doneCB = false;
711     result = OCProvisionPreconfigPin(NULL, g_motEnabledDevices, "12341234", strlen("12341234"), provisionPreconfiguredPinCB);
712     EXPECT_EQ(OC_STACK_OK, result);
713     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
714     {
715         OIC_LOG(ERROR, TAG, "OCProvisionPreconfigPin callback error");
716         return;
717     }
718
719     EXPECT_EQ(true, g_callbackResult);
720 }
721
722 TEST(SelectMOTMethod, NullParam)
723 {
724     OCStackResult result = OC_STACK_OK;
725
726     g_doneCB = false;
727     result = OCSelectMOTMethod(NULL, g_motEnabledDevices, OIC_PRECONFIG_PIN, updateDoxmForMOTCB);
728     EXPECT_EQ(OC_STACK_OK, result);
729     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
730     {
731         OIC_LOG(ERROR, TAG, "OCSelectMOTMethod callback error");
732         return;
733     }
734
735     EXPECT_EQ(true, g_callbackResult);
736 }
737
738 // TODO: Need to new server to perform MOT
739 /*
740 TEST(PerformMOT, NullParam)
741 {
742     OCStackResult result = OC_STACK_OK;
743
744     g_doneCB = false;
745     result = OCDoMultipleOwnershipTransfer(NULL, g_motEnabledDevices, ownershipTransferCB);
746     EXPECT_EQ(OC_STACK_OK, result);
747     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
748     {
749         OIC_LOG(ERROR, TAG, "OCDoMultipleOwnershipTransfer callback error");
750         return;
751     }
752     EXPECT_EQ(true, g_callbackResult);
753 }
754
755 TEST(DiscoverMultipleOwnedDevices, NullParam)
756 {
757     OCStackResult result = OC_STACK_OK;
758
759     if(g_multiplOwnedDevices)
760     {
761         PMDeleteDeviceList(g_multiplOwnedDevices);
762     }
763
764     OIC_LOG(INFO, TAG, "Discovering MOT Enabled Devices on Network..\n");
765     result = OCDiscoverMultipleOwnedDevices(DISCOVERY_TIMEOUT, &g_multiplOwnedDevices);
766     EXPECT_EQ(OC_STACK_OK, result);
767     RemoveUnknownDeviceFromDevList(g_multiplOwnedDevices);
768     EXPECT_TRUE(NULL != g_multiplOwnedDevices);
769 }*/
770
771 #endif //MULTIPLE_OWNER
772
773 TEST(PerformRemoveDevice, NullParam)
774 {
775     OicUuid_t myUuid;
776     OCStackResult result = OC_STACK_ERROR;
777     result = GetDoxmDeviceID(&myUuid);
778     EXPECT_EQ(OC_STACK_OK, result);
779
780     //Extract target device except PT to perform remove device.
781     OCProvisionDev_t* removeDev = g_ownedDevices;
782     while(removeDev)
783     {
784         if(memcmp(removeDev->doxm->deviceID.id, myUuid.id, UUID_LENGTH) != 0)
785         {
786             break;
787         }
788         removeDev = removeDev->next;
789     }
790     EXPECT_TRUE(NULL != removeDev);
791
792     g_doneCB = false;
793     g_callbackResult = false;
794
795     result = OCRemoveDevice((void*)g_otmCtx, DISCOVERY_TIMEOUT, removeDev, removeDeviceCB);
796     EXPECT_EQ(OC_STACK_OK, result);
797     EXPECT_EQ(true, g_callbackResult);
798     EXPECT_EQ(true, g_doneCB);
799 }
800
801 TEST(FinalizeOTMTest, NullParam)
802 {
803     OCStackResult result = OCStop();
804     EXPECT_EQ(OC_STACK_OK, result);
805
806     PMDeleteDeviceList(g_unownedDevices);
807     PMDeleteDeviceList(g_ownedDevices);
808     result = PDMClose();
809     EXPECT_EQ(OC_STACK_OK, result);
810
811     kill(g_myPID2, SIGKILL);
812     kill(g_myPID1, SIGKILL);
813
814     int interpreter_res1 = system("pkill -f \"sample_server1\"");
815     EXPECT_TRUE(0 <= interpreter_res1);
816     int interpreter_res2 = system("pkill -f \"sample_server2\"");
817     EXPECT_TRUE(0 <= interpreter_res2);
818 }
819