OSDN Git Service

Fixed paired device config UUID parsing logic
authorAndre Eisenbach <eisenbach@google.com>
Sat, 30 Jan 2016 00:11:13 +0000 (16:11 -0800)
committerAndre Eisenbach <eisenbach@google.com>
Tue, 2 Feb 2016 18:07:28 +0000 (18:07 +0000)
Also added unit tests to cover this bug.

Bug: 26883553
Change-Id: Ice8641fad5c38ee43f1b080665dde70979f9d60f

btif/Android.mk
btif/include/btif_storage.h
btif/include/btif_util.h
btif/src/btif_storage.c
btif/src/btif_util.c
btif/test/btif_storage_test.cpp

index 167c375..303f7b4 100644 (file)
@@ -121,7 +121,7 @@ LOCAL_SRC_FILES := $(btifCommonSrc)
 LOCAL_CFLAGS := $(btifCommonCFlags)
 # Many .h files have redefined typedefs
 LOCAL_CLANG_CFLAGS += -Wno-error=typedef-redefinition
-LOCAL_SHARED_LIBRARIES := libc liblog
+LOCAL_SHARED_LIBRARIES := libcutils liblog
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE := libbtif
index 758436f..32df1e7 100644 (file)
@@ -352,4 +352,9 @@ bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
 bt_status_t btif_storage_get_remote_version(const bt_bdaddr_t *remote_bd_addr,
                                   bt_remote_version_t *p_ver);
 
+/******************************************************************************
+ * Exported for unit tests
+ *****************************************************************************/
+size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids);
+
 #endif /* BTIF_STORAGE_H */
index 50ac9af..ebba177 100644 (file)
@@ -68,8 +68,14 @@ UINT32 devclass2uint(DEV_CLASS dev_class);
 void uint2devclass(UINT32 dev, DEV_CLASS dev_class);
 void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128);
 
+// Takes a |str| containing a 128-bit GUID formatted UUID and stores the
+// result in |p_uuid|. |str| must be formatted in this format:
+//   "12345678-1234-1234-1234-123456789012"
+// |p_uuid| cannot be null. Returns true if parsing was successful, false
+// otherwise. Returns false if |str| is null.
+bool string_to_uuid(const char *str, bt_uuid_t *p_uuid);
+
 void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str);
-void string_to_uuid(const char *str, bt_uuid_t *p_uuid);
 int ascii_2_hex (const char *p_ascii, int len, UINT8 *p_hex);
 
 #endif /* BTIF_UTIL_H */
index 6da4514..917508c 100644 (file)
@@ -187,38 +187,6 @@ bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
 **  Static functions
 ************************************************************************************/
 
-/*******************************************************************************
-**
-** Function         btif_in_split_uuids_string_to_list
-**
-** Description      Internal helper function to split the string of UUIDs
-**                  read from the NVRAM to an array
-**
-** Returns          None
-**
-*******************************************************************************/
-static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
-                                               uint32_t *p_num_uuid)
-{
-    char buf[64];
-    char *p_start = str;
-    char *p_needle;
-    uint32_t num = 0;
-    do
-    {
-        //p_needle = strchr(p_start, ';');
-        p_needle = strchr(p_start, ' ');
-        if (p_needle < p_start) break;
-        memset(buf, 0, sizeof(buf));
-        strncpy(buf, p_start, (p_needle-p_start));
-        string_to_uuid(buf, p_uuid + num);
-        num++;
-        p_start = ++p_needle;
-
-    } while (*p_start != 0);
-    *p_num_uuid = num;
-}
-
 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
 {
     bdstr_t bdstr = {0};
@@ -389,8 +357,7 @@ static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
                                     BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
             {
                 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
-                uint32_t num_uuids = 0;
-                btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
+                size_t num_uuids = btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS);
                 prop->len = num_uuids * sizeof(bt_uuid_t);
                 ret = TRUE;
             }
