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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/cryptauth/connection.h" |
| 13 #include "components/cryptauth/cryptauth_test_util.h" |
| 14 #include "components/cryptauth/fake_connection.h" |
12 #include "components/cryptauth/remote_device.h" | 15 #include "components/cryptauth/remote_device.h" |
13 #include "components/proximity_auth/connection.h" | 16 #include "components/cryptauth/wire_message.h" |
14 #include "components/proximity_auth/fake_connection.h" | |
15 #include "components/proximity_auth/fake_secure_context.h" | 17 #include "components/proximity_auth/fake_secure_context.h" |
16 #include "components/proximity_auth/messenger_observer.h" | 18 #include "components/proximity_auth/messenger_observer.h" |
17 #include "components/proximity_auth/proximity_auth_test_util.h" | |
18 #include "components/proximity_auth/remote_status_update.h" | 19 #include "components/proximity_auth/remote_status_update.h" |
19 #include "components/proximity_auth/wire_message.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 using testing::_; | 23 using testing::_; |
24 using testing::AllOf; | 24 using testing::AllOf; |
25 using testing::Eq; | 25 using testing::Eq; |
26 using testing::Field; | 26 using testing::Field; |
27 using testing::NiceMock; | 27 using testing::NiceMock; |
28 using testing::Pointee; | 28 using testing::Pointee; |
29 using testing::Return; | 29 using testing::Return; |
(...skipping 26 matching lines...) Expand all Loading... |
56 private: | 56 private: |
57 // The messenger that |this| instance observes. | 57 // The messenger that |this| instance observes. |
58 Messenger* const messenger_; | 58 Messenger* const messenger_; |
59 | 59 |
60 DISALLOW_COPY_AND_ASSIGN(MockMessengerObserver); | 60 DISALLOW_COPY_AND_ASSIGN(MockMessengerObserver); |
61 }; | 61 }; |
62 | 62 |
63 class TestMessenger : public MessengerImpl { | 63 class TestMessenger : public MessengerImpl { |
64 public: | 64 public: |
65 TestMessenger() | 65 TestMessenger() |
66 : MessengerImpl(base::MakeUnique<FakeConnection>( | 66 : MessengerImpl(base::MakeUnique<cryptauth::FakeConnection>( |
67 CreateClassicRemoteDeviceForTest()), | 67 cryptauth::CreateClassicRemoteDeviceForTest()), |
68 base::MakeUnique<FakeSecureContext>()) {} | 68 base::MakeUnique<FakeSecureContext>()) {} |
69 ~TestMessenger() override {} | 69 ~TestMessenger() override {} |
70 | 70 |
71 // Simple getters for the mock objects owned by |this| messenger. | 71 // Simple getters for the mock objects owned by |this| messenger. |
72 FakeConnection* GetFakeConnection() { | 72 cryptauth::FakeConnection* GetFakeConnection() { |
73 return static_cast<FakeConnection*>(connection()); | 73 return static_cast<cryptauth::FakeConnection*>(connection()); |
74 } | 74 } |
75 FakeSecureContext* GetFakeSecureContext() { | 75 FakeSecureContext* GetFakeSecureContext() { |
76 return static_cast<FakeSecureContext*>(GetSecureContext()); | 76 return static_cast<FakeSecureContext*>(GetSecureContext()); |
77 } | 77 } |
78 | 78 |
79 private: | 79 private: |
80 DISALLOW_COPY_AND_ASSIGN(TestMessenger); | 80 DISALLOW_COPY_AND_ASSIGN(TestMessenger); |
81 }; | 81 }; |
82 | 82 |
83 } // namespace | 83 } // namespace |
(...skipping 18 matching lines...) Expand all Loading... |
102 MockMessengerObserver observer(&messenger); | 102 MockMessengerObserver observer(&messenger); |
103 | 103 |
104 EXPECT_CALL(observer, OnDisconnected()); | 104 EXPECT_CALL(observer, OnDisconnected()); |
105 messenger.GetFakeConnection()->Disconnect(); | 105 messenger.GetFakeConnection()->Disconnect(); |
106 } | 106 } |
107 | 107 |
108 TEST(ProximityAuthMessengerImplTest, DispatchUnlockEvent_SendsExpectedMessage) { | 108 TEST(ProximityAuthMessengerImplTest, DispatchUnlockEvent_SendsExpectedMessage) { |
109 TestMessenger messenger; | 109 TestMessenger messenger; |
110 messenger.DispatchUnlockEvent(); | 110 messenger.DispatchUnlockEvent(); |
111 | 111 |
112 WireMessage* message = messenger.GetFakeConnection()->current_message(); | 112 cryptauth::WireMessage* message = |
| 113 messenger.GetFakeConnection()->current_message(); |
113 ASSERT_TRUE(message); | 114 ASSERT_TRUE(message); |
114 EXPECT_EQ(std::string(), message->permit_id()); | 115 EXPECT_EQ(std::string(), message->permit_id()); |
115 EXPECT_EQ( | 116 EXPECT_EQ( |
116 "{" | 117 "{" |
117 "\"name\":\"easy_unlock\"," | 118 "\"name\":\"easy_unlock\"," |
118 "\"type\":\"event\"" | 119 "\"type\":\"event\"" |
119 "}, but encoded", | 120 "}, but encoded", |
120 message->payload()); | 121 message->payload()); |
121 } | 122 } |
122 | 123 |
(...skipping 21 matching lines...) Expand all Loading... |
144 messenger.GetFakeSecureContext()->set_protocol_version( | 145 messenger.GetFakeSecureContext()->set_protocol_version( |
145 SecureContext::PROTOCOL_VERSION_THREE_ZERO); | 146 SecureContext::PROTOCOL_VERSION_THREE_ZERO); |
146 messenger.RequestDecryption(kChallenge); | 147 messenger.RequestDecryption(kChallenge); |
147 EXPECT_FALSE(messenger.GetFakeConnection()->current_message()); | 148 EXPECT_FALSE(messenger.GetFakeConnection()->current_message()); |
148 } | 149 } |
149 | 150 |
150 TEST(ProximityAuthMessengerImplTest, RequestDecryption_SendsExpectedMessage) { | 151 TEST(ProximityAuthMessengerImplTest, RequestDecryption_SendsExpectedMessage) { |
151 TestMessenger messenger; | 152 TestMessenger messenger; |
152 messenger.RequestDecryption(kChallenge); | 153 messenger.RequestDecryption(kChallenge); |
153 | 154 |
154 WireMessage* message = messenger.GetFakeConnection()->current_message(); | 155 cryptauth::WireMessage* message = |
| 156 messenger.GetFakeConnection()->current_message(); |
155 ASSERT_TRUE(message); | 157 ASSERT_TRUE(message); |
156 EXPECT_EQ(std::string(), message->permit_id()); | 158 EXPECT_EQ(std::string(), message->permit_id()); |
157 EXPECT_EQ( | 159 EXPECT_EQ( |
158 "{" | 160 "{" |
159 "\"encrypted_data\":\"YSBtb3N0IGRpZmZpY3VsdCBjaGFsbGVuZ2U=\"," | 161 "\"encrypted_data\":\"YSBtb3N0IGRpZmZpY3VsdCBjaGFsbGVuZ2U=\"," |
160 "\"type\":\"decrypt_request\"" | 162 "\"type\":\"decrypt_request\"" |
161 "}, but encoded", | 163 "}, but encoded", |
162 message->payload()); | 164 message->payload()); |
163 } | 165 } |
164 | 166 |
165 TEST(ProximityAuthMessengerImplTest, | 167 TEST(ProximityAuthMessengerImplTest, |
166 RequestDecryption_SendsExpectedMessage_UsingBase64UrlEncoding) { | 168 RequestDecryption_SendsExpectedMessage_UsingBase64UrlEncoding) { |
167 TestMessenger messenger; | 169 TestMessenger messenger; |
168 messenger.RequestDecryption("\xFF\xE6"); | 170 messenger.RequestDecryption("\xFF\xE6"); |
169 | 171 |
170 WireMessage* message = messenger.GetFakeConnection()->current_message(); | 172 cryptauth::WireMessage* message = |
| 173 messenger.GetFakeConnection()->current_message(); |
171 ASSERT_TRUE(message); | 174 ASSERT_TRUE(message); |
172 EXPECT_EQ(std::string(), message->permit_id()); | 175 EXPECT_EQ(std::string(), message->permit_id()); |
173 EXPECT_EQ( | 176 EXPECT_EQ( |
174 "{" | 177 "{" |
175 "\"encrypted_data\":\"_-Y=\"," | 178 "\"encrypted_data\":\"_-Y=\"," |
176 "\"type\":\"decrypt_request\"" | 179 "\"type\":\"decrypt_request\"" |
177 "}, but encoded", | 180 "}, but encoded", |
178 message->payload()); | 181 message->payload()); |
179 } | 182 } |
180 | 183 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 messenger.GetFakeSecureContext()->set_protocol_version( | 264 messenger.GetFakeSecureContext()->set_protocol_version( |
262 SecureContext::PROTOCOL_VERSION_THREE_ZERO); | 265 SecureContext::PROTOCOL_VERSION_THREE_ZERO); |
263 messenger.RequestUnlock(); | 266 messenger.RequestUnlock(); |
264 EXPECT_FALSE(messenger.GetFakeConnection()->current_message()); | 267 EXPECT_FALSE(messenger.GetFakeConnection()->current_message()); |
265 } | 268 } |
266 | 269 |
267 TEST(ProximityAuthMessengerImplTest, RequestUnlock_SendsExpectedMessage) { | 270 TEST(ProximityAuthMessengerImplTest, RequestUnlock_SendsExpectedMessage) { |
268 TestMessenger messenger; | 271 TestMessenger messenger; |
269 messenger.RequestUnlock(); | 272 messenger.RequestUnlock(); |
270 | 273 |
271 WireMessage* message = messenger.GetFakeConnection()->current_message(); | 274 cryptauth::WireMessage* message = |
| 275 messenger.GetFakeConnection()->current_message(); |
272 ASSERT_TRUE(message); | 276 ASSERT_TRUE(message); |
273 EXPECT_EQ(std::string(), message->permit_id()); | 277 EXPECT_EQ(std::string(), message->permit_id()); |
274 EXPECT_EQ("{\"type\":\"unlock_request\"}, but encoded", message->payload()); | 278 EXPECT_EQ("{\"type\":\"unlock_request\"}, but encoded", message->payload()); |
275 } | 279 } |
276 | 280 |
277 TEST(ProximityAuthMessengerImplTest, RequestUnlock_SendMessageFails) { | 281 TEST(ProximityAuthMessengerImplTest, RequestUnlock_SendMessageFails) { |
278 TestMessenger messenger; | 282 TestMessenger messenger; |
279 MockMessengerObserver observer(&messenger); | 283 MockMessengerObserver observer(&messenger); |
280 messenger.RequestUnlock(); | 284 messenger.RequestUnlock(); |
281 | 285 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 "\"data\":\"YSB3aW5uZXIgaXMgeW91\"" | 436 "\"data\":\"YSB3aW5uZXIgaXMgeW91\"" |
433 "}, but encoded"); | 437 "}, but encoded"); |
434 | 438 |
435 // The unlock request should have remained buffered, and should only now be | 439 // The unlock request should have remained buffered, and should only now be |
436 // sent. | 440 // sent. |
437 EXPECT_CALL(observer, OnUnlockResponse(false)); | 441 EXPECT_CALL(observer, OnUnlockResponse(false)); |
438 messenger.GetFakeConnection()->FinishSendingMessageWithSuccess(false); | 442 messenger.GetFakeConnection()->FinishSendingMessageWithSuccess(false); |
439 } | 443 } |
440 | 444 |
441 } // namespace proximity_auth | 445 } // namespace proximity_auth |
OLD | NEW |