1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proximity_auth/client.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "components/proximity_auth/client_observer.h"
#include "components/proximity_auth/connection.h"
#include "components/proximity_auth/remote_device.h"
#include "components/proximity_auth/remote_status_update.h"
#include "components/proximity_auth/secure_context.h"
#include "components/proximity_auth/wire_message.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::AllOf;
using testing::EndsWith;
using testing::Eq;
using testing::Field;
using testing::NiceMock;
using testing::Pointee;
using testing::Return;
using testing::StrictMock;
namespace proximity_auth {
namespace {
const char kChallenge[] = "a most difficult challenge";
const char kFakeEncodingSuffix[] = ", but encoded";
class MockSecureContext : public SecureContext {
public:
MockSecureContext() {
// By default, mock a secure context that uses the 3.1 protocol. Individual
// tests override this as needed.
ON_CALL(*this, GetProtocolVersion())
.WillByDefault(Return(SecureContext::PROTOCOL_VERSION_THREE_ONE));
}
virtual ~MockSecureContext() {}
MOCK_CONST_METHOD0(GetReceivedAuthMessage, std::string());
MOCK_CONST_METHOD0(GetProtocolVersion, ProtocolVersion());
virtual std::string Encode(const std::string& message) override {
return message + kFakeEncodingSuffix;
}
virtual std::string Decode(const std::string& encoded_message) override {
EXPECT_THAT(encoded_message, EndsWith(kFakeEncodingSuffix));
std::string decoded_message = encoded_message;
decoded_message.erase(decoded_message.rfind(kFakeEncodingSuffix));
return decoded_message;
}
private:
DISALLOW_COPY_AND_ASSIGN(MockSecureContext);
};
class FakeConnection : public Connection {
public:
FakeConnection() : Connection(RemoteDevice()) { Connect(); }
~FakeConnection() override { Disconnect(); }
void Connect() override { SetStatus(CONNECTED); }
void Disconnect() override { SetStatus(DISCONNECTED); }
void SendMessageImpl(scoped_ptr<WireMessage> message) override {
ASSERT_FALSE(current_message_);
current_message_ = message.Pass();
}
// Completes the current send operation with success |success|.
void FinishSendingMessageWithSuccess(bool success) {
ASSERT_TRUE(current_message_);
// Capture a copy of the message, as OnDidSendMessage() might reentrantly
// call SendMessage().
scoped_ptr<WireMessage> sent_message = current_message_.Pass();
OnDidSendMessage(*sent_message, success);
}
// Simulates receiving a wire message with the given |payload|.
void ReceiveMessageWithPayload(const std::string& payload) {
pending_payload_ = payload;
OnBytesReceived(std::string());
pending_payload_.clear();
}
// Returns a message containing the payload set via
// ReceiveMessageWithPayload().
scoped_ptr<WireMessage> DeserializeWireMessage(
bool* is_incomplete_message) override {
*is_incomplete_message = false;
return make_scoped_ptr(new WireMessage(std::string(), pending_payload_));
}
WireMessage* current_message() { return current_message_.get(); }
private:
// The message currently being sent. Only set between a call to
// SendMessageImpl() and FinishSendingMessageWithSuccess().
scoped_ptr<WireMessage> current_message_;
// The payload that should be returned when DeserializeWireMessage() is
// called.
std::string pending_payload_;
DISALLOW_COPY_AND_ASSIGN(FakeConnection);
};
class MockClientObserver : public ClientObserver {
public:
explicit MockClientObserver(Client* client) : client_(client) {
client_->AddObserver(this);
}
virtual ~MockClientObserver() { client_->RemoveObserver(this); }
MOCK_METHOD1(OnUnlockEventSent, void(bool success));
MOCK_METHOD1(OnRemoteStatusUpdate,
void(const RemoteStatusUpdate& status_update));
MOCK_METHOD1(OnDecryptResponseProxy,
void(const std::string* decrypted_bytes));
MOCK_METHOD1(OnUnlockResponse, void(bool success));
MOCK_METHOD0(OnDisconnected, void());
virtual void OnDecryptResponse(scoped_ptr<std::string> decrypted_bytes) {
OnDecryptResponseProxy(decrypted_bytes.get());
}
private:
// The client that |this| instance observes.
Client* const client_;
DISALLOW_COPY_AND_ASSIGN(MockClientObserver);
};
class TestClient : public Client {
public:
TestClient()
: Client(make_scoped_ptr(new NiceMock<FakeConnection>()),
make_scoped_ptr(new NiceMock<MockSecureContext>())) {}
~TestClient() override {}
// Simple getters for the mock objects owned by |this| client.
FakeConnection* GetFakeConnection() {
return static_cast<FakeConnection*>(connection());
}
MockSecureContext* GetMockSecureContext() {
return static_cast<MockSecureContext*>(secure_context());
}
private:
DISALLOW_COPY_AND_ASSIGN(TestClient);
};
} // namespace
TEST(ProximityAuthClientTest, SupportsSignIn_ProtocolVersionThreeZero) {
TestClient client;
ON_CALL(*client.GetMockSecureContext(), GetProtocolVersion())
.WillByDefault(Return(SecureContext::PROTOCOL_VERSION_THREE_ZERO));
EXPECT_FALSE(client.SupportsSignIn());
}
TEST(ProximityAuthClientTest, SupportsSignIn_ProtocolVersionThreeOne) {
TestClient client;
ON_CALL(*client.GetMockSecureContext(), GetProtocolVersion())
.WillByDefault(Return(SecureContext::PROTOCOL_VERSION_THREE_ONE));
EXPECT_TRUE(client.SupportsSignIn());
}
TEST(ProximityAuthClientTest, OnConnectionStatusChanged_ConnectionDisconnects) {
TestClient client;
MockClientObserver observer(&client);
EXPECT_CALL(observer, OnDisconnected());
client.GetFakeConnection()->Disconnect();
}
TEST(ProximityAuthClientTest, DispatchUnlockEvent_SendsExpectedMessage) {
TestClient client;
client.DispatchUnlockEvent();
WireMessage* message = client.GetFakeConnection()->current_message();
ASSERT_TRUE(message);
EXPECT_EQ(std::string(), message->permit_id());
EXPECT_EQ(
"{"
"\"name\":\"easy_unlock\","
"\"type\":\"event\""
"}, but encoded",
message->payload());
}
TEST(ProximityAuthClientTest, DispatchUnlockEvent_SendMessageFails) {
TestClient client;
MockClientObserver observer(&client);
client.DispatchUnlockEvent();
EXPECT_CALL(observer, OnUnlockEventSent(false));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(false);
}
TEST(ProximityAuthClientTest, DispatchUnlockEvent_SendMessageSucceeds) {
TestClient client;
MockClientObserver observer(&client);
client.DispatchUnlockEvent();
EXPECT_CALL(observer, OnUnlockEventSent(true));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
}
TEST(ProximityAuthClientTest,
RequestDecryption_SignInUnsupported_DoesntSendMessage) {
TestClient client;
ON_CALL(*client.GetMockSecureContext(), GetProtocolVersion())
.WillByDefault(Return(SecureContext::PROTOCOL_VERSION_THREE_ZERO));
client.RequestDecryption(kChallenge);
EXPECT_FALSE(client.GetFakeConnection()->current_message());
}
TEST(ProximityAuthClientTest, RequestDecryption_SendsExpectedMessage) {
TestClient client;
client.RequestDecryption(kChallenge);
WireMessage* message = client.GetFakeConnection()->current_message();
ASSERT_TRUE(message);
EXPECT_EQ(std::string(), message->permit_id());
EXPECT_EQ(
"{"
"\"encrypted_data\":\"YSBtb3N0IGRpZmZpY3VsdCBjaGFsbGVuZ2U=\","
"\"type\":\"decrypt_request\""
"}, but encoded",
message->payload());
}
TEST(ProximityAuthClientTest,
RequestDecryption_SendsExpectedMessage_UsingBase64UrlEncoding) {
TestClient client;
client.RequestDecryption("\xFF\xE6");
WireMessage* message = client.GetFakeConnection()->current_message();
ASSERT_TRUE(message);
EXPECT_EQ(std::string(), message->permit_id());
EXPECT_EQ(
"{"
"\"encrypted_data\":\"_-Y=\","
"\"type\":\"decrypt_request\""
"}, but encoded",
message->payload());
}
TEST(ProximityAuthClientTest, RequestDecryption_SendMessageFails) {
TestClient client;
MockClientObserver observer(&client);
client.RequestDecryption(kChallenge);
EXPECT_CALL(observer, OnDecryptResponseProxy(nullptr));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(false);
}
TEST(ProximityAuthClientTest, RequestDecryption_SendSucceeds_WaitsForReply) {
TestClient client;
MockClientObserver observer(&client);
client.RequestDecryption(kChallenge);
EXPECT_CALL(observer, OnDecryptResponseProxy(_)).Times(0);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
}
TEST(ProximityAuthClientTest,
RequestDecryption_SendSucceeds_NotifiesObserversOnReply_NoData) {
TestClient client;
MockClientObserver observer(&client);
client.RequestDecryption(kChallenge);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
EXPECT_CALL(observer, OnDecryptResponseProxy(nullptr));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{\"type\":\"decrypt_response\"}, but encoded");
}
TEST(ProximityAuthClientTest,
RequestDecryption_SendSucceeds_NotifiesObserversOnReply_InvalidData) {
TestClient client;
MockClientObserver observer(&client);
client.RequestDecryption(kChallenge);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
EXPECT_CALL(observer, OnDecryptResponseProxy(nullptr));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{"
"\"type\":\"decrypt_response\","
"\"data\":\"not a base64-encoded string\""
"}, but encoded");
}
TEST(ProximityAuthClientTest,
RequestDecryption_SendSucceeds_NotifiesObserversOnReply_ValidData) {
TestClient client;
MockClientObserver observer(&client);
client.RequestDecryption(kChallenge);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
EXPECT_CALL(observer, OnDecryptResponseProxy(Pointee(Eq("a winner is you"))));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{"
"\"type\":\"decrypt_response\","
"\"data\":\"YSB3aW5uZXIgaXMgeW91\"" // "a winner is you", base64-encoded
"}, but encoded");
}
// Verify that the client correctly parses base64url encoded data.
TEST(ProximityAuthClientTest,
RequestDecryption_SendSucceeds_ParsesBase64UrlEncodingInReply) {
TestClient client;
MockClientObserver observer(&client);
client.RequestDecryption(kChallenge);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
EXPECT_CALL(observer, OnDecryptResponseProxy(Pointee(Eq("\xFF\xE6"))));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{"
"\"type\":\"decrypt_response\","
"\"data\":\"_-Y=\"" // "\0xFF\0xE6", base64url-encoded.
"}, but encoded");
}
TEST(ProximityAuthClientTest,
RequestUnlock_SignInUnsupported_DoesntSendMessage) {
TestClient client;
ON_CALL(*client.GetMockSecureContext(), GetProtocolVersion())
.WillByDefault(Return(SecureContext::PROTOCOL_VERSION_THREE_ZERO));
client.RequestUnlock();
EXPECT_FALSE(client.GetFakeConnection()->current_message());
}
TEST(ProximityAuthClientTest, RequestUnlock_SendsExpectedMessage) {
TestClient client;
client.RequestUnlock();
WireMessage* message = client.GetFakeConnection()->current_message();
ASSERT_TRUE(message);
EXPECT_EQ(std::string(), message->permit_id());
EXPECT_EQ("{\"type\":\"unlock_request\"}, but encoded", message->payload());
}
TEST(ProximityAuthClientTest, RequestUnlock_SendMessageFails) {
TestClient client;
MockClientObserver observer(&client);
client.RequestUnlock();
EXPECT_CALL(observer, OnUnlockResponse(false));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(false);
}
TEST(ProximityAuthClientTest, RequestUnlock_SendSucceeds_WaitsForReply) {
TestClient client;
MockClientObserver observer(&client);
client.RequestUnlock();
EXPECT_CALL(observer, OnUnlockResponse(_)).Times(0);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
}
TEST(ProximityAuthClientTest,
RequestUnlock_SendSucceeds_NotifiesObserversOnReply) {
TestClient client;
MockClientObserver observer(&client);
client.RequestUnlock();
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
EXPECT_CALL(observer, OnUnlockResponse(true));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{\"type\":\"unlock_response\"}, but encoded");
}
TEST(ProximityAuthClientTest, OnMessageReceived_RemoteStatusUpdate_Invalid) {
TestClient client;
MockClientObserver observer(&client);
// Receive a status update message that's missing all the data.
EXPECT_CALL(observer, OnRemoteStatusUpdate(_)).Times(0);
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{\"type\":\"status_update\"}, but encoded");
}
TEST(ProximityAuthClientTest, OnMessageReceived_RemoteStatusUpdate_Valid) {
TestClient client;
MockClientObserver observer(&client);
EXPECT_CALL(observer,
OnRemoteStatusUpdate(
AllOf(Field(&RemoteStatusUpdate::user_presence, USER_PRESENT),
Field(&RemoteStatusUpdate::secure_screen_lock_state,
SECURE_SCREEN_LOCK_ENABLED),
Field(&RemoteStatusUpdate::trust_agent_state,
TRUST_AGENT_UNSUPPORTED))));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{"
"\"type\":\"status_update\","
"\"user_presence\":\"present\","
"\"secure_screen_lock\":\"enabled\","
"\"trust_agent\":\"unsupported\""
"}, but encoded");
}
TEST(ProximityAuthClientTest, OnMessageReceived_InvalidJSON) {
TestClient client;
StrictMock<MockClientObserver> observer(&client);
client.RequestUnlock();
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
// The StrictMock will verify that no observer methods are called.
client.GetFakeConnection()->ReceiveMessageWithPayload(
"Not JSON, but encoded");
}
TEST(ProximityAuthClientTest, OnMessageReceived_MissingTypeField) {
TestClient client;
StrictMock<MockClientObserver> observer(&client);
client.RequestUnlock();
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
// The StrictMock will verify that no observer methods are called.
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{\"some key that's not 'type'\":\"some value\"}, but encoded");
}
TEST(ProximityAuthClientTest, OnMessageReceived_UnexpectedReply) {
TestClient client;
StrictMock<MockClientObserver> observer(&client);
// The StrictMock will verify that no observer methods are called.
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{\"type\":\"unlock_response\"}, but encoded");
}
TEST(ProximityAuthClientTest,
OnMessageReceived_MismatchedReply_UnlockInReplyToDecrypt) {
TestClient client;
StrictMock<MockClientObserver> observer(&client);
client.RequestDecryption(kChallenge);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
// The StrictMock will verify that no observer methods are called.
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{\"type\":\"unlock_response\"}, but encoded");
}
TEST(ProximityAuthClientTest,
OnMessageReceived_MismatchedReply_DecryptInReplyToUnlock) {
TestClient client;
StrictMock<MockClientObserver> observer(&client);
client.RequestUnlock();
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
// The StrictMock will verify that no observer methods are called.
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{"
"\"type\":\"decrypt_response\","
"\"data\":\"YSB3aW5uZXIgaXMgeW91\""
"}, but encoded");
}
TEST(ProximityAuthClientTest, BuffersMessages_WhileSending) {
TestClient client;
MockClientObserver observer(&client);
// Initiate a decryption request, and then initiate an unlock request before
// the decryption request is even finished sending.
client.RequestDecryption(kChallenge);
client.RequestUnlock();
EXPECT_CALL(observer, OnDecryptResponseProxy(nullptr));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(false);
EXPECT_CALL(observer, OnUnlockResponse(false));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(false);
}
TEST(ProximityAuthClientTest, BuffersMessages_WhileAwaitingReply) {
TestClient client;
MockClientObserver observer(&client);
// Initiate a decryption request, and allow the message to be sent.
client.RequestDecryption(kChallenge);
client.GetFakeConnection()->FinishSendingMessageWithSuccess(true);
// At this point, the client is awaiting a reply to the decryption message.
// While it's waiting, initiate an unlock request.
client.RequestUnlock();
// Now simulate a response arriving for the original decryption request.
EXPECT_CALL(observer, OnDecryptResponseProxy(Pointee(Eq("a winner is you"))));
client.GetFakeConnection()->ReceiveMessageWithPayload(
"{"
"\"type\":\"decrypt_response\","
"\"data\":\"YSB3aW5uZXIgaXMgeW91\""
"}, but encoded");
// The unlock request should have remained buffered, and should only now be
// sent.
EXPECT_CALL(observer, OnUnlockResponse(false));
client.GetFakeConnection()->FinishSendingMessageWithSuccess(false);
}
} // namespace proximity_auth
|