@@ -594,6 +561,34 @@ static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bd
 
 /*******************************************************************************
 **
+** Function         btif_split_uuids_string
+**
+** Description      Internal helper function to split the string of UUIDs
+**                  read from the NVRAM to an array
+**
+** Returns          Number of UUIDs parsed from the supplied string
+**
+*******************************************************************************/
+size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids)
+{
+    assert(str);
+    assert(p_uuid);
+
+    size_t num_uuids = 0;
+    while (str && num_uuids < max_uuids)
+    {
+        bool rc = string_to_uuid(str, p_uuid++);
+        if (!rc) break;
+        num_uuids++;
+        str = strchr(str, ' ');
+        if (str) str++;
+    }
+
+    return num_uuids;
+}
+
+/*******************************************************************************
+**
 ** Function         btif_storage_get_adapter_property
 **
 ** Description      BTIF storage API - Fetches the adapter property->type
index 27d68c2..f4cffcb 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "btif_util.h"
 
+#include <assert.h>
 #include <ctype.h>
 #include <netinet/in.h>
 #include <stdio.h>
@@ -111,13 +112,19 @@ void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128)
     memcpy(uuid128->uu + 2, &uuid16_bo, sizeof(uint16_t));
 }
 
-void string_to_uuid(const char *str, bt_uuid_t *p_uuid)
+bool string_to_uuid(const char *str, bt_uuid_t *p_uuid)
 {
+    assert(p_uuid);
+    if (str == NULL)
+        return false;
+
     uint32_t uuid0, uuid4;
     uint16_t uuid1, uuid2, uuid3, uuid5;
 
-    sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
+    int rc = sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
                 &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
+    if (rc != 6)
+        return false;
 
     uuid0 = htonl(uuid0);
     uuid1 = htons(uuid1);
@@ -133,8 +140,7 @@ void string_to_uuid(const char *str, bt_uuid_t *p_uuid)
     memcpy(&(p_uuid->uu[10]), &uuid4, 4);
     memcpy(&(p_uuid->uu[14]), &uuid5, 2);
 
-    return;
-
+    return true;
 }
 
 void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str)
index 0e33327..76fcb95 100644 (file)
 #include <gtest/gtest.h>
 
 extern "C" {
+#include "btif/include/btif_storage.h"
 #include "btif/include/btif_util.h"
 }
 
 TEST(BtifStorageTest, test_string_to_uuid) {
   const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22";
-  const uint8_t o1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
-                     0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
+  const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
+                        0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
 
   bt_uuid_t uuid;
   memset(&uuid, 0, sizeof(uuid));
-  EXPECT_FALSE(memcmp(&uuid, o1, sizeof(o1)) == 0);
+  EXPECT_FALSE(memcmp(&uuid, u1, sizeof(u1)) == 0);
 
-  string_to_uuid(s1, &uuid);
-  EXPECT_TRUE(memcmp(&uuid, o1, sizeof(o1)) == 0);
+  bool rc = string_to_uuid(s1, &uuid);
+  EXPECT_TRUE(rc);
+  EXPECT_TRUE(memcmp(&uuid, u1, sizeof(u1)) == 0);
 }
 
+TEST(BtifStorageTest, test_string_to_uuid_invalid) {
+  bt_uuid_t uuid;
+  bool rc = string_to_uuid("This is not a UUID", &uuid);
+  EXPECT_FALSE(rc);
+}
+
+TEST(BtifStorageTest, test_uuid_split_multiple) {
+  const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22 e39c6285-867f-4b1d-9db0-35fbd9aebf23";
+  const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
+                        0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
+  const uint8_t u2[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
+                        0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x23};
+
+  bt_uuid_t uuids[2];
+  size_t num_uuids = btif_split_uuids_string(s1, uuids, 2);
+  EXPECT_EQ(num_uuids, 2u);
+  EXPECT_TRUE(memcmp(&uuids[0], u1, sizeof(u1)) == 0);
+  EXPECT_TRUE(memcmp(&uuids[1], u2, sizeof(u2)) == 0);
+}
+
+TEST(BtifStorageTest, test_uuid_split_partial) {
+  const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22 e39c6285-867f-4b1d-9db0-35fbd9aebf23";
+
+  bt_uuid_t uuids[2];
+  size_t num_uuids = btif_split_uuids_string(s1, uuids, 1);
+  EXPECT_EQ(num_uuids, 1u);
+}