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";
api::easy_unlock_private::ConnectionStatus ToApiConnectionStatus(
Connection::Status status) {
switch (status) {
case Connection::DISCONNECTED:
case 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;
case Connection::CONNECTED:
case Connection::Status::CONNECTED:
return api::easy_unlock_private::CONNECTION_STATUS_CONNECTED;
}
return api::easy_unlock_private::CONNECTION_STATUS_NONE;
......
......@@ -251,7 +251,7 @@ void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged(
FROM_HERE,
base::BindOnce(&BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync,
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.";
connection_->RemoveObserver(this);
connection_.reset();
......
......@@ -322,8 +322,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Creating a connection.
base::RunLoop run_loop;
EXPECT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::CONNECTED);
connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::Status::CONNECTED);
run_loop.RunUntilIdle();
EXPECT_TRUE(last_found_connection_);
}
......@@ -341,7 +341,7 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Trying to create a connection.
connection_finder_.DeviceAdded(adapter_.get(), device_.get());
ASSERT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
// Preparing to restart the discovery session.
device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
......@@ -353,7 +353,7 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Connection fails.
{
base::RunLoop run_loop;
connection->SetStatus(cryptauth::Connection::DISCONNECTED);
connection->SetStatus(cryptauth::Connection::Status::DISCONNECTED);
run_loop.RunUntilIdle();
}
......@@ -375,8 +375,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
{
base::RunLoop run_loop;
EXPECT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::CONNECTED);
connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::Status::CONNECTED);
run_loop.RunUntilIdle();
}
EXPECT_TRUE(last_found_connection_);
......@@ -426,8 +426,8 @@ TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
// Completing the connection.
base::RunLoop run_loop;
ASSERT_FALSE(last_found_connection_);
connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::CONNECTED);
connection->SetStatus(cryptauth::Connection::Status::IN_PROGRESS);
connection->SetStatus(cryptauth::Connection::Status::CONNECTED);
run_loop.RunUntilIdle();
EXPECT_TRUE(last_found_connection_);
}
......
......@@ -271,7 +271,7 @@ void MessengerImpl::OnConnectionStatusChanged(
cryptauth::Connection::Status old_status,
cryptauth::Connection::Status new_status) {
DCHECK_EQ(connection, connection_.get());
if (new_status == cryptauth::Connection::DISCONNECTED) {
if (new_status == cryptauth::Connection::Status::DISCONNECTED) {
PA_LOG(INFO) << "Secure channel disconnected...";
connection_->RemoveObserver(this);
for (auto& observer : observers_)
......
......@@ -16,13 +16,13 @@ namespace cryptauth {
Connection::Connection(const RemoteDevice& remote_device)
: remote_device_(remote_device),
status_(DISCONNECTED),
status_(Status::DISCONNECTED),
is_sending_message_(false) {}
Connection::~Connection() {}
bool Connection::IsConnected() const {
return status_ == CONNECTED;
return status_ == Status::CONNECTED;
}
void Connection::SendMessage(std::unique_ptr<WireMessage> message) {
......
......@@ -22,7 +22,7 @@ class WireMessage;
// persistent bidirectional channel for sending and receiving wire messages.
class Connection {
public:
enum Status {
enum class Status {
DISCONNECTED,
IN_PROGRESS,
CONNECTED,
......
......@@ -101,19 +101,19 @@ TEST(CryptAuthConnectionTest, IsConnected) {
StrictMock<MockConnection> connection;
EXPECT_FALSE(connection.IsConnected());
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
EXPECT_TRUE(connection.IsConnected());
connection.SetStatus(Connection::DISCONNECTED);
connection.SetStatus(Connection::Status::DISCONNECTED);
EXPECT_FALSE(connection.IsConnected());
connection.SetStatus(Connection::IN_PROGRESS);
connection.SetStatus(Connection::Status::IN_PROGRESS);
EXPECT_FALSE(connection.IsConnected());
}
TEST(CryptAuthConnectionTest, SendMessage_FailsWhenNotConnected) {
StrictMock<MockConnection> connection;
connection.SetStatus(Connection::IN_PROGRESS);
connection.SetStatus(Connection::Status::IN_PROGRESS);
EXPECT_CALL(connection, GetDeviceAddress()).Times(1);
EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
......@@ -123,7 +123,7 @@ TEST(CryptAuthConnectionTest, SendMessage_FailsWhenNotConnected) {
TEST(CryptAuthConnectionTest,
SendMessage_FailsWhenAnotherMessageSendIsInProgress) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
connection.SendMessage(std::unique_ptr<WireMessage>());
EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
......@@ -132,7 +132,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, SendMessage_SucceedsWhenConnected) {
StrictMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
EXPECT_CALL(connection, SendMessageImplProxy(_));
connection.SendMessage(std::unique_ptr<WireMessage>());
......@@ -141,7 +141,7 @@ TEST(CryptAuthConnectionTest, SendMessage_SucceedsWhenConnected) {
TEST(CryptAuthConnectionTest,
SendMessage_SucceedsAfterPreviousMessageSendCompletes) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
connection.SendMessage(std::unique_ptr<WireMessage>());
connection.OnDidSendMessage(TestWireMessage(), true /* success */);
......@@ -151,33 +151,33 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest, SetStatus_NotifiesObserversOfStatusChange) {
StrictMock<MockConnection> connection;
EXPECT_EQ(Connection::DISCONNECTED, connection.status());
EXPECT_EQ(Connection::Status::DISCONNECTED, connection.status());
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
EXPECT_CALL(observer,
OnConnectionStatusChanged(&connection, Connection::DISCONNECTED,
Connection::CONNECTED));
connection.SetStatus(Connection::CONNECTED);
EXPECT_CALL(observer, OnConnectionStatusChanged(
&connection, Connection::Status::DISCONNECTED,
Connection::Status::CONNECTED));
connection.SetStatus(Connection::Status::CONNECTED);
}
TEST(CryptAuthConnectionTest,
SetStatus_DoesntNotifyObserversIfStatusUnchanged) {
StrictMock<MockConnection> connection;
EXPECT_EQ(Connection::DISCONNECTED, connection.status());
EXPECT_EQ(Connection::Status::DISCONNECTED, connection.status());
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
EXPECT_CALL(observer, OnConnectionStatusChanged(_, _, _)).Times(0);
connection.SetStatus(Connection::DISCONNECTED);
connection.SetStatus(Connection::Status::DISCONNECTED);
}
TEST(CryptAuthConnectionTest,
OnDidSendMessage_NotifiesObserversIfMessageSendInProgress) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
connection.SendMessage(std::unique_ptr<WireMessage>());
StrictMock<MockConnectionObserver> observer;
......@@ -190,7 +190,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest,
OnDidSendMessage_DoesntNotifyObserversIfNoMessageSendInProgress) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
......@@ -202,7 +202,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest,
OnBytesReceived_NotifiesObserversOnValidMessage) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
......@@ -217,7 +217,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest,
OnBytesReceived_DoesntNotifyObserversIfNotConnected) {
StrictMock<MockConnection> connection;
connection.SetStatus(Connection::IN_PROGRESS);
connection.SetStatus(Connection::Status::IN_PROGRESS);
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
......@@ -230,7 +230,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest,
OnBytesReceived_DoesntNotifyObserversIfMessageIsIncomplete) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
......@@ -244,7 +244,7 @@ TEST(CryptAuthConnectionTest,
TEST(CryptAuthConnectionTest,
OnBytesReceived_DoesntNotifyObserversIfMessageIsInvalid) {
NiceMock<MockConnection> connection;
connection.SetStatus(Connection::CONNECTED);
connection.SetStatus(Connection::Status::CONNECTED);
StrictMock<MockConnectionObserver> observer;
connection.AddObserver(&observer);
......
......@@ -224,7 +224,7 @@ void DeviceToDeviceAuthenticator::OnConnectionStatusChanged(
Connection::Status old_status,
Connection::Status new_status) {
// 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",
Result::DISCONNECTED);
}
......
......@@ -28,14 +28,14 @@ FakeConnection::~FakeConnection() {
void FakeConnection::Connect() {
if (should_auto_connect_) {
SetStatus(CONNECTED);
SetStatus(Status::CONNECTED);
} else {
SetStatus(IN_PROGRESS);
SetStatus(Status::IN_PROGRESS);
}
}
void FakeConnection::Disconnect() {
SetStatus(DISCONNECTED);
SetStatus(Status::DISCONNECTED);
}
std::string FakeConnection::GetDeviceAddress() {
......@@ -56,12 +56,12 @@ void FakeConnection::RemoveObserver(ConnectionObserver* observer) {
void FakeConnection::CompleteInProgressConnection(bool success) {
DCHECK(!should_auto_connect_);
DCHECK(status() == IN_PROGRESS);
DCHECK(status() == Status::IN_PROGRESS);
if (success) {
SetStatus(CONNECTED);
SetStatus(Status::CONNECTED);
} 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