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