83a4d6adb92618081c02d9dd77fb0195623a860d
[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, int UNUSED1, OCProvisionResult_t* UNUSED2, bool hasError)
294 {
295     (void)UNUSED1;
296     (void)UNUSED2;
297     (void)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, int nOfRes, OCProvisionResult_t* arr, bool hasError)
313 {
314     if(!hasError)
315     {
316         OIC_LOG_V(INFO, TAG, "POST 'doxm' SUCCEEDED - ctx: %s", (char*) ctx);
317     }
318     else
319     {
320         OIC_LOG_V(ERROR, TAG, "POST 'doxm'  FAILED - ctx: %s", (char*) ctx);
321     }
322     g_callbackResult = !hasError;
323     g_doneCB = true;
324 }
325
326 static void provisionPreconfiguredPinCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
327 {
328     if(!hasError)
329     {
330         OIC_LOG_V(INFO, TAG, "Provision Preconfigured-PIN SUCCEEDED - ctx: %s", (char*) ctx);
331     }
332     else
333     {
334         OIC_LOG_V(ERROR, TAG, "Provision Preconfigured-PIN FAILED - ctx: %s", (char*) ctx);
335     }
336     g_callbackResult = !hasError;
337     g_doneCB = true;
338 }
339 #endif //MULTIPLE_OWNER
340
341 // callback function(s) for provisioning client using C-level provisioning API
342 static void removeDeviceCB(void* ctx, int UNUSED1, OCProvisionResult_t* UNUSED2, bool hasError)
343 {
344     (void)UNUSED1;
345     (void)UNUSED2;
346     (void)ctx;
347
348     if(!hasError)
349     {
350         OIC_LOG_V(INFO, TAG, "Remove device request SUCCEEDED - ctx: %s", (char*) ctx);
351     }
352     else
353     {
354         OIC_LOG_V(ERROR, TAG, "Remove device request FAILED - ctx: %s", (char*) ctx);
355     }
356     g_callbackResult = !hasError;
357     g_doneCB = true;
358 }
359
360
361 static int waitCallbackRet(void)
362 {
363     struct timespec timeout;
364     timeout.tv_sec  = 0;
365     timeout.tv_nsec = 100000000L;
366
367     for(long long i=0; !g_doneCB && OTM_TIMEOUT * 100000000L * 1000L > i; ++i)
368     {
369         nanosleep(&timeout, NULL);
370         if(OC_STACK_OK != OCProcess())
371         {
372             OIC_LOG(ERROR, TAG, "OCStack process error");
373             return -1;
374         }
375     }
376
377     return 0;
378 }
379
380 static void RemoveUnknownDeviceFromDevList(OCProvisionDev_t* devList)
381 {
382     OicUuid_t uuid1;
383     OicUuid_t uuid2;
384     memset(uuid1.id, 0x11, sizeof(uuid1.id));
385     memset(uuid2.id, 0x22, sizeof(uuid2.id));
386
387     OCProvisionDev_t* unknowDev = NULL;
388     OCProvisionDev_t* tempDev1 = NULL;
389     OCProvisionDev_t* tempDev2 =NULL;
390     LL_FOREACH_SAFE(devList, tempDev1, tempDev2)
391     {
392
393         if(!(memcmp(tempDev1->doxm->deviceID.id, uuid1.id, sizeof(uuid1.id)) == 0 ||
394              memcmp(tempDev1->doxm->deviceID.id, uuid2.id, sizeof(uuid2.id)) == 0))
395         {
396             LL_DELETE(devList, tempDev1);
397             LL_APPEND(unknowDev, tempDev1);
398         }
399     }
400     OCDeleteDiscoveredDevices(unknowDev);
401 }
402
403 TEST(InitForOTM, NullParam)
404 {
405     OCStackResult result = OC_STACK_ERROR;
406
407     OTMCallbackData_t otmcb;
408     otmcb.loadSecretCB = LoadSecretJustWorksCallback;
409     otmcb.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
410     otmcb.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
411     otmcb.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
412
413     static OCPersistentStorage pstStr;
414     pstStr.open = fopen_prvnMng;
415     pstStr.read = fread;
416     pstStr.write = fwrite;
417     pstStr.close = fclose;
418     pstStr.unlink = unlink;
419
420     //Get current path to execute the sample server.
421     char cwd[1024] = {0};
422     char server1_path[1024] = {0};
423     char server2_path[1024] = {0};
424     char pdb_path[1024] = {0};
425     char del_cmd[1024] = {0};
426     char svrdb_path[1024] = {0};
427     FILE* fp = NULL;
428
429     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
430     EXPECT_TRUE(0 < strlen(cwd));
431
432     //Delete previous PDB, if exist.
433     GetCurrentWorkingDirectory(cwd, sizeof(cwd));
434     snprintf(del_cmd, sizeof(del_cmd), "rm -rf %stest.db", cwd);
435     system(del_cmd);
436
437     //Delete previous SVR DB, if exist.
438     snprintf(del_cmd, sizeof(del_cmd), "rm -rf %s%s", cwd, SVR_DB_PATH);
439     system(del_cmd);
440
441     //Generate default SVR DB.
442     snprintf(svrdb_path, sizeof(svrdb_path), "%s%s", cwd, SVR_DB_PATH);
443     fp = fopen(svrdb_path, "w");
444     if(NULL != fp)
445     {
446         size_t numberItems = fwrite(DEFAULT_SVR_DB, 1, sizeof(DEFAULT_SVR_DB), fp);
447         fclose(fp);
448         ASSERT_TRUE(sizeof(DEFAULT_SVR_DB) == numberItems);
449     }
450
451     //Execute sample server to perform ownership transfer
452     int status1 = 0;
453     int status2 = 0;
454     if(0 == (g_myPID1 = fork()))
455     {
456         snprintf(server1_path, sizeof(server1_path), "%ssample_server1", cwd);
457         status1 = system(server1_path);
458         (void)status1;
459     }
460     if(0 == (g_myPID2 = fork()))
461     {
462         snprintf(server2_path, sizeof(server2_path), "%ssample_server2", cwd);
463         status2= system(server2_path);
464         (void)status2;
465     }
466
467     //Waiting for sample server initialization.
468     sleep(3);
469
470     // register the persistent storage handler for SVR
471     result = OCRegisterPersistentStorageHandler(&pstStr);
472     EXPECT_EQ(OC_STACK_OK, result);
473
474     // initialize OC stack and provisioning manager
475     result = OCInit(NULL, 0, OC_CLIENT_SERVER);
476     EXPECT_EQ(OC_STACK_OK, result);
477
478     //initialize Provisioning DB Manager
479
480     snprintf(pdb_path, sizeof(pdb_path), "%stest.db", cwd);
481     result = OCInitPM(pdb_path);
482     EXPECT_EQ(OC_STACK_OK, result);
483
484     // register callback function(s) for Justworks OxM
485     result = OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb);
486     EXPECT_EQ(OC_STACK_OK, result);
487
488     g_doneCB = false;
489     g_callbackResult = false;
490 }
491
492 TEST(PerformSecureResourceDiscovery, NullParam)
493 {
494     OCStackResult result = OC_STACK_ERROR;
495     OCProvisionDev_t* foundDevice = NULL;
496
497     OicUuid_t uuid;
498     ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
499
500     OIC_LOG(INFO, TAG, "Discovering Owned/Unowned Device using multicast\n");
501     result = OCDiscoverSingleDevice(DISCOVERY_TIMEOUT, &uuid, &foundDevice);
502     EXPECT_EQ(OC_STACK_OK, result);
503
504     int NumOfFoundDevice = 0;
505     OCProvisionDev_t* tempDev = foundDevice;
506     while(tempDev)
507     {
508         NumOfFoundDevice++;
509         tempDev = tempDev->next;
510     }
511     PMDeleteDeviceList(foundDevice);
512
513     EXPECT_EQ(true, NumOfFoundDevice > 0);
514 }
515
516 TEST(PerformUnownedDeviceDiscovery, NullParam)
517 {
518     OCStackResult result = OC_STACK_ERROR;
519
520     OIC_LOG(INFO, TAG, "Discovering Only Unowned Devices on Network..\n");
521     result = OCDiscoverUnownedDevices(DISCOVERY_TIMEOUT, &g_unownedDevices);
522     EXPECT_EQ(OC_STACK_OK, result);
523
524     RemoveUnknownDeviceFromDevList(g_unownedDevices);
525
526     int NumOfUnownDevice = 0;
527     OCProvisionDev_t* tempDev = g_unownedDevices;
528     while(tempDev)
529     {
530         NumOfUnownDevice++;
531         tempDev = tempDev->next;
532     }
533
534     //if two more devices are running on the subnet,
535     //stop the unittest process in order to prevent unittest failure.
536     if(2 < NumOfUnownDevice)
537     {
538         printf("%c[1;33;40m ******************************************************************\n", 0x1B);
539         printf("%c[1;33;40m   [%d] devices are running on the your sub-network.\n", 0x1B, NumOfUnownDevice);
540         printf("%c[1;33;40m   Please turn off the other devices, and then try once again.\n", 0x1B);
541         printf("%c[1;33;40m ******************************************************************", 0x1B);
542         printf("%c[1;0;40m \n", 0x1B);
543
544         OCStackResult result = OCStop();
545         EXPECT_EQ(OC_STACK_OK, result);
546
547         PMDeleteDeviceList(g_unownedDevices);
548         PMDeleteDeviceList(g_ownedDevices);
549         result = PDMClose();
550         EXPECT_EQ(OC_STACK_OK, result);
551
552         kill(g_myPID2, SIGKILL);
553         kill(g_myPID1, SIGKILL);
554
555         int interpreter_res1 = system("pkill -f \"sample_server1\"");
556         EXPECT_TRUE(0 <= interpreter_res1);
557         int interpreter_res2 = system("pkill -f \"sample_server2\"");
558         EXPECT_TRUE(0 <= interpreter_res2);
559
560         exit(0);
561     }
562
563     EXPECT_EQ(2, NumOfUnownDevice);
564 }
565
566 TEST(PerformJustWorksOxM, NullParam)
567 {
568     OCStackResult result = OC_STACK_ERROR;
569
570     OIC_LOG(INFO, TAG, "Try Ownership Transfer for Unowned Devices...\n");
571     result = OCDoOwnershipTransfer((void*)g_otmCtx, g_unownedDevices, ownershipTransferCB);
572     EXPECT_EQ(OC_STACK_OK, result);
573
574     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
575     {
576         OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
577         return;
578     }
579     OIC_LOG(INFO, TAG, "Registered Discovered Unowned Device...\n");
580
581     EXPECT_EQ(true, g_callbackResult);
582     EXPECT_EQ(true, g_doneCB);
583 }
584
585
586 TEST(PerformOwnedDeviceDiscovery, NullParam)
587 {
588     OCStackResult result = OC_STACK_ERROR;
589
590     OIC_LOG(INFO, TAG, "Discovering Only Owned Devices on Network..\n");
591     result = OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_ownedDevices);
592     EXPECT_EQ(OC_STACK_OK, result);
593
594     RemoveUnknownDeviceFromDevList(g_ownedDevices);
595
596     int NumOfOwnDevice = 0;
597     OCProvisionDev_t* tempDev = g_ownedDevices;
598     while(tempDev)
599     {
600         NumOfOwnDevice++;
601         tempDev = tempDev->next;
602     }
603
604     EXPECT_EQ(2 , NumOfOwnDevice);
605 }
606
607 TEST(PerformLinkDevices, NullParam)
608 {
609     OicUuid_t myUuid;
610     OCStackResult result = OC_STACK_ERROR;
611     result = GetDoxmDeviceID(&myUuid);
612     EXPECT_EQ(OC_STACK_OK, result);
613
614     //Extract target device except PT to perform link devices.
615     OCProvisionDev_t* dev1 = NULL;
616     OCProvisionDev_t* dev2 = NULL;
617     OCProvisionDev_t* tempDev = g_ownedDevices;
618
619     while(tempDev)
620     {
621         if(memcmp(tempDev->doxm->deviceID.id, myUuid.id, UUID_LENGTH) != 0)
622         {
623             if(NULL == dev1)
624             {
625                 dev1 = tempDev;
626             }
627             else if(NULL == dev2)
628             {
629                 dev2 = tempDev;
630                 break;
631             }
632         }
633         tempDev = tempDev->next;
634     }
635     EXPECT_TRUE(NULL != dev1);
636     EXPECT_TRUE(NULL != dev2);
637
638     // TODO: Pairwise provisioning (Cred & ACL)
639     // TODO: This part will be updated after ACL and credential data-structure is updated.
640
641     EXPECT_EQ(OC_STACK_OK, result);
642 }
643
644 TEST(PerformUnlinkDevices, NullParam)
645 {
646     OCStackResult result = OC_STACK_OK;
647
648     // TODO: Unlink devices
649     // TODO: This part will be updated after ACL and credential data-structure is updated.
650
651     EXPECT_EQ(OC_STACK_OK, result);
652 }
653
654 #ifdef MULTIPLE_OWNER
655 TEST(RegisterPreconfiguredPIN, NullParam)
656 {
657     OCStackResult result = SetPreconfigPin("12341234", strlen("12341234"));
658     EXPECT_EQ(OC_STACK_OK, result);
659 }
660
661 TEST(EnableMOT, NullParam)
662 {
663     OCStackResult result = OC_STACK_OK;
664
665     if(NULL == g_ownedDevices)
666     {
667         OIC_LOG(INFO, TAG, "Discovering Only Owned Devices on Network..\n");
668         result = OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_ownedDevices);
669         EXPECT_EQ(OC_STACK_OK, result);
670         RemoveUnknownDeviceFromDevList(g_ownedDevices);
671     }
672     EXPECT_NE((OCProvisionDev_t*)NULL, g_ownedDevices);
673
674     g_doneCB = false;
675     result = OCChangeMOTMode(NULL, g_ownedDevices, OIC_MULTIPLE_OWNER_ENABLE, updateDoxmForMOTCB);
676     EXPECT_EQ(OC_STACK_OK, result);
677     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
678     {
679         OIC_LOG(ERROR, TAG, "OCChangeMOTMode callback error");
680         return;
681     }
682
683     EXPECT_TRUE(g_callbackResult);
684 }
685
686 TEST(DiscoverMOTEnabledDevices, NullParam)
687 {
688     OCStackResult result = OC_STACK_OK;
689
690     if(g_motEnabledDevices)
691     {
692         PMDeleteDeviceList(g_motEnabledDevices);
693     }
694
695     OIC_LOG(INFO, TAG, "Discovering MOT Enabled Devices on Network..\n");
696     result = OCDiscoverMultipleOwnerEnabledDevices(DISCOVERY_TIMEOUT, &g_motEnabledDevices);
697     EXPECT_EQ(OC_STACK_OK, result);
698     RemoveUnknownDeviceFromDevList(g_motEnabledDevices);
699     EXPECT_NE((OCProvisionDev_t*)NULL, g_motEnabledDevices);
700 }
701
702 TEST(ProvisonPreconfiguredPIN, NullParam)
703 {
704     OCStackResult result = OC_STACK_OK;
705
706     g_doneCB = false;
707     result = OCProvisionPreconfigPin(NULL, g_motEnabledDevices, "12341234", strlen("12341234"), provisionPreconfiguredPinCB);
708     EXPECT_EQ(OC_STACK_OK, result);
709     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
710     {
711         OIC_LOG(ERROR, TAG, "OCProvisionPreconfigPin callback error");
712         return;
713     }
714
715     EXPECT_EQ(true, g_callbackResult);
716 }
717
718 TEST(SelectMOTMethod, NullParam)
719 {
720     OCStackResult result = OC_STACK_OK;
721
722     g_doneCB = false;
723     result = OCSelectMOTMethod(NULL, g_motEnabledDevices, OIC_PRECONFIG_PIN, updateDoxmForMOTCB);
724     EXPECT_EQ(OC_STACK_OK, result);
725     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
726     {
727         OIC_LOG(ERROR, TAG, "OCSelectMOTMethod callback error");
728         return;
729     }
730
731     EXPECT_EQ(true, g_callbackResult);
732 }
733
734 // TODO: Need to new server to perform MOT
735 /*
736 TEST(PerformMOT, NullParam)
737 {
738     OCStackResult result = OC_STACK_OK;
739
740     g_doneCB = false;
741     result = OCDoMultipleOwnershipTransfer(NULL, g_motEnabledDevices, ownershipTransferCB);
742     EXPECT_EQ(OC_STACK_OK, result);
743     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
744     {
745         OIC_LOG(ERROR, TAG, "OCDoMultipleOwnershipTransfer callback error");
746         return;
747     }
748     EXPECT_EQ(true, g_callbackResult);
749 }
750
751 TEST(DiscoverMultipleOwnedDevices, NullParam)
752 {
753     OCStackResult result = OC_STACK_OK;
754
755     if(g_multiplOwnedDevices)
756     {
757         PMDeleteDeviceList(g_multiplOwnedDevices);
758     }
759
760     OIC_LOG(INFO, TAG, "Discovering MOT Enabled Devices on Network..\n");
761     result = OCDiscoverMultipleOwnedDevices(DISCOVERY_TIMEOUT, &g_multiplOwnedDevices);
762     EXPECT_EQ(OC_STACK_OK, result);
763     RemoveUnknownDeviceFromDevList(g_multiplOwnedDevices);
764     EXPECT_TRUE(NULL != g_multiplOwnedDevices);
765 }*/
766
767 #endif //MULTIPLE_OWNER
768
769 TEST(PerformRemoveDevice, NullParam)
770 {
771     OicUuid_t myUuid;
772     OCStackResult result = OC_STACK_ERROR;
773     result = GetDoxmDeviceID(&myUuid);
774     EXPECT_EQ(OC_STACK_OK, result);
775
776     //Extract target device except PT to perform remove device.
777     OCProvisionDev_t* removeDev = g_ownedDevices;
778     while(removeDev)
779     {
780         if(memcmp(removeDev->doxm->deviceID.id, myUuid.id, UUID_LENGTH) != 0)
781         {
782             break;
783         }
784         removeDev = removeDev->next;
785     }
786     EXPECT_TRUE(NULL != removeDev);
787
788     g_doneCB = false;
789     g_callbackResult = false;
790
791     result = OCRemoveDevice((void*)g_otmCtx, DISCOVERY_TIMEOUT, removeDev, removeDeviceCB);
792     EXPECT_EQ(OC_STACK_OK, result);
793     EXPECT_EQ(true, g_callbackResult);
794     EXPECT_EQ(true, g_doneCB);
795 }
796
797 TEST(FinalizeOTMTest, NullParam)
798 {
799     OCStackResult result = OCStop();
800     EXPECT_EQ(OC_STACK_OK, result);
801
802     PMDeleteDeviceList(g_unownedDevices);
803     PMDeleteDeviceList(g_ownedDevices);
804     result = PDMClose();
805     EXPECT_EQ(OC_STACK_OK, result);
806
807     kill(g_myPID2, SIGKILL);
808     kill(g_myPID1, SIGKILL);
809
810     int interpreter_res1 = system("pkill -f \"sample_server1\"");
811     EXPECT_TRUE(0 <= interpreter_res1);
812     int interpreter_res2 = system("pkill -f \"sample_server2\"");
813     EXPECT_TRUE(0 <= interpreter_res2);
814 }
815