Commit 274542fe authored by James Hawkins's avatar James Hawkins Committed by Commit Bot

CryptAuth: Make Connection::Status enum class.

For compiler protection in switch statements, etc.

R=khorimoto@chromium.org

Bug: none
Test: none
Change-Id: Icf86013c87d89b233d6bc27646bad8f79830af6e
Reviewed-on: https://chromium-review.googlesource.com/1043098
Commit-Queue: James Hawkins <jhawkins@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555895}
parent eba6af48
...@@ -24,11 +24,11 @@ const char kEasyUnlockFeatureName[] = "easy_unlock"; ...@@ -24,11 +24,11 @@ const char kEasyUnlockFeatureName[] = "easy_unlock";
api::easy_unlock_private::ConnectionStatus ToApiConnectionStatus( api::easy_unlock_private::ConnectionStatus ToApiConnectionStatus(
Connection::Status status) { Connection::Status status) {
switch (status) { switch (status) {
case Connection::DISCONNECTED: case Connection::Status::DISCONNECTED:
return api::easy_unlock_private::CONNECTION_STATUS_DISCONNECTED; return api::easy_unlock_private::CONNECTION_STATUS_DISCONNECTED;
case Connection::IN_PROGRESS: case Connection::Status::IN_PROGRESS:
return api::easy_unlock_private::CONNECTION_STATUS_IN_PROGRESS; return api::easy_unlock_private::CONNECTION_STATUS_IN_PROGRESS;
case Connection::CONNECTED: case Connection::Status::CONNECTED:
return api::easy_unlock_private::CONNECTION_STATUS_CONNECTED; return api::easy_unlock_private::CONNECTION_STATUS_CONNECTED;
} }
return api::easy_unlock_private::CONNECTION_STATUS_NONE; return api::easy_unlock_private::CONNECTION_STATUS_NONE;
......
...@@ -251,7 +251,7 @@ void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( ...@@ -251,7 +251,7 @@ void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged(
FROM_HERE, FROM_HERE,
base::BindOnce(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync, base::BindOnce(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} else if (old_status == cryptauth::Connection::IN_PROGRESS) { } else if (old_status == cryptauth::Connection::Status::IN_PROGRESS) {
PA_LOG(WARNING) << "Connection failed. Retrying."; PA_LOG(WARNING) << "Connection failed. Retrying.";
connection_->RemoveObserver(this); connection_->RemoveObserver(this);
connection_.reset(); connection_.reset();
......
...@@ -322,8 +322,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, ...@@ -322,8 +322,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Creating a connection. // Creating a connection.
base::RunLoop run_loop; base::RunLoop run_loop;
EXPECT_FALSE(last_found_connection_); EXPECT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS); connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::CONNECTED); connection->SetStatus(cryptauth::Connection::Status::CONNECTED);
run_loop.RunUntilIdle(); run_loop.RunUntilIdle();
EXPECT_TRUE(last_found_connection_); EXPECT_TRUE(last_found_connection_);
} }
...@@ -341,7 +341,7 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, ...@@ -341,7 +341,7 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Trying to create a connection. // Trying to create a connection.
connection_finder_.DeviceAdded(adapter_.get(), device_.get()); connection_finder_.DeviceAdded(adapter_.get(), device_.get());
ASSERT_FALSE(last_found_connection_); ASSERT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS); connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
// Preparing to restart the discovery session. // Preparing to restart the discovery session.
device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
...@@ -353,7 +353,7 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, ...@@ -353,7 +353,7 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Connection fails. // Connection fails.
{ {
base::RunLoop run_loop; base::RunLoop run_loop;
connection->SetStatus(cryptauth::Connection::DISCONNECTED); connection->SetStatus(cryptauth::Connection::Status::DISCONNECTED);
run_loop.RunUntilIdle(); run_loop.RunUntilIdle();
} }
...@@ -375,8 +375,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, ...@@ -375,8 +375,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
{ {
base::RunLoop run_loop; base::RunLoop run_loop;
EXPECT_FALSE(last_found_connection_); EXPECT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS); connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::CONNECTED); connection->SetStatus(cryptauth::Connection::Status::CONNECTED);
run_loop.RunUntilIdle(); run_loop.RunUntilIdle();
} }
EXPECT_TRUE(last_found_connection_); EXPECT_TRUE(last_found_connection_);
...@@ -426,8 +426,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, ...@@ -426,8 +426,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Completing the connection. // Completing the connection.
base::RunLoop run_loop; base::RunLoop run_loop;
ASSERT_FALSE(last_found_connection_); ASSERT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS); connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::CONNECTED); connection->SetStatus(cryptauth::Connection::Status::CONNECTED);
run_loop.RunUntilIdle(); run_loop.RunUntilIdle();
EXPECT_TRUE(last_found_connection_); EXPECT_TRUE(last_found_connection_);
} }
......
...@@ -271,7 +271,7 @@ void MessengerImpl::OnConnectionStatusChanged( ...@@ -271,7 +271,7 @@ void MessengerImpl::OnConnectionStatusChanged(
cryptauth::Connection::Status old_status, cryptauth::Connection::Status old_status,
cryptauth::Connection::Status new_status) { cryptauth::Connection::Status new_status) {
DCHECK_EQ(connection, connection_.get()); DCHECK_EQ(connection, connection_.get());
if (new_status == cryptauth::Connection::DISCONNECTED) { if (new_status == cryptauth::Connection::Status::DISCONNECTED) {
PA_LOG(INFO) << "Secure channel disconnected..."; PA_LOG(INFO) << "Secure channel disconnected...";
connection_->RemoveObserver(this); connection_->RemoveObserver(this);
for (auto& observer : observers_) for (auto& observer : observers_)
......
...@@ -16,13 +16,13 @@ namespace cryptauth { ...@@ -16,13 +16,13 @@ namespace cryptauth {
Connection::Connection(const RemoteDevice& remote_device) Connection::Connection(const RemoteDevice& remote_device)
: remote_device_(remote_device), : remote_device_(remote_device),
status_(DISCONNECTED), status_(Status::DISCONNECTED),
is_sending_message_(false) {} is_sending_message_(false) {}
Connection::~Connection() {} Connection::~Connection() {}
bool Connection::IsConnected() const { bool Connection::IsConnected() const {
return status_ == CONNECTED; return status_ == Status::CONNECTED;
} }
void Connection::SendMessage(std::unique_ptr<WireMessage> message) { void Connection::SendMessage(std::unique_ptr<WireMessage> message) {
......
...@@ -22,7 +22,7 @@ class WireMessage; ...@@ -22,7 +22,7 @@ class WireMessage;
// persistent bidirectional channel for sending and receiving wire messages. // persistent bidirectional channel for sending and receiving wire messages.
class Connection { class Connection {
public: public:
enum Status { enum class Status {
DISCONNECTED, DISCONNECTED,
IN_PROGRESS, IN_PROGRESS,
CONNECTED, CONNECTED,
......
...@@ -101,19 +101,19 @@ TEST(CryptAuthConnectionTest, IsConnected) { ...@@ -101,19 +101,19 @@ TEST(CryptAuthConnectionTest, IsConnected) {
StrictMock<MockConnection> connection; StrictMock<MockConnection> connection;
EXPECT_FALSE(connection.IsConnected()); EXPECT_FALSE(connection.IsConnected());
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
EXPECT_TRUE(connection.IsConnected()); EXPECT_TRUE(connection.IsConnected());
connection.SetStatus(Connection::DISCONNECTED); connection.SetStatus(Connection::Status::DISCONNECTED);
EXPECT_FALSE(connection.IsConnected()); EXPECT_FALSE(connection.IsConnected());
connection.SetStatus(Connection::IN_PROGRESS); connection.SetStatus(Connection::Status::IN_PROGRESS);
EXPECT_FALSE(connection.IsConnected()); EXPECT_FALSE(connection.IsConnected());
} }
TEST(CryptAuthConnectionTest, SendMessage_FailsWhenNotConnected) { TEST(CryptAuthConnectionTest, SendMessage_FailsWhenNotConnected) {
StrictMock<MockConnection> connection; StrictMock<MockConnection> connection;
connection.SetStatus(Connection::IN_PROGRESS); connection.SetStatus(Connection::Status::IN_PROGRESS);
EXPECT_CALL(connection, GetDeviceAddress()).Times(1); EXPECT_CALL(connection, GetDeviceAddress()).Times(1);
EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0); EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
...@@ -123,7 +123,7 @@ TEST(CryptAuthConnectionTest, SendMessage_FailsWhenNotConnected) { ...@@ -123,7 +123,7 @@ TEST(CryptAuthConnectionTest, SendMessage_FailsWhenNotConnected) {
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
SendMessage_FailsWhenAnotherMessageSendIsInProgress) { SendMessage_FailsWhenAnotherMessageSendIsInProgress) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
connection.SendMessage(std::unique_ptr<WireMessage>()); connection.SendMessage(std::unique_ptr<WireMessage>());
EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0); EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
...@@ -132,7 +132,7 @@ TEST(CryptAuthConnectionTest, ...@@ -132,7 +132,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, SendMessage_SucceedsWhenConnected) { TEST(CryptAuthConnectionTest, SendMessage_SucceedsWhenConnected) {
StrictMock<MockConnection> connection; StrictMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
EXPECT_CALL(connection, SendMessageImplProxy(_)); EXPECT_CALL(connection, SendMessageImplProxy(_));
connection.SendMessage(std::unique_ptr<WireMessage>()); connection.SendMessage(std::unique_ptr<WireMessage>());
...@@ -141,7 +141,7 @@ TEST(CryptAuthConnectionTest, SendMessage_SucceedsWhenConnected) { ...@@ -141,7 +141,7 @@ TEST(CryptAuthConnectionTest, SendMessage_SucceedsWhenConnected) {
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
SendMessage_SucceedsAfterPreviousMessageSendCompletes) { SendMessage_SucceedsAfterPreviousMessageSendCompletes) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
connection.SendMessage(std::unique_ptr<WireMessage>()); connection.SendMessage(std::unique_ptr<WireMessage>());
connection.OnDidSendMessage(TestWireMessage(), true /* success */); connection.OnDidSendMessage(TestWireMessage(), true /* success */);
...@@ -151,33 +151,33 @@ TEST(CryptAuthConnectionTest, ...@@ -151,33 +151,33 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, SetStatus_NotifiesObserversOfStatusChange) { TEST(CryptAuthConnectionTest, SetStatus_NotifiesObserversOfStatusChange) {
StrictMock<MockConnection> connection; StrictMock<MockConnection> connection;
EXPECT_EQ(Connection::DISCONNECTED, connection.status()); EXPECT_EQ(Connection::Status::DISCONNECTED, connection.status());
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
EXPECT_CALL(observer, EXPECT_CALL(observer, OnConnectionStatusChanged(
OnConnectionStatusChanged(&connection, Connection::DISCONNECTED, &connection, Connection::Status::DISCONNECTED,
Connection::CONNECTED)); Connection::Status::CONNECTED));
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
} }
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
SetStatus_DoesntNotifyObserversIfStatusUnchanged) { SetStatus_DoesntNotifyObserversIfStatusUnchanged) {
StrictMock<MockConnection> connection; StrictMock<MockConnection> connection;
EXPECT_EQ(Connection::DISCONNECTED, connection.status()); EXPECT_EQ(Connection::Status::DISCONNECTED, connection.status());
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
EXPECT_CALL(observer, OnConnectionStatusChanged(_, _, _)).Times(0); EXPECT_CALL(observer, OnConnectionStatusChanged(_, _, _)).Times(0);
connection.SetStatus(Connection::DISCONNECTED); connection.SetStatus(Connection::Status::DISCONNECTED);
} }
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
OnDidSendMessage_NotifiesObserversIfMessageSendInProgress) { OnDidSendMessage_NotifiesObserversIfMessageSendInProgress) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
connection.SendMessage(std::unique_ptr<WireMessage>()); connection.SendMessage(std::unique_ptr<WireMessage>());
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
...@@ -190,7 +190,7 @@ TEST(CryptAuthConnectionTest, ...@@ -190,7 +190,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
OnDidSendMessage_DoesntNotifyObserversIfNoMessageSendInProgress) { OnDidSendMessage_DoesntNotifyObserversIfNoMessageSendInProgress) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
...@@ -202,7 +202,7 @@ TEST(CryptAuthConnectionTest, ...@@ -202,7 +202,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
OnBytesReceived_NotifiesObserversOnValidMessage) { OnBytesReceived_NotifiesObserversOnValidMessage) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
...@@ -217,7 +217,7 @@ TEST(CryptAuthConnectionTest, ...@@ -217,7 +217,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
OnBytesReceived_DoesntNotifyObserversIfNotConnected) { OnBytesReceived_DoesntNotifyObserversIfNotConnected) {
StrictMock<MockConnection> connection; StrictMock<MockConnection> connection;
connection.SetStatus(Connection::IN_PROGRESS); connection.SetStatus(Connection::Status::IN_PROGRESS);
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
...@@ -230,7 +230,7 @@ TEST(CryptAuthConnectionTest, ...@@ -230,7 +230,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
OnBytesReceived_DoesntNotifyObserversIfMessageIsIncomplete) { OnBytesReceived_DoesntNotifyObserversIfMessageIsIncomplete) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
...@@ -244,7 +244,7 @@ TEST(CryptAuthConnectionTest, ...@@ -244,7 +244,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, TEST(CryptAuthConnectionTest,
OnBytesReceived_DoesntNotifyObserversIfMessageIsInvalid) { OnBytesReceived_DoesntNotifyObserversIfMessageIsInvalid) {
NiceMock<MockConnection> connection; NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED); connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer; StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer); connection.AddObserver(&observer);
......
...@@ -224,7 +224,7 @@ void DeviceToDeviceAuthenticator::OnConnectionStatusChanged( ...@@ -224,7 +224,7 @@ void DeviceToDeviceAuthenticator::OnConnectionStatusChanged(
Connection::Status old_status, Connection::Status old_status,
Connection::Status new_status) { Connection::Status new_status) {
// We do not expect the connection to drop during authentication. // We do not expect the connection to drop during authentication.
if (new_status == Connection::DISCONNECTED) { if (new_status == Connection::Status::DISCONNECTED) {
Fail("Disconnected while authentication is in progress", Fail("Disconnected while authentication is in progress",
Result::DISCONNECTED); Result::DISCONNECTED);
} }
......
...@@ -28,14 +28,14 @@ FakeConnection::~FakeConnection() { ...@@ -28,14 +28,14 @@ FakeConnection::~FakeConnection() {
void FakeConnection::Connect() { void FakeConnection::Connect() {
if (should_auto_connect_) { if (should_auto_connect_) {
SetStatus(CONNECTED); SetStatus(Status::CONNECTED);
} else { } else {
SetStatus(IN_PROGRESS); SetStatus(Status::IN_PROGRESS);
} }
} }
void FakeConnection::Disconnect() { void FakeConnection::Disconnect() {
SetStatus(DISCONNECTED); SetStatus(Status::DISCONNECTED);
} }
std::string FakeConnection::GetDeviceAddress() { std::string FakeConnection::GetDeviceAddress() {
...@@ -56,12 +56,12 @@ void FakeConnection::RemoveObserver(ConnectionObserver* observer) { ...@@ -56,12 +56,12 @@ void FakeConnection::RemoveObserver(ConnectionObserver* observer) {
void FakeConnection::CompleteInProgressConnection(bool success) { void FakeConnection::CompleteInProgressConnection(bool success) {
DCHECK(!should_auto_connect_); DCHECK(!should_auto_connect_);
DCHECK(status() == IN_PROGRESS); DCHECK(status() == Status::IN_PROGRESS);
if (success) { if (success) {
SetStatus(CONNECTED); SetStatus(Status::CONNECTED);
} else { } else {
SetStatus(DISCONNECTED); SetStatus(Status::DISCONNECTED);
} }
} }
......
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