Update snapshot(2018-01-04)
[platform/upstream/iotivity.git] / resource / unittests / OCPlatformTest.cpp
index f526585..bed8cf3 100644 (file)
 
 #include <OCPlatform.h>
 #include <OCApi.h>
+#include <oic_malloc.h>
 #include <gtest/gtest.h>
 
 namespace OCPlatformTest
 {
     using namespace OC;
 
+    static const char* SVR_DB_FILE_NAME = "./oic_svr_db_server.dat";
     const OCResourceHandle HANDLE_ZERO = 0;
     const std::string gResourceTypeName = "core.res";
     const std::string gResourceInterface = DEFAULT_INTERFACE;
     const uint8_t gResourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
     OCResourceHandle resourceHandle;
-  //OCPersistent Storage Handlers
 
-   static FILE* client_open(const char *path, const char *mode)
-   {
-       std::cout << "<===Opening SVR DB file = './oic_svr_db_client.json' with mode = '"<< mode<<"' "<<std::endl;
-               return fopen("./oic_svr_db_client.json", mode);
-   }
+    //OCPersistent Storage Handlers
+    static FILE* client_open(const char * /*path*/, const char *mode)
+    {
+        std::cout << "<===Opening SVR DB file = './oic_svr_db_client.dat' with mode = '" << mode
+                << "' " << std::endl;
+        return fopen(SVR_DB_FILE_NAME, mode);
+    }
+        OCPersistentStorage gps {client_open, fread, fwrite, fclose, unlink, NULL, NULL };
+
     // Callbacks
-    OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
+    OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
     {
         return OC_EH_OK;
     }
 
-    void foundResource(std::shared_ptr<OCResource> resource)
+    void foundResource(std::shared_ptr<OCResource> /*resource*/)
+    {
+    }
+
+    void receivedDeviceInfo(const OCRepresentation& /*rep*/)
     {
     }
 
-    void receivedDeviceInfo(const OCRepresentation& rep)
+    void presenceHandler(OCStackResult /*result*/,
+            const unsigned int /*nonce*/, const std::string& /*hostAddress*/)
     {
     }
 
-    void presenceHandler(OCStackResult result,
-            const unsigned int nonce, const std::string& hostAddress)
+#ifdef WITH_CLOUD
+    void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
+    {
+    }
+#endif
+
+    void directPairHandler(std::shared_ptr<OCDirectPairing> /*dev*/, OCStackResult /*res*/)
+    {
+    }
+
+    void pairedHandler(const PairedDevices& /*list*/)
     {
     }
 
     //Helper methods
+    void DeleteStringLL(OCStringLL* ll)
+    {
+        if (!ll)
+        {
+            return;
+        }
+
+        DeleteStringLL(ll->next);
+        delete[] ll->value;
+        OICFree(ll);
+    }
+
     void DeleteDeviceInfo(OCDeviceInfo deviceInfo)
     {
         delete[] deviceInfo.deviceName;
-
+        DeleteStringLL(deviceInfo.types);
+        delete[] deviceInfo.specVersion;
+        DeleteStringLL(deviceInfo.dataModelVersions);
     }
 
     void DuplicateString(char ** targetString, std::string sourceString)
@@ -70,9 +103,35 @@ namespace OCPlatformTest
         strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
     }
 
