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"
39 #include "cainterface.h"
40 #include "OCPlatform.h"
42 #define PROV_RESOURCE_TYPE "ocf.wk.prov"
45 using namespace OIC::Service;
49 std::atomic_bool g_isStartedStack(false);
51 std::chrono::milliseconds g_waitForResponse(1000);
53 std::condition_variable responseCon;
54 std::mutex mutexForCondition;
56 ESEnrolleeSimulator g_enrolleeSimul;
57 std::shared_ptr<RemoteEnrollee> g_remoteEnrollee;
60 class TestWithMock: public testing::Test
66 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
70 virtual void TearDown()
84 class EasysetupMediatorTest : public TestWithMock
87 std::shared_ptr<OC::OCResource> m_enrolleeResource;
90 EasysetupMediatorTest() = default;
91 ~EasysetupMediatorTest() = default;
93 std::shared_ptr<OC::OCResource> CreateNotProvResource()
95 OCConnectivityType connectivityType = CT_DEFAULT;
96 std::vector<std::string> types = {"ocf.wk.notprov"};
97 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
99 return OCPlatform::constructResourceObject("coap://192.168.1.2:5000",
100 "/NotProvisioningResURI",
107 void discoverRemoteEnrollee()
109 std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
110 OC::OCPlatform::findResource("", uri,
111 OCConnectivityType::CT_DEFAULT,
112 std::bind(&EasysetupMediatorTest::discoverRemoteEnrolleeCb,
113 this, std::placeholders::_1));
115 std::unique_lock< std::mutex > lock{ mutexForCondition };
116 responseCon.wait_for(lock, g_waitForResponse);
122 TestWithMock::SetUp();
123 if (g_isStartedStack == false)
125 if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
127 printf("OCStack init error!!\n");
130 g_enrolleeSimul.initEnrollee();
131 g_isStartedStack = true;
137 TestWithMock::TearDown();
141 bool isValidResourceToTest(std::shared_ptr<OC::OCResource> resource)
143 if((resource->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
148 CAEndpoint_t *tempInfo = NULL;
149 uint32_t tempSize = 0;
151 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
152 if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
158 for (uint32_t index = 0; index < tempSize; index++)
160 if (CA_ADAPTER_IP == tempInfo[index].adapter)
162 if(resource->host().find(tempInfo[index].addr) != std::string::npos &&
163 resource->host().find(std::to_string(tempInfo[index].port).c_str()) != std::string::npos)
173 void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
175 if(!isValidResourceToTest(resource))
180 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE))
182 m_enrolleeResource = resource;
187 TEST_F(EasysetupMediatorTest, createremoteenrolleeFailedWithNotProvResource)
189 auto remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(CreateNotProvResource());
191 EXPECT_EQ(nullptr, remoteEnrollee);
194 TEST_F(EasysetupMediatorTest, createremoteenrolleeSucceedWithProvResource)
196 discoverRemoteEnrollee();
197 g_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(m_enrolleeResource);
199 ASSERT_NE(nullptr, g_remoteEnrollee);
202 class GetConfigurationTest : public EasysetupMediatorTest
205 GetConfigurationTest() = default;
206 ~GetConfigurationTest() = default;
208 static void onGetConfigurationCb(shared_ptr< GetConfigurationStatus > /*status*/)
215 TestWithMock::SetUp();
220 TestWithMock::TearDown();
224 TEST_F(GetConfigurationTest, ThrowExceptionWhenGetConfigurationFailedByCallbackIsNull)
226 EXPECT_ANY_THROW(g_remoteEnrollee->getConfiguration(nullptr));
229 TEST_F(GetConfigurationTest, GetConfigurationSucceed)
231 bool isWellConstructed = false;
233 g_enrolleeSimul.setDeviceProperty();
235 mocks.ExpectCallFunc(onGetConfigurationCb).Do(
236 [&isWellConstructed](std::shared_ptr< GetConfigurationStatus > status)
238 if(status->getESResult() == ES_OK)
240 EnrolleeConf conf = status->getEnrolleeConf();
241 if(!conf.getWiFiModes().empty())
243 if(conf.getWiFiModes().at(0) == WIFI_11G &&
244 conf.getWiFiFreq() == WIFI_5G &&
245 !strcmp(conf.getDeviceName().c_str(), "Test Device") &&
246 !strcmp(conf.getModelNumber().c_str(), "Test Model Number"))
248 isWellConstructed = true;
250 cout << "getDeviceName : " << conf.getDeviceName().c_str() << endl;
251 cout << "getModelNumber : " << conf.getModelNumber().c_str() << endl;
256 g_remoteEnrollee->getConfiguration(onGetConfigurationCb);
258 std::unique_lock< std::mutex > lock{ mutexForCondition };
259 responseCon.wait_for(lock, g_waitForResponse);
261 EXPECT_TRUE(isWellConstructed);
264 class GetStatusTest : public EasysetupMediatorTest
268 GetStatusTest() = default;
269 ~GetStatusTest() = default;
271 static void onGetStatusCb(shared_ptr< GetEnrolleeStatus > /*status*/)
278 TestWithMock::SetUp();
283 TestWithMock::TearDown();
287 TEST_F(GetStatusTest, ThrowExceptionWhenGetStatusFailedByCallbackIsNull)
289 EXPECT_ANY_THROW(g_remoteEnrollee->getStatus(nullptr));
292 TEST_F(GetStatusTest, GetStatusSucceed)
294 g_enrolleeSimul.setESState();
295 g_enrolleeSimul.setESErrorCode();
297 bool isWellConstructed = false;
298 mocks.ExpectCallFunc(onGetStatusCb).Do(
299 [&isWellConstructed](std::shared_ptr< GetEnrolleeStatus > status)
301 if(status->getESResult() == ES_OK)
303 EnrolleeStatus enrolleeStatus = status->getEnrolleeStatus();
305 if(enrolleeStatus.getProvStatus() == ES_STATE_CONNECTED_TO_ENROLLER &&
306 enrolleeStatus.getLastErrCode() == ES_ERRCODE_NO_INTERNETCONNECTION)
308 isWellConstructed = true;
313 g_remoteEnrollee->getStatus(onGetStatusCb);
315 std::unique_lock< std::mutex > lock{ mutexForCondition };
316 responseCon.wait_for(lock, g_waitForResponse);
318 EXPECT_TRUE(isWellConstructed);
321 class ProvisionDevicePropertiesTest : public EasysetupMediatorTest
325 ProvisionDevicePropertiesTest() = default;
326 ~ProvisionDevicePropertiesTest() = default;
328 static void deviceProvisioningStatusCb(
329 shared_ptr< DevicePropProvisioningStatus > /*status*/)
336 TestWithMock::SetUp();
341 TestWithMock::TearDown();
345 TEST_F(ProvisionDevicePropertiesTest,
346 ThrowExceptionWhenProvisionDeviceProperiesFailedByCallbackIsNull)
349 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
350 devProp.setDevConfProp("korean", "Korea", "Location");
352 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp, nullptr));
355 TEST_F(ProvisionDevicePropertiesTest,
356 ThrowExceptionWhenProvisionDeviceProperiesFailedWithoutSSID)
359 devProp.setWiFiProp("", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
360 devProp.setDevConfProp("korean", "Korea", "Location");
361 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp,
362 deviceProvisioningStatusCb));
365 TEST_F(ProvisionDevicePropertiesTest,
366 ProvisionDeviceProperiesSucceed)
369 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
370 devProp.setDevConfProp("korean", "Korea", "Location");
372 int cntForReceivedCallbackWithSuccess = 0;
374 mocks.OnCallFunc(deviceProvisioningStatusCb).Do(
375 [&cntForReceivedCallbackWithSuccess]
376 (std::shared_ptr< DevicePropProvisioningStatus > status)
378 if(status->getESResult() == ES_OK)
380 cntForReceivedCallbackWithSuccess++;
385 g_remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCb);
387 std::unique_lock< std::mutex > lock{ mutexForCondition };
388 responseCon.wait_for(lock, g_waitForResponse);
390 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 1);
393 class ProvisionCloudPropertiesTest : public EasysetupMediatorTest
397 ProvisionCloudPropertiesTest() = default;
398 ~ProvisionCloudPropertiesTest() = default;
400 static void cloudPropProvStatusCb(shared_ptr< CloudPropProvisioningStatus > /*status*/)
407 TestWithMock::SetUp();
412 TestWithMock::TearDown();
416 TEST_F(ProvisionCloudPropertiesTest,
417 ThrowExceptionWhenProvisionCloudPropertiesFailedByCallbackIsNull)
420 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
421 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
423 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp, nullptr));
426 TEST_F(ProvisionCloudPropertiesTest,
427 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthCode)
430 cloudProp.setCloudProp("", "authProvider", "ciServer");
431 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
432 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
433 cloudPropProvStatusCb));
436 TEST_F(ProvisionCloudPropertiesTest,
437 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthProvider)
440 cloudProp.setCloudProp("authCode", "", "ciServer");
441 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
442 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
443 cloudPropProvStatusCb));
446 TEST_F(ProvisionCloudPropertiesTest,
447 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutCIServer)
450 cloudProp.setCloudProp("authCode", "authProvider", "");
451 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
452 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
453 cloudPropProvStatusCb));
456 TEST_F(ProvisionCloudPropertiesTest, ProvisionCloudPropertiesSucceed)
459 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
460 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
462 int cntForReceivedCallbackWithSuccess = 0;
464 mocks.OnCallFunc(cloudPropProvStatusCb).Do(
465 [& cntForReceivedCallbackWithSuccess](std::shared_ptr< CloudPropProvisioningStatus > status)
468 if(status->getESResult() == ES_OK)
470 cntForReceivedCallbackWithSuccess++;
474 g_remoteEnrollee->provisionCloudProperties(cloudProp, cloudPropProvStatusCb);
476 std::unique_lock< std::mutex > lock{ mutexForCondition };
477 responseCon.wait_for(lock, g_waitForResponse);
479 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 2);
481 ESTerminateEnrollee();