bluetooth_local_gatt_descriptor_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2016 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 "device/bluetooth/bluetooth_local_gatt_descriptor.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "base/memory/weak_ptr.h"
  7. #include "build/build_config.h"
  8. #include "device/bluetooth/bluetooth_gatt_characteristic.h"
  9. #include "device/bluetooth/test/bluetooth_gatt_server_test.h"
  10. #include "device/bluetooth/test/bluetooth_test.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace device {
  13. class BluetoothLocalGattDescriptorTest : public BluetoothGattServerTest {
  14. public:
  15. void SetUp() override {
  16. BluetoothGattServerTest::SetUp();
  17. StartGattSetup();
  18. // We will need this device to use with simulating read/write attribute
  19. // value events.
  20. device_ = SimulateLowEnergyDevice(1);
  21. characteristic_ = BluetoothLocalGattCharacteristic::Create(
  22. BluetoothUUID(kTestUUIDGenericAttribute),
  23. device::BluetoothLocalGattCharacteristic::Properties(),
  24. device::BluetoothLocalGattCharacteristic::Permissions(),
  25. service_.get());
  26. read_descriptor_ = BluetoothLocalGattDescriptor::Create(
  27. BluetoothUUID(kTestUUIDGenericAttribute),
  28. device::BluetoothLocalGattCharacteristic::PERMISSION_READ,
  29. characteristic_.get());
  30. write_descriptor_ = BluetoothLocalGattDescriptor::Create(
  31. BluetoothUUID(kTestUUIDGenericAttribute),
  32. device::BluetoothLocalGattCharacteristic::
  33. PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED,
  34. characteristic_.get());
  35. EXPECT_LT(0u, read_descriptor_->GetIdentifier().size());
  36. EXPECT_LT(0u, write_descriptor_->GetIdentifier().size());
  37. CompleteGattSetup();
  38. }
  39. protected:
  40. base::WeakPtr<BluetoothLocalGattCharacteristic> characteristic_;
  41. base::WeakPtr<BluetoothLocalGattDescriptor> read_descriptor_;
  42. base::WeakPtr<BluetoothLocalGattDescriptor> write_descriptor_;
  43. raw_ptr<BluetoothDevice> device_;
  44. };
  45. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  46. #define MAYBE_ReadLocalDescriptorValue ReadLocalDescriptorValue
  47. #else
  48. #define MAYBE_ReadLocalDescriptorValue DISABLED_ReadLocalDescriptorValue
  49. #endif
  50. TEST_F(BluetoothLocalGattDescriptorTest, MAYBE_ReadLocalDescriptorValue) {
  51. delegate_->value_to_write_ = 0x1337;
  52. SimulateLocalGattDescriptorValueReadRequest(
  53. device_, read_descriptor_.get(),
  54. GetReadValueCallback(Call::EXPECTED, Result::SUCCESS));
  55. EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_));
  56. EXPECT_EQ(device_->GetIdentifier(), delegate_->last_seen_device_);
  57. }
  58. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  59. #define MAYBE_WriteLocalDescriptorValue WriteLocalDescriptorValue
  60. #else
  61. #define MAYBE_WriteLocalDescriptorValue DISABLED_WriteLocalDescriptorValue
  62. #endif
  63. TEST_F(BluetoothLocalGattDescriptorTest, MAYBE_WriteLocalDescriptorValue) {
  64. const uint64_t kValueToWrite = 0x7331ul;
  65. SimulateLocalGattDescriptorValueWriteRequest(
  66. device_, write_descriptor_.get(), GetValue(kValueToWrite),
  67. GetCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED));
  68. EXPECT_EQ(kValueToWrite, delegate_->last_written_value_);
  69. EXPECT_EQ(device_->GetIdentifier(), delegate_->last_seen_device_);
  70. }
  71. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  72. #define MAYBE_ReadLocalDescriptorValueFail ReadLocalDescriptorValueFail
  73. #else
  74. #define MAYBE_ReadLocalDescriptorValueFail DISABLED_ReadLocalDescriptorValueFail
  75. #endif
  76. TEST_F(BluetoothLocalGattDescriptorTest, MAYBE_ReadLocalDescriptorValueFail) {
  77. delegate_->value_to_write_ = 0x1337;
  78. delegate_->should_fail_ = true;
  79. SimulateLocalGattDescriptorValueReadRequest(
  80. device_, read_descriptor_.get(),
  81. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  82. EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_));
  83. EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_);
  84. }
  85. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  86. #define MAYBE_WriteLocalDescriptorValueFail WriteLocalDescriptorValueFail
  87. #else
  88. #define MAYBE_WriteLocalDescriptorValueFail \
  89. DISABLED_WriteLocalDescriptorValueFail
  90. #endif
  91. TEST_F(BluetoothLocalGattDescriptorTest, MAYBE_WriteLocalDescriptorValueFail) {
  92. const uint64_t kValueToWrite = 0x7331ul;
  93. delegate_->should_fail_ = true;
  94. SimulateLocalGattDescriptorValueWriteRequest(
  95. device_, write_descriptor_.get(), GetValue(kValueToWrite),
  96. GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED));
  97. EXPECT_NE(kValueToWrite, delegate_->last_written_value_);
  98. EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_);
  99. }
  100. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  101. #define MAYBE_ReadLocalDescriptorValueWrongPermissions \
  102. ReadLocalDescriptorValueWrongPermissions
  103. #else
  104. #define MAYBE_ReadLocalDescriptorValueWrongPermissions \
  105. DISABLED_ReadLocalDescriptorValueWrongPermissions
  106. #endif
  107. TEST_F(BluetoothLocalGattDescriptorTest,
  108. MAYBE_ReadLocalDescriptorValueWrongPermissions) {
  109. delegate_->value_to_write_ = 0x1337;
  110. SimulateLocalGattDescriptorValueReadRequest(
  111. device_, write_descriptor_.get(),
  112. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  113. EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_));
  114. EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_);
  115. }
  116. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  117. #define MAYBE_WriteLocalDescriptorValueWrongPermissions \
  118. WriteLocalDescriptorValueWrongPermissions
  119. #else
  120. #define MAYBE_WriteLocalDescriptorValueWrongPermissions \
  121. DISABLED_WriteLocalDescriptorValueWrongPermissions
  122. #endif
  123. TEST_F(BluetoothLocalGattDescriptorTest,
  124. MAYBE_WriteLocalDescriptorValueWrongPermissions) {
  125. const uint64_t kValueToWrite = 0x7331ul;
  126. SimulateLocalGattDescriptorValueWriteRequest(
  127. device_, read_descriptor_.get(), GetValue(kValueToWrite),
  128. GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED));
  129. EXPECT_NE(kValueToWrite, delegate_->last_written_value_);
  130. EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_);
  131. }
  132. } // namespace device