Fixed presence payload parsing logic (IOT-406)
authorSachin Agrawal <sachin.agrawal@intel.com>
Mon, 23 Mar 2015 19:14:23 +0000 (12:14 -0700)
committerSashi Penta <sashi.kumar.penta@intel.com>
Mon, 23 Mar 2015 19:58:48 +0000 (19:58 +0000)
Updated method parsePresencePayload to handle scenarios
when payload is not in the expected presence format.
Also added unit test to verify parsing functionality.

Change-Id: I47d995b8f49cc96805bf837ac2c2f77c1859da82
Signed-off-by: Sachin Agrawal <sachin.agrawal@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/538
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
Reviewed-by: Sashi Penta <sashi.kumar.penta@intel.com>
resource/csdk/stack/src/ocstack.c
resource/csdk/stack/test/stacktests.cpp

index 7f7079c6ab2238ca4ac8af21d99f1a0dd9cccd11..92a94b88bd26fd2f1a7402cf60d1fd5566fcd448 100644 (file)
@@ -357,6 +357,7 @@ exit:
     return ret;
 }
 
+// Note: Caller should invoke OCFree after done with resType pointer
 void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType)
 {
     char * tok = NULL;
@@ -365,27 +366,47 @@ void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, cha
     // %u : sequence number,
     // %u : max age
     // %s : Resource Type (Optional)
+
+    if (!payload || !seqNum || !maxAge || !resType)
+    {
+        return;
+    }
     tok = strtok_r(payload, "[:]}", &savePtr);
     payload[strlen(payload)] = ':';
+
+    //Retrieve sequence number
     tok = strtok_r(NULL, "[:]}", &savePtr);
+    if(tok == NULL)
+    {
+        return;
+    }
     payload[strlen((char *)payload)] = ':';
     *seqNum = (uint32_t) atoi(tok);
 
+    //Retrieve MaxAge
     tok = strtok_r(NULL, "[:]}", &savePtr);
+    if(tok == NULL)
+    {
+        return;
+    }
     *maxAge = (uint32_t) atoi(tok);
+
+    //Retrieve ResourceType
     tok = strtok_r(NULL, "[:]}",&savePtr);
+    if(tok == NULL)
+    {
+        return;
+    }
 
-    if(tok)
+    *resType = (char *)OCMalloc(strlen(tok) + 1);
+    if(!*resType)
     {
-        *resType = (char *)OCMalloc(strlen(tok) + 1);
-        if(!*resType)
-        {
-            return;
-        }
-        payload[strlen((char *)payload)] = ':';
-        strcpy(*resType, tok);
-        OC_LOG_V(DEBUG, TAG, "resourceTypeName %s", *resType);
+        return;
     }
+    payload[strlen((char *)payload)] = ':';
+    strcpy(*resType, tok);
+    OC_LOG_V(DEBUG, TAG, "resourceTypeName %s", *resType);
+
     payload[strlen((char *)payload)] = ']';
 }
 
index 63dbe04c5c072302d8c8ad834e22dfa0f4e1a3e1..2d79c9e570410c77d8848a6e9a3705f1d0307cfc 100644 (file)
@@ -23,6 +23,7 @@ extern "C"
 {
     #include "ocstack.h"
     #include "logger.h"
+    #include "ocmalloc.h"
 }
 
 #include "gtest/gtest.h"
@@ -1225,3 +1226,78 @@ TEST(StackResourceAccess, DeleteMiddleResource)
     EXPECT_EQ(OC_STACK_OK, OCStop());
 }
 
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+    void parsePresencePayload(char* payload, uint32_t* seqNum, uint32_t* maxAge, char** resType);
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+TEST(StackPresence, ParsePresencePayload)
+{
+
+    OC_LOG(INFO, TAG, "Starting ParsePresencePayload test");
+
+    char payload[100];
+    uint32_t seqNum = 0, maxAge = 0;
+    char * resType = NULL;
+
+    //Good Scenario
+    strncpy(payload, "{\"oc\":[100:99:presence]}", sizeof(payload));
+    parsePresencePayload(payload, &seqNum, &maxAge, &resType);
+    EXPECT_TRUE(100 == seqNum);
+    EXPECT_TRUE(99 == maxAge);
+    EXPECT_STREQ("presence", resType);
+    OCFree(resType);
+
+    //Bad Scenario -- should not result in Seg Fault
+    parsePresencePayload(payload, NULL, &maxAge, &resType);
+
+    //Bad Scenario
+    seqNum = 0; maxAge = 0; resType = NULL;
+    strncpy(payload, "{abracadabra}", sizeof(payload));
+    parsePresencePayload(payload, &seqNum, &maxAge, &resType);
+    EXPECT_TRUE(0 == seqNum);
+    EXPECT_TRUE(0 == maxAge);
+    EXPECT_EQ(NULL, resType);
+    OCFree(resType);
+
+    //Bad Scenario
+    seqNum = 0; maxAge = 0; resType = NULL;
+    strncpy(payload, "{\"oc\":[100]}", sizeof(payload));
+    parsePresencePayload(payload, &seqNum, &maxAge, &resType);
+    EXPECT_TRUE(100 == seqNum);
+    EXPECT_TRUE(0 == maxAge);
+    EXPECT_EQ(NULL, resType);
+    OCFree(resType);
+
+    //Bad Scenario
+    seqNum = 0; maxAge = 0; resType = NULL;
+    strncpy(payload, "{\"oc\":[]}", sizeof(payload));
+    parsePresencePayload(payload, &seqNum, &maxAge, &resType);
+    EXPECT_TRUE(0 == seqNum);
+    EXPECT_TRUE(0 == maxAge);
+    EXPECT_EQ(NULL, resType);
+    OCFree(resType);
+
+    //Bad Scenario
+    strncpy(payload, "{:]}", sizeof(payload));
+    parsePresencePayload(payload, &seqNum, &maxAge, &resType);
+    EXPECT_TRUE(0 == seqNum);
+    EXPECT_TRUE(0 == maxAge);
+    EXPECT_EQ(NULL, resType);
+    OCFree(resType);
+
+    //Bad Scenario
+    strncpy(payload, "{:[presence}", sizeof(payload));
+    parsePresencePayload(payload, &seqNum, &maxAge, &resType);
+    EXPECT_TRUE(0 == seqNum);
+    EXPECT_TRUE(0 == maxAge);
+    EXPECT_EQ(NULL, resType);
+    OCFree(resType);
+
+}
+
+