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