| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 6 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 7 #include "device/bluetooth/bluetooth_local_gatt_descriptor.h" | 7 #include "device/bluetooth/bluetooth_local_gatt_descriptor.h" |
| 8 #include "device/bluetooth/test/bluetooth_gatt_server_test.h" | 8 #include "device/bluetooth/test/bluetooth_gatt_server_test.h" |
| 9 #include "device/bluetooth/test/bluetooth_test.h" | 9 #include "device/bluetooth/test/bluetooth_test.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace device { | 12 namespace device { |
| 13 | 13 |
| 14 class BluetoothLocalGattDescriptorTest : public BluetoothGattServerTest { | 14 class BluetoothLocalGattDescriptorTest : public BluetoothGattServerTest { |
| 15 public: | 15 public: |
| 16 void SetUp() override { | 16 void SetUp() override { |
| 17 BluetoothGattServerTest::SetUp(); | 17 BluetoothGattServerTest::SetUp(); |
| 18 | 18 |
| 19 StartGattSetup(); | 19 StartGattSetup(); |
| 20 // We will need this device to use with simulating read/write attribute |
| 21 // value events. |
| 22 device_ = SimulateLowEnergyDevice(1); |
| 20 characteristic_ = BluetoothLocalGattCharacteristic::Create( | 23 characteristic_ = BluetoothLocalGattCharacteristic::Create( |
| 21 BluetoothUUID(kTestUUIDGenericAttribute), | 24 BluetoothUUID(kTestUUIDGenericAttribute), |
| 22 device::BluetoothLocalGattCharacteristic::Properties(), | 25 device::BluetoothLocalGattCharacteristic::Properties(), |
| 23 device::BluetoothLocalGattCharacteristic::Permissions(), | 26 device::BluetoothLocalGattCharacteristic::Permissions(), |
| 24 service_.get()); | 27 service_.get()); |
| 25 read_descriptor_ = BluetoothLocalGattDescriptor::Create( | 28 read_descriptor_ = BluetoothLocalGattDescriptor::Create( |
| 26 BluetoothUUID(kTestUUIDGenericAttribute), | 29 BluetoothUUID(kTestUUIDGenericAttribute), |
| 27 device::BluetoothLocalGattCharacteristic::PERMISSION_READ, | 30 device::BluetoothLocalGattCharacteristic::PERMISSION_READ, |
| 28 characteristic_.get()); | 31 characteristic_.get()); |
| 29 write_descriptor_ = BluetoothLocalGattDescriptor::Create( | 32 write_descriptor_ = BluetoothLocalGattDescriptor::Create( |
| 30 BluetoothUUID(kTestUUIDGenericAttribute), | 33 BluetoothUUID(kTestUUIDGenericAttribute), |
| 31 device::BluetoothLocalGattCharacteristic:: | 34 device::BluetoothLocalGattCharacteristic:: |
| 32 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED, | 35 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED, |
| 33 characteristic_.get()); | 36 characteristic_.get()); |
| 34 EXPECT_LT(0u, read_descriptor_->GetIdentifier().size()); | 37 EXPECT_LT(0u, read_descriptor_->GetIdentifier().size()); |
| 35 EXPECT_LT(0u, write_descriptor_->GetIdentifier().size()); | 38 EXPECT_LT(0u, write_descriptor_->GetIdentifier().size()); |
| 36 CompleteGattSetup(); | 39 CompleteGattSetup(); |
| 37 } | 40 } |
| 38 | 41 |
| 39 protected: | 42 protected: |
| 40 base::WeakPtr<BluetoothLocalGattCharacteristic> characteristic_; | 43 base::WeakPtr<BluetoothLocalGattCharacteristic> characteristic_; |
| 41 base::WeakPtr<BluetoothLocalGattDescriptor> read_descriptor_; | 44 base::WeakPtr<BluetoothLocalGattDescriptor> read_descriptor_; |
| 42 base::WeakPtr<BluetoothLocalGattDescriptor> write_descriptor_; | 45 base::WeakPtr<BluetoothLocalGattDescriptor> write_descriptor_; |
| 46 BluetoothDevice* device_; |
| 43 }; | 47 }; |
| 44 | 48 |
| 45 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 49 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 46 TEST_F(BluetoothLocalGattDescriptorTest, ReadLocalDescriptorValue) { | 50 TEST_F(BluetoothLocalGattDescriptorTest, ReadLocalDescriptorValue) { |
| 47 delegate_->value_to_write_ = 0x1337; | 51 delegate_->value_to_write_ = 0x1337; |
| 48 SimulateLocalGattDescriptorValueReadRequest( | 52 SimulateLocalGattDescriptorValueReadRequest( |
| 49 read_descriptor_.get(), GetReadValueCallback(Call::EXPECTED), | 53 device_, read_descriptor_.get(), GetReadValueCallback(Call::EXPECTED), |
| 50 GetCallback(Call::NOT_EXPECTED)); | 54 GetCallback(Call::NOT_EXPECTED)); |
| 51 | 55 |
| 52 EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_)); | 56 EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| 57 EXPECT_EQ(device_->GetIdentifier(), delegate_->last_seen_device_); |
| 53 } | 58 } |
| 54 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 59 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 55 | 60 |
| 56 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 61 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 57 TEST_F(BluetoothLocalGattDescriptorTest, WriteLocalDescriptorValue) { | 62 TEST_F(BluetoothLocalGattDescriptorTest, WriteLocalDescriptorValue) { |
| 58 const uint64_t kValueToWrite = 0x7331ul; | 63 const uint64_t kValueToWrite = 0x7331ul; |
| 59 SimulateLocalGattDescriptorValueWriteRequest( | 64 SimulateLocalGattDescriptorValueWriteRequest( |
| 60 write_descriptor_.get(), GetValue(kValueToWrite), | 65 device_, write_descriptor_.get(), GetValue(kValueToWrite), |
| 61 GetCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); | 66 GetCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); |
| 62 | 67 |
| 63 EXPECT_EQ(kValueToWrite, delegate_->last_written_value_); | 68 EXPECT_EQ(kValueToWrite, delegate_->last_written_value_); |
| 69 EXPECT_EQ(device_->GetIdentifier(), delegate_->last_seen_device_); |
| 64 } | 70 } |
| 65 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 71 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 66 | 72 |
| 67 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 73 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 68 TEST_F(BluetoothLocalGattDescriptorTest, ReadLocalDescriptorValueFail) { | 74 TEST_F(BluetoothLocalGattDescriptorTest, ReadLocalDescriptorValueFail) { |
| 69 delegate_->value_to_write_ = 0x1337; | 75 delegate_->value_to_write_ = 0x1337; |
| 70 delegate_->should_fail_ = true; | 76 delegate_->should_fail_ = true; |
| 71 SimulateLocalGattDescriptorValueReadRequest( | 77 SimulateLocalGattDescriptorValueReadRequest( |
| 72 read_descriptor_.get(), GetReadValueCallback(Call::NOT_EXPECTED), | 78 device_, read_descriptor_.get(), GetReadValueCallback(Call::NOT_EXPECTED), |
| 73 GetCallback(Call::EXPECTED)); | 79 GetCallback(Call::EXPECTED)); |
| 74 | 80 |
| 75 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); | 81 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| 82 EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_); |
| 76 } | 83 } |
| 77 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 84 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 78 | 85 |
| 79 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 86 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 80 TEST_F(BluetoothLocalGattDescriptorTest, WriteLocalDescriptorValueFail) { | 87 TEST_F(BluetoothLocalGattDescriptorTest, WriteLocalDescriptorValueFail) { |
| 81 const uint64_t kValueToWrite = 0x7331ul; | 88 const uint64_t kValueToWrite = 0x7331ul; |
| 82 delegate_->should_fail_ = true; | 89 delegate_->should_fail_ = true; |
| 83 SimulateLocalGattDescriptorValueWriteRequest( | 90 SimulateLocalGattDescriptorValueWriteRequest( |
| 84 write_descriptor_.get(), GetValue(kValueToWrite), | 91 device_, write_descriptor_.get(), GetValue(kValueToWrite), |
| 85 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); | 92 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 86 | 93 |
| 87 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); | 94 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); |
| 95 EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_); |
| 88 } | 96 } |
| 89 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 97 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 90 | 98 |
| 91 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 99 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 92 TEST_F(BluetoothLocalGattDescriptorTest, | 100 TEST_F(BluetoothLocalGattDescriptorTest, |
| 93 ReadLocalDescriptorValueWrongPermissions) { | 101 ReadLocalDescriptorValueWrongPermissions) { |
| 94 delegate_->value_to_write_ = 0x1337; | 102 delegate_->value_to_write_ = 0x1337; |
| 95 SimulateLocalGattDescriptorValueReadRequest( | 103 SimulateLocalGattDescriptorValueReadRequest( |
| 96 write_descriptor_.get(), GetReadValueCallback(Call::NOT_EXPECTED), | 104 device_, write_descriptor_.get(), |
| 97 GetCallback(Call::EXPECTED)); | 105 GetReadValueCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 98 | 106 |
| 99 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); | 107 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| 108 EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_); |
| 100 } | 109 } |
| 101 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 110 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 102 | 111 |
| 103 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 112 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 104 TEST_F(BluetoothLocalGattDescriptorTest, | 113 TEST_F(BluetoothLocalGattDescriptorTest, |
| 105 WriteLocalDescriptorValueWrongPermissions) { | 114 WriteLocalDescriptorValueWrongPermissions) { |
| 106 const uint64_t kValueToWrite = 0x7331ul; | 115 const uint64_t kValueToWrite = 0x7331ul; |
| 107 SimulateLocalGattDescriptorValueWriteRequest( | 116 SimulateLocalGattDescriptorValueWriteRequest( |
| 108 read_descriptor_.get(), GetValue(kValueToWrite), | 117 device_, read_descriptor_.get(), GetValue(kValueToWrite), |
| 109 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); | 118 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 110 | 119 |
| 111 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); | 120 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); |
| 121 EXPECT_NE(device_->GetIdentifier(), delegate_->last_seen_device_); |
| 112 } | 122 } |
| 113 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 123 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 114 | 124 |
| 115 } // namespace device | 125 } // namespace device |
| OLD | NEW |