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*/)
65 /* Test cases for easysetyup class*/
68 TEST_F(MediatorRichTest, testGetInstance)
70 EasySetup *easySetupIntance1 = EasySetup::getInstance();
71 EasySetup *easySetupIntance2 = EasySetup::getInstance();
73 EXPECT_EQ(easySetupIntance1, easySetupIntance2);
76 //createEnrolleeDevice [positive]
77 TEST_F(MediatorRichTest, testCreateEnrolleeDevice)
79 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
81 WiFiOnboadingConnection onboardingConn;
83 netInfo.connType = CT_ADAPTER_IP;
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());
88 onboardingConn.isSecured = false;
89 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
91 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
93 EXPECT_TRUE(remoteEnrollee != NULL);
96 //createEnrolleeDevice [Negative]
97 TEST_F(MediatorRichTest, testCreateEnrolleeDeviceNegative)
99 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
101 WiFiOnboadingConnection onboardingConn;
102 EasySetup *easysetupInstance = EasySetup::getInstance();
104 netInfo.connType = CT_ADAPTER_IP;
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());
109 onboardingConn.isSecured = false;
110 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
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));
118 //createEnrolleeDevice [positive (creating the two RemoteEnrolleeDevice with different config)]
119 TEST_F(MediatorRichTest, testCreateEnrolleeMultipleDevice)
121 RemoteEnrollee::shared_ptr remoteEnrollee1 = NULL;
122 RemoteEnrollee::shared_ptr remoteEnrollee2 = NULL;
123 WiFiOnboadingConnection onboardingConn1;
124 WiFiOnboadingConnection onboardingConn2;
127 std::string ipaddress2 = "0.0.0.0";
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());
134 onboardingConn1.isSecured = false;
135 OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
136 remoteEnrollee1 = EasySetup::getInstance()->createEnrolleeDevice(netInfo1, onboardingConn1);
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());
143 onboardingConn1.isSecured = false;
144 OICStrcpy(onboardingConn1.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress2.c_str());
145 remoteEnrollee2 = EasySetup::getInstance()->createEnrolleeDevice(netInfo2, onboardingConn2);
147 EXPECT_TRUE(remoteEnrollee1 != NULL);
148 EXPECT_TRUE(remoteEnrollee2 != NULL);
151 /*Test cases for RemoteEnrollee class*/
153 //registerEasySetupStatusHandler [positive]
154 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandler)
156 //creation of the Remote Enrollee Device
157 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
159 WiFiOnboadingConnection onboardingConn;
161 netInfo.connType = CT_ADAPTER_IP;
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());
166 onboardingConn.isSecured = false;
167 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
169 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
172 EXPECT_NO_THROW( remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback));
175 //registerEasySetupStatusHandler [Negative]
176 TEST_F(MediatorRichTest, testRegisterEasySetupStatusHandlerNegative)
178 //creation of the Remote Enrollee Device
179 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
181 WiFiOnboadingConnection onboardingConn;
183 netInfo.connType = CT_ADAPTER_IP;
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());
188 onboardingConn.isSecured = false;
189 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
191 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
194 EXPECT_ANY_THROW( remoteEnrollee->registerEasySetupStatusHandler(NULL));
197 //isEnrolleeProvisioned
198 TEST_F(MediatorRichTest, testIsEnrolleeProvisioned)
200 //creation of the Remote Enrollee Device
201 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
203 WiFiOnboadingConnection onboardingConn;
205 netInfo.connType = CT_ADAPTER_IP;
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());
210 onboardingConn.isSecured = false;
211 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
213 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
216 bool isEnrolleeProvisioned = remoteEnrollee->isEnrolleeProvisioned();
218 EXPECT_TRUE(false == isEnrolleeProvisioned);
221 //startProvisioning [positive]
222 TEST_F(MediatorRichTest, testStartProvisioning)
224 //creation of the Remote Enrollee Device
225 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
227 WiFiOnboadingConnection onboardingConn;
229 netInfo.connType = CT_ADAPTER_IP;
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());
234 onboardingConn.isSecured = false;
235 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
237 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
238 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
240 #ifdef REMOTE_ARDUINO_ENROLEE
241 EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
244 /* It will throw the exception
245 * as it will not able to discover the provisioning resource in the network
247 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
251 //startProvisioning [Negative]
252 TEST_F(MediatorRichTest, testStartProvisioningNegative)
254 //creation of the Remote Enrollee Device
255 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
257 WiFiOnboadingConnection onboardingConn;
259 netInfo.connType = CT_ADAPTER_IP;
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());
264 onboardingConn.isSecured = false;
265 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
267 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
269 /* We are not registering the EasySetupStatusHandler, so startProvisioning
270 * will throw the Exception & we are checking the same
272 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
275 //stopProvisioning [positive]
276 TEST_F(MediatorRichTest, testStopProvisioning)
278 //creation of the Remote Enrollee Device
279 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
281 WiFiOnboadingConnection onboardingConn;
283 netInfo.connType = CT_ADAPTER_IP;
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());
288 onboardingConn.isSecured = false;
289 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
291 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
292 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
294 #ifdef REMOTE_ARDUINO_ENROLEE
295 EXPECT_NO_THROW(remoteEnrollee->startProvisioning());
296 EXPECT_NO_THROW(remoteEnrollee->stopProvisioning());
299 /* It will throw the exception as
300 * it will not able to discover the provisioning resource in the network
302 EXPECT_ANY_THROW(remoteEnrollee->startProvisioning());
304 /* It will throw an exception
305 * as internally resource will be empty [startProvisioning is not succeed]
307 EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());
311 //stopProvisioning [Negative]
312 TEST_F(MediatorRichTest, testStopProvisioningNegative)
314 //creation of the Remote Enrollee Device
315 RemoteEnrollee::shared_ptr remoteEnrollee = NULL;
317 WiFiOnboadingConnection onboardingConn;
319 netInfo.connType = CT_ADAPTER_IP;
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());
324 onboardingConn.isSecured = false;
325 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
327 remoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo, onboardingConn);
328 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
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.
333 EXPECT_ANY_THROW(remoteEnrollee->stopProvisioning());