Add unittests for ConstructResourceTest for invalid host values
authorRavi Nanjundappa <nravi.n@samsung.com>
Mon, 10 Aug 2015 10:10:24 +0000 (15:40 +0530)
committerErich Keane <erich.keane@intel.com>
Mon, 10 Aug 2015 14:43:42 +0000 (14:43 +0000)
Added a test case for ConstructResourceTest to simulate the scenario of
Resource creation with host
* not having the ip or device address and
* not having the "coap://" protocol prefix.

Change-Id: Ic3f1b250950ad2c3a251a56a85e7356c06ac34fb
Signed-off-by: Ravi Nanjundappa <nravi.n@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2154
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/src/OCResource.cpp
resource/unittests/OCResourceTest.cpp

index 1d35ea2..a06d1fa 100644 (file)
@@ -125,7 +125,7 @@ void OCResource::setHost(const std::string& host)
 
         size_t found = host_token.find(']');
 
-        if(found == std::string::npos)
+        if(found == std::string::npos || found == 0)
         {
             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
@@ -141,7 +141,7 @@ void OCResource::setHost(const std::string& host)
     {
         size_t found = host_token.find(':');
 
-        if(found == std::string::npos)
+        if(found == std::string::npos || found == 0)
         {
             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
index 2a3c142..dda67ac 100644 (file)
@@ -76,6 +76,16 @@ namespace OCResourceTest
         EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2", "/resource"));
     }
 
+    TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost)
+    {
+        EXPECT_ANY_THROW(ConstructResourceObject("192.168.1.2:5000", "/resource"));
+    }
+
+    TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost2)
+    {
+        EXPECT_ANY_THROW(ConstructResourceObject("coap://:5000", "/resource"));
+    }
+
     TEST(ResourceGetTest, DISABLED_ResourceGetForValidUri)
     {
         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");