Commit faa083ee authored by Nico Weber's avatar Nico Weber Committed by Commit Bot

Revert "[CrOS Multidevice] Integrate SecureChannel API into ProximityAuthMonitor."

This reverts commit e092b1bd.

Reason for revert:
SecureChannelClientImplTest.TestMultipleConnections has been failing
consistently on the waterfall since this landed. Started here:
https://ci.chromium.org/p/chromium/builders/luci.chromium.ci/linux-chromeos-rel/9728


Original change's description:
> [CrOS Multidevice] Integrate SecureChannel API into ProximityAuthMonitor.
> 
> This injects a ClientChannel into ProximityAuthMonitor, which is used if the
> chromeos::features::kMultiDeviceApi is enabled. The ClientChannel is used
> to get the current RSSI of the connected remote device.
> 
> In the future, the "rolling average RSSI" that is calculated in
> ProximityAuthMonitor will be moved to the SecureChannel API, and returned
> by it. However, to reduce immediate migration work, that logic is kept
> in ProximityAuthMonitor for now.
> 
> R=​jhawkins@chromium.org, khorimoto@chromium.org
> 
> Bug: 824568, 752273
> Change-Id: I8d6485a5a0018fe43595b880c25a2fa9af5a1b75
> Reviewed-on: https://chromium-review.googlesource.com/1106616
> Commit-Queue: Ryan Hansberry <hansberry@chromium.org>
> Reviewed-by: James Hawkins <jhawkins@chromium.org>
> Reviewed-by: Kyle Horimoto <khorimoto@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#569425}

TBR=jhawkins@chromium.org,khorimoto@chromium.org,hansberry@chromium.org

