Commit bc0aae7d authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Implement BleListenerOperation.

This operation attempts to connect to a remote device over BLE via the
listener role via BleConnectionManager.

Bug: 824568, 752273
Change-Id: Ie6be7b139b8234049df4d1f1c2535254a1b962a0
Reviewed-on: https://chromium-review.googlesource.com/1103009
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568220}
parent 1a7fde0c
...@@ -4,9 +4,12 @@ ...@@ -4,9 +4,12 @@
#include "chromeos/services/secure_channel/ble_listener_operation.h" #include "chromeos/services/secure_channel/ble_listener_operation.h"
#include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "chromeos/services/secure_channel/ble_connection_manager.h"
#include "chromeos/services/secure_channel/public/cpp/shared/authenticated_channel.h"
namespace chromeos { namespace chromeos {
...@@ -35,6 +38,7 @@ BleListenerOperation::Factory::~Factory() = default; ...@@ -35,6 +38,7 @@ BleListenerOperation::Factory::~Factory() = default;
std::unique_ptr<ConnectToDeviceOperation<BleListenerFailureType>> std::unique_ptr<ConnectToDeviceOperation<BleListenerFailureType>>
BleListenerOperation::Factory::BuildInstance( BleListenerOperation::Factory::BuildInstance(
BleConnectionManager* ble_connection_manager,
ConnectToDeviceOperation<BleListenerFailureType>::ConnectionSuccessCallback ConnectToDeviceOperation<BleListenerFailureType>::ConnectionSuccessCallback
success_callback, success_callback,
const ConnectToDeviceOperation< const ConnectToDeviceOperation<
...@@ -43,13 +47,13 @@ BleListenerOperation::Factory::BuildInstance( ...@@ -43,13 +47,13 @@ BleListenerOperation::Factory::BuildInstance(
ConnectionPriority connection_priority, ConnectionPriority connection_priority,
scoped_refptr<base::TaskRunner> task_runner) { scoped_refptr<base::TaskRunner> task_runner) {
return base::WrapUnique(new BleListenerOperation( return base::WrapUnique(new BleListenerOperation(
std::move(success_callback), std::move(failure_callback), device_id_pair, ble_connection_manager, std::move(success_callback),
connection_priority, task_runner)); std::move(failure_callback), device_id_pair, connection_priority,
task_runner));
} }
BleListenerOperation::~BleListenerOperation() = default;
BleListenerOperation::BleListenerOperation( BleListenerOperation::BleListenerOperation(
BleConnectionManager* ble_connection_manager,
ConnectToDeviceOperation<BleListenerFailureType>::ConnectionSuccessCallback ConnectToDeviceOperation<BleListenerFailureType>::ConnectionSuccessCallback
success_callback, success_callback,
const ConnectToDeviceOperation< const ConnectToDeviceOperation<
...@@ -62,23 +66,43 @@ BleListenerOperation::BleListenerOperation( ...@@ -62,23 +66,43 @@ BleListenerOperation::BleListenerOperation(
std::move(failure_callback), std::move(failure_callback),
device_id_pair, device_id_pair,
connection_priority, connection_priority,
task_runner) {} task_runner),
ble_connection_manager_(ble_connection_manager),
weak_ptr_factory_(this) {}
BleListenerOperation::~BleListenerOperation() = default;
void BleListenerOperation::AttemptConnectionToDevice( void BleListenerOperation::AttemptConnectionToDevice(
ConnectionPriority connection_priority) { ConnectionPriority connection_priority) {
// TODO(khorimoto): Implement. is_attempt_active_ = true;
NOTIMPLEMENTED(); ble_connection_manager_->AttemptBleListenerConnection(
device_id_pair(), connection_priority,
base::BindOnce(&BleListenerOperation::OnSuccessfulConnection,
weak_ptr_factory_.GetWeakPtr()),
base::BindRepeating(&BleListenerOperation::OnConnectionFailure,
weak_ptr_factory_.GetWeakPtr()));
} }
void BleListenerOperation::PerformCancellation() { void BleListenerOperation::PerformCancellation() {
// TODO(khorimoto): Implement. is_attempt_active_ = false;
NOTIMPLEMENTED(); ble_connection_manager_->CancelBleListenerConnectionAttempt(device_id_pair());
} }
void BleListenerOperation::PerformUpdateConnectionPriority( void BleListenerOperation::PerformUpdateConnectionPriority(
ConnectionPriority connection_priority) { ConnectionPriority connection_priority) {
// TODO(khorimoto): Implement. ble_connection_manager_->UpdateBleListenerConnectionPriority(
NOTIMPLEMENTED(); device_id_pair(), connection_priority);
}
void BleListenerOperation::OnSuccessfulConnection(
std::unique_ptr<AuthenticatedChannel> authenticated_channel) {
is_attempt_active_ = false;
OnSuccessfulConnectionAttempt(std::move(authenticated_channel));
}
void BleListenerOperation::OnConnectionFailure(
BleListenerFailureType failure_type) {
OnFailedConnectionAttempt(failure_type);
} }
} // namespace secure_channel } // namespace secure_channel
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chromeos/services/secure_channel/ble_listener_failure_type.h" #include "chromeos/services/secure_channel/ble_listener_failure_type.h"
#include "chromeos/services/secure_channel/connect_to_device_operation.h" #include "chromeos/services/secure_channel/connect_to_device_operation.h"
...@@ -18,6 +19,8 @@ namespace chromeos { ...@@ -18,6 +19,8 @@ namespace chromeos {
namespace secure_channel { namespace secure_channel {
class BleConnectionManager;
// Attempts to connect to a remote device over BLE via the listener role. // Attempts to connect to a remote device over BLE via the listener role.
class BleListenerOperation class BleListenerOperation
: public ConnectToDeviceOperationBase<BleListenerFailureType> { : public ConnectToDeviceOperationBase<BleListenerFailureType> {
...@@ -29,6 +32,7 @@ class BleListenerOperation ...@@ -29,6 +32,7 @@ class BleListenerOperation
virtual ~Factory(); virtual ~Factory();
virtual std::unique_ptr<ConnectToDeviceOperation<BleListenerFailureType>> virtual std::unique_ptr<ConnectToDeviceOperation<BleListenerFailureType>>
BuildInstance( BuildInstance(
BleConnectionManager* ble_connection_manager,
ConnectToDeviceOperation< ConnectToDeviceOperation<
BleListenerFailureType>::ConnectionSuccessCallback success_callback, BleListenerFailureType>::ConnectionSuccessCallback success_callback,
const ConnectToDeviceOperation< const ConnectToDeviceOperation<
...@@ -46,6 +50,7 @@ class BleListenerOperation ...@@ -46,6 +50,7 @@ class BleListenerOperation
private: private:
BleListenerOperation( BleListenerOperation(
BleConnectionManager* ble_connection_manager,
ConnectToDeviceOperation< ConnectToDeviceOperation<
BleListenerFailureType>::ConnectionSuccessCallback success_callback, BleListenerFailureType>::ConnectionSuccessCallback success_callback,
const ConnectToDeviceOperation< const ConnectToDeviceOperation<
...@@ -61,6 +66,15 @@ class BleListenerOperation ...@@ -61,6 +66,15 @@ class BleListenerOperation
void PerformUpdateConnectionPriority( void PerformUpdateConnectionPriority(
ConnectionPriority connection_priority) override; ConnectionPriority connection_priority) override;
void OnSuccessfulConnection(
std::unique_ptr<AuthenticatedChannel> authenticated_channel);
void OnConnectionFailure(BleListenerFailureType failure_type);
BleConnectionManager* ble_connection_manager_;
bool is_attempt_active_ = false;
base::WeakPtrFactory<BleListenerOperation> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BleListenerOperation); DISALLOW_COPY_AND_ASSIGN(BleListenerOperation);
}; };
......
...@@ -6,29 +6,139 @@ ...@@ -6,29 +6,139 @@
#include <memory> #include <memory>
#include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "chromeos/services/secure_channel/ble_listener_failure_type.h"
#include "chromeos/services/secure_channel/device_id_pair.h"
#include "chromeos/services/secure_channel/fake_ble_connection_manager.h"
#include "chromeos/services/secure_channel/public/cpp/shared/connection_priority.h"
#include "chromeos/services/secure_channel/public/cpp/shared/fake_authenticated_channel.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace chromeos { namespace chromeos {
namespace secure_channel { namespace secure_channel {
const char kTestRemoteDeviceId[] = "testRemoteDeviceId";
const char kTestLocalDeviceId[] = "testLocalDeviceId";
constexpr const ConnectionPriority kTestConnectionPriority =
ConnectionPriority::kLow;
class SecureChannelBleListenerOperationTest : public testing::Test { class SecureChannelBleListenerOperationTest : public testing::Test {
protected: protected:
SecureChannelBleListenerOperationTest() = default; SecureChannelBleListenerOperationTest()
: device_id_pair_(kTestRemoteDeviceId, kTestLocalDeviceId) {}
~SecureChannelBleListenerOperationTest() override = default; ~SecureChannelBleListenerOperationTest() override = default;
// testing::Test: // testing::Test:
void SetUp() override {} void SetUp() override {
fake_ble_connection_manager_ = std::make_unique<FakeBleConnectionManager>();
auto test_task_runner = base::MakeRefCounted<base::TestSimpleTaskRunner>();
operation_ = BleListenerOperation::Factory::Get()->BuildInstance(
fake_ble_connection_manager_.get(),
base::BindOnce(&SecureChannelBleListenerOperationTest::
OnSuccessfulConnectionAttempt,
base::Unretained(this)),
base::BindRepeating(
&SecureChannelBleListenerOperationTest::OnFailedConnectionAttempt,
base::Unretained(this)),
device_id_pair_, kTestConnectionPriority, test_task_runner);
test_task_runner->RunUntilIdle();
EXPECT_EQ(kTestConnectionPriority,
fake_ble_connection_manager_->GetPriorityForAttempt(
device_id_pair_, ConnectionRole::kListenerRole));
}
const DeviceIdPair& device_id_pair() { return device_id_pair_; }
void FailAttempt(BleListenerFailureType failure_type) {
fake_ble_connection_manager_->NotifyBleListenerFailure(device_id_pair_,
failure_type);
EXPECT_EQ(failure_type, failure_type_from_callback_);
}
void CompleteAttemptSuccessfully() {
auto fake_authenticated_channel =
std::make_unique<FakeAuthenticatedChannel>();
FakeAuthenticatedChannel* fake_authenticated_channel_raw =
fake_authenticated_channel.get();
fake_ble_connection_manager_->NotifyConnectionSuccess(
device_id_pair(), ConnectionRole::kListenerRole,
std::move(fake_authenticated_channel));
EXPECT_EQ(fake_authenticated_channel_raw, channel_from_callback_.get());
// The operation should no longer be present in BleConnectionManager.
EXPECT_FALSE(fake_ble_connection_manager()->DoesAttemptExist(
device_id_pair(), ConnectionRole::kListenerRole));
}
FakeBleConnectionManager* fake_ble_connection_manager() {
return fake_ble_connection_manager_.get();
}
ConnectToDeviceOperation<BleListenerFailureType>* operation() {
return operation_.get();
}
private: private:
std::unique_ptr<BleListenerOperation> operation_; void OnSuccessfulConnectionAttempt(
std::unique_ptr<AuthenticatedChannel> authenticated_channel) {
EXPECT_FALSE(channel_from_callback_);
channel_from_callback_ = std::move(authenticated_channel);
}
void OnFailedConnectionAttempt(BleListenerFailureType failure_type) {
failure_type_from_callback_ = failure_type;
}
const base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
DeviceIdPair device_id_pair_;
std::unique_ptr<AuthenticatedChannel> channel_from_callback_;
base::Optional<BleListenerFailureType> failure_type_from_callback_;
std::unique_ptr<ConnectToDeviceOperation<BleListenerFailureType>> operation_;
DISALLOW_COPY_AND_ASSIGN(SecureChannelBleListenerOperationTest); DISALLOW_COPY_AND_ASSIGN(SecureChannelBleListenerOperationTest);
}; };
TEST_F(SecureChannelBleListenerOperationTest, Test) { TEST_F(SecureChannelBleListenerOperationTest, UpdateThenFailThenCancel) {
// TODO(khorimoto): Add tests once implementation is complete. operation()->UpdateConnectionPriority(ConnectionPriority::kMedium);
EXPECT_EQ(ConnectionPriority::kMedium,
fake_ble_connection_manager()->GetPriorityForAttempt(
device_id_pair(), ConnectionRole::kListenerRole));
// After failure, the attempt should still be present in BleConnectionManager.
FailAttempt(BleListenerFailureType::kAuthenticationError);
EXPECT_EQ(ConnectionPriority::kMedium,
fake_ble_connection_manager()->GetPriorityForAttempt(
device_id_pair(), ConnectionRole::kListenerRole));
// One more failure.
FailAttempt(BleListenerFailureType::kAuthenticationError);
EXPECT_EQ(ConnectionPriority::kMedium,
fake_ble_connection_manager()->GetPriorityForAttempt(
device_id_pair(), ConnectionRole::kListenerRole));
operation()->Cancel();
EXPECT_FALSE(fake_ble_connection_manager()->DoesAttemptExist(
device_id_pair(), ConnectionRole::kListenerRole));
}
TEST_F(SecureChannelBleListenerOperationTest, UpdateThenSucceed) {
operation()->UpdateConnectionPriority(ConnectionPriority::kMedium);
EXPECT_EQ(ConnectionPriority::kMedium,
fake_ble_connection_manager()->GetPriorityForAttempt(
device_id_pair(), ConnectionRole::kListenerRole));
CompleteAttemptSuccessfully();
} }
} // namespace secure_channel } // namespace secure_channel
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment