diff options
| author | Jakub Pawlowski <jpawlowski@google.com> | 2017-07-14 15:37:57 -0700 |
|---|---|---|
| committer | Sam Mortimer <sam@mortimer.me.uk> | 2017-11-09 22:25:26 -0800 |
| commit | 2f800e29417b1fb4bb25ca21d5b639dab5f4ee56 (patch) | |
| tree | a1016c89f3e7add7e0508e021177959bece0fb19 | |
| parent | 9fb197b6bee3f8ecabf1f193bbff5227398e5014 (diff) | |
| download | android_system_bt-lineage-15.0.tar.gz android_system_bt-lineage-15.0.tar.bz2 android_system_bt-lineage-15.0.zip | |
Advertise data parser - allow zero padding at end of packetlineage-15.0
Test: AdvertiseDataParserTest
Bug: 63123881
Change-Id: I8be9e693de557951b1048840759b5658331e9b3b
| -rw-r--r-- | stack/include/advertise_data_parser.h | 10 | ||||
| -rw-r--r-- | stack/test/ad_parser_unittest.cc | 26 |
2 files changed, 32 insertions, 4 deletions
diff --git a/stack/include/advertise_data_parser.h b/stack/include/advertise_data_parser.h index 24ee2b378..1c5f99f8f 100644 --- a/stack/include/advertise_data_parser.h +++ b/stack/include/advertise_data_parser.h @@ -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. diff --git a/stack/test/ad_parser_unittest.cc b/stack/test/ad_parser_unittest.cc index 46ff3ec48..3202130a3 100644 --- a/stack/test/ad_parser_unittest.cc +++ b/stack/test/ad_parser_unittest.cc @@ -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) { |