Change-Id: Ie22413bae9011ed505220115fa1c711fc1c7e626
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 824568, 752273
Reviewed-on: https://chromium-review.googlesource.com/1111937Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Commit-Queue: Nico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569590}
parent 1a6d088d
......@@ -55,8 +55,6 @@ static_library("proximity_auth") {
"//chromeos",
"//chromeos/components/proximity_auth/logging",
"//chromeos/components/proximity_auth/public/interfaces",
"//chromeos/services/secure_channel/public/cpp/client",
"//chromeos/services/secure_channel/public/mojom",
"//components/account_id",
"//components/cryptauth",
"//components/cryptauth/ble",
......@@ -115,7 +113,6 @@ source_set("unit_tests") {
"//chromeos",
"//chromeos/components/proximity_auth/logging",
"//chromeos/components/proximity_auth/logging:unit_tests",
"//chromeos/services/secure_channel/public/cpp/client:test_support",
"//components/cryptauth:test_support",
"//components/prefs:test_support",
"//components/sync_preferences:test_support",
......
......@@ -16,7 +16,6 @@
#include "chromeos/components/proximity_auth/metrics.h"
#include "chromeos/components/proximity_auth/proximity_auth_pref_manager.h"
#include "chromeos/components/proximity_auth/proximity_monitor_observer.h"
#include "chromeos/services/secure_channel/public/cpp/client/client_channel.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
......@@ -34,17 +33,13 @@ const double kRssiSampleWeight = 0.3;
const int kDefaultRssiThreshold = -70;
ProximityMonitorImpl::ProximityMonitorImpl(
cryptauth::RemoteDeviceRef remote_device,
chromeos::secure_channel::ClientChannel* channel,
cryptauth::Connection* connection,
ProximityAuthPrefManager* pref_manager)
: remote_device_(remote_device),
channel_(channel),
connection_(connection),
pref_manager_(pref_manager),
: connection_(connection),
remote_device_is_in_proximity_(false),
is_active_(false),
rssi_threshold_(kDefaultRssiThreshold),
pref_manager_(pref_manager),
polling_weak_ptr_factory_(this),
weak_ptr_factory_(this) {
if (device::BluetoothAdapterFactory::IsBluetoothSupported()) {
......@@ -81,7 +76,7 @@ void ProximityMonitorImpl::RecordProximityMetricsOnAuthSuccess() {
: metrics::kUnknownProximityValue;
std::string remote_device_model = metrics::kUnknownDeviceModel;
cryptauth::RemoteDeviceRef remote_device = remote_device_;
cryptauth::RemoteDeviceRef remote_device = connection_->remote_device();
if (!remote_device.name().empty())
remote_device_model = remote_device.name();
......@@ -136,69 +131,37 @@ bool ProximityMonitorImpl::ShouldPoll() const {
void ProximityMonitorImpl::Poll() {
DCHECK(ShouldPoll());
if (base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)) {
if (channel_->is_disconnected()) {
PA_LOG(ERROR) << "Channel is disconnected.";
ClearProximityState();
return;
}
channel_->GetConnectionMetadata(
base::BindOnce(&ProximityMonitorImpl::OnGetConnectionMetadata,
weak_ptr_factory_.GetWeakPtr()));
} else {
std::string address = connection_->GetDeviceAddress();
BluetoothDevice* device = bluetooth_adapter_->GetDevice(address);
std::string address = connection_->GetDeviceAddress();
BluetoothDevice* device = bluetooth_adapter_->GetDevice(address);
if (!device) {
PA_LOG(ERROR) << "Unknown Bluetooth device with address " << address;
ClearProximityState();
return;
}
if (!device->IsConnected()) {
PA_LOG(ERROR) << "Bluetooth device with address " << address
<< " is not connected.";
ClearProximityState();
return;
}
device->GetConnectionInfo(
base::BindRepeating(&ProximityMonitorImpl::OnConnectionInfo,
weak_ptr_factory_.GetWeakPtr()));
if (!device) {
PA_LOG(ERROR) << "Unknown Bluetooth device with address " << address;
ClearProximityState();
return;
}
if (!device->IsConnected()) {
PA_LOG(ERROR) << "Bluetooth device with address " << address
<< " is not connected.";
ClearProximityState();
return;
}
}
void ProximityMonitorImpl::OnGetConnectionMetadata(
chromeos::secure_channel::mojom::ConnectionMetadataPtr
connection_metadata) {
DCHECK(base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi));
if (connection_metadata->bluetooth_connection_metadata)
OnGetRssi(connection_metadata->bluetooth_connection_metadata->current_rssi);
else
OnGetRssi(base::nullopt);
device->GetConnectionInfo(base::Bind(&ProximityMonitorImpl::OnConnectionInfo,
weak_ptr_factory_.GetWeakPtr()));
}
void ProximityMonitorImpl::OnConnectionInfo(
const BluetoothDevice::ConnectionInfo& connection_info) {
DCHECK(!base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi));
if (connection_info.rssi == BluetoothDevice::kUnknownPower)
OnGetRssi(base::nullopt);
else
OnGetRssi(connection_info.rssi);
}
void ProximityMonitorImpl::OnGetRssi(const base::Optional<int32_t>& rssi) {
if (!is_active_) {
PA_LOG(INFO) << "Received RSSI after stopping.";
PA_LOG(INFO) << "[Proximity] Got connection info after stopping";
return;
}
if (rssi) {
AddSample(*rssi);
if (connection_info.rssi != BluetoothDevice::kUnknownPower) {
AddSample(connection_info);
} else {
PA_LOG(WARNING) << "Received invalid RSSI value.";
PA_LOG(WARNING) << "[Proximity] Unknown values received from API: "
<< connection_info.rssi;
rssi_rolling_average_.reset();
CheckForProximityStateChange();
}
......@@ -214,13 +177,14 @@ void ProximityMonitorImpl::ClearProximityState() {
rssi_rolling_average_.reset();
}
void ProximityMonitorImpl::AddSample(int32_t rssi) {
void ProximityMonitorImpl::AddSample(
const BluetoothDevice::ConnectionInfo& connection_info) {
double weight = kRssiSampleWeight;
if (!rssi_rolling_average_) {
rssi_rolling_average_.reset(new double(rssi));
rssi_rolling_average_.reset(new double(connection_info.rssi));
} else {
*rssi_rolling_average_ =
weight * rssi + (1 - weight) * (*rssi_rolling_average_);
weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_);
}
CheckForProximityStateChange();
......
......@@ -11,23 +11,14 @@
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/optional.h"
#include "chromeos/chromeos_features.h"
#include "chromeos/components/proximity_auth/proximity_monitor.h"
#include "chromeos/services/secure_channel/public/mojom/secure_channel.mojom.h"
#include "components/cryptauth/connection.h"
#include "components/cryptauth/remote_device_ref.h"
#include "device/bluetooth/bluetooth_device.h"
namespace chromeos {
namespace secure_channel {
class ClientChannel;
} // namespace secure_channel
} // namespace chromeos
namespace device {
class BluetoothAdapter;
} // namespace device
}
namespace proximity_auth {
......@@ -38,9 +29,7 @@ class ProximityMonitorObserver;
class ProximityMonitorImpl : public ProximityMonitor {
public:
// The |connection| is not owned, and must outlive |this| instance.
ProximityMonitorImpl(cryptauth::RemoteDeviceRef remote_device,
chromeos::secure_channel::ClientChannel* channel,
cryptauth::Connection* connection,
ProximityMonitorImpl(cryptauth::Connection* connection,
ProximityAuthPrefManager* pref_manager);
~ProximityMonitorImpl() override;
......@@ -73,19 +62,18 @@ class ProximityMonitorImpl : public ProximityMonitor {
// Polls the connection information.
void Poll();
void OnGetConnectionMetadata(
chromeos::secure_channel::mojom::ConnectionMetadataPtr
connection_metadata);
// Callback to received the polled-for connection info.
void OnConnectionInfo(
const device::BluetoothDevice::ConnectionInfo& connection_info);
void OnGetRssi(const base::Optional<int32_t>& rssi);
// Resets the proximity state to |false|, and clears all member variables
// tracking the proximity state.
void ClearProximityState();
// Updates the proximity state with a new sample of the current RSSI.
void AddSample(int32_t rssi);
// Updates the proximity state with a new |connection_info| sample of the
// current RSSI.
void AddSample(
const device::BluetoothDevice::ConnectionInfo& connection_info);
// Checks whether the proximity state has changed based on the current
// samples. Notifies |observers_| on a change.
......@@ -95,19 +83,10 @@ class ProximityMonitorImpl : public ProximityMonitor {
// RSSI value.
void GetRssiThresholdFromPrefs();
// Used to get the name of the remote device that ProximitMonitor is
// communicating with, for metrics purposes.
cryptauth::RemoteDeviceRef remote_device_;
// Used to communicate with the remote device to gauge its proximity via RSSI
// measurement.
chromeos::secure_channel::ClientChannel* channel_;
// The current connection being monitored. Not owned and must outlive this
// instance.
cryptauth::Connection* connection_;
// Used to get determine the user pref for how far away the phone is allowed
// to be.
ProximityAuthPrefManager* pref_manager_;
// The observers attached to the ProximityMonitor.
base::ObserverList<ProximityMonitorObserver> observers_;
......@@ -131,6 +110,10 @@ class ProximityMonitorImpl : public ProximityMonitor {
// measurement.
std::unique_ptr<double> rssi_rolling_average_;
// Contains perferences that outlive the lifetime of this object and across
// process restarts. Not owned and must outlive this instance.
ProximityAuthPrefManager* pref_manager_;
// Used to vend weak pointers for polling. Using a separate factory for these
// weak pointers allows the weak pointers to be invalidated when polling
// stops, which effectively cancels the scheduled tasks.
......
......@@ -11,16 +11,13 @@
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chromeos/chromeos_features.h"
#include "chromeos/components/proximity_auth/logging/logging.h"
#include "chromeos/components/proximity_auth/proximity_auth_profile_pref_manager.h"
#include "chromeos/components/proximity_auth/proximity_monitor_observer.h"
#include "chromeos/services/secure_channel/public/cpp/client/fake_client_channel.h"
#include "components/cryptauth/fake_connection.h"
#include "components/cryptauth/remote_device_ref.h"
#include "components/cryptauth/remote_device_test_util.h"
......@@ -93,18 +90,13 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
"",
false /* paired */,
true /* connected */),
fake_client_channel_(
std::make_unique<chromeos::secure_channel::FakeClientChannel>()),
remote_device_(cryptauth::RemoteDeviceRefBuilder()
.SetUserId(kRemoteDeviceUserId)
.SetName(kRemoteDeviceName)
.Build()),
connection_(remote_device_),
pref_manager_(new NiceMock<MockProximityAuthPrefManager>()),
monitor_(remote_device_,
fake_client_channel_.get(),
&connection_,
pref_manager_.get()),
monitor_(&connection_, pref_manager_.get()),
task_runner_(new base::TestSimpleTaskRunner()),
thread_task_runner_handle_(task_runner_) {
ON_CALL(*bluetooth_adapter_, GetDevice(std::string()))
......@@ -118,42 +110,16 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
~ProximityAuthProximityMonitorImplTest() override {}
void SetMultiDeviceApiEnabled() {
scoped_feature_list_.InitAndEnableFeature(
chromeos::features::kMultiDeviceApi);
}
void RunPendingTasks() { task_runner_->RunPendingTasks(); }
void ProvideRssi(base::Optional<int32_t> rssi) {
void ProvideConnectionInfo(
const BluetoothDevice::ConnectionInfo& connection_info) {
RunPendingTasks();
connection_info_callback_.Run(connection_info);
if (base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)) {
std::vector<chromeos::secure_channel::mojom::ConnectionCreationDetail>
creation_details{
chromeos::secure_channel::mojom::ConnectionCreationDetail::
REMOTE_DEVICE_USED_BACKGROUND_BLE_ADVERTISING};
chromeos::secure_channel::mojom::BluetoothConnectionMetadataPtr
bluetooth_connection_metadata_ptr;
if (rssi) {
bluetooth_connection_metadata_ptr =
chromeos::secure_channel::mojom::BluetoothConnectionMetadata::New(
*rssi);
}
chromeos::secure_channel::mojom::ConnectionMetadataPtr
connection_metadata_ptr =
chromeos::secure_channel::mojom::ConnectionMetadata::New(
creation_details,
std::move(bluetooth_connection_metadata_ptr));
fake_client_channel_->InvokePendingGetConnectionMetadataCallback(
std::move(connection_metadata_ptr));
} else {
ProvideConnectionInfo({rssi ? *rssi : BluetoothDevice::kUnknownPower,
4 /* transmit_power */,
4 /* max_transmit_power */});
}
// Reset the callback to ensure that tests correctly only respond at most
// once per call to GetConnectionInfo().
connection_info_callback_ = BluetoothDevice::ConnectionInfoCallback();
}
protected:
......@@ -163,8 +129,6 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
// Mocks used for verifying interactions with the Bluetooth subsystem.
scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_;
NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_;
std::unique_ptr<chromeos::secure_channel::FakeClientChannel>
fake_client_channel_;
cryptauth::RemoteDeviceRef remote_device_;
cryptauth::FakeConnection connection_;
......@@ -175,23 +139,10 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
ProximityMonitorImpl monitor_;
private:
void ProvideConnectionInfo(
const BluetoothDevice::ConnectionInfo& connection_info) {
DCHECK(!base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi));
connection_info_callback_.Run(connection_info);
// Reset the callback to ensure that tests correctly only respond at most
// once per call to GetConnectionInfo().
connection_info_callback_ = BluetoothDevice::ConnectionInfoCallback();
}
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::ThreadTaskRunnerHandle thread_task_runner_handle_;
BluetoothDevice::ConnectionInfoCallback connection_info_callback_;
ScopedDisableLoggingForTesting disable_logging_;
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_NeverStarted) {
......@@ -199,51 +150,22 @@ TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_NeverStarted) {
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_Started_NoRssiReceivedYet) {
monitor_.Start();
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_Started_NoRssiReceivedYet_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
IsUnlockAllowed_Started_NoConnectionInfoReceivedYet) {
monitor_.Start();
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_RssiInRange) {
monitor_.Start();
ProvideRssi(4);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_RssiInRange_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(4);
ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_UnknownRssi) {
monitor_.Start();
ProvideRssi(0);
ProvideRssi(base::nullopt);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_UnknownRssi_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(0);
ProvideRssi(base::nullopt);
ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
......@@ -256,53 +178,20 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
// Simulate receiving an RSSI reading in proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideRssi(kRssiThreshold / 2);
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate a reading indicating non-proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideRssi(kRssiThreshold * 2);
ProvideRssi(kRssiThreshold * 2);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_InformsObserverOfChanges_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
// Initially, the device is not in proximity.
monitor_.Start();
EXPECT_FALSE(monitor_.IsUnlockAllowed());
// Simulate receiving an RSSI reading in proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideRssi(kRssiThreshold / 2);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate a reading indicating non-proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideRssi(kRssiThreshold * 2);
ProvideRssi(kRssiThreshold * 2);
ProvideConnectionInfo({2 * kRssiThreshold, 4, 4});
ProvideConnectionInfo({2 * kRssiThreshold, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_StartThenStop) {
monitor_.Start();
ProvideRssi(0);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
monitor_.Stop();
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_StartThenStop_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(0);
ProvideConnectionInfo({0, 0, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
monitor_.Stop();
......@@ -312,39 +201,18 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_StartThenStopThenStartAgain) {
monitor_.Start();
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
monitor_.Stop();
// Restarting the monitor should immediately reset the proximity state, rather
// than building on the previous rolling average.
monitor_.Start();
ProvideRssi(kRssiThreshold - 1);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_StartThenStopThenStartAgain_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
ProvideRssi(kRssiThreshold / 2);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
monitor_.Stop();
// Restarting the monitor should immediately reset the proximity state, rather
// than building on the previous rolling average.
monitor_.Start();
ProvideRssi(kRssiThreshold - 1);
ProvideConnectionInfo({kRssiThreshold - 1, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
......@@ -352,33 +220,15 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_RemoteDeviceRemainsInProximity) {
monitor_.Start();
ProvideRssi(kRssiThreshold / 2 + 1);
ProvideRssi(kRssiThreshold / 2 - 1);
ProvideRssi(kRssiThreshold / 2 + 2);
ProvideRssi(kRssiThreshold / 2 - 3);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Brief drops in RSSI should be handled by weighted averaging.
ProvideRssi(kRssiThreshold - 5);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_RemoteDeviceRemainsInProximity_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(kRssiThreshold / 2 + 1);
ProvideRssi(kRssiThreshold / 2 - 1);
ProvideRssi(kRssiThreshold / 2 + 2);
ProvideRssi(kRssiThreshold / 2 - 3);
ProvideConnectionInfo({kRssiThreshold / 2 + 1, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2 - 1, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2 + 2, 4, 4});
ProvideConnectionInfo({kRssiThreshold / 2 - 3, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Brief drops in RSSI should be handled by weighted averaging.
ProvideRssi(kRssiThreshold - 5);
ProvideConnectionInfo({kRssiThreshold - 5, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
}
......@@ -388,49 +238,22 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor_.Start();
// Start with a device in proximity.
ProvideRssi(0);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate readings for the remote device leaving proximity.
ProvideRssi(-1);
ProvideRssi(-4);
ProvideRssi(0);
ProvideRssi(-10);
ProvideRssi(-15);
ProvideRssi(-20);
ProvideRssi(kRssiThreshold);
ProvideRssi(kRssiThreshold - 10);
ProvideRssi(kRssiThreshold - 20);
ProvideRssi(kRssiThreshold - 20);
ProvideRssi(kRssiThreshold - 20);
ProvideRssi(kRssiThreshold - 20);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_RemoteDeviceLeavesProximity_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
// Start with a device in proximity.
ProvideRssi(0);
ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate readings for the remote device leaving proximity.
ProvideRssi(-1);
ProvideRssi(-4);
ProvideRssi(0);
ProvideRssi(-10);
ProvideRssi(-15);
ProvideRssi(-20);
ProvideRssi(kRssiThreshold);
ProvideRssi(kRssiThreshold - 10);
ProvideRssi(kRssiThreshold - 20);
ProvideRssi(kRssiThreshold - 20);
ProvideRssi(kRssiThreshold - 20);
ProvideRssi(kRssiThreshold - 20);
ProvideConnectionInfo({-1, 4, 4});
ProvideConnectionInfo({-4, 4, 4});
ProvideConnectionInfo({0, 4, 4});
ProvideConnectionInfo({-10, 4, 4});
ProvideConnectionInfo({-15, 4, 4});
ProvideConnectionInfo({-20, 4, 4});
ProvideConnectionInfo({kRssiThreshold, 4, 4});
ProvideConnectionInfo({kRssiThreshold - 10, 4, 4});
ProvideConnectionInfo({kRssiThreshold - 20, 4, 4});
ProvideConnectionInfo({kRssiThreshold - 20, 4, 4});
ProvideConnectionInfo({kRssiThreshold - 20, 4, 4});
ProvideConnectionInfo({kRssiThreshold - 20, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
......@@ -440,41 +263,19 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor_.Start();
// Start with a device out of proximity.
ProvideRssi(kRssiThreshold * 2);
ProvideConnectionInfo({2 * kRssiThreshold, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
// Simulate readings for the remote device entering proximity.
ProvideRssi(-15);
ProvideRssi(-8);
ProvideRssi(-12);
ProvideRssi(-18);
ProvideRssi(-7);
ProvideRssi(-3);
ProvideRssi(-2);
ProvideRssi(0);
ProvideRssi(0);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_RemoteDeviceEntersProximity_MultiDeviceApiEnabled) {
monitor_.Start();
// Start with a device out of proximity.
ProvideRssi(kRssiThreshold * 2);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
// Simulate readings for the remote device entering proximity.
ProvideRssi(-15);
ProvideRssi(-8);
ProvideRssi(-12);
ProvideRssi(-18);
ProvideRssi(-7);
ProvideRssi(-3);
ProvideRssi(-2);
ProvideRssi(0);
ProvideRssi(0);
ProvideConnectionInfo({-15, 4, 4});
ProvideConnectionInfo({-8, 4, 4});
ProvideConnectionInfo({-12, 4, 4});
ProvideConnectionInfo({-18, 4, 4});
ProvideConnectionInfo({-7, 4, 4});
ProvideConnectionInfo({-3, 4, 4});
ProvideConnectionInfo({-2, 4, 4});
ProvideConnectionInfo({0, 4, 4});
ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
}
......@@ -484,24 +285,7 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor_.Start();
// Start with the device known to the adapter and in proximity.
ProvideRssi(0);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate it being forgotten.
ON_CALL(*bluetooth_adapter_, GetDevice(std::string()))
.WillByDefault(Return(nullptr));
EXPECT_CALL(observer_, OnProximityStateChanged());
RunPendingTasks();
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_DeviceNotKnownToAdapter_MultiDeviceApiEnabled) {
monitor_.Start();
// Start with the device known to the adapter and in proximity.
ProvideRssi(0);
ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate it being forgotten.
......@@ -518,7 +302,7 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor_.Start();
// Start with the device connected and in proximity.
ProvideRssi(0);
ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate it disconnecting.
......@@ -529,67 +313,20 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_DeviceNotConnected_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
// Start with the device connected and in proximity.
ProvideRssi(0);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate it disconnecting.
fake_client_channel_->NotifyDisconnected();
EXPECT_CALL(observer_, OnProximityStateChanged());
RunPendingTasks();
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_ConnectionInfoReceivedAfterStopping) {
monitor_.Start();
monitor_.Stop();
ProvideRssi(0);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(
ProximityAuthProximityMonitorImplTest,
IsUnlockAllowed_ConnectionInfoReceivedAfterStopping_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
monitor_.Stop();
ProvideRssi(0);
ProvideConnectionInfo({0, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_NormalValues) {
monitor_.Start();
ProvideRssi(0);
ProvideRssi(-20);
base::HistogramTester histogram_tester;
monitor_.RecordProximityMetricsOnAuthSuccess();
histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
-6, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.RemoteDeviceModelHash",
1881443083 /* hash of "LGE Nexus 5" */, 1);
}
TEST_F(ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_NormalValues_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(0);
ProvideConnectionInfo({0, 0, 4});
ProvideRssi(-20);
ProvideConnectionInfo({-20, 3, 4});
base::HistogramTester histogram_tester;
monitor_.RecordProximityMetricsOnAuthSuccess();
......@@ -603,21 +340,7 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
TEST_F(ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_ClampedValues) {
monitor_.Start();
ProvideRssi(-99999);
base::HistogramTester histogram_tester;
monitor_.RecordProximityMetricsOnAuthSuccess();
histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
-100, 1);
}
TEST_F(
ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_ClampedValues_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
monitor_.Start();
ProvideRssi(-99999);
ProvideConnectionInfo({-99999, 99999, 12345});
base::HistogramTester histogram_tester;
monitor_.RecordProximityMetricsOnAuthSuccess();
......@@ -628,44 +351,15 @@ TEST_F(
TEST_F(ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_UnknownValues) {
// Note: A device without a recorded name will have "Unknown" as its name.
cryptauth::RemoteDeviceRef remote_device = cryptauth::RemoteDeviceRefBuilder()
.SetUserId(kRemoteDeviceUserId)
.SetName(std::string())
.Build();
cryptauth::FakeConnection connection(remote_device);
ProximityMonitorImpl monitor(remote_device, fake_client_channel_.get(),
&connection, pref_manager_.get());
monitor.AddObserver(&observer_);
monitor.Start();
ProvideRssi(127);
cryptauth::FakeConnection connection(cryptauth::RemoteDeviceRefBuilder()
.SetUserId(kRemoteDeviceUserId)
.SetName(std::string())
.Build());
base::HistogramTester histogram_tester;
monitor.RecordProximityMetricsOnAuthSuccess();
histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
127, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.RemoteDeviceModelHash",
-1808066424 /* hash of "Unknown" */, 1);
}
TEST_F(
ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_UnknownValues_MultiDeviceApiEnabled) {
SetMultiDeviceApiEnabled();
// Note: A device without a recorded name will have "Unknown" as its name.
cryptauth::RemoteDeviceRef remote_device = cryptauth::RemoteDeviceRefBuilder()
.SetUserId(kRemoteDeviceUserId)
.SetName(std::string())
.Build();
cryptauth::FakeConnection connection(remote_device);
ProximityMonitorImpl monitor(remote_device, fake_client_channel_.get(),
&connection, pref_manager_.get());
ProximityMonitorImpl monitor(&connection, pref_manager_.get());
monitor.AddObserver(&observer_);
monitor.Start();
ProvideRssi(127);
ProvideConnectionInfo({127, 127, 127});
base::HistogramTester histogram_tester;
monitor.RecordProximityMetricsOnAuthSuccess();
......
......@@ -324,10 +324,7 @@ void UnlockManagerImpl::OnAuthAttempted(mojom::AuthType auth_type) {
std::unique_ptr<ProximityMonitor> UnlockManagerImpl::CreateProximityMonitor(
cryptauth::Connection* connection,
ProximityAuthPrefManager* pref_manager) {
// TODO(crbug.com/752273): Inject a real ClientChannel.
return std::make_unique<ProximityMonitorImpl>(connection->remote_device(),
nullptr /* channel */,
connection, pref_manager);
return std::make_unique<ProximityMonitorImpl>(connection, pref_manager);
}
void UnlockManagerImpl::SendSignInChallenge() {
......
......@@ -14,16 +14,9 @@ FakeClientChannel::FakeClientChannel() = default;
FakeClientChannel::~FakeClientChannel() = default;
void FakeClientChannel::InvokePendingGetConnectionMetadataCallback(
mojom::ConnectionMetadataPtr connection_metadata) {
std::move(get_connection_metadata_callback_queue_.front())
.Run(std::move(connection_metadata));
get_connection_metadata_callback_queue_.pop();
}
void FakeClientChannel::PerformGetConnectionMetadata(
base::OnceCallback<void(mojom::ConnectionMetadataPtr)> callback) {
get_connection_metadata_callback_queue_.push(std::move(callback));
std::move(callback).Run(std::move(connection_metadata_for_next_call_));
}
void FakeClientChannel::PerformSendMessage(const std::string& payload,
......
......@@ -5,8 +5,6 @@
#ifndef CHROMEOS_SERVICES_SECURE_CHANNEL_PUBLIC_CPP_CLIENT_FAKE_CLIENT_CHANNEL_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_PUBLIC_CPP_CLIENT_FAKE_CLIENT_CHANNEL_H_
#include <queue>
#include "base/macros.h"
#include "chromeos/services/secure_channel/public/cpp/client/client_channel.h"
#include "chromeos/services/secure_channel/public/mojom/secure_channel.mojom.h"
......@@ -24,8 +22,11 @@ class FakeClientChannel : public ClientChannel {
using ClientChannel::NotifyDisconnected;
using ClientChannel::NotifyMessageReceived;
void InvokePendingGetConnectionMetadataCallback(
mojom::ConnectionMetadataPtr connection_metadata);
void set_connection_metadata_for_next_call(
mojom::ConnectionMetadataPtr connection_metadata_for_next_call) {
connection_metadata_for_next_call_ =
std::move(connection_metadata_for_next_call);
}
std::vector<std::pair<std::string, base::OnceClosure>>& sent_messages() {
return sent_messages_;
......@@ -40,10 +41,7 @@ class FakeClientChannel : public ClientChannel {
void PerformSendMessage(const std::string& payload,
base::OnceClosure on_sent_callback) override;
// Queues up callbacks passed into PerformGetConnectionMetadata(), to be
// invoked later.
std::queue<base::OnceCallback<void(mojom::ConnectionMetadataPtr)>>
get_connection_metadata_callback_queue_;
mojom::ConnectionMetadataPtr connection_metadata_for_next_call_;
std::vector<std::pair<std::string, base::OnceClosure>> sent_messages_;
DISALLOW_COPY_AND_ASSIGN(FakeClientChannel);
......
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