Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0019da21 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by android-build-merger
Browse files

Merge "Advertise data parser - allow zero padding at end of packet" am: ce64cd08

am: 81cc8a24

Change-Id: I348cb08f9ab8bf1bd5935e5a6e8820be45f2068d
parents a84b9f42 81cc8a24
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -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.
+24 −2
Original line number Diff line number Diff line
@@ -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) {