Skip to content

Commit

Permalink
[Bluetooth][WinRT] Implement Pairing
Browse files Browse the repository at this point in the history
This change implements pairing on WinRT and adds appropriate tests. In order
to encapsulate the implementation, a new BluetoothPairingWinrt helper class
is introduced. For now only pairing via PIN code is supported.

Bug: 821766
Change-Id: Ic0a64b4e79dd38f9c0ac86a25c0a878f1f70192c
Reviewed-on: https://chromium-review.googlesource.com/1177741
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584391}
  • Loading branch information
jdoerrie authored and Commit Bot committed Aug 20, 2018
1 parent 5334ca5 commit 8b9a1e0
Show file tree
Hide file tree
Showing 20 changed files with 818 additions and 59 deletions.
2 changes: 2 additions & 0 deletions device/bluetooth/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ component("bluetooth") {
"bluetooth_device_winrt.h",
"bluetooth_gatt_discoverer_winrt.cc",
"bluetooth_gatt_discoverer_winrt.h",
"bluetooth_pairing_winrt.cc",
"bluetooth_pairing_winrt.h",
"bluetooth_remote_gatt_characteristic_winrt.cc",
"bluetooth_remote_gatt_characteristic_winrt.h",
"bluetooth_remote_gatt_descriptor_winrt.cc",
Expand Down
3 changes: 2 additions & 1 deletion device/bluetooth/bluetooth_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
// ignores the |IsPaired()| value.
//
// In most cases |Connect()| should be preferred. This method is only
// implemented on ChromeOS and Linux.
// implemented on ChromeOS, Linux and Windows 10. On Windows, only pairing
// with a pin code is currently supported.
virtual void Pair(PairingDelegate* pairing_delegate,
const base::Closure& callback,
const ConnectErrorCallback& error_callback);
Expand Down
140 changes: 140 additions & 0 deletions device/bluetooth/bluetooth_device_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h"
#include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
#include "device/bluetooth/test/test_pairing_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_ANDROID)
Expand Down Expand Up @@ -118,6 +119,145 @@ TEST_P(BluetoothTestWinrtOnly, DeviceIsPaired) {
SimulateDevicePaired(device, false);
EXPECT_FALSE(device->IsPaired());
}

// Tests that providing a correct pin code results in a paired device.
TEST_P(BluetoothTestWinrtOnly, DevicePairRequestPinCodeCorrect) {
if (!PlatformSupportsLowEnergy()) {
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
return;
}
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = SimulateLowEnergyDevice(1);

device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());

SimulatePairingPinCode(device, "123456");
TestPairingDelegate pairing_delegate;
device->Pair(&pairing_delegate, GetCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
base::RunLoop().RunUntilIdle();

EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->ExpectingPinCode());

device->SetPinCode("123456");
base::RunLoop().RunUntilIdle();

EXPECT_TRUE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());
}

// Tests that providing a wrong pin code does not result in a paired device.
TEST_P(BluetoothTestWinrtOnly, DevicePairRequestPinCodeWrong) {
if (!PlatformSupportsLowEnergy()) {
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
return;
}
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = SimulateLowEnergyDevice(1);

device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());

SimulatePairingPinCode(device, "123456");
TestPairingDelegate pairing_delegate;
device->Pair(&pairing_delegate, GetCallback(Call::NOT_EXPECTED),
GetConnectErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();

EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->ExpectingPinCode());

device->SetPinCode("000000");
base::RunLoop().RunUntilIdle();

EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());
EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
}

// Tests that rejecting the pairing does not result in a paired device.
TEST_P(BluetoothTestWinrtOnly, DevicePairRequestPinCodeRejectPairing) {
if (!PlatformSupportsLowEnergy()) {
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
return;
}
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = SimulateLowEnergyDevice(1);

device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());

SimulatePairingPinCode(device, "123456");
TestPairingDelegate pairing_delegate;
device->Pair(&pairing_delegate, GetCallback(Call::NOT_EXPECTED),
GetConnectErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();

EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->ExpectingPinCode());

device->RejectPairing();
base::RunLoop().RunUntilIdle();

EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_code_);
}

// Tests that cancelling the pairing does not result in a paired device.
TEST_P(BluetoothTestWinrtOnly, DevicePairRequestPinCodeCancelPairing) {
if (!PlatformSupportsLowEnergy()) {
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
return;
}
InitWithFakeAdapter();
StartLowEnergyDiscoverySession();
BluetoothDevice* device = SimulateLowEnergyDevice(1);

device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
GetConnectErrorCallback(Call::NOT_EXPECTED));
SimulateGattConnection(device);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());

SimulatePairingPinCode(device, "123456");
TestPairingDelegate pairing_delegate;
device->Pair(&pairing_delegate, GetCallback(Call::NOT_EXPECTED),
GetConnectErrorCallback(Call::EXPECTED));
base::RunLoop().RunUntilIdle();

EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->ExpectingPinCode());

device->CancelPairing();
base::RunLoop().RunUntilIdle();

EXPECT_FALSE(device->IsPaired());
EXPECT_FALSE(device->ExpectingPinCode());
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_code_);
}
#endif

// Verifies basic device properties, e.g. GetAddress, GetName, ...
Expand Down
Loading

0 comments on commit 8b9a1e0

Please sign in to comment.