OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/bluetooth_connection.h" | 5 #include "components/proximity_auth/bluetooth_connection.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/numerics/safe_conversions.h" | 8 #include "base/numerics/safe_conversions.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "components/proximity_auth/remote_device.h" | 10 #include "components/proximity_auth/remote_device.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize); | 50 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize); |
51 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize); | 51 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize); |
52 return buffer; | 52 return buffer; |
53 } | 53 } |
54 | 54 |
55 class MockBluetoothConnection : public BluetoothConnection { | 55 class MockBluetoothConnection : public BluetoothConnection { |
56 public: | 56 public: |
57 MockBluetoothConnection() | 57 MockBluetoothConnection() |
58 : BluetoothConnection(kRemoteDevice, device::BluetoothUUID(kUuid)) {} | 58 : BluetoothConnection(kRemoteDevice, device::BluetoothUUID(kUuid)) {} |
59 | 59 |
60 // Bluetooth dependencies. | |
61 typedef device::BluetoothDevice::ConnectToServiceCallback | |
62 ConnectToServiceCallback; | |
63 typedef device::BluetoothDevice::ConnectToServiceErrorCallback | |
64 ConnectToServiceErrorCallback; | |
65 MOCK_METHOD4(ConnectToService, | |
66 void(device::BluetoothDevice* device, | |
67 const device::BluetoothUUID& uuid, | |
68 const ConnectToServiceCallback& callback, | |
69 const ConnectToServiceErrorCallback& error_callback)); | |
70 | |
71 // Calls back into the parent Connection class. | 60 // Calls back into the parent Connection class. |
72 MOCK_METHOD1(SetStatusProxy, void(Status status)); | 61 MOCK_METHOD1(SetStatusProxy, void(Status status)); |
73 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | 62 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); |
74 MOCK_METHOD2(OnDidSendMessage, | 63 MOCK_METHOD2(OnDidSendMessage, |
75 void(const WireMessage& message, bool success)); | 64 void(const WireMessage& message, bool success)); |
76 | 65 |
77 virtual void SetStatus(Status status) override { | 66 virtual void SetStatus(Status status) override { |
78 SetStatusProxy(status); | 67 SetStatusProxy(status); |
79 BluetoothConnection::SetStatus(status); | 68 BluetoothConnection::SetStatus(status); |
80 } | 69 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber()); | 103 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber()); |
115 } | 104 } |
116 | 105 |
117 // Transition the connection into an in-progress state. | 106 // Transition the connection into an in-progress state. |
118 void BeginConnecting(MockBluetoothConnection* connection) { | 107 void BeginConnecting(MockBluetoothConnection* connection) { |
119 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); | 108 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); |
120 | 109 |
121 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); | 110 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); |
122 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); | 111 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); |
123 EXPECT_CALL(*adapter_, AddObserver(connection)); | 112 EXPECT_CALL(*adapter_, AddObserver(connection)); |
124 EXPECT_CALL(*connection, ConnectToService(&device_, uuid_, _, _)); | 113 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)); |
125 connection->Connect(); | 114 connection->Connect(); |
126 | 115 |
127 EXPECT_EQ(Connection::IN_PROGRESS, connection->status()); | 116 EXPECT_EQ(Connection::IN_PROGRESS, connection->status()); |
128 } | 117 } |
129 | 118 |
130 // Transition the connection into a connected state. | 119 // Transition the connection into a connected state. |
131 // Saves the success and error callbacks passed into OnReceive(), which can be | 120 // Saves the success and error callbacks passed into OnReceive(), which can be |
132 // accessed via receive_callback() and receive_success_callback(). | 121 // accessed via receive_callback() and receive_success_callback(). |
133 void Connect(MockBluetoothConnection* connection) { | 122 void Connect(MockBluetoothConnection* connection) { |
134 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); | 123 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); |
135 | 124 |
136 device::BluetoothDevice::ConnectToServiceCallback callback; | 125 device::BluetoothDevice::ConnectToServiceCallback callback; |
137 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); | 126 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); |
138 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); | 127 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); |
139 EXPECT_CALL(*adapter_, AddObserver(connection)); | 128 EXPECT_CALL(*adapter_, AddObserver(connection)); |
140 EXPECT_CALL(*connection, ConnectToService(_, _, _, _)) | 129 EXPECT_CALL(device_, ConnectToServiceInsecurely(_, _, _)) |
141 .WillOnce(SaveArg<2>(&callback)); | 130 .WillOnce(SaveArg<1>(&callback)); |
142 connection->Connect(); | 131 connection->Connect(); |
143 ASSERT_FALSE(callback.is_null()); | 132 ASSERT_FALSE(callback.is_null()); |
144 | 133 |
145 EXPECT_CALL(*connection, SetStatusProxy(Connection::CONNECTED)); | 134 EXPECT_CALL(*connection, SetStatusProxy(Connection::CONNECTED)); |
146 EXPECT_CALL(*socket_, Receive(_, _, _)) | 135 EXPECT_CALL(*socket_, Receive(_, _, _)) |
147 .WillOnce(DoAll(SaveArg<1>(&receive_callback_), | 136 .WillOnce(DoAll(SaveArg<1>(&receive_callback_), |
148 SaveArg<2>(&receive_error_callback_))); | 137 SaveArg<2>(&receive_error_callback_))); |
149 callback.Run(socket_); | 138 callback.Run(socket_); |
150 | 139 |
151 EXPECT_EQ(Connection::CONNECTED, connection->status()); | 140 EXPECT_EQ(Connection::CONNECTED, connection->status()); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 243 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
255 } | 244 } |
256 | 245 |
257 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionFails) { | 246 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionFails) { |
258 StrictMock<MockBluetoothConnection> connection; | 247 StrictMock<MockBluetoothConnection> connection; |
259 | 248 |
260 device::BluetoothDevice::ConnectToServiceErrorCallback error_callback; | 249 device::BluetoothDevice::ConnectToServiceErrorCallback error_callback; |
261 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); | 250 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); |
262 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | 251 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); |
263 EXPECT_CALL(*adapter_, AddObserver(&connection)); | 252 EXPECT_CALL(*adapter_, AddObserver(&connection)); |
264 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | 253 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)) |
265 .WillOnce(SaveArg<3>(&error_callback)); | 254 .WillOnce(SaveArg<2>(&error_callback)); |
266 connection.Connect(); | 255 connection.Connect(); |
267 ASSERT_FALSE(error_callback.is_null()); | 256 ASSERT_FALSE(error_callback.is_null()); |
268 | 257 |
269 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | 258 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); |
270 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 259 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
271 error_callback.Run("super descriptive error message"); | 260 error_callback.Run("super descriptive error message"); |
272 } | 261 } |
273 | 262 |
274 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionSucceeds) { | 263 TEST_F(ProximityAuthBluetoothConnectionTest, Connect_ConnectionSucceeds) { |
275 StrictMock<MockBluetoothConnection> connection; | 264 StrictMock<MockBluetoothConnection> connection; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 connection.Disconnect(); | 364 connection.Disconnect(); |
376 } | 365 } |
377 | 366 |
378 TEST_F(ProximityAuthBluetoothConnectionTest, | 367 TEST_F(ProximityAuthBluetoothConnectionTest, |
379 Connect_ThenDisconnectWhileInProgress_ThenBackingConnectionSucceeds) { | 368 Connect_ThenDisconnectWhileInProgress_ThenBackingConnectionSucceeds) { |
380 StrictMock<MockBluetoothConnection> connection; | 369 StrictMock<MockBluetoothConnection> connection; |
381 device::BluetoothDevice::ConnectToServiceCallback callback; | 370 device::BluetoothDevice::ConnectToServiceCallback callback; |
382 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); | 371 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); |
383 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); | 372 EXPECT_CALL(connection, SetStatusProxy(Connection::IN_PROGRESS)); |
384 EXPECT_CALL(*adapter_, AddObserver(&connection)); | 373 EXPECT_CALL(*adapter_, AddObserver(&connection)); |
385 EXPECT_CALL(connection, ConnectToService(&device_, uuid_, _, _)) | 374 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)) |
386 .WillOnce(SaveArg<2>(&callback)); | 375 .WillOnce(SaveArg<1>(&callback)); |
387 connection.Connect(); | 376 connection.Connect(); |
388 ASSERT_FALSE(callback.is_null()); | 377 ASSERT_FALSE(callback.is_null()); |
389 | 378 |
390 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | 379 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); |
391 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 380 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
392 connection.Disconnect(); | 381 connection.Disconnect(); |
393 | 382 |
394 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); | 383 EXPECT_CALL(connection, SetStatusProxy(_)).Times(0); |
395 EXPECT_CALL(*socket_, Receive(_, _, _)).Times(0); | 384 EXPECT_CALL(*socket_, Receive(_, _, _)).Times(0); |
396 callback.Run(socket_); | 385 callback.Run(socket_); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 | 442 |
454 ASSERT_FALSE(error_callback.is_null()); | 443 ASSERT_FALSE(error_callback.is_null()); |
455 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); | 444 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); |
456 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); | 445 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); |
457 EXPECT_CALL(*socket_, Disconnect(_)); | 446 EXPECT_CALL(*socket_, Disconnect(_)); |
458 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); | 447 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); |
459 error_callback.Run("The most helpful of error messages"); | 448 error_callback.Run("The most helpful of error messages"); |
460 } | 449 } |
461 | 450 |
462 } // namespace proximity_auth | 451 } // namespace proximity_auth |
OLD | NEW |