From cf2d4e6709a2ec30054cc4e6b83eed4da14d49a0 Mon Sep 17 00:00:00 2001 From: "sy01.youn" Date: Thu, 18 Aug 2016 17:20:17 +0900 Subject: [PATCH] Add & fix unittest of easy setup. - Add unit test cases (1) Enrollee handles individual get request from client. (2) Enrollee handles put request from client. (3) ESSetCallbackForUserdata API unit test is added. - Remove GetTargetNetworkInfoFromProvResource API in resourcehandler. . This API is not need anymore. Change-Id: If634923b0daaf9760b78b3d7e853c823769db6ba Signed-off-by: sy01.youn Reviewed-on: https://gerrit.iotivity.org/gerrit/10603 Tested-by: jenkins-iotivity Reviewed-by: Jihun Ha Reviewed-by: Madan Lanka --- service/easy-setup/enrollee/src/resourcehandler.c | 9 - service/easy-setup/enrollee/src/resourcehandler.h | 1 - .../enrollee/unittests/ESEnrolleeTest.cpp | 133 +++++++++++++- .../enrollee/unittests/ESMediatorSimulator.h | 197 ++++++++++++++++++++- 4 files changed, 326 insertions(+), 14 deletions(-) diff --git a/service/easy-setup/enrollee/src/resourcehandler.c b/service/easy-setup/enrollee/src/resourcehandler.c index 872a8b7..e22d6d4 100755 --- a/service/easy-setup/enrollee/src/resourcehandler.c +++ b/service/easy-setup/enrollee/src/resourcehandler.c @@ -107,15 +107,6 @@ void UnRegisterResourceEventCallBack() } } -void GetTargetNetworkInfoFromProvResource(char *name, char *pass) -{ - if (name != NULL && pass != NULL) - { - OICStrcpy(name, MAX_WEBLINKLEN, gWiFiResource.ssid); - OICStrcpy(pass, MAX_WEBLINKLEN, gWiFiResource.cred); - } -} - OCStackResult initProvResource(bool isSecured) { gProvResource.status = ES_STATE_INIT; diff --git a/service/easy-setup/enrollee/src/resourcehandler.h b/service/easy-setup/enrollee/src/resourcehandler.h index cc9ae8d..c59f1b7 100755 --- a/service/easy-setup/enrollee/src/resourcehandler.h +++ b/service/easy-setup/enrollee/src/resourcehandler.h @@ -87,7 +87,6 @@ OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty); OCStackResult SetEnrolleeState(ESEnrolleeState esState); OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode); -void GetTargetNetworkInfoFromProvResource(char *, char *); void RegisterWifiRsrcEventCallBack(ESWiFiCB); void RegisterCloudRsrcEventCallBack(ESCloudCB); void RegisterDevConfRsrcEventCallBack(ESDevConfCB); diff --git a/service/easy-setup/enrollee/unittests/ESEnrolleeTest.cpp b/service/easy-setup/enrollee/unittests/ESEnrolleeTest.cpp index 6232bce..b782a9a 100755 --- a/service/easy-setup/enrollee/unittests/ESEnrolleeTest.cpp +++ b/service/easy-setup/enrollee/unittests/ESEnrolleeTest.cpp @@ -88,6 +88,26 @@ public: std::cout << __func__ << std::endl; } + static void onGetWifiRsrc(const OCRepresentation& ) + { + std::cout << __func__ << std::endl; + } + + static void onGetCloudRsrc(const OCRepresentation& ) + { + std::cout << __func__ << std::endl; + } + + static void onGetDeviceConf(const OCRepresentation& ) + { + std::cout << __func__ << std::endl; + } + + static void onPutRequest(const OCRepresentation& ) + { + std::cout << __func__ << std::endl; + } + static void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >) { std::cout << __func__ << std::endl; @@ -126,15 +146,30 @@ public: return ESInitEnrollee(false, resourcemMask, callbacks); } + ESResult startEnrolleeWithInvalidRsrcMask() + { + ESProvisioningCallbacks callbacks; + callbacks.WiFiProvCb = &EasysetupEnrolleeTest::WiFiProvCbInApp; + callbacks.DevConfProvCb = &EasysetupEnrolleeTest::DevConfProvCbInApp; + callbacks.CloudDataProvCb = &EasysetupEnrolleeTest::CloudDataCbInApp; + + return ESInitEnrollee(false, (ESResourceMask)0, callbacks); + } + ESResult setDeviceProperty() { ESDeviceProperty deviceProperty = { - {{WIFI_11G, WiFi_EOF}, WIFI_5G}, {"Test Device"} + {{WIFI_11G, WiFi_EOF}, WIFI_5G}, {"Test Device", "Test Model Number"} }; return ESSetDeviceProperty(&deviceProperty); } + ESResult setCallabckForUserDataNULL() + { + return ESSetCallbackForUserdata(NULL, NULL); + } + protected: void SetUp() @@ -167,6 +202,13 @@ TEST_F(EasysetupEnrolleeTest, ESInitEnrolleeSuccess) ESTerminateEnrollee(); } +TEST_F(EasysetupEnrolleeTest, ESInitEnrolleeFailedInvalidRsrcMask) +{ + ESResult ret = startEnrolleeWithInvalidRsrcMask(); + EXPECT_EQ(ret, ES_ERROR); + ESTerminateEnrollee(); +} + TEST_F(EasysetupEnrolleeTest, ESInitEnrolleeFailedByWiFiCbIsNull) { ESResourceMask resourcemMask = (ESResourceMask)(ES_WIFI_RESOURCE | @@ -410,4 +452,93 @@ TEST_F(EasysetupEnrolleeTest, CloudServerProperiesProvisionedWithSuccess) ESTerminateEnrollee(); } +TEST_F(EasysetupEnrolleeTest, GetWifiRsrcTest) +{ + bool isRepFlag = false; + mocks.ExpectCallFunc(onGetWifiRsrc).Do( + [& isRepFlag](const OCRepresentation& /*rep*/) + { + + isRepFlag = true; + }); + + ESResult ret = startEnrollee(); + g_mediatorSimul.getWifiRsrc(onGetWifiRsrc); + + std::unique_lock< std::mutex > lock{ mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(ret, ES_OK); + + ESTerminateEnrollee(); +} +TEST_F(EasysetupEnrolleeTest, GetCloudRsrcTest) +{ + bool isRepFlag = false; + mocks.ExpectCallFunc(onGetCloudRsrc).Do( + [& isRepFlag](const OCRepresentation& /*rep*/) + { + isRepFlag = true; + }); + + ESResult ret = startEnrollee(); + g_mediatorSimul.getCloudRsrc(onGetCloudRsrc); + + std::unique_lock< std::mutex > lock{ mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(ret, ES_OK); + + ESTerminateEnrollee(); +} + +TEST_F(EasysetupEnrolleeTest, GetDevConfTest) +{ + bool isRepFlag = false; + mocks.ExpectCallFunc(onGetDeviceConf).Do( + [& isRepFlag](const OCRepresentation& /*rep*/) + { + isRepFlag = true; + }); + + ESResult ret = startEnrollee(); + ret = setDeviceProperty(); + + g_mediatorSimul.getDevConfiguration(onGetDeviceConf); + + std::unique_lock< std::mutex > lock{ mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(ret, ES_OK); + + ESTerminateEnrollee(); +} + +TEST_F(EasysetupEnrolleeTest, PutRequestTest) +{ + bool isRepFlag = false; + mocks.ExpectCallFunc(onPutRequest).Do( + [& isRepFlag](const OCRepresentation& /*rep*/) + { + isRepFlag = true; + }); + + ESResult ret = startEnrollee(); + + g_mediatorSimul.putProvRsrc(onPutRequest); + + std::unique_lock< std::mutex > lock{ mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(ret, ES_OK); + + ESTerminateEnrollee(); +} + +TEST_F(EasysetupEnrolleeTest, ESSetCallabckForUserDataFailed) +{ + ESResult ret = setCallabckForUserDataNULL(); + EXPECT_EQ(ret, ES_ERROR); + ESTerminateEnrollee(); +} diff --git a/service/easy-setup/enrollee/unittests/ESMediatorSimulator.h b/service/easy-setup/enrollee/unittests/ESMediatorSimulator.h index aa43055..41a0b62 100755 --- a/service/easy-setup/enrollee/unittests/ESMediatorSimulator.h +++ b/service/easy-setup/enrollee/unittests/ESMediatorSimulator.h @@ -35,24 +35,31 @@ #include "OCPlatform.h" #define PROV_RESOURCE_TYPE "ocf.wk.prov" +#define WIFI_RESOURCE_TYPE "ocf.wk.wifi" +#define DEV_RESOURCE_TYPE "ocf.wk.devconf" +#define CLOUD_RESOURCE_TYPE "ocf.wk.cloudserver" using namespace OIC::Service; class ESMediatorSimulator { private: + std::shared_ptr m_remoteEnrollee; std::function resource)> m_discoveryCb; std::function status)> m_getConfigurationCb; std::function)> m_getStatusCb; std::function)> m_DevicePropProvisioningCb; std::function)> m_CloudPropProvisioningCb; - - std::shared_ptr m_remoteEnrollee; + std::function m_getWifiCb; + std::function m_getCloudCb; + std::function m_getDevConfCb; + std::function m_provPutCb; public: ESMediatorSimulator() : m_remoteEnrollee(), m_discoveryCb(), m_getConfigurationCb(), m_getStatusCb(), - m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb() + m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb(), + m_getWifiCb(), m_getCloudCb(), m_getDevConfCb(), m_provPutCb() { }; ~ESMediatorSimulator() = default; @@ -85,6 +92,36 @@ public: this, std::placeholders::_1)); } + void getWifiRsrc(std::function cb) + { + m_getWifiCb = cb; + std::string uri = std::string("/oic/res?rt=") + WIFI_RESOURCE_TYPE; + OC::OCPlatform::findResource("", uri, + OCConnectivityType::CT_DEFAULT, + std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetWifiRsrc, + this, std::placeholders::_1)); + } + + void getCloudRsrc(std::function cb) + { + m_getCloudCb = cb; + std::string uri = std::string("/oic/res?rt=") + CLOUD_RESOURCE_TYPE; + OC::OCPlatform::findResource("", uri, + OCConnectivityType::CT_DEFAULT, + std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetCloudRsrc, + this, std::placeholders::_1)); + } + + void getDevConfiguration(std::function cb) + { + m_getDevConfCb = cb; + std::string uri = std::string("/oic/res?rt=") + DEV_RESOURCE_TYPE; + OC::OCPlatform::findResource("", uri, + OCConnectivityType::CT_DEFAULT, + std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetDevConf, + this, std::placeholders::_1)); + } + void getStatus(std::function)> cb) { m_getStatusCb = cb; @@ -118,6 +155,17 @@ public: this, std::placeholders::_1)); } + void putProvRsrc(std::function cb) + { + m_provPutCb = cb; + m_remoteEnrollee = NULL; + std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE; + OC::OCPlatform::findResource("", uri, + OCConnectivityType::CT_DEFAULT, + std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToPutProvRsrc, + this, std::placeholders::_1)); + } + private: bool isValidResourceToTest(std::shared_ptr resource) { @@ -194,6 +242,118 @@ private: } } + void getWifiRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode) + { + try + { + if(eCode == OC_STACK_OK) + { + std::cout << "GET request was successful" << std::endl; + std::cout << "Resource URI: " << rep.getUri() << std::endl; + if(m_getWifiCb != NULL) + { + m_getWifiCb(rep); + m_getWifiCb = NULL; + } + } + else + { + std::cout << "onGET Response error: " << eCode << std::endl; + std::exit(-1); + } + } + catch(std::exception& e) + { + std::cout << "Exception: " << e.what() << " in onGet" << std::endl; + } + } + + void discoverRemoteEnrolleeCbToGetWifiRsrc(std::shared_ptr resource) + { + if(!resource->getResourceTypes().at(0).compare(WIFI_RESOURCE_TYPE) && m_getWifiCb) + { + QueryParamsMap test; + resource->get(test, std::bind( + &ESMediatorSimulator::getWifiRsrcCallback, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3)); + } + } + + void getCloudRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode) + { + try + { + if(eCode == OC_STACK_OK) + { + std::cout << "GET request was successful" << std::endl; + std::cout << "Resource URI: " << rep.getUri() << std::endl; + if(m_getCloudCb != NULL) + { + m_getCloudCb(rep); + m_getCloudCb = NULL; + } + } + else + { + std::cout << "onGET Response error: " << eCode << std::endl; + std::exit(-1); + } + } + catch(std::exception& e) + { + std::cout << "Exception: " << e.what() << " in onGet" << std::endl; + } + } + + + void discoverRemoteEnrolleeCbToGetCloudRsrc(std::shared_ptr resource) + { + if(!resource->getResourceTypes().at(0).compare(CLOUD_RESOURCE_TYPE) && m_getCloudCb) + { + QueryParamsMap test; + resource->get(test, std::bind( + &ESMediatorSimulator::getCloudRsrcCallback, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3)); + } + } + + void getDevConfCallback(const HeaderOptions&, const OCRepresentation& rep, const int eCode) + { + try + { + if(eCode == OC_STACK_OK) + { + std::cout << "GET request was successful" << std::endl; + std::cout << "Resource URI: " << rep.getUri() << std::endl; + if(m_getDevConfCb != NULL) + { + m_getDevConfCb(rep); + m_getDevConfCb = NULL; + } + } + else + { + std::cout << "onGET Response error: " << eCode << std::endl; + std::exit(-1); + } + } + catch(std::exception& e) + { + std::cout << "Exception: " << e.what() << " in onGet" << std::endl; + } + } + + void discoverRemoteEnrolleeCbToGetDevConf(std::shared_ptr resource) + { + if(!resource->getResourceTypes().at(0).compare(DEV_RESOURCE_TYPE) && m_getDevConfCb) + { + QueryParamsMap test; + resource->get(test, std::bind( + &ESMediatorSimulator::getDevConfCallback, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3)); + } + } + void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus) { if(m_getStatusCb != NULL) @@ -294,6 +454,37 @@ private: } } } + + void putProvRsrcCallabck(const HeaderOptions&, const OCRepresentation& rep, const int ) + { + cout << "putProvRsrcCallback is called" << endl; + + if(m_provPutCb != NULL){ + m_provPutCb(rep); + m_provPutCb = NULL; + } + + } + + void discoverRemoteEnrolleeCbToPutProvRsrc(std::shared_ptr resource) + { + m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource); + + if(m_remoteEnrollee != NULL) + { + if(m_provPutCb) + { + OCRepresentation rep; + QueryParamsMap test; + resource->put(rep, test, std::bind( + &ESMediatorSimulator::putProvRsrcCallabck, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3)); + } + + } + + } + }; -- 2.7.4