replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / unittests / ESMediatorTest.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 0110b64..853f132
 #include "ESEnrolleeSimulator.h"
 #include "escommon.h"
 #include "ESRichCommon.h"
-#include "EasySetup.h"
+#include "EasySetup.hpp"
 #include "RemoteEnrollee.h"
 
 #include "ESEnrolleeCommon.h"
 #include "easysetup.h"
 
-#define PROV_RESOURCE_TYPE "ocf.wk.prov"
+#include "ocrandom.h"
+#include "cainterface.h"
+#include "OCPlatform.h"
 
 using namespace OC;
 using namespace OIC::Service;
@@ -86,14 +88,14 @@ public:
     EasysetupMediatorTest() = default;
     ~EasysetupMediatorTest() = default;
 
-    std::shared_ptr<OC::OCResource> CreateNotProvResource()
+    std::shared_ptr<OC::OCResource> CreateNotEasySetupResource()
     {
         OCConnectivityType connectivityType = CT_DEFAULT;
-        std::vector<std::string> types = {"ocf.wk.notprov"};
+        std::vector<std::string> types = {"oic.r.noteasysetup"};
         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
 
         return OCPlatform::constructResourceObject("coap://192.168.1.2:5000",
-                                                   "/NotProvisioningResURI",
+                                                   "/NotEasySetupResURI",
                                                    connectivityType,
                                                    false,
                                                    types,
@@ -102,7 +104,7 @@ public:
 
     void discoverRemoteEnrollee()
     {
-        std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
+        std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_EASYSETUP;
         OC::OCPlatform::findResource("", uri,
                 OCConnectivityType::CT_DEFAULT,
                 std::bind(&EasysetupMediatorTest::discoverRemoteEnrolleeCb,
@@ -134,23 +136,60 @@ protected:
     }
 
 private:
+    bool isValidResourceToTest(std::shared_ptr<OC::OCResource> resource)
+    {
+        if((resource->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
+        {
+            return false;
+        }
+
+        CAEndpoint_t *tempInfo = NULL;
+        uint32_t tempSize = 0;
+
+        CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
+        if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
+        {
+            free(tempInfo);
+            return false;
+        }
+
+        for (uint32_t index = 0; index  < tempSize; index++)
+        {
+            if (CA_ADAPTER_IP == tempInfo[index].adapter)
+            {
+                if(resource->host().find(tempInfo[index].addr) != std::string::npos &&
+                    resource->host().find(std::to_string(tempInfo[index].port).c_str()) != std::string::npos)
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
     void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
     {
-        if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE))
+        if(!isValidResourceToTest(resource))
+        {
+            return ;
+        }
+
+        if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_EASYSETUP))
         {
             m_enrolleeResource = resource;
         }
     }
 };
 
-TEST_F(EasysetupMediatorTest, createremoteenrolleeFailedWithNotProvResource)
+TEST_F(EasysetupMediatorTest, createremoteenrolleeFailedWithNotEasySetupResource)
 {
-    auto remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(CreateNotProvResource());
+    auto remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(CreateNotEasySetupResource());
 
     EXPECT_EQ(nullptr, remoteEnrollee);
 }
 
-TEST_F(EasysetupMediatorTest, createremoteenrolleeSucceedWithProvResource)
+TEST_F(EasysetupMediatorTest, createremoteenrolleeSucceedWithEasySetupResource)
 {
     discoverRemoteEnrollee();
     g_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(m_enrolleeResource);
@@ -205,6 +244,7 @@ TEST_F(GetConfigurationTest, GetConfigurationSucceed)
                     {
                         isWellConstructed = true;
                     }
+                    cout << "getDeviceName : " << conf.getDeviceName().c_str() << endl;
                 }
             }
         });
@@ -303,7 +343,6 @@ TEST_F(ProvisionDevicePropertiesTest,
 {
     DeviceProp devProp;
     devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
-    devProp.setDevConfProp("korean", "Korea");
 
     EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp, nullptr));
 }
@@ -313,7 +352,6 @@ TEST_F(ProvisionDevicePropertiesTest,
 {
     DeviceProp devProp;
     devProp.setWiFiProp("", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
-    devProp.setDevConfProp("korean", "Korea");
     EXPECT_ANY_THROW(g_remoteEnrollee->provisionDeviceProperties(devProp,
                                                                  deviceProvisioningStatusCb));
 }
@@ -323,7 +361,6 @@ TEST_F(ProvisionDevicePropertiesTest,
 {
     DeviceProp devProp;
     devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
-    devProp.setDevConfProp("korean", "Korea");
 
     int cntForReceivedCallbackWithSuccess = 0;
 
@@ -420,7 +457,6 @@ TEST_F(ProvisionCloudPropertiesTest, ProvisionCloudPropertiesSucceed)
     mocks.OnCallFunc(cloudPropProvStatusCb).Do(
         [& cntForReceivedCallbackWithSuccess](std::shared_ptr< CloudPropProvisioningStatus > status)
         {
-            // Will called twice
             if(status->getESResult() == ES_OK)
             {
                cntForReceivedCallbackWithSuccess++;
@@ -432,7 +468,7 @@ TEST_F(ProvisionCloudPropertiesTest, ProvisionCloudPropertiesSucceed)
     std::unique_lock< std::mutex > lock{ mutexForCondition };
     responseCon.wait_for(lock, g_waitForResponse);
 
-    EXPECT_EQ(cntForReceivedCallbackWithSuccess, 2);
+    EXPECT_EQ(cntForReceivedCallbackWithSuccess, 1);
 
     ESTerminateEnrollee();
 }