OSDN Git Service

Advertise data parser - allow zero padding at end of packet
authorJakub Pawlowski <jpawlowski@google.com>
Fri, 14 Jul 2017 22:37:57 +0000 (15:37 -0700)
committerpkanwar <pkanwar@google.com>
Sat, 15 Jul 2017 00:15:56 +0000 (17:15 -0700)
Test: AdvertiseDataParserTest
Bug: 63123881
Change-Id: I8be9e693de557951b1048840759b5658331e9b3b
(cherry picked from commit 08b907b58b0fd8334268413f5e383c1a546f9b3a)

stack/include/advertise_data_parser.h
stack/test/ad_parser_unittest.cc

index 24ee2b3..1c5f99f 100644 (file)
@@ -33,8 +33,14 @@ class AdvertiseDataParser {
       uint8_t len = ad[position];
 
       // A field length of 0 would be invalid as it should at least contain the
-      // EIR field type.
-      if (len == 0) return false;
+      // EIR field type. However, some existing devices send zero padding at the
+      // end of advertisement. If this is the case, treat the packet as valid.
+      if (len == 0) {
+        for (size_t i = position + 1; i < ad_len; i++) {
+          if (ad[i] != 0) return false;
+        }
+        return true;
+      }
 
       // If the length of the current field would exceed the total data length,
       // then the data is badly formatted.
index 46ff3ec..3202130 100644 (file)
@@ -23,9 +23,9 @@ TEST(AdvertiseDataParserTest, IsValidEmpty) {
   const std::vector<uint8_t> data0;
   EXPECT_TRUE(AdvertiseDataParser::IsValid(data0));
 
-  // Single empty field not allowed.
+  // Single empty field allowed (treated as zero padding).
   const std::vector<uint8_t> data1{0x00};
-  EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
+  EXPECT_TRUE(AdvertiseDataParser::IsValid(data1));
 }
 
 TEST(AdvertiseDataParserTest, IsValidBad) {
@@ -44,6 +44,16 @@ TEST(AdvertiseDataParserTest, IsValidBad) {
   // Two fields, second field empty.
   const std::vector<uint8_t> data3{0x02, 0x02, 0x00, 0x01};
   EXPECT_FALSE(AdvertiseDataParser::IsValid(data3));
+
+  // Non-zero padding at end of packet.
+  const std::vector<uint8_t> data4{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01,
+                                   0x00, 0x00, 0xBA, 0xBA, 0x00, 0x00};
+  EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
+
+  // Non-zero padding at end of packet.
+  const std::vector<uint8_t> data5{0x03, 0x02, 0x01, 0x02, 0x02,
+                                   0x03, 0x01, 0x00, 0xBA};
+  EXPECT_FALSE(AdvertiseDataParser::IsValid(data1));
 }
 
 TEST(AdvertiseDataParserTest, IsValidGood) {
@@ -54,6 +64,18 @@ TEST(AdvertiseDataParserTest, IsValidGood) {
   // Two fields.
   const std::vector<uint8_t> data1{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01};
   EXPECT_TRUE(AdvertiseDataParser::IsValid(data1));
+
+  // Zero padding at end of packet.
+  const std::vector<uint8_t> data2{0x03, 0x02, 0x01, 0x02,
+                                   0x02, 0x03, 0x01, 0x00};
+  EXPECT_TRUE(AdvertiseDataParser::IsValid(data2));
+
+  // zero padding at end of packet, sample data from real device
+  const std::vector<uint8_t> data3{
+      0x10, 0x096, 0x85, 0x44, 0x32, 0x04, 0x74, 0x32, 0x03, 0x13, 0x93,
+      0xa,  0x32,  0x39, 0x3a, 0x65, 0x32, 0x05, 0x12, 0x50, 0x00, 0x50,
+      0x00, 0x02,  0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+  EXPECT_TRUE(AdvertiseDataParser::IsValid(data3));
 }
 
 TEST(AdvertiseDataParserTest, GetFieldByType) {