1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "UnitTestHelper.h"
24 #include "OCPlatform.h"
26 #include "oic_string.h"
29 #include "EasySetup.h"
30 #include "RemoteEnrollee.h"
31 #include "ESException.h"
36 #include <condition_variable>
38 using namespace OIC::Service;
40 const std::string ipaddress = "1.1.1.1";
41 const std::string ssid = "test";
42 const std::string pwd = "test@123";
44 class MediatorRichTest: public TestWithMock
49 TestWithMock::SetUp();
54 TestWithMock::TearDown();
59 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > /*easySetupStatus*/)
63 /* Test cases for easysetyup class*/
66 TEST_F(MediatorRichTest, testGetInstance)
68 EasySetup *easySetupIntance1 = EasySetup::getInstance();
69 EasySetup *easySetupIntance2 = EasySetup::getInstance();
71 EXPECT_EQ(easySetupIntance1, easySetupIntance2);
74 //createEnrolleeDevice [positive]
75 TEST_F(MediatorRichTest, testCreateEnrolleeDevice)
77 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
79 WiFiOnboadingConnection onboardingConn;
81 netInfo.connType = CT_ADAPTER_IP;
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());
86 onboardingConn.isSecured = false;
87 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
89 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
91 EXPECT_TRUE(remoteEnrollee != NULL);
94 //createEnrolleeDevice [Negative]
95 TEST_F(MediatorRichTest, testCreateEnrolleeDeviceNegative)
97 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
99 WiFiOnboadingConnection onboardingConn;
100 EasySetup *easysetupInstance = EasySetup::getInstance();
102 netInfo.connType = CT_ADAPTER_IP;
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());
107 onboardingConn.isSecured = false;
108 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
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));
116 //createEnrolleeDevice [positive (creating the two RemoteEnrolleeDevice with different config)]
117 TEST_F(MediatorRichTest, testCreateEnrolleeMultipleDevice)
119 RemoteEnrollee::shared_ptr remoteEnrollee1 = NULL;
120 RemoteEnrollee::shared_ptr remoteEnrollee2 = NULL;
121 WiFiOnboadingConnection onboardingConn1;
122 WiFiOnboadingConnection onboardingConn2;
125 std::string ipaddress2 = "0.0.0.0";
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());
132 onboardingConn1.isSecured = false;
133 OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
134 remoteEnrollee1 = EasySetup::getInstance()->createEnrolleeDevice(netInfo1, onboardingConn1);
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());
141 onboardingConn1.isSecured = false;
142 OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress2.c_str());
143 remoteEnrollee2 = EasySetup::getInstance()->createEnrolleeDevice(netInfo2, onboardingConn2);
145 EXPECT_TRUE(remoteEnrollee1 != NULL);
146 EXPECT_TRUE(remoteEnrollee2 != NULL);
149 /*Test cases for RemoteEnrollee class*/
151 //registerEasySetupStatusHandler [positive]
152 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandler)
154 //creation of the Remote Enrollee Device
155 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
157 WiFiOnboadingConnection onboardingConn;
159 netInfo.connType = CT_ADAPTER_IP;
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());
164 onboardingConn.isSecured = false;
165 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
167 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
170 EXPECT_NO_THROW( remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback));
173 //registerEasySetupStatusHandler [Negative]
174 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandlerNegative)
176 //creation of the Remote Enrollee Device
177 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
179 WiFiOnboadingConnection onboardingConn;
181 netInfo.connType = CT_ADAPTER_IP;
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());
186 onboardingConn.isSecured = false;
187 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
189 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
192 EXPECT_ANY_THROW( remoteEnrollee->registerEasySetupStatusHandler(NULL));
195 //isEnrolleeProvisioned
196 TEST_F(MediatorRichTest, testIsEnrolleeProvisioned)
198 //creation of the Remote Enrollee Device
199 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
201 WiFiOnboadingConnection onboardingConn;
203 netInfo.connType = CT_ADAPTER_IP;
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());
208 onboardingConn.isSecured = false;
209 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
211 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
214 bool isEnrolleeProvisioned = remoteEnrollee->isEnrolleeProvisioned();
216 EXPECT_TRUE(false == isEnrolleeProvisioned);
219 //startProvisioning [positive]
220 TEST_F(MediatorRichTest, testStartProvisioning)
222 //creation of the Remote Enrollee Device
223 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
225 WiFiOnboadingConnection onboardingConn;
227 netInfo.connType = CT_ADAPTER_IP;
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());
232 onboardingConn.isSecured = false;
233 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
235 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
236 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
238 #ifdef REMOTE_ARDUINO_ENROLEE
239 EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
242 /* It will throw the exception
243 * as it will not able to discover the provisioning resource in the network
245 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
249 //startProvisioning [Negative]
250 TEST_F(MediatorRichTest, testStartProvisioningNegative)
252 //creation of the Remote Enrollee Device
253 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
255 WiFiOnboadingConnection onboardingConn;
257 netInfo.connType = CT_ADAPTER_IP;
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());
262 onboardingConn.isSecured = false;
263 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
265 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
267 /* We are not registering the EasySetupStatusHandler, so startProvisioning
268 * will throw the Exception & we are checking the same
270 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
273 //stopProvisioning [positive]
274 TEST_F(MediatorRichTest, testStopProvisioning)
276 //creation of the Remote Enrollee Device
277 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
279 WiFiOnboadingConnection onboardingConn;
281 netInfo.connType = CT_ADAPTER_IP;
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());
286 onboardingConn.isSecured = false;
287 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
289 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
290 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
292 #ifdef REMOTE_ARDUINO_ENROLEE
293 EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
294 EXPECT_NO_THROW(remoteEnrollee->stopProvisioning());
297 /* It will throw the exception as
298 * it will not able to discover the provisioning resource in the network
300 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
302 /* It will throw an exception
303 * as internally resource will be empty [startProvisioning is not succeed]
305 EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());
309 //stopProvisioning [Negative]
310 TEST_F(MediatorRichTest, testStopProvisioningNegative)
312 //creation of the Remote Enrollee Device
313 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
315 WiFiOnboadingConnection onboardingConn;
317 netInfo.connType = CT_ADAPTER_IP;
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());
322 onboardingConn.isSecured = false;
323 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
325 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
326 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
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.
331 EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());