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.hpp"
33 #include "RemoteEnrollee.h"
35 #include "ESEnrolleeCommon.h"
36 #include "easysetup.h"
39 #include "cainterface.h"
40 #include "OCPlatform.h"
43 using namespace OIC::Service;
47 std::atomic_bool g_isStartedStack(false);
49 std::chrono::milliseconds g_waitForResponse(1000);
51 std::condition_variable responseCon;
52 std::mutex mutexForCondition;
54 ESEnrolleeSimulator g_enrolleeSimul;
55 std::shared_ptr<RemoteEnrollee> g_remoteEnrollee;
58 class TestWithMock: public testing::Test
64 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
68 virtual void TearDown()
82 class EasysetupMediatorTest : public TestWithMock
85 std::shared_ptr<OC::OCResource> m_enrolleeResource;
88 EasysetupMediatorTest() = default;
89 ~EasysetupMediatorTest() = default;
91 std::shared_ptr<OC::OCResource> CreateNotEasySetupResource()
93 OCConnectivityType connectivityType = CT_DEFAULT;
94 std::vector<std::string> types = {"oic.r.noteasysetup"};
95 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
97 return OCPlatform::constructResourceObject("coap://192.168.1.2:5000",
98 "/NotEasySetupResURI",
105 void discoverRemoteEnrollee()
107 std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_EASYSETUP;
108 OC::OCPlatform::findResource("", uri,
109 OCConnectivityType::CT_DEFAULT,
110 std::bind(&EasysetupMediatorTest::discoverRemoteEnrolleeCb,
111 this, std::placeholders::_1));
113 std::unique_lock< std::mutex > lock{ mutexForCondition };
114 responseCon.wait_for(lock, g_waitForResponse);
120 TestWithMock::SetUp();
121 if (g_isStartedStack == false)
123 if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
125 printf("OCStack init error!!\n");
128 g_enrolleeSimul.initEnrollee();
129 g_isStartedStack = true;
135 TestWithMock::TearDown();
139 bool isValidResourceToTest(std::shared_ptr<OC::OCResource> resource)
141 if((resource->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
146 CAEndpoint_t *tempInfo = NULL;
147 uint32_t tempSize = 0;
149 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
150 if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
156 for (uint32_t index = 0; index < tempSize; index++)
158 if (CA_ADAPTER_IP == tempInfo[index].adapter)
160 if(resource->host().find(tempInfo[index].addr) != std::string::npos &&
161 resource->host().find(std::to_string(tempInfo[index].port).c_str()) != std::string::npos)
171 void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
173 if(!isValidResourceToTest(resource))
178 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_EASYSETUP))
180 m_enrolleeResource = resource;
185 TEST_F(EasysetupMediatorTest, createremoteenrolleeFailedWithNotEasySetupResource)
187 auto remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(CreateNotEasySetupResource());
189 EXPECT_EQ(nullptr, remoteEnrollee);
192 TEST_F(EasysetupMediatorTest, createremoteenrolleeSucceedWithEasySetupResource)
194 discoverRemoteEnrollee();
195 g_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(m_enrolleeResource);
197 ASSERT_NE(nullptr, g_remoteEnrollee);
200 class GetConfigurationTest : public EasysetupMediatorTest
203 GetConfigurationTest() = default;
204 ~GetConfigurationTest() = default;
206 static void onGetConfigurationCb(shared_ptr< GetConfigurationStatus > /*status*/)
213 TestWithMock::SetUp();
218 TestWithMock::TearDown();
222 TEST_F(GetConfigurationTest, ThrowExceptionWhenGetConfigurationFailedByCallbackIsNull)
224 EXPECT_ANY_THROW(g_remoteEnrollee->getConfiguration(nullptr));
227 TEST_F(GetConfigurationTest, GetConfigurationSucceed)
229 bool isWellConstructed = false;
231 g_enrolleeSimul.setDeviceProperty();
233 mocks.ExpectCallFunc(onGetConfigurationCb).Do(
234 [&isWellConstructed](std::shared_ptr< GetConfigurationStatus > status)
236 if(status->getESResult() == ES_OK)
238 EnrolleeConf conf = status->getEnrolleeConf();
239 if(!conf.getWiFiModes().empty())
241 if(conf.getWiFiModes().at(0) == WIFI_11G &&
242 conf.getWiFiFreq() == WIFI_5G &&
243 !strcmp(conf.getDeviceName().c_str(), "Test Device"))
245 isWellConstructed = true;
247 cout << "getDeviceName : " << conf.getDeviceName().c_str() << endl;
252 g_remoteEnrollee->getConfiguration(onGetConfigurationCb);
254 std::unique_lock< std::mutex > lock{ mutexForCondition };
255 responseCon.wait_for(lock, g_waitForResponse);
257 EXPECT_TRUE(isWellConstructed);
260 class GetStatusTest : public EasysetupMediatorTest
264 GetStatusTest() = default;
265 ~GetStatusTest() = default;
267 static void onGetStatusCb(shared_ptr< GetEnrolleeStatus > /*status*/)
274 TestWithMock::SetUp();
279 TestWithMock::TearDown();
283 TEST_F(GetStatusTest, ThrowExceptionWhenGetStatusFailedByCallbackIsNull)
285 EXPECT_ANY_THROW(g_remoteEnrollee->getStatus(nullptr));
288 TEST_F(GetStatusTest, GetStatusSucceed)
290 g_enrolleeSimul.setESState();
291 g_enrolleeSimul.setESErrorCode();
293 bool isWellConstructed = false;
294 mocks.ExpectCallFunc(onGetStatusCb).Do(
295 [&isWellConstructed](std::shared_ptr< GetEnrolleeStatus > status)
297 if(status->getESResult() == ES_OK)
299 EnrolleeStatus enrolleeStatus = status->getEnrolleeStatus();
301 if(enrolleeStatus.getProvStatus() == ES_STATE_CONNECTED_TO_ENROLLER &&
302 enrolleeStatus.getLastErrCode() == ES_ERRCODE_NO_INTERNETCONNECTION)
304 isWellConstructed = true;
309 g_remoteEnrollee->getStatus(onGetStatusCb);
311 std::unique_lock< std::mutex > lock{ mutexForCondition };
312 responseCon.wait_for(lock, g_waitForResponse);
314 EXPECT_TRUE(isWellConstructed);
317 class ProvisionDevicePropertiesTest : public EasysetupMediatorTest
321 ProvisionDevicePropertiesTest() = default;
322 ~ProvisionDevicePropertiesTest() = default;
324 static void deviceProvisioningStatusCb(
325 shared_ptr< DevicePropProvisioningStatus > /*status*/)
332 TestWithMock::SetUp();
337 TestWithMock::TearDown();
341 TEST_F(ProvisionDevicePropertiesTest,
342 ThrowExceptionWhenProvisionDeviceProperiesFailedByCallbackIsNull)
345 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
347 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp, nullptr));
350 TEST_F(ProvisionDevicePropertiesTest,
351 ThrowExceptionWhenProvisionDeviceProperiesFailedWithoutSSID)
354 devProp.setWiFiProp("", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
355 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp,
356 deviceProvisioningStatusCb));
359 TEST_F(ProvisionDevicePropertiesTest,
360 ProvisionDeviceProperiesSucceed)
363 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
365 int cntForReceivedCallbackWithSuccess = 0;
367 mocks.OnCallFunc(deviceProvisioningStatusCb).Do(
368 [&cntForReceivedCallbackWithSuccess]
369 (std::shared_ptr< DevicePropProvisioningStatus > status)
371 if(status->getESResult() == ES_OK)
373 cntForReceivedCallbackWithSuccess++;
378 g_remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCb);
380 std::unique_lock< std::mutex > lock{ mutexForCondition };
381 responseCon.wait_for(lock, g_waitForResponse);
383 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 1);
386 class ProvisionCloudPropertiesTest : public EasysetupMediatorTest
390 ProvisionCloudPropertiesTest() = default;
391 ~ProvisionCloudPropertiesTest() = default;
393 static void cloudPropProvStatusCb(shared_ptr< CloudPropProvisioningStatus > /*status*/)
400 TestWithMock::SetUp();
405 TestWithMock::TearDown();
409 TEST_F(ProvisionCloudPropertiesTest,
410 ThrowExceptionWhenProvisionCloudPropertiesFailedByCallbackIsNull)
413 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
414 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
416 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp, nullptr));
419 TEST_F(ProvisionCloudPropertiesTest,
420 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthCode)
423 cloudProp.setCloudProp("", "authProvider", "ciServer");
424 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
425 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
426 cloudPropProvStatusCb));
429 TEST_F(ProvisionCloudPropertiesTest,
430 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthProvider)
433 cloudProp.setCloudProp("authCode", "", "ciServer");
434 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
435 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
436 cloudPropProvStatusCb));
439 TEST_F(ProvisionCloudPropertiesTest,
440 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutCIServer)
443 cloudProp.setCloudProp("authCode", "authProvider", "");
444 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
445 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
446 cloudPropProvStatusCb));
449 TEST_F(ProvisionCloudPropertiesTest, ProvisionCloudPropertiesSucceed)
452 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
453 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
455 int cntForReceivedCallbackWithSuccess = 0;
457 mocks.OnCallFunc(cloudPropProvStatusCb).Do(
458 [& cntForReceivedCallbackWithSuccess](std::shared_ptr< CloudPropProvisioningStatus > status)
460 if(status->getESResult() == ES_OK)
462 cntForReceivedCallbackWithSuccess++;
466 g_remoteEnrollee->provisionCloudProperties(cloudProp, cloudPropProvStatusCb);
468 std::unique_lock< std::mutex > lock{ mutexForCondition };
469 responseCon.wait_for(lock, g_waitForResponse);
471 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 1);
473 ESTerminateEnrollee();