Loading system/stack/include/advertise_data_parser.h +8 −2 Original line number Diff line number Diff line Loading @@ -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. Loading system/stack/test/ad_parser_unittest.cc +24 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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) { Loading @@ -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) { Loading Loading
system/stack/include/advertise_data_parser.h +8 −2 Original line number Diff line number Diff line Loading @@ -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. Loading
system/stack/test/ad_parser_unittest.cc +24 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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) { Loading @@ -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) { Loading