Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(925)

Side by Side Diff: components/proximity_auth/bluetooth_connection_unittest.cc

Issue 1356943004: Add RemoteDeviceLoader to create RemoteDevice from CryptAuth data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webui
Patch Set: move RemoteDevice creation in tests to util function Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/proximity_auth_test_util.h"
10 #include "components/proximity_auth/remote_device.h" 11 #include "components/proximity_auth/remote_device.h"
11 #include "components/proximity_auth/wire_message.h" 12 #include "components/proximity_auth/wire_message.h"
12 #include "device/bluetooth/bluetooth_adapter_factory.h" 13 #include "device/bluetooth/bluetooth_adapter_factory.h"
13 #include "device/bluetooth/bluetooth_uuid.h" 14 #include "device/bluetooth/bluetooth_uuid.h"
14 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 15 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
15 #include "device/bluetooth/test/mock_bluetooth_device.h" 16 #include "device/bluetooth/test/mock_bluetooth_device.h"
16 #include "device/bluetooth/test/mock_bluetooth_socket.h" 17 #include "device/bluetooth/test/mock_bluetooth_socket.h"
17 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 using testing::_; 22 using testing::_;
22 using testing::AnyNumber; 23 using testing::AnyNumber;
23 using testing::NiceMock; 24 using testing::NiceMock;
24 using testing::Ref; 25 using testing::Ref;
25 using testing::Return; 26 using testing::Return;
26 using testing::SaveArg; 27 using testing::SaveArg;
27 using testing::StrictMock; 28 using testing::StrictMock;
28 29
29 namespace proximity_auth { 30 namespace proximity_auth {
30 namespace { 31 namespace {
31 32
32 const char kDeviceName[] = "Device name";
33 const char kOtherDeviceName[] = "Other device name"; 33 const char kOtherDeviceName[] = "Other device name";
34 34 const char kOtherBluetoothAddress[] = "FF:BB:CC:DD:EE:FF";
35 const char kBluetoothAddress[] = "11:22:33:44:55:66";
36 const char kOtherBluetoothAddress[] = "AA:BB:CC:DD:EE:FF";
37
38 const char kPublicKey[] = "Public key";
39 const char kPersistentSymmetricKey[] = "PSK";
40 35
41 const char kSerializedMessage[] = "Yarrr, this be a serialized message. Yarr!"; 36 const char kSerializedMessage[] = "Yarrr, this be a serialized message. Yarr!";
42 const int kSerializedMessageLength = strlen(kSerializedMessage); 37 const int kSerializedMessageLength = strlen(kSerializedMessage);
43 38
44 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; 39 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF";
45 40
46 const int kReceiveBufferSize = 6; 41 const int kReceiveBufferSize = 6;
47 const char kReceiveBufferContents[] = "bytes"; 42 const char kReceiveBufferContents[] = "bytes";
48 43
49 // Create a buffer for testing received data. 44 // Create a buffer for testing received data.
50 scoped_refptr<net::IOBuffer> CreateReceiveBuffer() { 45 scoped_refptr<net::IOBuffer> CreateReceiveBuffer() {
51 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize); 46 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kReceiveBufferSize);
52 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize); 47 memcpy(buffer->data(), kReceiveBufferContents, kReceiveBufferSize);
53 return buffer; 48 return buffer;
54 } 49 }
55 50
56 class MockBluetoothConnection : public BluetoothConnection { 51 class MockBluetoothConnection : public BluetoothConnection {
57 public: 52 public:
58 MockBluetoothConnection() 53 MockBluetoothConnection()
59 : BluetoothConnection(CreateRemoteDevice(), 54 : BluetoothConnection(CreateClassicRemoteDeviceForTest(),
60 device::BluetoothUUID(kUuid)) {} 55 device::BluetoothUUID(kUuid)) {}
61 56
62 // Calls back into the parent Connection class. 57 // Calls back into the parent Connection class.
63 MOCK_METHOD1(SetStatusProxy, void(Status status)); 58 MOCK_METHOD1(SetStatusProxy, void(Status status));
64 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); 59 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes));
65 MOCK_METHOD2(OnDidSendMessage, 60 MOCK_METHOD2(OnDidSendMessage,
66 void(const WireMessage& message, bool success)); 61 void(const WireMessage& message, bool success));
67 62
68 void SetStatus(Status status) override { 63 void SetStatus(Status status) override {
69 SetStatusProxy(status); 64 SetStatusProxy(status);
70 BluetoothConnection::SetStatus(status); 65 BluetoothConnection::SetStatus(status);
71 } 66 }
72 67
73 using BluetoothConnection::status; 68 using BluetoothConnection::status;
74 using BluetoothConnection::Connect; 69 using BluetoothConnection::Connect;
75 using BluetoothConnection::DeviceRemoved; 70 using BluetoothConnection::DeviceRemoved;
76 using BluetoothConnection::Disconnect; 71 using BluetoothConnection::Disconnect;
77 72
78 private: 73 private:
79 RemoteDevice CreateRemoteDevice() {
80 return RemoteDevice(kDeviceName, kPublicKey, kBluetoothAddress,
81 kPersistentSymmetricKey);
82 }
83
84 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection); 74 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection);
85 }; 75 };
86 76
87 class TestWireMessage : public WireMessage { 77 class TestWireMessage : public WireMessage {
88 public: 78 public:
89 TestWireMessage() : WireMessage("permit id", "payload") {} 79 TestWireMessage() : WireMessage("permit id", "payload") {}
90 ~TestWireMessage() override {} 80 ~TestWireMessage() override {}
91 81
92 std::string Serialize() const override { return kSerializedMessage; } 82 std::string Serialize() const override { return kSerializedMessage; }
93 83
94 private: 84 private:
95 DISALLOW_COPY_AND_ASSIGN(TestWireMessage); 85 DISALLOW_COPY_AND_ASSIGN(TestWireMessage);
96 }; 86 };
97 87
98 } // namespace 88 } // namespace
99 89
100 class ProximityAuthBluetoothConnectionTest : public testing::Test { 90 class ProximityAuthBluetoothConnectionTest : public testing::Test {
101 public: 91 public:
102 ProximityAuthBluetoothConnectionTest() 92 ProximityAuthBluetoothConnectionTest()
103 : adapter_(new device::MockBluetoothAdapter), 93 : adapter_(new device::MockBluetoothAdapter),
104 device_(adapter_.get(), 0, kDeviceName, kBluetoothAddress, true, true), 94 device_(adapter_.get(),
95 0,
96 kTestRemoteDeviceName,
97 kTestRemoteDeviceBluetoothAddress,
98 true,
99 true),
105 socket_(new StrictMock<device::MockBluetoothSocket>), 100 socket_(new StrictMock<device::MockBluetoothSocket>),
106 uuid_(kUuid) { 101 uuid_(kUuid) {
107 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 102 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
108 103
109 // Suppress uninteresting Gmock call warnings. 104 // Suppress uninteresting Gmock call warnings.
110 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber()); 105 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber());
111 } 106 }
112 107
113 // Transition the connection into an in-progress state. 108 // Transition the connection into an in-progress state.
114 void BeginConnecting(MockBluetoothConnection* connection) { 109 void BeginConnecting(MockBluetoothConnection* connection) {
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 444
450 ASSERT_FALSE(error_callback.is_null()); 445 ASSERT_FALSE(error_callback.is_null());
451 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); 446 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false));
452 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 447 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED));
453 EXPECT_CALL(*socket_, Disconnect(_)); 448 EXPECT_CALL(*socket_, Disconnect(_));
454 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 449 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
455 error_callback.Run("The most helpful of error messages"); 450 error_callback.Run("The most helpful of error messages");
456 } 451 }
457 452
458 } // namespace proximity_auth 453 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698