Platform ID must be a UUID to be valid
authorDave Thaler <dthaler@microsoft.com>
Thu, 1 Dec 2016 02:10:31 +0000 (18:10 -0800)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Tue, 6 Dec 2016 19:08:53 +0000 (19:08 +0000)
Change-Id: Ifbbeb18fa2f2bf73c857ad3bfc25b3706d972d61
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14991
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
resource/csdk/security/src/srmutility.c
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/test/stacktests.cpp

index 93d1e45..6263113 100644 (file)
@@ -25,6 +25,7 @@
 #include "logger.h"
 #include "oic_malloc.h"
 #include "base64.h"
+#include "ocrandom.h"
 
 #define TAG  "OIC_SRM_UTILITY"
 
@@ -139,44 +140,6 @@ exit:
 
 OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
 {
-    if(NULL == strUuid || NULL == uuid)
-    {
-        OIC_LOG(ERROR, TAG, "ConvertStrToUuid : Invalid param");
-        return OC_STACK_INVALID_PARAM;
-    }
-
-    size_t urnIdx = 0;
-    size_t uuidIdx = 0;
-    size_t strUuidLen = 0;
-    char convertedUuid[UUID_LENGTH * 2] = {0};
-
-    strUuidLen = strlen(strUuid);
-    if(0 == strUuidLen)
-    {
-        OIC_LOG(INFO, TAG, "The empty string detected, The UUID will be converted to "\
-                           "\"00000000-0000-0000-0000-000000000000\"");
-    }
-    else if(UUID_LENGTH * 2 + 4 == strUuidLen)
-    {
-        for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2)
-        {
-            if(*(strUuid + urnIdx) == '-')
-            {
-                urnIdx++;
-            }
-            sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
-        }
-    }
-    else
-    {
-        OIC_LOG(ERROR, TAG, "Invalid string uuid format, Please set the uuid as correct format");
-        OIC_LOG(ERROR, TAG, "e.g) \"72616E64-5069-6E44-6576-557569643030\" (4-2-2-2-6)");
-        OIC_LOG(ERROR, TAG, "e.g) \"\"");
-
-        return OC_STACK_INVALID_PARAM;
-    }
-
-    memcpy(uuid->id, convertedUuid, UUID_LENGTH);
-
-    return OC_STACK_OK;
+    OCRandomUuidResult result = OCConvertStringToUuid(strUuid, uuid->id);
+    return (result == RAND_UUID_OK) ? OC_STACK_OK : OC_STACK_INVALID_PARAM;
 }
index 0a0b126..65ea233 100755 (executable)
@@ -1432,6 +1432,18 @@ OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
         goto exit;
     }
 
+    /*
+     * @todo (IOT-1541) There are several versions of a UUID structure and conversion
+     * methods scattered around the IoTivity code base.  They need to be combined
+     * into one PAL API.
+     */
+    uint8_t uuid[UUID_SIZE];
+    if (OCConvertStringToUuid(info.platformID, uuid) != RAND_UUID_OK)
+    {
+        OIC_LOG(ERROR, TAG, "Platform ID is not a UUID.");
+        goto exit;
+    }
+
     resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
     if (!resource)
     {
index 8fd4cd6..9f7acfd 100644 (file)
@@ -63,7 +63,7 @@ namespace itst = iotivity::test;
 //-----------------------------------------------------------------------------
 static const char TAG[] = "TestHarness";
 
-char gDeviceUUID[] = "myDeviceUUID";
+char gDeviceUUID[] = "fe3f9a68-4931-4cb0-9ea4-81702b43116c";
 char gManufacturerName[] = "myName";
 static OCPrm_t pmSel;
 static char pinNumber;
@@ -301,6 +301,22 @@ TEST(StackStart, SetPlatformInfoWithNoPlatformID)
     EXPECT_EQ(OC_STACK_OK, OCStop());
 }
 
+TEST(StackStart, SetPlatformInfoWithBadPlatformID)
+{
+    itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
+    EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
+
+    OCPlatformInfo info =
+     {
+         "myDeviceUUID",
+         gManufacturerName,
+         0, 0, 0, 0, 0, 0, 0, 0, 0
+     };
+
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSetPlatformInfo(info));
+    EXPECT_EQ(OC_STACK_OK, OCStop());
+}
+
 TEST(StackStart, SetPlatformInfoWithNoManufacturerName)
 {
     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);