Commit bfabbfb6 authored by tengs's avatar tengs Committed by Commit bot

[EasyUnlock] Update ProximityMonitor to only check for RSSI proximity.

This CL also changes UnlockManager to use the new interface for ProximityMonitor.

Review-Url: https://codereview.chromium.org/2845433003
Cr-Commit-Position: refs/heads/master@{#467860}
parent 97681b52
...@@ -22,6 +22,10 @@ cryptauth::RemoteDevice FakeRemoteDeviceLifeCycle::GetRemoteDevice() const { ...@@ -22,6 +22,10 @@ cryptauth::RemoteDevice FakeRemoteDeviceLifeCycle::GetRemoteDevice() const {
return remote_device_; return remote_device_;
} }
cryptauth::Connection* FakeRemoteDeviceLifeCycle::GetConnection() const {
return connection_;
}
RemoteDeviceLifeCycle::State FakeRemoteDeviceLifeCycle::GetState() const { RemoteDeviceLifeCycle::State FakeRemoteDeviceLifeCycle::GetState() const {
return state_; return state_;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "components/cryptauth/fake_connection.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
#include "components/proximity_auth/remote_device_life_cycle.h" #include "components/proximity_auth/remote_device_life_cycle.h"
...@@ -20,6 +21,7 @@ class FakeRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle { ...@@ -20,6 +21,7 @@ class FakeRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle {
// RemoteDeviceLifeCycle: // RemoteDeviceLifeCycle:
void Start() override; void Start() override;
cryptauth::RemoteDevice GetRemoteDevice() const override; cryptauth::RemoteDevice GetRemoteDevice() const override;
cryptauth::Connection* GetConnection() const override;
State GetState() const override; State GetState() const override;
Messenger* GetMessenger() override; Messenger* GetMessenger() override;
void AddObserver(Observer* observer) override; void AddObserver(Observer* observer) override;
...@@ -30,6 +32,10 @@ class FakeRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle { ...@@ -30,6 +32,10 @@ class FakeRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle {
void set_messenger(Messenger* messenger) { messenger_ = messenger; } void set_messenger(Messenger* messenger) { messenger_ = messenger; }
void set_connection(cryptauth::Connection* connection) {
connection_ = connection;
}
bool started() { return started_; } bool started() { return started_; }
base::ObserverList<Observer>& observers() { return observers_; } base::ObserverList<Observer>& observers() { return observers_; }
...@@ -43,6 +49,8 @@ class FakeRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle { ...@@ -43,6 +49,8 @@ class FakeRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle {
State state_; State state_;
cryptauth::Connection* connection_;
Messenger* messenger_; Messenger* messenger_;
DISALLOW_COPY_AND_ASSIGN(FakeRemoteDeviceLifeCycle); DISALLOW_COPY_AND_ASSIGN(FakeRemoteDeviceLifeCycle);
......
...@@ -42,6 +42,11 @@ class Messenger { ...@@ -42,6 +42,11 @@ class Messenger {
// Returns the SecureContext instance used by the messenger. Ownership of the // Returns the SecureContext instance used by the messenger. Ownership of the
// SecureContext is not passed. // SecureContext is not passed.
virtual cryptauth::SecureContext* GetSecureContext() const = 0; virtual cryptauth::SecureContext* GetSecureContext() const = 0;
// Returns the underlying raw connection. Note that you should use
// |GetSecureContext()| instead if you want to send and receive messages
// securely.
virtual cryptauth::Connection* GetConnection() const = 0;
}; };
} // namespace proximity_auth } // namespace proximity_auth
......
...@@ -154,6 +154,10 @@ cryptauth::SecureContext* MessengerImpl::GetSecureContext() const { ...@@ -154,6 +154,10 @@ cryptauth::SecureContext* MessengerImpl::GetSecureContext() const {
return secure_context_.get(); return secure_context_.get();
} }
cryptauth::Connection* MessengerImpl::GetConnection() const {
return connection_.get();
}
MessengerImpl::PendingMessage::PendingMessage() {} MessengerImpl::PendingMessage::PendingMessage() {}
MessengerImpl::PendingMessage::PendingMessage( MessengerImpl::PendingMessage::PendingMessage(
......
...@@ -44,6 +44,7 @@ class MessengerImpl : public Messenger, public cryptauth::ConnectionObserver { ...@@ -44,6 +44,7 @@ class MessengerImpl : public Messenger, public cryptauth::ConnectionObserver {
void RequestDecryption(const std::string& challenge) override; void RequestDecryption(const std::string& challenge) override;
void RequestUnlock() override; void RequestUnlock() override;
cryptauth::SecureContext* GetSecureContext() const override; cryptauth::SecureContext* GetSecureContext() const override;
cryptauth::Connection* GetConnection() const override;
// Exposed for testing. // Exposed for testing.
cryptauth::Connection* connection() { return connection_.get(); } cryptauth::Connection* connection() { return connection_.get(); }
......
...@@ -13,8 +13,6 @@ namespace proximity_auth { ...@@ -13,8 +13,6 @@ namespace proximity_auth {
// sufficiently close to the local device to permit unlocking. // sufficiently close to the local device to permit unlocking.
class ProximityMonitor { class ProximityMonitor {
public: public:
enum class Strategy { NONE, CHECK_RSSI, CHECK_TRANSMIT_POWER };
virtual ~ProximityMonitor() {} virtual ~ProximityMonitor() {}
// Activates the proximity monitor. No-op if the proximity monitor is already // Activates the proximity monitor. No-op if the proximity monitor is already
...@@ -25,18 +23,10 @@ class ProximityMonitor { ...@@ -25,18 +23,10 @@ class ProximityMonitor {
// already inactive. // already inactive.
virtual void Stop() = 0; virtual void Stop() = 0;
// Returns the strategy used to determine whether the remote device is in
// proximity.
virtual Strategy GetStrategy() const = 0;
// Returns |true| iff the remote device is close enough to the local device, // Returns |true| iff the remote device is close enough to the local device,
// given the user's current settings. // given the user's current settings.
virtual bool IsUnlockAllowed() const = 0; virtual bool IsUnlockAllowed() const = 0;
// Returns |true| iff the remote device is close enough to the local device,
// according to its RSSI measurement.
virtual bool IsInRssiRange() const = 0;
// Records the current proximity measurements to UMA. This should be called // Records the current proximity measurements to UMA. This should be called
// when the user successfully authenticates using proximity auth. // when the user successfully authenticates using proximity auth.
virtual void RecordProximityMetricsOnAuthSuccess() = 0; virtual void RecordProximityMetricsOnAuthSuccess() = 0;
......
...@@ -27,16 +27,15 @@ const int kPollingTimeoutMs = 250; ...@@ -27,16 +27,15 @@ const int kPollingTimeoutMs = 250;
// The RSSI threshold below which we consider the remote device to not be in // The RSSI threshold below which we consider the remote device to not be in
// proximity. // proximity.
const int kRssiThreshold = -5; const int kRssiThreshold = -45;
// The weight of the most recent RSSI sample. // The weight of the most recent RSSI sample.
const double kRssiSampleWeight = 0.3; const double kRssiSampleWeight = 0.3;
ProximityMonitorImpl::ProximityMonitorImpl( ProximityMonitorImpl::ProximityMonitorImpl(
const cryptauth::RemoteDevice& remote_device, cryptauth::Connection* connection,
std::unique_ptr<base::TickClock> clock) std::unique_ptr<base::TickClock> clock)
: remote_device_(remote_device), : connection_(connection),
strategy_(Strategy::NONE),
remote_device_is_in_proximity_(false), remote_device_is_in_proximity_(false),
is_active_(false), is_active_(false),
clock_(std::move(clock)), clock_(std::move(clock)),
...@@ -50,11 +49,6 @@ ProximityMonitorImpl::ProximityMonitorImpl( ...@@ -50,11 +49,6 @@ ProximityMonitorImpl::ProximityMonitorImpl(
PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: "
<< "Bluetooth is unsupported on this platform."; << "Bluetooth is unsupported on this platform.";
} }
// TODO(isherman): Test prefs to set the strategy. Need to read from "Local
// State" prefs on the sign-in screen, and per-user prefs on the lock screen.
// TODO(isherman): Unlike in the JS app, destroy and recreate the proximity
// monitor when the connection state changes.
} }
ProximityMonitorImpl::~ProximityMonitorImpl() { ProximityMonitorImpl::~ProximityMonitorImpl() {
...@@ -71,17 +65,8 @@ void ProximityMonitorImpl::Stop() { ...@@ -71,17 +65,8 @@ void ProximityMonitorImpl::Stop() {
UpdatePollingState(); UpdatePollingState();
} }
ProximityMonitor::Strategy ProximityMonitorImpl::GetStrategy() const {
return strategy_;
}
bool ProximityMonitorImpl::IsUnlockAllowed() const { bool ProximityMonitorImpl::IsUnlockAllowed() const {
return strategy_ == Strategy::NONE || remote_device_is_in_proximity_; return remote_device_is_in_proximity_;
}
bool ProximityMonitorImpl::IsInRssiRange() const {
return (strategy_ != Strategy::NONE && rssi_rolling_average_ &&
*rssi_rolling_average_ > kRssiThreshold);
} }
void ProximityMonitorImpl::RecordProximityMetricsOnAuthSuccess() { void ProximityMonitorImpl::RecordProximityMetricsOnAuthSuccess() {
...@@ -89,26 +74,12 @@ void ProximityMonitorImpl::RecordProximityMetricsOnAuthSuccess() { ...@@ -89,26 +74,12 @@ void ProximityMonitorImpl::RecordProximityMetricsOnAuthSuccess() {
? *rssi_rolling_average_ ? *rssi_rolling_average_
: metrics::kUnknownProximityValue; : metrics::kUnknownProximityValue;
int last_transmit_power_delta =
last_transmit_power_reading_
? (last_transmit_power_reading_->transmit_power -
last_transmit_power_reading_->max_transmit_power)
: metrics::kUnknownProximityValue;
// If no zero RSSI value has been read, then record an overflow.
base::TimeDelta time_since_last_zero_rssi;
if (last_zero_rssi_timestamp_)
time_since_last_zero_rssi = clock_->NowTicks() - *last_zero_rssi_timestamp_;
else
time_since_last_zero_rssi = base::TimeDelta::FromDays(100);
std::string remote_device_model = metrics::kUnknownDeviceModel; std::string remote_device_model = metrics::kUnknownDeviceModel;
if (remote_device_.name != remote_device_.bluetooth_address) cryptauth::RemoteDevice remote_device = connection_->remote_device();
remote_device_model = remote_device_.name; if (!remote_device.name.empty())
remote_device_model = remote_device.name;
metrics::RecordAuthProximityRollingRssi(round(rssi_rolling_average)); metrics::RecordAuthProximityRollingRssi(round(rssi_rolling_average));
metrics::RecordAuthProximityTransmitPowerDelta(last_transmit_power_delta);
metrics::RecordAuthProximityTimeSinceLastZeroRssi(time_since_last_zero_rssi);
metrics::RecordAuthProximityRemoteDeviceModelHash(remote_device_model); metrics::RecordAuthProximityRemoteDeviceModelHash(remote_device_model);
} }
...@@ -120,24 +91,6 @@ void ProximityMonitorImpl::RemoveObserver(ProximityMonitorObserver* observer) { ...@@ -120,24 +91,6 @@ void ProximityMonitorImpl::RemoveObserver(ProximityMonitorObserver* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
void ProximityMonitorImpl::SetStrategy(Strategy strategy) {
if (strategy_ == strategy)
return;
strategy_ = strategy;
CheckForProximityStateChange();
UpdatePollingState();
}
ProximityMonitorImpl::TransmitPowerReading::TransmitPowerReading(
int transmit_power,
int max_transmit_power)
: transmit_power(transmit_power), max_transmit_power(max_transmit_power) {
}
bool ProximityMonitorImpl::TransmitPowerReading::IsInProximity() const {
return transmit_power < max_transmit_power;
}
void ProximityMonitorImpl::OnAdapterInitialized( void ProximityMonitorImpl::OnAdapterInitialized(
scoped_refptr<device::BluetoothAdapter> adapter) { scoped_refptr<device::BluetoothAdapter> adapter) {
bluetooth_adapter_ = adapter; bluetooth_adapter_ = adapter;
...@@ -170,25 +123,23 @@ void ProximityMonitorImpl::PerformScheduledUpdatePollingState() { ...@@ -170,25 +123,23 @@ void ProximityMonitorImpl::PerformScheduledUpdatePollingState() {
} }
bool ProximityMonitorImpl::ShouldPoll() const { bool ProximityMonitorImpl::ShouldPoll() const {
// Note: We poll even if the strategy is NONE so we can record measurements.
return is_active_ && bluetooth_adapter_; return is_active_ && bluetooth_adapter_;
} }
void ProximityMonitorImpl::Poll() { void ProximityMonitorImpl::Poll() {
DCHECK(ShouldPoll()); DCHECK(ShouldPoll());
BluetoothDevice* device = std::string address = connection_->GetDeviceAddress();
bluetooth_adapter_->GetDevice(remote_device_.bluetooth_address); BluetoothDevice* device = bluetooth_adapter_->GetDevice(address);
if (!device) { if (!device) {
PA_LOG(ERROR) << "Unknown Bluetooth device with address " PA_LOG(ERROR) << "Unknown Bluetooth device with address " << address;
<< remote_device_.bluetooth_address;
ClearProximityState(); ClearProximityState();
return; return;
} }
if (!device->IsConnected()) { if (!device->IsConnected()) {
PA_LOG(ERROR) << "Bluetooth device with address " PA_LOG(ERROR) << "Bluetooth device with address " << address
<< remote_device_.bluetooth_address << " is not connected."; << " is not connected.";
ClearProximityState(); ClearProximityState();
return; return;
} }
...@@ -204,17 +155,12 @@ void ProximityMonitorImpl::OnConnectionInfo( ...@@ -204,17 +155,12 @@ void ProximityMonitorImpl::OnConnectionInfo(
return; return;
} }
if (connection_info.rssi != BluetoothDevice::kUnknownPower && if (connection_info.rssi != BluetoothDevice::kUnknownPower) {
connection_info.transmit_power != BluetoothDevice::kUnknownPower &&
connection_info.max_transmit_power != BluetoothDevice::kUnknownPower) {
AddSample(connection_info); AddSample(connection_info);
} else { } else {
PA_LOG(WARNING) << "[Proximity] Unkown values received from API: " PA_LOG(WARNING) << "[Proximity] Unkown values received from API: "
<< connection_info.rssi << " " << connection_info.rssi;
<< connection_info.transmit_power << " "
<< connection_info.max_transmit_power;
rssi_rolling_average_.reset(); rssi_rolling_average_.reset();
last_transmit_power_reading_.reset();
CheckForProximityStateChange(); CheckForProximityStateChange();
} }
} }
...@@ -227,8 +173,6 @@ void ProximityMonitorImpl::ClearProximityState() { ...@@ -227,8 +173,6 @@ void ProximityMonitorImpl::ClearProximityState() {
remote_device_is_in_proximity_ = false; remote_device_is_in_proximity_ = false;
rssi_rolling_average_.reset(); rssi_rolling_average_.reset();
last_transmit_power_reading_.reset();
last_zero_rssi_timestamp_.reset();
} }
void ProximityMonitorImpl::AddSample( void ProximityMonitorImpl::AddSample(
...@@ -240,33 +184,16 @@ void ProximityMonitorImpl::AddSample( ...@@ -240,33 +184,16 @@ void ProximityMonitorImpl::AddSample(
*rssi_rolling_average_ = *rssi_rolling_average_ =
weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_);
} }
last_transmit_power_reading_.reset(new TransmitPowerReading(
connection_info.transmit_power, connection_info.max_transmit_power));
// It's rare but possible for the RSSI to be positive briefly.
if (connection_info.rssi >= 0)
last_zero_rssi_timestamp_.reset(new base::TimeTicks(clock_->NowTicks()));
CheckForProximityStateChange(); CheckForProximityStateChange();
} }
void ProximityMonitorImpl::CheckForProximityStateChange() { void ProximityMonitorImpl::CheckForProximityStateChange() {
if (strategy_ == Strategy::NONE) bool is_now_in_proximity =
return; rssi_rolling_average_ && *rssi_rolling_average_ > kRssiThreshold;
bool is_now_in_proximity = false;
switch (strategy_) {
case Strategy::NONE:
return;
case Strategy::CHECK_RSSI:
is_now_in_proximity = IsInRssiRange();
break;
case Strategy::CHECK_TRANSMIT_POWER: if (rssi_rolling_average_) {
is_now_in_proximity = (last_transmit_power_reading_ && LOG(WARNING) << "RSSI: " << *rssi_rolling_average_;
last_transmit_power_reading_->IsInProximity());
break;
} }
if (remote_device_is_in_proximity_ != is_now_in_proximity) { if (remote_device_is_in_proximity_ != is_now_in_proximity) {
......
...@@ -11,13 +11,13 @@ ...@@ -11,13 +11,13 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "components/cryptauth/connection.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
#include "components/proximity_auth/proximity_monitor.h" #include "components/proximity_auth/proximity_monitor.h"
#include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_device.h"
namespace base { namespace base {
class TickClock; class TickClock;
class TimeTicks;
} }
namespace device { namespace device {
...@@ -31,41 +31,20 @@ class ProximityMonitorObserver; ...@@ -31,41 +31,20 @@ class ProximityMonitorObserver;
// The concrete implemenation of the proximity monitor interface. // The concrete implemenation of the proximity monitor interface.
class ProximityMonitorImpl : public ProximityMonitor { class ProximityMonitorImpl : public ProximityMonitor {
public: public:
// The |observer| is not owned, and must outlive |this| instance. // The |connection| is not owned, and must outlive |this| instance.
ProximityMonitorImpl(const cryptauth::RemoteDevice& remote_device, ProximityMonitorImpl(cryptauth::Connection* connection,
std::unique_ptr<base::TickClock> clock); std::unique_ptr<base::TickClock> clock);
~ProximityMonitorImpl() override; ~ProximityMonitorImpl() override;
// ProximityMonitor: // ProximityMonitor:
void Start() override; void Start() override;
void Stop() override; void Stop() override;
Strategy GetStrategy() const override;
bool IsUnlockAllowed() const override; bool IsUnlockAllowed() const override;
bool IsInRssiRange() const override;
void RecordProximityMetricsOnAuthSuccess() override; void RecordProximityMetricsOnAuthSuccess() override;
void AddObserver(ProximityMonitorObserver* observer) override; void AddObserver(ProximityMonitorObserver* observer) override;
void RemoveObserver(ProximityMonitorObserver* observer) override; void RemoveObserver(ProximityMonitorObserver* observer) override;
protected:
// Sets the proximity detection strategy. Exposed for testing.
// TODO(isherman): Stop exposing this for testing once prefs are properly
// hooked up.
virtual void SetStrategy(Strategy strategy);
private: private:
struct TransmitPowerReading {
TransmitPowerReading(int transmit_power, int max_transmit_power);
// Returns true if |this| transmit power reading indicates proximity.
bool IsInProximity() const;
// The current transmit power.
int transmit_power;
// The maximum possible transmit power.
int max_transmit_power;
};
// Callback for asynchronous initialization of the Bluetooth adpater. // Callback for asynchronous initialization of the Bluetooth adpater.
void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
...@@ -95,7 +74,7 @@ class ProximityMonitorImpl : public ProximityMonitor { ...@@ -95,7 +74,7 @@ class ProximityMonitorImpl : public ProximityMonitor {
void ClearProximityState(); void ClearProximityState();
// Updates the proximity state with a new |connection_info| sample of the // Updates the proximity state with a new |connection_info| sample of the
// current RSSI and Tx power, and the device's maximum Tx power. // current RSSI.
void AddSample( void AddSample(
const device::BluetoothDevice::ConnectionInfo& connection_info); const device::BluetoothDevice::ConnectionInfo& connection_info);
...@@ -103,8 +82,9 @@ class ProximityMonitorImpl : public ProximityMonitor { ...@@ -103,8 +82,9 @@ class ProximityMonitorImpl : public ProximityMonitor {
// samples. Notifies |observers_| on a change. // samples. Notifies |observers_| on a change.
void CheckForProximityStateChange(); void CheckForProximityStateChange();
// The remote device being monitored. // The current connection being monitored. Not owned and must outlive this
const cryptauth::RemoteDevice remote_device_; // instance.
cryptauth::Connection* connection_;
// The observers attached to the ProximityMonitor. // The observers attached to the ProximityMonitor.
base::ObserverList<ProximityMonitorObserver> observers_; base::ObserverList<ProximityMonitorObserver> observers_;
...@@ -112,9 +92,6 @@ class ProximityMonitorImpl : public ProximityMonitor { ...@@ -112,9 +92,6 @@ class ProximityMonitorImpl : public ProximityMonitor {
// The Bluetooth adapter that will be polled for connection info. // The Bluetooth adapter that will be polled for connection info.
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_; scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
// The strategy used to determine whether the remote device is in proximity.
Strategy strategy_;
// Whether the remote device is currently in close proximity to the local // Whether the remote device is currently in close proximity to the local
// device. // device.
bool remote_device_is_in_proximity_; bool remote_device_is_in_proximity_;
...@@ -129,18 +106,6 @@ class ProximityMonitorImpl : public ProximityMonitor { ...@@ -129,18 +106,6 @@ class ProximityMonitorImpl : public ProximityMonitor {
// measurement. // measurement.
std::unique_ptr<double> rssi_rolling_average_; std::unique_ptr<double> rssi_rolling_average_;
// The last TX power reading. Null if the monitor is inactive, has not
// recently observed a TX power reading, or the most recent connection info
// included an invalid measurement.
std::unique_ptr<TransmitPowerReading> last_transmit_power_reading_;
// The timestamp of the last zero RSSI reading. An RSSI value of 0 is special
// because both devices adjust their transmit powers such that the RSSI is in
// this golden range, if possible. Null if the monitor is inactive, has not
// recently observed an RSSI reading, or the most recent connection info
// included an invalid measurement.
std::unique_ptr<base::TimeTicks> last_zero_rssi_timestamp_;
// Used to access non-decreasing time measurements. // Used to access non-decreasing time measurements.
std::unique_ptr<base::TickClock> clock_; std::unique_ptr<base::TickClock> clock_;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "components/cryptauth/fake_connection.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
#include "components/proximity_auth/logging/logging.h" #include "components/proximity_auth/logging/logging.h"
#include "components/proximity_auth/proximity_monitor_observer.h" #include "components/proximity_auth/proximity_monitor_observer.h"
...@@ -33,24 +34,11 @@ namespace proximity_auth { ...@@ -33,24 +34,11 @@ namespace proximity_auth {
namespace { namespace {
const char kRemoteDeviceUserId[] = "example@gmail.com"; const char kRemoteDeviceUserId[] = "example@gmail.com";
const char kBluetoothAddress[] = "AA:BB:CC:DD:EE:FF";
const char kRemoteDevicePublicKey[] = "Remote Public Key"; const char kRemoteDevicePublicKey[] = "Remote Public Key";
const char kRemoteDeviceName[] = "LGE Nexus 5"; const char kRemoteDeviceName[] = "LGE Nexus 5";
const char kBluetoothAddress[] = "AA:BB:CC:DD:EE:FF";
const char kPersistentSymmetricKey[] = "PSK"; const char kPersistentSymmetricKey[] = "PSK";
const int kRssiThreshold = -45;
class TestProximityMonitorImpl : public ProximityMonitorImpl {
public:
explicit TestProximityMonitorImpl(
const cryptauth::RemoteDevice& remote_device,
std::unique_ptr<base::TickClock> clock)
: ProximityMonitorImpl(remote_device, std::move(clock)) {}
~TestProximityMonitorImpl() override {}
using ProximityMonitorImpl::SetStrategy;
private:
DISALLOW_COPY_AND_ASSIGN(TestProximityMonitorImpl);
};
class MockProximityMonitorObserver : public ProximityMonitorObserver { class MockProximityMonitorObserver : public ProximityMonitorObserver {
public: public:
...@@ -83,17 +71,17 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test { ...@@ -83,17 +71,17 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
remote_bluetooth_device_(&*bluetooth_adapter_, remote_bluetooth_device_(&*bluetooth_adapter_,
0, 0,
kRemoteDeviceName, kRemoteDeviceName,
kBluetoothAddress, "",
false /* paired */, false /* paired */,
true /* connected */), true /* connected */),
monitor_( remote_device_(kRemoteDeviceUserId,
cryptauth::RemoteDevice(kRemoteDeviceUserId, kRemoteDeviceName,
kRemoteDeviceName, kRemoteDevicePublicKey,
kRemoteDevicePublicKey, kBluetoothAddress,
kBluetoothAddress, kPersistentSymmetricKey,
kPersistentSymmetricKey, std::string()),
std::string()), connection_(remote_device_),
base::WrapUnique(clock_)), monitor_(&connection_, base::WrapUnique(clock_)),
task_runner_(new base::TestSimpleTaskRunner()), task_runner_(new base::TestSimpleTaskRunner()),
thread_task_runner_handle_(task_runner_) { thread_task_runner_handle_(task_runner_) {
ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress))
...@@ -102,6 +90,7 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test { ...@@ -102,6 +90,7 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
.WillByDefault(SaveArg<0>(&connection_info_callback_)); .WillByDefault(SaveArg<0>(&connection_info_callback_));
monitor_.AddObserver(&observer_); monitor_.AddObserver(&observer_);
} }
~ProximityAuthProximityMonitorImplTest() override {} ~ProximityAuthProximityMonitorImplTest() override {}
void RunPendingTasks() { task_runner_->RunPendingTasks(); } void RunPendingTasks() { task_runner_->RunPendingTasks(); }
...@@ -126,9 +115,11 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test { ...@@ -126,9 +115,11 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
// Mocks used for verifying interactions with the Bluetooth subsystem. // Mocks used for verifying interactions with the Bluetooth subsystem.
scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_;
NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_; NiceMock<device::MockBluetoothDevice> remote_bluetooth_device_;
cryptauth::RemoteDevice remote_device_;
cryptauth::FakeConnection connection_;
// The proximity monitor under test. // The proximity monitor under test.
TestProximityMonitorImpl monitor_; ProximityMonitorImpl monitor_;
private: private:
scoped_refptr<base::TestSimpleTaskRunner> task_runner_; scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
...@@ -137,304 +128,124 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test { ...@@ -137,304 +128,124 @@ class ProximityAuthProximityMonitorImplTest : public testing::Test {
ScopedDisableLoggingForTesting disable_logging_; ScopedDisableLoggingForTesting disable_logging_;
}; };
TEST_F(ProximityAuthProximityMonitorImplTest, GetStrategy) { TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_NeverStarted) {
EXPECT_EQ(ProximityMonitor::Strategy::NONE, monitor_.GetStrategy());
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
EXPECT_EQ(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER,
monitor_.GetStrategy());
}
TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_StrategyIsNone) {
monitor_.SetStrategy(ProximityMonitor::Strategy::NONE);
EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_NeverStarted) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_Started_NoConnectionInfoReceivedYet) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start();
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_InformsObserverOfChanges) { IsUnlockAllowed_Started_NoConnectionInfoReceivedYet) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
// Initially, the device is not in proximity.
monitor_.Start(); monitor_.Start();
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
// Simulate a reading indicating proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideConnectionInfo({0, 0, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
// Simulate a reading indicating non-proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideConnectionInfo({0, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_RssiInRange) {
ProximityState_CheckRssi_RssiIndicatesProximity_TxPowerDoesNot) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckRssi_TxPowerIndicatesProximity_RssiDoesNot) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start();
ProvideConnectionInfo({-10, 0, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckRssi_NeitherRssiNorTxPowerIndicatesProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start();
ProvideConnectionInfo({-10, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckRssi_BothRssiAndTxPowerIndicateProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_UnknownRssi) {
ProximityState_CheckRssi_UnknownRssi) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({0, 0, 4}); ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4}); ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckRssi_UnknownTxPower) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({0, BluetoothDevice::kUnknownPower, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckRssi_UnknownMaxTxPower) { IsUnlockAllowed_InformsObserverOfChanges) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI); // Initially, the device is not in proximity.
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({0, 0, BluetoothDevice::kUnknownPower});
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckTxPower_RssiIndicatesProximity_TxPowerDoesNot) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start();
ProvideConnectionInfo({0, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckTxPower_TxPowerIndicatesProximity_RssiDoesNot) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start();
ProvideConnectionInfo({-10, 0, 4});
// Simulate receiving an RSSI reading in proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckTxPower_NeitherRssiNorTxPowerIndicatesProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start();
ProvideConnectionInfo({-10, 4, 4});
// Simulate a reading indicating non-proximity.
EXPECT_CALL(observer_, OnProximityStateChanged()).Times(1);
ProvideConnectionInfo({2 * kRssiThreshold, 4, 4});
ProvideConnectionInfo({2 * kRssiThreshold, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest, IsUnlockAllowed_StartThenStop) {
ProximityState_CheckTxPower_BothRssiAndTxPowerIndicateProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({0, 0, 4}); ProvideConnectionInfo({0, 0, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckTxPower_UnknownRssi) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({BluetoothDevice::kUnknownPower, 0, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckTxPower_UnknownTxPower) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({0, BluetoothDevice::kUnknownPower, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_CheckTxPower_UnknownMaxTxPower) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER);
monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
ProvideConnectionInfo({0, 0, BluetoothDevice::kUnknownPower});
EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
}
TEST_F(ProximityAuthProximityMonitorImplTest, ProximityState_StartThenStop) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start();
ProvideConnectionInfo({0, 0, 4});
monitor_.Stop(); monitor_.Stop();
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_StartThenStopThenStartAgain) { IsUnlockAllowed_StartThenStopThenStartAgain) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed());
monitor_.Stop(); monitor_.Stop();
// Restarting the monitor should immediately reset the proximity state, rather // Restarting the monitor should immediately reset the proximity state, rather
// than building on the previous rolling average. // than building on the previous rolling average.
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({kRssiThreshold - 1, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_RemoteDeviceRemainsInProximity) { IsUnlockAllowed_RemoteDeviceRemainsInProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2 + 1, 4, 4});
ProvideConnectionInfo({-1, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2 - 1, 4, 4});
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2 + 2, 4, 4});
ProvideConnectionInfo({-2, 4, 4}); ProvideConnectionInfo({kRssiThreshold / 2 - 3, 4, 4});
ProvideConnectionInfo({-1, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
// Brief drops in RSSI should be handled by weighted averaging. // Brief drops in RSSI should be handled by weighted averaging.
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold - 5, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_RemoteDeviceLeavesProximity) { IsUnlockAllowed_RemoteDeviceLeavesProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
// Start with a device in proximity. // Start with a device in proximity.
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
// Simulate readings for the remote device leaving proximity. // Simulate readings for the remote device leaving proximity.
ProvideConnectionInfo({-1, 4, 4}); ProvideConnectionInfo({-1, 4, 4});
ProvideConnectionInfo({-4, 4, 4}); ProvideConnectionInfo({-4, 4, 4});
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({-10, 4, 4});
ProvideConnectionInfo({-8, 4, 4});
ProvideConnectionInfo({-15, 4, 4}); ProvideConnectionInfo({-15, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({-20, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold - 10, 4, 4});
ProvideConnectionInfo({-10, 4, 4}); ProvideConnectionInfo({kRssiThreshold - 20, 4, 4});
ProvideConnectionInfo({kRssiThreshold - 20, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_RemoteDeviceEntersProximity) { IsUnlockAllowed_RemoteDeviceEntersProximity) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
// Start with a device in proximity. // Start with a device out of proximity.
ProvideConnectionInfo({-20, 4, 4}); ProvideConnectionInfo({2 * kRssiThreshold, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
// Simulate readings for the remote device entering proximity. // Simulate readings for the remote device entering proximity.
ProvideConnectionInfo({-15, 4, 4}); ProvideConnectionInfo({-15, 4, 4});
...@@ -448,18 +259,15 @@ TEST_F(ProximityAuthProximityMonitorImplTest, ...@@ -448,18 +259,15 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_DeviceNotKnownToAdapter) { IsUnlockAllowed_DeviceNotKnownToAdapter) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
// Start with the device known to the adapter and in proximity. // Start with the device known to the adapter and in proximity.
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
// Simulate it being forgotten. // Simulate it being forgotten.
ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress)) ON_CALL(*bluetooth_adapter_, GetDevice(kBluetoothAddress))
...@@ -468,18 +276,15 @@ TEST_F(ProximityAuthProximityMonitorImplTest, ...@@ -468,18 +276,15 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
RunPendingTasks(); RunPendingTasks();
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_DeviceNotConnected) { IsUnlockAllowed_DeviceNotConnected) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
// Start with the device connected and in proximity. // Start with the device connected and in proximity.
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
EXPECT_TRUE(monitor_.IsUnlockAllowed()); EXPECT_TRUE(monitor_.IsUnlockAllowed());
EXPECT_TRUE(monitor_.IsInRssiRange());
// Simulate it disconnecting. // Simulate it disconnecting.
ON_CALL(remote_bluetooth_device_, IsConnected()).WillByDefault(Return(false)); ON_CALL(remote_bluetooth_device_, IsConnected()).WillByDefault(Return(false));
...@@ -487,19 +292,14 @@ TEST_F(ProximityAuthProximityMonitorImplTest, ...@@ -487,19 +292,14 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
RunPendingTasks(); RunPendingTasks();
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
ProximityState_ConnectionInfoReceivedAfterStopping) { IsUnlockAllowed_ConnectionInfoReceivedAfterStopping) {
monitor_.SetStrategy(ProximityMonitor::Strategy::CHECK_RSSI);
monitor_.Start(); monitor_.Start();
monitor_.Stop(); monitor_.Stop();
ProvideConnectionInfo({0, 4, 4}); ProvideConnectionInfo({0, 4, 4});
EXPECT_FALSE(monitor_.IsUnlockAllowed()); EXPECT_FALSE(monitor_.IsUnlockAllowed());
EXPECT_FALSE(monitor_.IsInRssiRange());
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
...@@ -515,10 +315,6 @@ TEST_F(ProximityAuthProximityMonitorImplTest, ...@@ -515,10 +315,6 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor_.RecordProximityMetricsOnAuthSuccess(); monitor_.RecordProximityMetricsOnAuthSuccess();
histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
-6, 1); -6, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.TransmitPowerDelta", -1, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.TimeSinceLastZeroRssi", 304, 1);
histogram_tester.ExpectUniqueSample( histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.RemoteDeviceModelHash", "EasyUnlock.AuthProximity.RemoteDeviceModelHash",
1881443083 /* hash of "LGE Nexus 5" */, 1); 1881443083 /* hash of "LGE Nexus 5" */, 1);
...@@ -533,20 +329,18 @@ TEST_F(ProximityAuthProximityMonitorImplTest, ...@@ -533,20 +329,18 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor_.RecordProximityMetricsOnAuthSuccess(); monitor_.RecordProximityMetricsOnAuthSuccess();
histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
-100, 1); -100, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.TransmitPowerDelta", 50, 1);
} }
TEST_F(ProximityAuthProximityMonitorImplTest, TEST_F(ProximityAuthProximityMonitorImplTest,
RecordProximityMetricsOnAuthSuccess_UnknownValues) { RecordProximityMetricsOnAuthSuccess_UnknownValues) {
// Note: A device without a recorded name will have its Bluetooth address as // Note: A device without a recorded name will have "Unknown" as its name.
// its name.
cryptauth::RemoteDevice unnamed_remote_device( cryptauth::RemoteDevice unnamed_remote_device(
kRemoteDeviceUserId, kBluetoothAddress, kRemoteDevicePublicKey, kRemoteDeviceUserId, "" /* name */, kRemoteDevicePublicKey,
kBluetoothAddress, kPersistentSymmetricKey, std::string()); kBluetoothAddress, kPersistentSymmetricKey, std::string());
cryptauth::FakeConnection connection(unnamed_remote_device);
std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock()); std::unique_ptr<base::TickClock> clock(new base::SimpleTestTickClock());
ProximityMonitorImpl monitor(unnamed_remote_device, std::move(clock)); ProximityMonitorImpl monitor(&connection, std::move(clock));
monitor.AddObserver(&observer_); monitor.AddObserver(&observer_);
monitor.Start(); monitor.Start();
ProvideConnectionInfo({127, 127, 127}); ProvideConnectionInfo({127, 127, 127});
...@@ -555,11 +349,6 @@ TEST_F(ProximityAuthProximityMonitorImplTest, ...@@ -555,11 +349,6 @@ TEST_F(ProximityAuthProximityMonitorImplTest,
monitor.RecordProximityMetricsOnAuthSuccess(); monitor.RecordProximityMetricsOnAuthSuccess();
histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi", histogram_tester.ExpectUniqueSample("EasyUnlock.AuthProximity.RollingRssi",
127, 1); 127, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.TransmitPowerDelta", 127, 1);
histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.TimeSinceLastZeroRssi",
base::TimeDelta::FromSeconds(10).InMilliseconds(), 1);
histogram_tester.ExpectUniqueSample( histogram_tester.ExpectUniqueSample(
"EasyUnlock.AuthProximity.RemoteDeviceModelHash", "EasyUnlock.AuthProximity.RemoteDeviceModelHash",
-1808066424 /* hash of "Unknown" */, 1); -1808066424 /* hash of "Unknown" */, 1);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_H #define COMPONENTS_PROXIMITY_AUTH_REMOTE_DEVICE_LIFE_CYCLE_H
#include "base/macros.h" #include "base/macros.h"
#include "components/cryptauth/connection.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
namespace proximity_auth { namespace proximity_auth {
...@@ -57,6 +58,9 @@ class RemoteDeviceLifeCycle { ...@@ -57,6 +58,9 @@ class RemoteDeviceLifeCycle {
// Returns the RemoteDevice instance that this life cycle manages. // Returns the RemoteDevice instance that this life cycle manages.
virtual cryptauth::RemoteDevice GetRemoteDevice() const = 0; virtual cryptauth::RemoteDevice GetRemoteDevice() const = 0;
// Returns the current Connection, or null if the device is not yet connected.
virtual cryptauth::Connection* GetConnection() const = 0;
// Returns the current state of in the life cycle. // Returns the current state of in the life cycle.
virtual State GetState() const = 0; virtual State GetState() const = 0;
......
...@@ -62,6 +62,14 @@ cryptauth::RemoteDevice RemoteDeviceLifeCycleImpl::GetRemoteDevice() const { ...@@ -62,6 +62,14 @@ cryptauth::RemoteDevice RemoteDeviceLifeCycleImpl::GetRemoteDevice() const {
return remote_device_; return remote_device_;
} }
cryptauth::Connection* RemoteDeviceLifeCycleImpl::GetConnection() const {
if (connection_)
return connection_.get();
if (messenger_)
return messenger_->GetConnection();
return nullptr;
}
RemoteDeviceLifeCycle::State RemoteDeviceLifeCycleImpl::GetState() const { RemoteDeviceLifeCycle::State RemoteDeviceLifeCycleImpl::GetState() const {
return state_; return state_;
} }
......
...@@ -41,6 +41,7 @@ class RemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycle, ...@@ -41,6 +41,7 @@ class RemoteDeviceLifeCycleImpl : public RemoteDeviceLifeCycle,
// RemoteDeviceLifeCycle: // RemoteDeviceLifeCycle:
void Start() override; void Start() override;
cryptauth::RemoteDevice GetRemoteDevice() const override; cryptauth::RemoteDevice GetRemoteDevice() const override;
cryptauth::Connection* GetConnection() const override;
RemoteDeviceLifeCycle::State GetState() const override; RemoteDeviceLifeCycle::State GetState() const override;
Messenger* GetMessenger() override; Messenger* GetMessenger() override;
void AddObserver(Observer* observer) override; void AddObserver(Observer* observer) override;
......
...@@ -141,7 +141,6 @@ void UnlockManagerImpl::SetRemoteDeviceLifeCycle( ...@@ -141,7 +141,6 @@ void UnlockManagerImpl::SetRemoteDeviceLifeCycle(
life_cycle_ = life_cycle; life_cycle_ = life_cycle;
if (life_cycle_) { if (life_cycle_) {
proximity_monitor_ = CreateProximityMonitor(life_cycle->GetRemoteDevice());
SetWakingUpState(true); SetWakingUpState(true);
} else { } else {
proximity_monitor_.reset(); proximity_monitor_.reset();
...@@ -156,8 +155,12 @@ void UnlockManagerImpl::OnLifeCycleStateChanged() { ...@@ -156,8 +155,12 @@ void UnlockManagerImpl::OnLifeCycleStateChanged() {
<< static_cast<int>(state); << static_cast<int>(state);
remote_screenlock_state_.reset(); remote_screenlock_state_.reset();
if (state == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED) if (state == RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED) {
DCHECK(life_cycle_->GetConnection());
DCHECK(GetMessenger());
proximity_monitor_ = CreateProximityMonitor(life_cycle_->GetConnection());
GetMessenger()->AddObserver(this); GetMessenger()->AddObserver(this);
}
if (state == RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED) if (state == RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED)
SetWakingUpState(false); SetWakingUpState(false);
...@@ -320,9 +323,9 @@ void UnlockManagerImpl::OnAuthAttempted( ...@@ -320,9 +323,9 @@ void UnlockManagerImpl::OnAuthAttempted(
} }
std::unique_ptr<ProximityMonitor> UnlockManagerImpl::CreateProximityMonitor( std::unique_ptr<ProximityMonitor> UnlockManagerImpl::CreateProximityMonitor(
const cryptauth::RemoteDevice& remote_device) { cryptauth::Connection* connection) {
return base::MakeUnique<ProximityMonitorImpl>( return base::MakeUnique<ProximityMonitorImpl>(
remote_device, base::WrapUnique(new base::DefaultTickClock())); connection, base::WrapUnique(new base::DefaultTickClock()));
} }
void UnlockManagerImpl::SendSignInChallenge() { void UnlockManagerImpl::SendSignInChallenge() {
...@@ -356,7 +359,10 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() { ...@@ -356,7 +359,10 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() {
RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED) RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED)
return ScreenlockState::PHONE_NOT_AUTHENTICATED; return ScreenlockState::PHONE_NOT_AUTHENTICATED;
if (is_waking_up_) if (is_waking_up_ ||
life_cycle_->GetState() == RemoteDeviceLifeCycle::State::AUTHENTICATING ||
life_cycle_->GetState() ==
RemoteDeviceLifeCycle::State::FINDING_CONNECTION)
return ScreenlockState::BLUETOOTH_CONNECTING; return ScreenlockState::BLUETOOTH_CONNECTING;
if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPowered()) if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPowered())
...@@ -370,8 +376,7 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() { ...@@ -370,8 +376,7 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() {
// If the RSSI is too low, then the remote device is nowhere near the local // If the RSSI is too low, then the remote device is nowhere near the local
// device. This message should take priority over messages about screen lock // device. This message should take priority over messages about screen lock
// states. // states.
if (!proximity_monitor_->IsUnlockAllowed() && if (!proximity_monitor_->IsUnlockAllowed())
!proximity_monitor_->IsInRssiRange())
return ScreenlockState::RSSI_TOO_LOW; return ScreenlockState::RSSI_TOO_LOW;
if (remote_screenlock_state_) { if (remote_screenlock_state_) {
...@@ -380,11 +385,6 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() { ...@@ -380,11 +385,6 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() {
return ScreenlockState::PHONE_NOT_LOCKABLE; return ScreenlockState::PHONE_NOT_LOCKABLE;
case RemoteScreenlockState::LOCKED: case RemoteScreenlockState::LOCKED:
if (proximity_monitor_->GetStrategy() ==
ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER &&
!proximity_monitor_->IsUnlockAllowed()) {
return ScreenlockState::PHONE_LOCKED_AND_TX_POWER_TOO_HIGH;
}
return ScreenlockState::PHONE_LOCKED; return ScreenlockState::PHONE_LOCKED;
case RemoteScreenlockState::UNKNOWN: case RemoteScreenlockState::UNKNOWN:
...@@ -396,18 +396,6 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() { ...@@ -396,18 +396,6 @@ ScreenlockState UnlockManagerImpl::GetScreenlockState() {
} }
} }
if (!proximity_monitor_->IsUnlockAllowed()) {
ProximityMonitor::Strategy strategy = proximity_monitor_->GetStrategy();
if (strategy != ProximityMonitor::Strategy::CHECK_TRANSMIT_POWER) {
// CHECK_RSSI should have been handled above, and no other states should
// prevent unlocking.
PA_LOG(ERROR) << "[Unlock] Invalid ProximityMonitor strategy: "
<< static_cast<int>(strategy);
return ScreenlockState::NO_PHONE;
}
return ScreenlockState::TX_POWER_TOO_HIGH;
}
return ScreenlockState::NO_PHONE; return ScreenlockState::NO_PHONE;
} }
......
...@@ -52,10 +52,10 @@ class UnlockManagerImpl : public UnlockManager, ...@@ -52,10 +52,10 @@ class UnlockManagerImpl : public UnlockManager,
ScreenlockBridge::LockHandler::AuthType auth_type) override; ScreenlockBridge::LockHandler::AuthType auth_type) override;
protected: protected:
// Creates a ProximityMonitor instance for the given |remote_device|. // Creates a ProximityMonitor instance for the given |connection|.
// Exposed for testing. // Exposed for testing.
virtual std::unique_ptr<ProximityMonitor> CreateProximityMonitor( virtual std::unique_ptr<ProximityMonitor> CreateProximityMonitor(
const cryptauth::RemoteDevice& remote_device); cryptauth::Connection* connection);
private: private:
// The possible lock screen states for the remote device. // The possible lock screen states for the remote device.
......
...@@ -13,9 +13,11 @@ ...@@ -13,9 +13,11 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/cryptauth/cryptauth_test_util.h" #include "components/cryptauth/cryptauth_test_util.h"
#include "components/cryptauth/fake_connection.h"
#include "components/cryptauth/fake_secure_context.h" #include "components/cryptauth/fake_secure_context.h"
#include "components/cryptauth/secure_context.h" #include "components/cryptauth/secure_context.h"
#include "components/proximity_auth/fake_lock_handler.h" #include "components/proximity_auth/fake_lock_handler.h"
#include "components/proximity_auth/fake_remote_device_life_cycle.h"
#include "components/proximity_auth/logging/logging.h" #include "components/proximity_auth/logging/logging.h"
#include "components/proximity_auth/messenger.h" #include "components/proximity_auth/messenger.h"
#include "components/proximity_auth/mock_proximity_auth_client.h" #include "components/proximity_auth/mock_proximity_auth_client.h"
...@@ -66,6 +68,7 @@ class MockRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle { ...@@ -66,6 +68,7 @@ class MockRemoteDeviceLifeCycle : public RemoteDeviceLifeCycle {
MOCK_CONST_METHOD0(GetRemoteDevice, cryptauth::RemoteDevice()); MOCK_CONST_METHOD0(GetRemoteDevice, cryptauth::RemoteDevice());
MOCK_CONST_METHOD0(GetState, State()); MOCK_CONST_METHOD0(GetState, State());
MOCK_METHOD0(GetMessenger, Messenger*()); MOCK_METHOD0(GetMessenger, Messenger*());
MOCK_CONST_METHOD0(GetConnection, cryptauth::Connection*());
MOCK_METHOD1(AddObserver, void(Observer*)); MOCK_METHOD1(AddObserver, void(Observer*));
MOCK_METHOD1(RemoveObserver, void(Observer*)); MOCK_METHOD1(RemoveObserver, void(Observer*));
}; };
...@@ -82,6 +85,7 @@ class MockMessenger : public Messenger { ...@@ -82,6 +85,7 @@ class MockMessenger : public Messenger {
MOCK_METHOD1(RequestDecryption, void(const std::string& challenge)); MOCK_METHOD1(RequestDecryption, void(const std::string& challenge));
MOCK_METHOD0(RequestUnlock, void()); MOCK_METHOD0(RequestUnlock, void());
MOCK_CONST_METHOD0(GetSecureContext, cryptauth::SecureContext*()); MOCK_CONST_METHOD0(GetSecureContext, cryptauth::SecureContext*());
MOCK_CONST_METHOD0(GetConnection, cryptauth::Connection*());
private: private:
DISALLOW_COPY_AND_ASSIGN(MockMessenger); DISALLOW_COPY_AND_ASSIGN(MockMessenger);
...@@ -89,24 +93,25 @@ class MockMessenger : public Messenger { ...@@ -89,24 +93,25 @@ class MockMessenger : public Messenger {
class MockProximityMonitor : public ProximityMonitor { class MockProximityMonitor : public ProximityMonitor {
public: public:
MockProximityMonitor() { MockProximityMonitor() : started_(false), stopped_(false) {
ON_CALL(*this, GetStrategy())
.WillByDefault(Return(ProximityMonitor::Strategy::NONE));
ON_CALL(*this, IsUnlockAllowed()).WillByDefault(Return(true)); ON_CALL(*this, IsUnlockAllowed()).WillByDefault(Return(true));
ON_CALL(*this, IsInRssiRange()).WillByDefault(Return(false));
} }
~MockProximityMonitor() override {} ~MockProximityMonitor() override {}
MOCK_METHOD0(Start, void()); void Start() override { started_ = true; }
MOCK_METHOD0(Stop, void()); void Stop() override { stopped_ = true; }
MOCK_CONST_METHOD0(GetStrategy, Strategy());
MOCK_CONST_METHOD0(IsUnlockAllowed, bool()); MOCK_CONST_METHOD0(IsUnlockAllowed, bool());
MOCK_CONST_METHOD0(IsInRssiRange, bool());
MOCK_METHOD0(RecordProximityMetricsOnAuthSuccess, void()); MOCK_METHOD0(RecordProximityMetricsOnAuthSuccess, void());
MOCK_METHOD1(AddObserver, void(ProximityMonitorObserver*)); MOCK_METHOD1(AddObserver, void(ProximityMonitorObserver*));
MOCK_METHOD1(RemoveObserver, void(ProximityMonitorObserver*)); MOCK_METHOD1(RemoveObserver, void(ProximityMonitorObserver*));
bool started() { return started_; }
bool stopped() { return stopped_; }
private: private:
bool started_;
bool stopped_;
DISALLOW_COPY_AND_ASSIGN(MockProximityMonitor); DISALLOW_COPY_AND_ASSIGN(MockProximityMonitor);
}; };
...@@ -132,8 +137,9 @@ class TestUnlockManager : public UnlockManagerImpl { ...@@ -132,8 +137,9 @@ class TestUnlockManager : public UnlockManagerImpl {
private: private:
std::unique_ptr<ProximityMonitor> CreateProximityMonitor( std::unique_ptr<ProximityMonitor> CreateProximityMonitor(
const cryptauth::RemoteDevice& remote_device) override { cryptauth::Connection* connection) override {
EXPECT_EQ(cryptauth::kTestRemoteDevicePublicKey, remote_device.public_key); EXPECT_EQ(cryptauth::kTestRemoteDevicePublicKey,
connection->remote_device().public_key);
std::unique_ptr<MockProximityMonitor> proximity_monitor( std::unique_ptr<MockProximityMonitor> proximity_monitor(
new NiceMock<MockProximityMonitor>()); new NiceMock<MockProximityMonitor>());
proximity_monitor_ = proximity_monitor.get(); proximity_monitor_ = proximity_monitor.get();
...@@ -162,17 +168,18 @@ class ProximityAuthUnlockManagerImplTest : public testing::Test { ...@@ -162,17 +168,18 @@ class ProximityAuthUnlockManagerImplTest : public testing::Test {
public: public:
ProximityAuthUnlockManagerImplTest() ProximityAuthUnlockManagerImplTest()
: remote_device_(cryptauth::CreateClassicRemoteDeviceForTest()), : remote_device_(cryptauth::CreateClassicRemoteDeviceForTest()),
life_cycle_(remote_device_),
connection_(remote_device_),
bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()), bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()),
task_runner_(new base::TestSimpleTaskRunner()), task_runner_(new base::TestSimpleTaskRunner()),
thread_task_runner_handle_(task_runner_) { thread_task_runner_handle_(task_runner_) {
ON_CALL(*bluetooth_adapter_, IsPowered()).WillByDefault(Return(true)); ON_CALL(*bluetooth_adapter_, IsPowered()).WillByDefault(Return(true));
ON_CALL(life_cycle_, GetMessenger()).WillByDefault(Return(&messenger_));
ON_CALL(life_cycle_, GetRemoteDevice())
.WillByDefault(Return(remote_device_));
ON_CALL(messenger_, SupportsSignIn()).WillByDefault(Return(true)); ON_CALL(messenger_, SupportsSignIn()).WillByDefault(Return(true));
ON_CALL(messenger_, GetSecureContext()) ON_CALL(messenger_, GetSecureContext())
.WillByDefault(Return(&secure_context_)); .WillByDefault(Return(&secure_context_));
life_cycle_.set_connection(&connection_);
life_cycle_.set_messenger(&messenger_);
ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); ScreenlockBridge::Get()->SetLockHandler(&lock_handler_);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -207,15 +214,10 @@ class ProximityAuthUnlockManagerImplTest : public testing::Test { ...@@ -207,15 +214,10 @@ class ProximityAuthUnlockManagerImplTest : public testing::Test {
} }
void SimulateUserPresentState() { void SimulateUserPresentState() {
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(
ON_CALL(life_cycle_, GetState()) RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
} }
...@@ -227,12 +229,13 @@ class ProximityAuthUnlockManagerImplTest : public testing::Test { ...@@ -227,12 +229,13 @@ class ProximityAuthUnlockManagerImplTest : public testing::Test {
protected: protected:
cryptauth::RemoteDevice remote_device_; cryptauth::RemoteDevice remote_device_;
FakeRemoteDeviceLifeCycle life_cycle_;
cryptauth::FakeConnection connection_;
// Mock used for verifying interactions with the Bluetooth subsystem. // Mock used for verifying interactions with the Bluetooth subsystem.
scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_;
NiceMock<MockProximityAuthClient> proximity_auth_client_; NiceMock<MockProximityAuthClient> proximity_auth_client_;
NiceMock<MockRemoteDeviceLifeCycle> life_cycle_;
NiceMock<MockMessenger> messenger_; NiceMock<MockMessenger> messenger_;
std::unique_ptr<TestUnlockManager> unlock_manager_; std::unique_ptr<TestUnlockManager> unlock_manager_;
cryptauth::FakeSecureContext secure_context_; cryptauth::FakeSecureContext secure_context_;
...@@ -253,10 +256,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -253,10 +256,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_SessionLock_AllGood) { IsUnlockAllowed_SessionLock_AllGood) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
EXPECT_TRUE(unlock_manager_->IsUnlockAllowed()); EXPECT_TRUE(unlock_manager_->IsUnlockAllowed());
...@@ -264,14 +267,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -264,14 +267,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
TEST_F(ProximityAuthUnlockManagerImplTest, IsUnlockAllowed_SignIn_AllGood) { TEST_F(ProximityAuthUnlockManagerImplTest, IsUnlockAllowed_SignIn_AllGood) {
CreateUnlockManager(ProximityAuthSystem::SIGN_IN); CreateUnlockManager(ProximityAuthSystem::SIGN_IN);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState()) life_cycle_.ChangeState(
.WillByDefault( RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
ON_CALL(messenger_, SupportsSignIn()).WillByDefault(Return(true)); ON_CALL(messenger_, SupportsSignIn()).WillByDefault(Return(true));
...@@ -283,14 +282,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, IsUnlockAllowed_SignIn_AllGood) { ...@@ -283,14 +282,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, IsUnlockAllowed_SignIn_AllGood) {
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_SignIn_MessengerDoesNotSupportSignIn) { IsUnlockAllowed_SignIn_MessengerDoesNotSupportSignIn) {
CreateUnlockManager(ProximityAuthSystem::SIGN_IN); CreateUnlockManager(ProximityAuthSystem::SIGN_IN);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState()) life_cycle_.ChangeState(
.WillByDefault( RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
ON_CALL(messenger_, SupportsSignIn()).WillByDefault(Return(false)); ON_CALL(messenger_, SupportsSignIn()).WillByDefault(Return(false));
...@@ -299,31 +294,17 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -299,31 +294,17 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
} }
TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_SignIn_MessengerIsNull) {
CreateUnlockManager(ProximityAuthSystem::SIGN_IN);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
ON_CALL(life_cycle_, GetMessenger()).WillByDefault(Return(nullptr));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
}
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_DisallowedByProximityMonitor) { IsUnlockAllowed_DisallowedByProximityMonitor) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
ON_CALL(*proximity_monitor(), IsUnlockAllowed()).WillByDefault(Return(false)); ON_CALL(*proximity_monitor(), IsUnlockAllowed()).WillByDefault(Return(false));
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
} }
...@@ -331,9 +312,9 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -331,9 +312,9 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_SecureChannelNotEstablished) { IsUnlockAllowed_SecureChannelNotEstablished) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::AUTHENTICATING));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::AUTHENTICATING);
unlock_manager_->OnLifeCycleStateChanged();
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked);
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
...@@ -353,10 +334,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -353,10 +334,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_RemoteScreenlockStateLocked) { IsUnlockAllowed_RemoteScreenlockStateLocked) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenLocked); unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenLocked);
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
...@@ -366,10 +347,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -366,10 +347,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_RemoteScreenlockStateUnknown) { IsUnlockAllowed_RemoteScreenlockStateUnknown) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockStateUnknown); unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockStateUnknown);
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
...@@ -379,10 +360,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -379,10 +360,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_RemoteScreenlockStateDisabled) { IsUnlockAllowed_RemoteScreenlockStateDisabled) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockDisabled); unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockDisabled);
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
...@@ -392,10 +373,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -392,10 +373,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
IsUnlockAllowed_RemoteScreenlockStateNotYetReceived) { IsUnlockAllowed_RemoteScreenlockStateNotYetReceived) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); EXPECT_FALSE(unlock_manager_->IsUnlockAllowed());
} }
...@@ -418,20 +399,6 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -418,20 +399,6 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
} }
TEST_F(ProximityAuthUnlockManagerImplTest,
SetRemoteDeviceLifeCycle_NullThenExistingRemoteDeviceLifeCycle) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState();
EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::INACTIVE));
unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::AUTHENTICATED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
}
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
SetRemoteDeviceLifeCycle_AuthenticationFailed) { SetRemoteDeviceLifeCycle_AuthenticationFailed) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
...@@ -439,9 +406,8 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -439,9 +406,8 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
unlock_manager_->SetRemoteDeviceLifeCycle(nullptr); unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
ON_CALL(life_cycle_, GetState()) life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED);
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED)); UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
...@@ -453,8 +419,8 @@ TEST_F(ProximityAuthUnlockManagerImplTest, SetRemoteDeviceLifeCycle_WakingUp) { ...@@ -453,8 +419,8 @@ TEST_F(ProximityAuthUnlockManagerImplTest, SetRemoteDeviceLifeCycle_WakingUp) {
unlock_manager_->SetRemoteDeviceLifeCycle(nullptr); unlock_manager_->SetRemoteDeviceLifeCycle(nullptr);
ON_CALL(life_cycle_, GetState()) life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION);
.WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
...@@ -473,27 +439,23 @@ TEST_F( ...@@ -473,27 +439,23 @@ TEST_F(
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
EXPECT_CALL(*proximity_monitor(), Stop()).Times(AtLeast(1));
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION);
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_TRUE(proximity_monitor()->stopped());
} }
TEST_F( TEST_F(
ProximityAuthUnlockManagerImplTest, ProximityAuthUnlockManagerImplTest,
SetRemoteDeviceLifeCycle_ConnectedRemoteDeviceLifeCycle_StartsProximityMonitor) { SetRemoteDeviceLifeCycle_ConnectedRemoteDeviceLifeCycle_StartsProximityMonitor) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
EXPECT_CALL(*proximity_monitor(), Start()).Times(AtLeast(1));
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_TRUE(proximity_monitor()->started());
} }
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
...@@ -508,7 +470,7 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -508,7 +470,7 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
OnLifeCycleStateChanged_StartsProximityMonitor) { OnLifeCycleStateChanged_StartsProximityMonitor) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
EXPECT_CALL(*proximity_monitor(), Start()).Times(AtLeast(1)); EXPECT_TRUE(proximity_monitor()->started());
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
} }
...@@ -517,12 +479,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -517,12 +479,10 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
EXPECT_CALL(*proximity_monitor(), Stop()).Times(AtLeast(1));
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED);
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_TRUE(proximity_monitor()->stopped());
} }
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
...@@ -530,11 +490,9 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -530,11 +490,9 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::INACTIVE)); UpdateScreenlockState(ScreenlockState::INACTIVE));
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::STOPPED);
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
} }
...@@ -543,44 +501,31 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -543,44 +501,31 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED)); UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED));
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED);
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
} }
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
OnLifeCycleStateChanged_FindingConnection_UpdatesScreenlockState) { OnLifeCycleStateChanged_FindingConnection_UpdatesScreenlockState) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION);
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
} }
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
OnLifeCycleStateChanged_Authenticating_UpdatesScreenlockState) { OnLifeCycleStateChanged_Authenticating_UpdatesScreenlockState) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::AUTHENTICATING));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::AUTHENTICATING);
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
} }
...@@ -588,17 +533,12 @@ TEST_F( ...@@ -588,17 +533,12 @@ TEST_F(
ProximityAuthUnlockManagerImplTest, ProximityAuthUnlockManagerImplTest,
OnLifeCycleStateChanged_SecureChannelEstablished_UpdatesScreenlockState) { OnLifeCycleStateChanged_SecureChannelEstablished_UpdatesScreenlockState) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged(); unlock_manager_->OnLifeCycleStateChanged();
} }
...@@ -606,10 +546,8 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -606,10 +546,8 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
OnDisconnected_UnregistersAsObserver) { OnDisconnected_UnregistersAsObserver) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED);
ON_CALL(life_cycle_, GetState()) unlock_manager_->OnLifeCycleStateChanged();
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::AUTHENTICATION_FAILED));
EXPECT_CALL(messenger_, RemoveObserver(unlock_manager_.get())) EXPECT_CALL(messenger_, RemoveObserver(unlock_manager_.get()))
.Times(testing::AtLeast(1)); .Times(testing::AtLeast(1));
...@@ -622,27 +560,23 @@ TEST_F(ProximityAuthUnlockManagerImplTest, ...@@ -622,27 +560,23 @@ TEST_F(ProximityAuthUnlockManagerImplTest,
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
SimulateUserPresentState(); SimulateUserPresentState();
EXPECT_CALL(*proximity_monitor(), Stop());
unlock_manager_.get()->OnScreenDidUnlock( unlock_manager_.get()->OnScreenDidUnlock(
ScreenlockBridge::LockHandler::LOCK_SCREEN); ScreenlockBridge::LockHandler::LOCK_SCREEN);
EXPECT_TRUE(proximity_monitor()->stopped());
} }
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
OnScreenDidLock_StartsProximityMonitor) { OnScreenDidLock_StartsProximityMonitor) {
CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK); CreateUnlockManager(ProximityAuthSystem::SESSION_LOCK);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::STOPPED));
unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_); unlock_manager_->SetRemoteDeviceLifeCycle(&life_cycle_);
ON_CALL(life_cycle_, GetState())
.WillByDefault(
Return(RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED));
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_CALL(*proximity_monitor(), Start());
unlock_manager_.get()->OnScreenDidLock( unlock_manager_.get()->OnScreenDidLock(
ScreenlockBridge::LockHandler::LOCK_SCREEN); ScreenlockBridge::LockHandler::LOCK_SCREEN);
life_cycle_.ChangeState(
RemoteDeviceLifeCycle::State::SECURE_CHANNEL_ESTABLISHED);
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_TRUE(proximity_monitor()->started());
} }
TEST_F(ProximityAuthUnlockManagerImplTest, OnScreenDidLock_SetsWakingUpState) { TEST_F(ProximityAuthUnlockManagerImplTest, OnScreenDidLock_SetsWakingUpState) {
...@@ -652,14 +586,11 @@ TEST_F(ProximityAuthUnlockManagerImplTest, OnScreenDidLock_SetsWakingUpState) { ...@@ -652,14 +586,11 @@ TEST_F(ProximityAuthUnlockManagerImplTest, OnScreenDidLock_SetsWakingUpState) {
unlock_manager_.get()->OnScreenDidUnlock( unlock_manager_.get()->OnScreenDidUnlock(
ScreenlockBridge::LockHandler::LOCK_SCREEN); ScreenlockBridge::LockHandler::LOCK_SCREEN);
ON_CALL(life_cycle_, GetState())
.WillByDefault(Return(RemoteDeviceLifeCycle::State::FINDING_CONNECTION));
unlock_manager_->OnLifeCycleStateChanged();
EXPECT_CALL(proximity_auth_client_, EXPECT_CALL(proximity_auth_client_,
UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING));
unlock_manager_.get()->OnScreenDidLock(
ScreenlockBridge::LockHandler::LOCK_SCREEN); life_cycle_.ChangeState(RemoteDeviceLifeCycle::State::FINDING_CONNECTION);
unlock_manager_->OnLifeCycleStateChanged();
} }
TEST_F(ProximityAuthUnlockManagerImplTest, TEST_F(ProximityAuthUnlockManagerImplTest,
......
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