| 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/messenger_impl.h" | 5 #include "components/proximity_auth/messenger_impl.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "components/proximity_auth/connection.h" | 10 #include "components/proximity_auth/connection.h" |
| 11 #include "components/proximity_auth/fake_connection.h" |
| 11 #include "components/proximity_auth/messenger_observer.h" | 12 #include "components/proximity_auth/messenger_observer.h" |
| 12 #include "components/proximity_auth/remote_device.h" | 13 #include "components/proximity_auth/remote_device.h" |
| 13 #include "components/proximity_auth/remote_status_update.h" | 14 #include "components/proximity_auth/remote_status_update.h" |
| 14 #include "components/proximity_auth/secure_context.h" | 15 #include "components/proximity_auth/secure_context.h" |
| 15 #include "components/proximity_auth/wire_message.h" | 16 #include "components/proximity_auth/wire_message.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 using testing::_; | 20 using testing::_; |
| 20 using testing::AllOf; | 21 using testing::AllOf; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 EXPECT_THAT(encoded_message, EndsWith(kFakeEncodingSuffix)); | 56 EXPECT_THAT(encoded_message, EndsWith(kFakeEncodingSuffix)); |
| 56 std::string decoded_message = encoded_message; | 57 std::string decoded_message = encoded_message; |
| 57 decoded_message.erase(decoded_message.rfind(kFakeEncodingSuffix)); | 58 decoded_message.erase(decoded_message.rfind(kFakeEncodingSuffix)); |
| 58 callback.Run(decoded_message); | 59 callback.Run(decoded_message); |
| 59 } | 60 } |
| 60 | 61 |
| 61 private: | 62 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(MockSecureContext); | 63 DISALLOW_COPY_AND_ASSIGN(MockSecureContext); |
| 63 }; | 64 }; |
| 64 | 65 |
| 65 class FakeConnection : public Connection { | |
| 66 public: | |
| 67 FakeConnection() : Connection(RemoteDevice()) { Connect(); } | |
| 68 ~FakeConnection() override { Disconnect(); } | |
| 69 | |
| 70 void Connect() override { SetStatus(CONNECTED); } | |
| 71 | |
| 72 void Disconnect() override { SetStatus(DISCONNECTED); } | |
| 73 | |
| 74 void SendMessageImpl(scoped_ptr<WireMessage> message) override { | |
| 75 ASSERT_FALSE(current_message_); | |
| 76 current_message_ = message.Pass(); | |
| 77 } | |
| 78 | |
| 79 // Completes the current send operation with success |success|. | |
| 80 void FinishSendingMessageWithSuccess(bool success) { | |
| 81 ASSERT_TRUE(current_message_); | |
| 82 // Capture a copy of the message, as OnDidSendMessage() might reentrantly | |
| 83 // call SendMessage(). | |
| 84 scoped_ptr<WireMessage> sent_message = current_message_.Pass(); | |
| 85 OnDidSendMessage(*sent_message, success); | |
| 86 } | |
| 87 | |
| 88 // Simulates receiving a wire message with the given |payload|. | |
| 89 void ReceiveMessageWithPayload(const std::string& payload) { | |
| 90 pending_payload_ = payload; | |
| 91 OnBytesReceived(std::string()); | |
| 92 pending_payload_.clear(); | |
| 93 } | |
| 94 | |
| 95 // Returns a message containing the payload set via | |
| 96 // ReceiveMessageWithPayload(). | |
| 97 scoped_ptr<WireMessage> DeserializeWireMessage( | |
| 98 bool* is_incomplete_message) override { | |
| 99 *is_incomplete_message = false; | |
| 100 return make_scoped_ptr(new WireMessage(pending_payload_)); | |
| 101 } | |
| 102 | |
| 103 WireMessage* current_message() { return current_message_.get(); } | |
| 104 | |
| 105 private: | |
| 106 // The message currently being sent. Only set between a call to | |
| 107 // SendMessageImpl() and FinishSendingMessageWithSuccess(). | |
| 108 scoped_ptr<WireMessage> current_message_; | |
| 109 | |
| 110 // The payload that should be returned when DeserializeWireMessage() is | |
| 111 // called. | |
| 112 std::string pending_payload_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(FakeConnection); | |
| 115 }; | |
| 116 | |
| 117 class MockMessengerObserver : public MessengerObserver { | 66 class MockMessengerObserver : public MessengerObserver { |
| 118 public: | 67 public: |
| 119 explicit MockMessengerObserver(Messenger* messenger) : messenger_(messenger) { | 68 explicit MockMessengerObserver(Messenger* messenger) : messenger_(messenger) { |
| 120 messenger_->AddObserver(this); | 69 messenger_->AddObserver(this); |
| 121 } | 70 } |
| 122 virtual ~MockMessengerObserver() { messenger_->RemoveObserver(this); } | 71 virtual ~MockMessengerObserver() { messenger_->RemoveObserver(this); } |
| 123 | 72 |
| 124 MOCK_METHOD1(OnUnlockEventSent, void(bool success)); | 73 MOCK_METHOD1(OnUnlockEventSent, void(bool success)); |
| 125 MOCK_METHOD1(OnRemoteStatusUpdate, | 74 MOCK_METHOD1(OnRemoteStatusUpdate, |
| 126 void(const RemoteStatusUpdate& status_update)); | 75 void(const RemoteStatusUpdate& status_update)); |
| 127 MOCK_METHOD1(OnDecryptResponseProxy, | 76 MOCK_METHOD1(OnDecryptResponseProxy, |
| 128 void(const std::string* decrypted_bytes)); | 77 void(const std::string* decrypted_bytes)); |
| 129 MOCK_METHOD1(OnUnlockResponse, void(bool success)); | 78 MOCK_METHOD1(OnUnlockResponse, void(bool success)); |
| 130 MOCK_METHOD0(OnDisconnected, void()); | 79 MOCK_METHOD0(OnDisconnected, void()); |
| 131 | 80 |
| 132 virtual void OnDecryptResponse(scoped_ptr<std::string> decrypted_bytes) { | 81 virtual void OnDecryptResponse(scoped_ptr<std::string> decrypted_bytes) { |
| 133 OnDecryptResponseProxy(decrypted_bytes.get()); | 82 OnDecryptResponseProxy(decrypted_bytes.get()); |
| 134 } | 83 } |
| 135 | 84 |
| 136 private: | 85 private: |
| 137 // The messenger that |this| instance observes. | 86 // The messenger that |this| instance observes. |
| 138 Messenger* const messenger_; | 87 Messenger* const messenger_; |
| 139 | 88 |
| 140 DISALLOW_COPY_AND_ASSIGN(MockMessengerObserver); | 89 DISALLOW_COPY_AND_ASSIGN(MockMessengerObserver); |
| 141 }; | 90 }; |
| 142 | 91 |
| 143 class TestMessenger : public MessengerImpl { | 92 class TestMessenger : public MessengerImpl { |
| 144 public: | 93 public: |
| 145 TestMessenger() | 94 TestMessenger() |
| 146 : MessengerImpl(make_scoped_ptr(new NiceMock<FakeConnection>()), | 95 : MessengerImpl(make_scoped_ptr(new FakeConnection(RemoteDevice())), |
| 147 make_scoped_ptr(new NiceMock<MockSecureContext>())) {} | 96 make_scoped_ptr(new NiceMock<MockSecureContext>())) {} |
| 148 ~TestMessenger() override {} | 97 ~TestMessenger() override {} |
| 149 | 98 |
| 150 // Simple getters for the mock objects owned by |this| messenger. | 99 // Simple getters for the mock objects owned by |this| messenger. |
| 151 FakeConnection* GetFakeConnection() { | 100 FakeConnection* GetFakeConnection() { |
| 152 return static_cast<FakeConnection*>(connection()); | 101 return static_cast<FakeConnection*>(connection()); |
| 153 } | 102 } |
| 154 MockSecureContext* GetMockSecureContext() { | 103 MockSecureContext* GetMockSecureContext() { |
| 155 return static_cast<MockSecureContext*>(secure_context()); | 104 return static_cast<MockSecureContext*>(secure_context()); |
| 156 } | 105 } |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 "\"data\":\"YSB3aW5uZXIgaXMgeW91\"" | 460 "\"data\":\"YSB3aW5uZXIgaXMgeW91\"" |
| 512 "}, but encoded"); | 461 "}, but encoded"); |
| 513 | 462 |
| 514 // The unlock request should have remained buffered, and should only now be | 463 // The unlock request should have remained buffered, and should only now be |
| 515 // sent. | 464 // sent. |
| 516 EXPECT_CALL(observer, OnUnlockResponse(false)); | 465 EXPECT_CALL(observer, OnUnlockResponse(false)); |
| 517 messenger.GetFakeConnection()->FinishSendingMessageWithSuccess(false); | 466 messenger.GetFakeConnection()->FinishSendingMessageWithSuccess(false); |
| 518 } | 467 } |
| 519 | 468 |
| 520 } // namespace proximity_auth | 469 } // namespace proximity_auth |
| OLD | NEW |