+    bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, std::string value)
+    {
+        char *dup = NULL;
+        DuplicateString(&dup, value);
+        if (!*stringLL)
+        {
+            *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+            (*stringLL)->value = dup;
+            return true;
+        }
+        else
+        {
+            OCStringLL *temp = *stringLL;
+            while(temp->next)
+            {
+                temp = temp->next;
+            }
+            temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+            temp->next->value = dup;
+            return true;
+        }
+        return false;
+    }
+
     OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
     {
-        PlatformConfig cfg = {};
+        PlatformConfig cfg
+        { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+                OC::QualityOfService::LowQos, &gps };
         OCPlatform::Configure(cfg);
         EXPECT_EQ(OC_STACK_OK,OCPlatform::registerResource(
                                         resourceHandle, uri, type,
@@ -82,7 +141,9 @@ namespace OCPlatformTest
 
     OCResourceHandle RegisterResource(std::string uri, std::string type)
     {
-        PlatformConfig cfg = {};
+        PlatformConfig cfg
+        { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+                OC::QualityOfService::LowQos, &gps };
         OCPlatform::Configure(cfg);
         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
                                         resourceHandle, uri, type,
@@ -92,7 +153,9 @@ namespace OCPlatformTest
 
     OCResourceHandle RegisterResource(std::string uri)
     {
-        PlatformConfig cfg = {};
+        PlatformConfig cfg
+        { OC::ServiceType::OutOfProc, OC::ModeType::Server, "0.0.0.0", 0,
+                OC::QualityOfService::LowQos, &gps };
         OCPlatform::Configure(cfg);
         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
                                         resourceHandle, uri, gResourceTypeName,
@@ -153,12 +216,13 @@ namespace OCPlatformTest
 
     TEST(ConfigureTest, ConfigureServerOutProc)
     {
-        PlatformConfig cfg {
+        PlatformConfig cfg
+        {
             OC::ServiceType::OutOfProc,
             OC::ModeType::Server,
             "0.0.0.0",
             0,
-            OC::QualityOfService::LowQos
+            OC::QualityOfService::LowQos, &gps
         };
         std::string uri = "/a/light67";
         std::string type = "core.light";
@@ -193,7 +257,7 @@ namespace OCPlatformTest
             OC::ModeType::Server,
             "0.0.0.0",
             0,
-            OC::QualityOfService::LowQos
+            OC::QualityOfService::LowQos, &gps
         };
         OCPlatform::Configure(cfg);
 
@@ -207,14 +271,15 @@ namespace OCPlatformTest
         std::string uri = "/a/light70";
         std::string type = "core.light";
         uint8_t gResourceProperty = 0;
-        PlatformConfig cfg {
+        PlatformConfig cfg
+        {
             OC::ServiceType::InProc,
             OC::ModeType::Client,
             "0.0.0.0",
             0,
-            OC::QualityOfService::LowQos
+            OC::QualityOfService::LowQos,
+            &gps
         };
-        OCPlatform::Configure(cfg);
 
         EXPECT_NO_THROW(OCPlatform::registerResource(
                  resourceHandle, uri, type,
@@ -252,14 +317,13 @@ namespace OCPlatformTest
     //PersistentStorageTest
     TEST(ConfigureTest, ConfigurePersistentStorage)
     {
-        OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
-        PlatformConfig cfg {
+         PlatformConfig cfg {
              OC::ServiceType::InProc,
              OC::ModeType::Both,
              "0.0.0.0",
              0,
              OC::QualityOfService::LowQos,
-             &ps
+             &gps
          };
          OCPlatform::Configure(cfg);
          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
@@ -268,14 +332,13 @@ namespace OCPlatformTest
     //PersistentStorageTest
     TEST(ConfigureTest, ConfigureNULLHandlersPersistentStorage)
     {
-        OCPersistentStorage ps {client_open, nullptr, nullptr, nullptr, nullptr };
         PlatformConfig cfg {
              OC::ServiceType::InProc,
              OC::ModeType::Both,
              "0.0.0.0",
              0,
              OC::QualityOfService::LowQos,
-             &ps
+             &gps
          };
          OCPlatform::Configure(cfg);
          EXPECT_NO_THROW(OCPlatform::setDefaultDeviceEntityHandler(nullptr));
@@ -688,16 +751,20 @@ namespace OCPlatformTest
     TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithValidParameters)
     {
         OCDeviceInfo deviceInfo;
-
         DuplicateString(&deviceInfo.deviceName, "myDeviceName");
-
+        deviceInfo.types = NULL;
+        OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+        OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+        DuplicateString(&deviceInfo.specVersion, "mySpecVersion");
+        deviceInfo.dataModelVersions = nullptr;
+        OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, "myDataModelVersions");
         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerDeviceInfo(deviceInfo));
         EXPECT_NO_THROW(DeleteDeviceInfo(deviceInfo));
     }
 
     TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithEmptyObject)
     {
-        OCDeviceInfo di = {};
+        OCDeviceInfo di = {0, 0, 0, 0};
         EXPECT_ANY_THROW(OCPlatform::registerDeviceInfo(di));
     }
 
@@ -766,4 +833,89 @@ namespace OCPlatformTest
                 OC_MULTICAST_IP, CT_DEFAULT, &presenceHandler));
         EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
     }
+
+#ifdef WITH_CLOUD
+    // SubscribeDevicePresence Test
+    TEST(SubscribeDevicePresenceTest, DISABLED_SubscribeDevicePresenceWithValidParameters)
+    {
+        std::string hostAddress = "192.168.1.2:5000";
+        OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+        std::vector<std::string> di;
+
+        EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribeDevicePresence(presenceHandle,
+                hostAddress, di, CT_DEFAULT, &onObserve));
+    }
+
+    TEST(SubscribeDevicePresenceTest, SubscribeDevicePresenceWithNullHost)
+    {
+        OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+        std::vector<std::string> di;
+
+        EXPECT_ANY_THROW(OCPlatform::subscribeDevicePresence(presenceHandle,
+                        nullptr, di, CT_DEFAULT, &onObserve));
+    }
+
+    TEST(SubscribeDevicePresenceTest, SubscribeDevicePresenceWithNullOnObserve)
+    {
+        std::string hostAddress = "192.168.1.2:5000";
+        OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+        std::vector<std::string> di;
+
+        EXPECT_ANY_THROW(OCPlatform::subscribeDevicePresence(presenceHandle,
+                        hostAddress, di, CT_DEFAULT, NULL));
+    }
+
+    TEST(SubscribeDevicePresenceTest, DISABLED_UnsubscribePresenceWithValidHandle)
+    {
+        std::string hostAddress = "192.168.1.2:5000";
+        OCPlatform::OCPresenceHandle presenceHandle = nullptr;
+        std::vector<std::string> di;
+
+        EXPECT_EQ(OC_STACK_OK, OCPlatform::subscribeDevicePresence(presenceHandle,
+                hostAddress, di, CT_DEFAULT, &onObserve));
+        EXPECT_EQ(OC_STACK_OK, OCPlatform::unsubscribePresence(presenceHandle));
+    }
+#endif
+
+    TEST(FindDirectPairingTest, FindDirectPairingNullCallback)
+    {
+        EXPECT_ANY_THROW(OCPlatform::findDirectPairingDevices(1, nullptr));
+    }
+
+    TEST(FindDirectPairingTest, FindDirectPairingZeroTimeout)
+    {
+        EXPECT_ANY_THROW(OCPlatform::findDirectPairingDevices(0, &pairedHandler));
+    }
+
+    TEST(GetDirectPairedTest, GetDirectPairedNullCallback)
+    {
+        EXPECT_ANY_THROW(OCPlatform::getDirectPairedDevices(nullptr));
+    }
+
+    TEST(DoDirectPairingTest, DoDirectPairingNullCallback)
+    {
+        OCDPDev_t peer;
+        OCPrm_t pmSel = DP_PRE_CONFIGURED;
+        std::string pin("");
+        std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
+        EXPECT_ANY_THROW(OCPlatform::doDirectPairing(s_dp, pmSel, pin, nullptr));
+    }
+
+    TEST(DoDirectPairingTest, DoDirectPairingNullPeer)
+    {
+        OCDPDev_t peer;
+        OCPrm_t pmSel = DP_PRE_CONFIGURED;
+        std::string pin("");
+        std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
+        EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, &directPairHandler));
+    }
+
+    TEST(DoDirectPairingTest, DoDirectPairingNullPeerNullCallback)
+    {
+        OCDPDev_t peer;
+        OCPrm_t pmSel = DP_PRE_CONFIGURED;
+        std::string pin("");
+        std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
+        EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, nullptr));
+    }
 }