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"
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> CreateNotProvResource()
93 OCConnectivityType connectivityType = CT_DEFAULT;
94 std::vector<std::string> types = {"oic.wk.notprov"};
95 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
97 return OCPlatform::constructResourceObject("coap://192.168.1.2:5000",
98 "/NotProvisioningResURI",
105 void discoverRemoteEnrollee()
107 std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
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_PROV))
180 m_enrolleeResource = resource;
185 TEST_F(EasysetupMediatorTest, createremoteenrolleeFailedWithNotProvResource)
187 auto remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(CreateNotProvResource());
189 EXPECT_EQ(nullptr, remoteEnrollee);
192 TEST_F(EasysetupMediatorTest, createremoteenrolleeSucceedWithProvResource)
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") &&
244 !strcmp(conf.getModelNumber().c_str(), "Test Model Number"))
246 isWellConstructed = true;
248 cout << "getDeviceName : " << conf.getDeviceName().c_str() << endl;
249 cout << "getModelNumber : " << conf.getModelNumber().c_str() << endl;
254 g_remoteEnrollee->getConfiguration(onGetConfigurationCb);
256 std::unique_lock< std::mutex > lock{ mutexForCondition };
257 responseCon.wait_for(lock, g_waitForResponse);
259 EXPECT_TRUE(isWellConstructed);
262 class GetStatusTest : public EasysetupMediatorTest
266 GetStatusTest() = default;
267 ~GetStatusTest() = default;
269 static void onGetStatusCb(shared_ptr< GetEnrolleeStatus > /*status*/)
276 TestWithMock::SetUp();
281 TestWithMock::TearDown();
285 TEST_F(GetStatusTest, ThrowExceptionWhenGetStatusFailedByCallbackIsNull)
287 EXPECT_ANY_THROW(g_remoteEnrollee->getStatus(nullptr));
290 TEST_F(GetStatusTest, GetStatusSucceed)
292 g_enrolleeSimul.setESState();
293 g_enrolleeSimul.setESErrorCode();
295 bool isWellConstructed = false;
296 mocks.ExpectCallFunc(onGetStatusCb).Do(
297 [&isWellConstructed](std::shared_ptr< GetEnrolleeStatus > status)
299 if(status->getESResult() == ES_OK)
301 EnrolleeStatus enrolleeStatus = status->getEnrolleeStatus();
303 if(enrolleeStatus.getProvStatus() == ES_STATE_CONNECTED_TO_ENROLLER &&
304 enrolleeStatus.getLastErrCode() == ES_ERRCODE_NO_INTERNETCONNECTION)
306 isWellConstructed = true;
311 g_remoteEnrollee->getStatus(onGetStatusCb);
313 std::unique_lock< std::mutex > lock{ mutexForCondition };
314 responseCon.wait_for(lock, g_waitForResponse);
316 EXPECT_TRUE(isWellConstructed);
319 class ProvisionDevicePropertiesTest : public EasysetupMediatorTest
323 ProvisionDevicePropertiesTest() = default;
324 ~ProvisionDevicePropertiesTest() = default;
326 static void deviceProvisioningStatusCb(
327 shared_ptr< DevicePropProvisioningStatus > /*status*/)
334 TestWithMock::SetUp();
339 TestWithMock::TearDown();
343 TEST_F(ProvisionDevicePropertiesTest,
344 ThrowExceptionWhenProvisionDeviceProperiesFailedByCallbackIsNull)
347 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
348 devProp.setDevConfProp("korean", "Korea", "Location");
350 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp, nullptr));
353 TEST_F(ProvisionDevicePropertiesTest,
354 ThrowExceptionWhenProvisionDeviceProperiesFailedWithoutSSID)
357 devProp.setWiFiProp("", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
358 devProp.setDevConfProp("korean", "Korea", "Location");
359 EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp,
360 deviceProvisioningStatusCb));
363 TEST_F(ProvisionDevicePropertiesTest,
364 ProvisionDeviceProperiesSucceed)
367 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
368 devProp.setDevConfProp("korean", "Korea", "Location");
370 int cntForReceivedCallbackWithSuccess = 0;
372 mocks.OnCallFunc(deviceProvisioningStatusCb).Do(
373 [&cntForReceivedCallbackWithSuccess]
374 (std::shared_ptr< DevicePropProvisioningStatus > status)
376 if(status->getESResult() == ES_OK)
378 cntForReceivedCallbackWithSuccess++;
383 g_remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCb);
385 std::unique_lock< std::mutex > lock{ mutexForCondition };
386 responseCon.wait_for(lock, g_waitForResponse);
388 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 1);
391 class ProvisionCloudPropertiesTest : public EasysetupMediatorTest
395 ProvisionCloudPropertiesTest() = default;
396 ~ProvisionCloudPropertiesTest() = default;
398 static void cloudPropProvStatusCb(shared_ptr< CloudPropProvisioningStatus > /*status*/)
405 TestWithMock::SetUp();
410 TestWithMock::TearDown();
414 TEST_F(ProvisionCloudPropertiesTest,
415 ThrowExceptionWhenProvisionCloudPropertiesFailedByCallbackIsNull)
418 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
419 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
421 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp, nullptr));
424 TEST_F(ProvisionCloudPropertiesTest,
425 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthCode)
428 cloudProp.setCloudProp("", "authProvider", "ciServer");
429 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
430 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
431 cloudPropProvStatusCb));
434 TEST_F(ProvisionCloudPropertiesTest,
435 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutAuthProvider)
438 cloudProp.setCloudProp("authCode", "", "ciServer");
439 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
440 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
441 cloudPropProvStatusCb));
444 TEST_F(ProvisionCloudPropertiesTest,
445 ThrowExceptionWhenProvisionCloudPropertiesFailedWithoutCIServer)
448 cloudProp.setCloudProp("authCode", "authProvider", "");
449 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
450 EXPECT_ANY_THROW(g_remoteEnrollee->provisionCloudProperties(cloudProp,
451 cloudPropProvStatusCb));
454 TEST_F(ProvisionCloudPropertiesTest, ProvisionCloudPropertiesSucceed)
457 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
458 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
460 int cntForReceivedCallbackWithSuccess = 0;
462 mocks.OnCallFunc(cloudPropProvStatusCb).Do(
463 [& cntForReceivedCallbackWithSuccess](std::shared_ptr< CloudPropProvisioningStatus > status)
466 if(status->getESResult() == ES_OK || status->getESResult() == ES_FOUND_ENROLLEE)
468 cntForReceivedCallbackWithSuccess++;
472 g_remoteEnrollee->provisionCloudProperties(cloudProp, cloudPropProvStatusCb);
474 std::unique_lock< std::mutex > lock{ mutexForCondition };
475 responseCon.wait_for(lock, g_waitForResponse);
477 EXPECT_EQ(cntForReceivedCallbackWithSuccess, 2);
479 ESTerminateEnrollee();