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;
// %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)] = ']';
}
{
#include "ocstack.h"
#include "logger.h"
+ #include "ocmalloc.h"
}
#include "gtest/gtest.h"
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);
+
+}
+
+