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 //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));
108 //createEnrolleeDevice [positive (creating the two RemoteEnrolleeDevice with different config)]
109 TEST_F(MediatorRichTest, testCreateEnrolleeMultipleDevice)
111 RemoteEnrollee::shared_ptr remoteEnrollee1 = NULL;
112 RemoteEnrollee::shared_ptr remoteEnrollee2 = NULL;
113 WiFiOnboadingConnection onboardingConn1;
114 WiFiOnboadingConnection onboardingConn2;
117 std::string ipaddress2 = "0.0.0.0";
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());
124 onboardingConn1.isSecured = false;
125 OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
126 remoteEnrollee1 = EasySetup::getInstance()->createEnrolleeDevice(netInfo1, onboardingConn1);
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());
133 onboardingConn1.isSecured = false;
134 OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress2.c_str());
135 remoteEnrollee2 = EasySetup::getInstance()->createEnrolleeDevice(netInfo2, onboardingConn2);
137 EXPECT_TRUE(remoteEnrollee1 != NULL);
138 EXPECT_TRUE(remoteEnrollee2 != NULL);
141 /*Test cases for RemoteEnrollee class*/
143 //registerEasySetupStatusHandler [positive]
144 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandler)
146 //creation of the Remote Enrollee Device
147 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
149 WiFiOnboadingConnection onboardingConn;
151 netInfo.connType = CT_ADAPTER_IP;
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());
156 onboardingConn.isSecured = false;
157 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
159 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
162 EXPECT_NO_THROW( remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback));
165 //registerEasySetupStatusHandler [Negative]
166 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandlerNegative)
168 //creation of the Remote Enrollee Device
169 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
171 WiFiOnboadingConnection onboardingConn;
173 netInfo.connType = CT_ADAPTER_IP;
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());
178 onboardingConn.isSecured = false;
179 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
181 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
184 EXPECT_ANY_THROW( remoteEnrollee->registerEasySetupStatusHandler(NULL));
187 //isEnrolleeProvisioned
188 TEST_F(MediatorRichTest, testIsEnrolleeProvisioned)
190 //creation of the Remote Enrollee Device
191 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
193 WiFiOnboadingConnection onboardingConn;
195 netInfo.connType = CT_ADAPTER_IP;
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());
200 onboardingConn.isSecured = false;
201 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
203 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
206 bool isEnrolleeProvisioned = remoteEnrollee->isEnrolleeProvisioned();
208 EXPECT_TRUE(false == isEnrolleeProvisioned);
211 //startProvisioning [positive]
212 TEST_F(MediatorRichTest, testStartProvisioning)
214 //creation of the Remote Enrollee Device
215 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
217 WiFiOnboadingConnection onboardingConn;
219 netInfo.connType = CT_ADAPTER_IP;
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());
224 onboardingConn.isSecured = false;
225 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
227 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
228 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
230 #ifdef REMOTE_ARDUINO_ENROLEE
231 EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
234 /* It will throw the exception
235 * as it will not able to discover the provisioning resource in the network
237 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
241 //startProvisioning [Negative]
242 TEST_F(MediatorRichTest, testStartProvisioningNegative)
244 //creation of the Remote Enrollee Device
245 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
247 WiFiOnboadingConnection onboardingConn;
249 netInfo.connType = CT_ADAPTER_IP;
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());
254 onboardingConn.isSecured = false;
255 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
257 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
259 /* We are not registering the EasySetupStatusHandler, so startProvisioning
260 * will throw the Exception & we are checking the same
262 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
265 //stopProvisioning [positive]
266 TEST_F(MediatorRichTest, testStopProvisioning)
268 //creation of the Remote Enrollee Device
269 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
271 WiFiOnboadingConnection onboardingConn;
273 netInfo.connType = CT_ADAPTER_IP;
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());
278 onboardingConn.isSecured = false;
279 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
281 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
282 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
284 #ifdef REMOTE_ARDUINO_ENROLEE
285 EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
286 EXPECT_NO_THROW(remoteEnrollee->stopProvisioning());
289 /* It will throw the exception as
290 * it will not able to discover the provisioning resource in the network
292 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
294 /* It will throw an exception
295 * as internally resource will be empty [startProvisioning is not succeed]
297 EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());
301 //stopProvisioning [Negative]
302 TEST_F(MediatorRichTest, testStopProvisioningNegative)
304 //creation of the Remote Enrollee Device
305 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
307 WiFiOnboadingConnection onboardingConn;
309 netInfo.connType = CT_ADAPTER_IP;
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());
314 onboardingConn.isSecured = false;
315 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
317 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
318 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
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.
323 EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());