Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / unittests / MediatorRichTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 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
21 #include "UnitTestHelper.h"
22
23 #include "ocstack.h"
24 #include "OCPlatform.h"
25 #include "OCApi.h"
26 #include "oic_string.h"
27 #include "logger.h"
28
29 #include "EasySetup.h"
30 #include "RemoteEnrollee.h"
31 #include "ESException.h"
32
33 #include <iostream>
34 #include <functional>
35 #include <pthread.h>
36 #include <condition_variable>
37
38 using namespace OIC::Service;
39
40 const std::string  ipaddress = "1.1.1.1";
41 const std::string  ssid = "test";
42 const std::string  pwd = "test@123";
43
44 class MediatorRichTest: public TestWithMock
45 {
46     protected:
47         void SetUp()
48         {
49             TestWithMock::SetUp();
50         }
51
52         void TearDown()
53         {
54             TestWithMock::TearDown();
55         }
56 };
57
58 //callbacks
59 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > /*easySetupStatus*/)
60 {
61
62
63 }
64
65 /* Test cases for easysetyup class*/
66
67 //getInstance
68 TEST_F(MediatorRichTest, testGetInstance)
69 {
70     EasySetup *easySetupIntance1 = EasySetup::getInstance();
71     EasySetup *easySetupIntance2 = EasySetup::getInstance();
72
73     EXPECT_EQ(easySetupIntance1, easySetupIntance2);
74 }
75
76 //createEnrolleeDevice [positive]
77 TEST_F(MediatorRichTest, testCreateEnrolleeDevice)
78 {
79     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
80     ProvConfig netInfo;
81     WiFiOnboadingConnection onboardingConn;
82
83     netInfo.connType = CT_ADAPTER_IP;
84
85     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
86     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
87
88     onboardingConn.isSecured = false;
89     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
90
91     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
92
93     EXPECT_TRUE(remoteEnrollee != NULL);
94 }
95
96 //createEnrolleeDevice [Negative]
97 TEST_F(MediatorRichTest, testCreateEnrolleeDeviceNegative)
98 {
99     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
100     ProvConfig netInfo;
101     WiFiOnboadingConnection onboardingConn;
102     EasySetup *easysetupInstance = EasySetup::getInstance();
103
104     netInfo.connType = CT_ADAPTER_IP;
105
106     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
107     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
108
109     onboardingConn.isSecured = false;
110     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
111
112     //calling the createEnrolleeDevice
113     remoteEnrollee = easysetupInstance->createEnrolleeDevice(netInfo, onboardingConn);
114     //calling again the createEnrolleeDevice with same config & connection info [API throws the exception]
115     EXPECT_ANY_THROW(easysetupInstance->createEnrolleeDevice(netInfo, onboardingConn));
116 }
117
118 //createEnrolleeDevice [positive (creating the two RemoteEnrolleeDevice with different config)]
119 TEST_F(MediatorRichTest, testCreateEnrolleeMultipleDevice)
120 {
121     RemoteEnrollee::shared_ptr remoteEnrollee1 = NULL;
122     RemoteEnrollee::shared_ptr remoteEnrollee2 = NULL;
123     WiFiOnboadingConnection onboardingConn1;
124     WiFiOnboadingConnection onboardingConn2;
125     ProvConfig netInfo1;
126     ProvConfig netInfo2;
127     std::string  ipaddress2 = "0.0.0.0";
128
129     //calling the API for the first device
130     netInfo1.connType = CT_ADAPTER_IP;
131     OICStrcpy(netInfo1.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
132     OICStrcpy(netInfo1.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
133
134     onboardingConn1.isSecured = false;
135     OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
136     remoteEnrollee1 = EasySetup::getInstance()->createEnrolleeDevice(netInfo1, onboardingConn1);
137
138     //calling the API for the second device
139     netInfo1.connType = CT_ADAPTER_IP;
140     OICStrcpy(netInfo1.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
141     OICStrcpy(netInfo1.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
142
143     onboardingConn1.isSecured = false;
144     OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress2.c_str());
145     remoteEnrollee2 = EasySetup::getInstance()->createEnrolleeDevice(netInfo2, onboardingConn2);
146
147     EXPECT_TRUE(remoteEnrollee1 != NULL);
148     EXPECT_TRUE(remoteEnrollee2 != NULL);
149 }
150
151 /*Test cases for RemoteEnrollee class*/
152
153 //registerEasySetupStatusHandler [positive]
154 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandler)
155 {
156     //creation of the Remote Enrollee Device
157     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
158     ProvConfig netInfo;
159     WiFiOnboadingConnection onboardingConn;
160
161     netInfo.connType = CT_ADAPTER_IP;
162
163     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
164     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
165
166     onboardingConn.isSecured = false;
167     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
168
169     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
170
171     //calling the API
172     EXPECT_NO_THROW( remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback));
173 }
174
175 //registerEasySetupStatusHandler [Negative]
176 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandlerNegative)
177 {
178     //creation of the Remote Enrollee Device
179     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
180     ProvConfig netInfo;
181     WiFiOnboadingConnection onboardingConn;
182
183     netInfo.connType = CT_ADAPTER_IP;
184
185     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
186     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
187
188     onboardingConn.isSecured = false;
189     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
190
191     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
192
193     //calling the API
194     EXPECT_ANY_THROW( remoteEnrollee->registerEasySetupStatusHandler(NULL));
195 }
196
197 //isEnrolleeProvisioned
198 TEST_F(MediatorRichTest, testIsEnrolleeProvisioned)
199 {
200     //creation of the Remote Enrollee Device
201     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
202     ProvConfig netInfo;
203     WiFiOnboadingConnection onboardingConn;
204
205     netInfo.connType = CT_ADAPTER_IP;
206
207     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
208     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
209
210     onboardingConn.isSecured = false;
211     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
212
213     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
214
215     //calling the API
216     bool isEnrolleeProvisioned = remoteEnrollee->isEnrolleeProvisioned();
217
218     EXPECT_TRUE(false == isEnrolleeProvisioned);
219 }
220
221 //startProvisioning [positive]
222 TEST_F(MediatorRichTest, testStartProvisioning)
223 {
224     //creation of the Remote Enrollee Device
225     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
226     ProvConfig netInfo;
227     WiFiOnboadingConnection onboardingConn;
228
229     netInfo.connType = CT_ADAPTER_IP;
230
231     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
232     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
233
234     onboardingConn.isSecured = false;
235     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
236
237     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
238     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
239
240 #ifdef REMOTE_ARDUINO_ENROLEE
241     EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
242
243 #else
244     /* It will throw the exception
245      * as it will not able to discover the provisioning resource in the network
246      */
247     EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
248 #endif
249 }
250
251 //startProvisioning [Negative]
252 TEST_F(MediatorRichTest, testStartProvisioningNegative)
253 {
254     //creation of the Remote Enrollee Device
255     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
256     ProvConfig netInfo;
257     WiFiOnboadingConnection onboardingConn;
258
259     netInfo.connType = CT_ADAPTER_IP;
260
261     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
262     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
263
264     onboardingConn.isSecured = false;
265     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
266
267     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
268
269     /* We are not registering the EasySetupStatusHandler, so startProvisioning
270      * will throw the Exception &  we are checking the same
271      */
272     EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
273 }
274
275 //stopProvisioning [positive]
276 TEST_F(MediatorRichTest, testStopProvisioning)
277 {
278     //creation of the Remote Enrollee Device
279     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
280     ProvConfig netInfo;
281     WiFiOnboadingConnection onboardingConn;
282
283     netInfo.connType = CT_ADAPTER_IP;
284
285     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
286     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
287
288     onboardingConn.isSecured = false;
289     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
290
291     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
292     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
293
294 #ifdef REMOTE_ARDUINO_ENROLEE
295         EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
296         EXPECT_NO_THROW(remoteEnrollee->stopProvisioning());
297
298 #else
299     /* It will throw the exception as
300      * it will not able to discover the provisioning resource in the network
301      */
302      EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
303
304     /* It will throw an exception
305      * as internally resource will be empty [startProvisioning is not succeed]
306      */
307     EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());
308 #endif
309 }
310
311 //stopProvisioning [Negative]
312 TEST_F(MediatorRichTest, testStopProvisioningNegative)
313 {
314     //creation of the Remote Enrollee Device
315     RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
316     ProvConfig netInfo;
317     WiFiOnboadingConnection onboardingConn;
318
319     netInfo.connType = CT_ADAPTER_IP;
320
321     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
322     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
323
324     onboardingConn.isSecured = false;
325     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
326
327     remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
328     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
329
330     /* we didn't call the start provisioning API and directly calling stopProvisioning API.
331      * In this case API will throw the exception & we are checking the same.
332      */
333     EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());
334 }
335