ble_scan_parser_impl_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/data_decoder/ble_scan_parser_impl.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace data_decoder {
  7. TEST(BleScanParserImplTest, ParseBadUuidLengthReturnsEmptyString) {
  8. std::vector<uint8_t> bad_uuid(0xab, 5);
  9. EXPECT_EQ(
  10. 0ul,
  11. BleScanParserImpl::ParseUuid(bad_uuid, UuidFormat::kFormat16Bit).size());
  12. EXPECT_EQ(
  13. 0ul,
  14. BleScanParserImpl::ParseUuid(bad_uuid, UuidFormat::kFormat32Bit).size());
  15. EXPECT_EQ(
  16. 0ul,
  17. BleScanParserImpl::ParseUuid(bad_uuid, UuidFormat::kFormat128Bit).size());
  18. }
  19. TEST(BleScanParserImplTest, Parse16BitUuid) {
  20. const uint8_t kUuid16[] = {0xab, 0xcd};
  21. const char kExpected[] = "0000CDAB-0000-1000-8000-00805F9B34FB";
  22. std::string actual =
  23. BleScanParserImpl::ParseUuid(kUuid16, UuidFormat::kFormat16Bit);
  24. EXPECT_STREQ(kExpected, actual.c_str());
  25. }
  26. TEST(BleScanParserImplTest, Parse32BitUuid) {
  27. const uint8_t kUuid32[] = {0xab, 0xcd, 0xef, 0x01};
  28. const char kExpected[] = "01EFCDAB-0000-1000-8000-00805F9B34FB";
  29. std::string actual =
  30. BleScanParserImpl::ParseUuid(kUuid32, UuidFormat::kFormat32Bit);
  31. EXPECT_STREQ(kExpected, actual.c_str());
  32. }
  33. TEST(BleScanParserImplTest, Parse128BitUuid) {
  34. const uint8_t kUuid128[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
  35. 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89};
  36. const char kExpected[] = "89674523-01EF-CDAB-8967-452301EFCDAB";
  37. std::string actual =
  38. BleScanParserImpl::ParseUuid(kUuid128, UuidFormat::kFormat128Bit);
  39. EXPECT_STREQ(kExpected, actual.c_str());
  40. }
  41. TEST(BleScanParserImplTest, Parse16BitServiceUuids) {
  42. std::vector<device::BluetoothUUID> expected = {
  43. device::BluetoothUUID("0000CDAB-0000-1000-8000-00805F9B34FB"),
  44. device::BluetoothUUID("000001EF-0000-1000-8000-00805F9B34FB"),
  45. device::BluetoothUUID("00004523-0000-1000-8000-00805F9B34FB"),
  46. device::BluetoothUUID("00008967-0000-1000-8000-00805F9B34FB"),
  47. device::BluetoothUUID("0000CDAB-0000-1000-8000-00805F9B34FB"),
  48. device::BluetoothUUID("000001EF-0000-1000-8000-00805F9B34FB"),
  49. device::BluetoothUUID("00004523-0000-1000-8000-00805F9B34FB"),
  50. device::BluetoothUUID("00008967-0000-1000-8000-00805F9B34FB"),
  51. };
  52. const uint8_t kUuids[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
  53. 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89};
  54. std::vector<device::BluetoothUUID> actual;
  55. BleScanParserImpl::ParseServiceUuids(kUuids, UuidFormat::kFormat16Bit,
  56. &actual);
  57. EXPECT_EQ(expected, actual);
  58. }
  59. TEST(BleScanParserImplTest, Parse32BitServiceUuids) {
  60. std::vector<device::BluetoothUUID> expected = {
  61. device::BluetoothUUID("01EFCDAB-0000-1000-8000-00805F9B34FB"),
  62. device::BluetoothUUID("89674523-0000-1000-8000-00805F9B34FB"),
  63. device::BluetoothUUID("01EFCDAB-0000-1000-8000-00805F9B34FB"),
  64. device::BluetoothUUID("89674523-0000-1000-8000-00805F9B34FB"),
  65. };
  66. const uint8_t kUuids[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
  67. 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89};
  68. std::vector<device::BluetoothUUID> actual;
  69. BleScanParserImpl::ParseServiceUuids(kUuids, UuidFormat::kFormat32Bit,
  70. &actual);
  71. EXPECT_EQ(expected, actual);
  72. }
  73. TEST(BleScanParserImplTest, Parse128BitServiceUuids) {
  74. std::vector<device::BluetoothUUID> expected = {
  75. device::BluetoothUUID("89674523-01EF-CDAB-8967-452301EFCDAB"),
  76. device::BluetoothUUID("89674523-01EF-CDAB-01EF-CDAB89674523"),
  77. };
  78. const uint8_t kUuids[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
  79. 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
  80. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
  81. 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89};
  82. std::vector<device::BluetoothUUID> actual;
  83. BleScanParserImpl::ParseServiceUuids(kUuids, UuidFormat::kFormat128Bit,
  84. &actual);
  85. EXPECT_EQ(expected, actual);
  86. }
  87. TEST(BleScanParserImplTest, ParseBleAdvertisingScan) {
  88. std::vector<device::BluetoothUUID> expected_service_uuids = {
  89. device::BluetoothUUID("0000ABCD-0000-1000-8000-00805F9B34FB"),
  90. device::BluetoothUUID("0000EF01-0000-1000-8000-00805F9B34FB"),
  91. device::BluetoothUUID("ABCDEF01-0000-1000-8000-00805F9B34FB"),
  92. device::BluetoothUUID("23456789-0000-1000-8000-00805F9B34FB"),
  93. device::BluetoothUUID("ABCDEF01-2345-6789-ABCD-EF0123456789"),
  94. };
  95. std::vector<uint8_t> service_data = {0xa1, 0xb2, 0xc3, 0xd4, 0xe5};
  96. base::flat_map<std::string, std::vector<uint8_t>> expected_service_data_map;
  97. expected_service_data_map["0000DCAB-0000-1000-8000-00805F9B34FB"] =
  98. service_data;
  99. std::vector<uint8_t> manufacturer_data = {0x1a, 0x2b, 0x3c, 0x4d};
  100. base::flat_map<uint16_t, std::vector<uint8_t>> expected_manufacturer_data_map;
  101. expected_manufacturer_data_map[0xd00d] = manufacturer_data;
  102. const uint8_t kRawData[] = {
  103. // Length of the rest of the section, field type, data.
  104. // Advertising flag = 0x42
  105. 0x02, 0x01, 0x42,
  106. // 16-bit service UUIDs 0000abcd-... and 0000ef01-...
  107. 0x05, 0x02, 0xcd, 0xab, 0x01, 0xef,
  108. // TX power = 0x1b
  109. 0x02, 0x0a, 0x1b,
  110. // 32-bit service UUIDs abcdef01-... and 23456789-...
  111. 0x09, 0x05, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23,
  112. // Local name 'Steve'
  113. 0x06, 0x08, 0x53, 0x74, 0x65, 0x76, 0x65,
  114. // 128-bit service UUID abcdef01-2345-6789-abcd-ef0123456789
  115. 0x11, 0x06, 0x89, 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x67,
  116. 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab,
  117. // Service data map 0000dcab-... => { 0xa1, 0xb2, 0xc3, 0xd4, 0xe5 }
  118. 0x08, 0x16, 0xab, 0xdc, 0xa1, 0xb2, 0xc3, 0xd4, 0xe5,
  119. // Manufacturer data map 0xd00d => { 0x1a, 0x2b, 0x3c, 0x4d }
  120. 0x07, 0xff, 0x0d, 0xd0, 0x1a, 0x2b, 0x3c, 0x4d};
  121. mojom::ScanRecordPtr actual = BleScanParserImpl::ParseBleScan(kRawData);
  122. ASSERT_TRUE(actual);
  123. EXPECT_EQ(0x42, actual->advertising_flags);
  124. EXPECT_EQ(0x1b, actual->tx_power);
  125. EXPECT_EQ("Steve", actual->advertisement_name);
  126. EXPECT_EQ(expected_service_uuids, actual->service_uuids);
  127. EXPECT_EQ(expected_service_data_map, actual->service_data_map);
  128. EXPECT_EQ(expected_manufacturer_data_map, actual->manufacturer_data_map);
  129. }
  130. } // namespace data_decoder