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 <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
25 #include <condition_variable>
29 #include "ESEnrolleeSimulator.h"
31 #include "ESRichCommon.h"
32 #include "EasySetup.h"
33 #include "RemoteEnrollee.h"
35 #include "ESEnrolleeCommon.h"
36 #include "easysetup.h"
38 #define PROV_RESOURCE_TYPE "ocf.wk.prov"
41 using namespace OIC::Service;
45 std::atomic_bool g_isStartedStack(false);
47 std::chrono::milliseconds g_waitForResponse(1000);
49 std::condition_variable responseCon;
50 std::mutex mutexForCondition;
52 ESEnrolleeSimulator g_enrolleeSimul;
53 std::shared_ptr<RemoteEnrollee> g_remoteEnrollee;
56 class TestWithMock: public testing::Test
62 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
66 virtual void TearDown()
80 class EasysetupMediatorTest : public TestWithMock
83 std::shared_ptr<OC::OCResource> m_enrolleeResource;
86 EasysetupMediatorTest() = default;
87 ~EasysetupMediatorTest() = default;
89 std::shared_ptr<OC::OCResource> CreateNotProvResource()
91 OCConnectivityType connectivityType = CT_DEFAULT;
92 std::vector<std::string> types = {"ocf.wk.notprov"};
93 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
95 return OCPlatform::constructResourceObject("coap://192.168.1.2:5000",
96 "/NotProvisioningResURI",
103 void discoverRemoteEnrollee()
105 std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
106 OC::OCPlatform::findResource("", uri,
107 OCConnectivityType::CT_DEFAULT,
108 std::bind(&EasysetupMediatorTest::discoverRemoteEnrolleeCb,
109 this, std::placeholders::_1));
111 std::unique_lock< std::mutex > lock{ mutexForCondition };
112 responseCon.wait_for(lock, g_waitForResponse);
118 TestWithMock::SetUp();
119 if (g_isStartedStack == false)
121 if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
123 printf("OCStack init error!!\n");
126 g_enrolleeSimul.initEnrollee();
127 g_isStartedStack = true;
133 TestWithMock::TearDown();
137 void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
139 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE))
141 m_enrolleeResource = resource;
146 TEST_F(EasysetupMediatorTest, createremoteenrolleeFailedWithNotProvResource)
148 auto remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(CreateNotProvResource());
150 EXPECT_EQ(nullptr, remoteEnrollee);
153 TEST_F(EasysetupMediatorTest, createremoteenrolleeSucceedWithProvResource)
155 discoverRemoteEnrollee();
156 g_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(m_enrolleeResource);
158 ASSERT_NE(nullptr, g_remoteEnrollee);
161 class GetConfigurationTest : public EasysetupMediatorTest
164 GetConfigurationTest() = default;
165 ~GetConfigurationTest() = default;
167 static void onGetConfigurationCb(shared_ptr< GetConfigurationStatus > /*status*/)
174 TestWithMock::SetUp();
179 TestWithMock::TearDown();
183 TEST_F(GetConfigurationTest, ThrowExceptionWhenGetConfigurationFailedByCallbackIsNull)
185 EXPECT_ANY_THROW(g_remoteEnrollee->getConfiguration(nullptr));
188 TEST_F(GetConfigurationTest, GetConfigurationSucceed)
190 bool isWellConstructed = false;
192 g_enrolleeSimul.setDeviceProperty();
194 mocks.ExpectCallFunc(onGetConfigurationCb).Do(
195 [&isWellConstructed](std::shared_ptr< GetConfigurationStatus > status)
197 if(status->getESResult() == ES_OK)
199 EnrolleeConf conf = status->getEnrolleeConf();
200 if(!conf.getWiFiModes().empty())
202 if(conf.getWiFiModes().at(0) == WIFI_11G &&
203 conf.getWiFiFreq() == WIFI_5G &&
204 !strcmp(conf.getDeviceName().c_str(), "Test Device"))
206 isWellConstructed = true;
212 g_remoteEnrollee->getConfiguration(onGetConfigurationCb);
214 std::unique_lock< std::mutex > lock{ mutexForCondition };
215 responseCon.wait_for(lock, g_waitForResponse);
217 EXPECT_TRUE(isWellConstructed);
220 class GetStatusTest : public EasysetupMediatorTest
224 GetStatusTest() = default;
225 ~GetStatusTest() = default;
227 static void onGetStatusCb(shared_ptr< GetEnrolleeStatus > /*status*/)
234 TestWithMock::SetUp();
239 TestWithMock::TearDown();
243 TEST_F(GetStatusTest, ThrowExceptionWhenGetStatusFailedByCallbackIsNull)
245 EXPECT_ANY_THROW(g_remoteEnrollee->getStatus(nullptr));
248 TEST_F(GetStatusTest, GetStatusSucceed)
250 g_enrolleeSimul.setESState();
251 g_enrolleeSimul.setESErrorCode();
253 bool isWellConstructed = false;
254 mocks.ExpectCallFunc(onGetStatusCb).Do(
255 [&isWellConstructed](std::shared_ptr< GetEnrolleeStatus > status)
257 if(status->getESResult() == ES_OK)
259 EnrolleeStatus enrolleeStatus = status->getEnrolleeStatus();
261 if(enrolleeStatus.getProvStatus() == ES_STATE_CONNECTED_TO_ENROLLER &&
262 enrolleeStatus.getLastErrCode() == ES_ERRCODE_NO_INTERNETCONNECTION)
264 isWellConstructed = true;
269 g_remoteEnrollee->getStatus(onGetStatusCb);
271 std::unique_lock< std::mutex > lock{ mutexForCondition };
272 responseCon.wait_for(lock, g_waitForResponse);
274 EXPECT_TRUE(isWellConstructed);
277 class ProvisionDevicePropertiesTest : public EasysetupMediatorTest
281 ProvisionDevicePropertiesTest() = default;
282 ~ProvisionDevicePropertiesTest() = default;
284 static void deviceProvisioningStatusCb(
285 shared_ptr< DevicePropProvisioningStatus > /*status*/)
292 TestWithMock::SetUp();
297 TestWithMock::TearDown();
301 TEST_F(ProvisionDevicePropertiesTest,
302 ThrowExceptionWhenProvisionDeviceProperiesFailedByCallbackIsNull)
305 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
306 devProp.setDevConfProp("korean", "Korea");
308 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp, nullptr));
311 TEST_F(ProvisionDevicePropertiesTest,
312 ThrowExceptionWhenProvisionDeviceProperiesFailedWithoutSSID)
315 devProp.setWiFiProp("", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
316 devProp.setDevConfProp("korean", "Korea");
317 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp,
318 deviceProvisioningStatusCb));
321 TEST_F(ProvisionDevicePropertiesTest,
322 ProvisionDeviceProperiesSucceed)
325 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
326 devProp.setDevConfProp("korean", "Korea");
328 int cntForReceivedCallbackWithSuccess = 0;
330 mocks.OnCallFunc(deviceProvisioningStatusCb).Do(
331 [&cntForReceivedCallbackWithSuccess]
332 (std::shared_ptr< DevicePropProvisioningStatus > status)
334 if(status->getESResult() == ES_OK)
336 cntForReceivedCallbackWithSuccess++;
341 g_remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCb);
343 std::unique_lock< std::mutex > lock{ mutexForCondition };
344 responseCon.wait_for(lock, g_waitForResponse);
346 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 1);
349 class ProvisionCloudPropertiesTest : public EasysetupMediatorTest
353 ProvisionCloudPropertiesTest() = default;
354 ~ProvisionCloudPropertiesTest() = default;
356 static void cloudPropProvStatusCb(shared_ptr< CloudPropProvisioningStatus > /*status*/)
363 TestWithMock::SetUp();
368 TestWithMock::TearDown();
372 TEST_F(ProvisionCloudPropertiesTest,
373 ThrowExceptionWhenProvisionCloudPropertiesFailedByCallbackIsNull)
376 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
377 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
379 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp, nullptr));
382 TEST_F(ProvisionCloudPropertiesTest,
383 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthCode)
386 cloudProp.setCloudProp("", "authProvider", "ciServer");
387 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
388 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
389 cloudPropProvStatusCb));
392 TEST_F(ProvisionCloudPropertiesTest,
393 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthProvider)
396 cloudProp.setCloudProp("authCode", "", "ciServer");
397 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
398 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
399 cloudPropProvStatusCb));
402 TEST_F(ProvisionCloudPropertiesTest,
403 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutCIServer)
406 cloudProp.setCloudProp("authCode", "authProvider", "");
407 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
408 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
409 cloudPropProvStatusCb));
412 TEST_F(ProvisionCloudPropertiesTest, ProvisionCloudPropertiesSucceed)
415 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
416 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
418 int cntForReceivedCallbackWithSuccess = 0;
420 mocks.OnCallFunc(cloudPropProvStatusCb).Do(
421 [& cntForReceivedCallbackWithSuccess](std::shared_ptr< CloudPropProvisioningStatus > status)
424 if(status->getESResult() == ES_OK)
426 cntForReceivedCallbackWithSuccess++;
430 g_remoteEnrollee->provisionCloudProperties(cloudProp, cloudPropProvStatusCb);
432 std::unique_lock< std::mutex > lock{ mutexForCondition };
433 responseCon.wait_for(lock, g_waitForResponse);
435 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 2);
437 ESTerminateEnrollee();