Allow empty array in OCRepPayloadSetIntArray
authorAbhishek Pandey <abhi.siso@samsung.com>
Mon, 24 Jul 2017 12:00:14 +0000 (17:30 +0530)
committerAshok Babu Channa <ashok.channa@samsung.com>
Mon, 31 Jul 2017 07:05:57 +0000 (07:05 +0000)
[IOT-2458] OCRepPayloadSetIntArray() did not allow empty
array.  Fixed by not returning false when dimTotal is 0
and instead letting the function to get executed when input
array is NULL & dimTotal is 0.

Also fixed the usage of API in easy setup enrollee code.
Now using OCRepPayloadSetIntArrayAsOwner() instead of
OCRepPayloadSetIntArray() as the later duplicates the
memory which can be avoided "by giving payload ownership
to stack".

Added unit test in cbortests to test new API behavior.

Change-Id: If5352b1faf5a9355dd0f8a22882a6af545df6d29
Signed-off-by: Abhishek Pandey <abhi.siso@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21591
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Reviewed-by: Todd Malsbary <todd.malsbary@intel.com>
Reviewed-by: Harish Marappa <h.marappa@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/csdk/stack/src/ocpayload.c
resource/csdk/stack/test/cbortests.cpp
service/easy-setup/enrollee/src/resourcehandler.c

index 837515e..41c6bd0 100644 (file)
@@ -1039,21 +1039,14 @@ bool OC_CALL OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
         const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
 {
     size_t dimTotal = calcDimTotal(dimensions);
-    if (dimTotal == 0)
-    {
-        return false;
-    }
 
     int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
 
-    if (!newArray)
+    if (newArray && array)
     {
-        return false;
+        memcpy(newArray, array, dimTotal * sizeof(int64_t));
     }
 
-    memcpy(newArray, array, dimTotal * sizeof(int64_t));
-
-
     bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
     if (!b)
     {
index 1ac75b6..4d78597 100644 (file)
@@ -401,3 +401,35 @@ TEST(CborHeterogeneousArrayTest, ConvertParseTest)
     OICFree(payload_cbor);
     OCPayloadDestroy(payload_out);
 }
+
+TEST(CborEmptyArrayTest, EmptyArrayArraySetGetTest)
+{
+    OCRepPayload* payload_in = OCRepPayloadCreate();
+    ASSERT_TRUE(payload_in != NULL);
+
+    OCRepPayloadSetUri(payload_in, "/a/quake_sensor");
+    OCRepPayloadSetPropInt(payload_in, "scale", 4);
+
+    size_t dimensions_in[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
+
+    EXPECT_EQ(true, OCRepPayloadSetIntArray(payload_in, "quakedata",
+                NULL, dimensions_in));
+
+    int64_t* quakedata_out = NULL;
+    size_t dimensions_out[MAX_REP_ARRAY_DEPTH] = {0};
+
+    ///TODO: Change Assert to true after fixing OCRepPayloadGetIntArray behaviour
+    ASSERT_EQ(false, OCRepPayloadGetIntArray(payload_in, "quakedata",
+                &quakedata_out, dimensions_out));
+
+    ASSERT_TRUE(quakedata_out == NULL);
+
+    for(size_t i = 0; i < MAX_REP_ARRAY_DEPTH; i++)
+    {
+        EXPECT_EQ(0, dimensions_out[i]);
+    }
+
+    // Cleanup
+    OCRepPayloadDestroy(payload_in);
+}
+
index 2f67684..60a9972 100644 (file)
@@ -998,7 +998,7 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
             (ehRequest->query && !strcmp(ehRequest->query, "")) ||
             (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
         {
-            OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
+            OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Default interface)");
             OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
@@ -1023,13 +1023,19 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
                 {
                     connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
                 }
-                OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
+
+                bool b = OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
+                if (!b)
+                {
+                    OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
+                    OICFree(connectRequest);
+                }
             }
             else
             {
                 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
                 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
-                OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
+                OCRepPayloadSetIntArrayAsOwner(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
             }
 
             if (gWriteUserdataCb)
@@ -1047,7 +1053,7 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
         ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
 
     {
-        OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
+        OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res (Batch Interface)");
         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
 
         OCRepPayload* repPayload = NULL;
@@ -1102,13 +1108,19 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
             {
                 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
             }
-            OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
+
+            bool b = OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
+            if (!b)
+            {
+                OIC_LOG(ERROR, ES_RH_TAG, "Failed to set array value for Connect property");
+                OICFree(connectRequest);
+            }
         }
         else
         {
             OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
-            OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
+            OCRepPayloadSetIntArrayAsOwner(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
         }
 
         if (gWriteUserdataCb)