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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/ash/secure_channel/nearby_connection_broker_impl.h"
#include <memory>
#include <vector>
#include "ash/constants/ash_features.h"
#include "base/containers/span.h"
#include "base/files/file_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/timer/mock_timer.h"
#include "chrome/browser/ash/secure_channel/fake_nearby_endpoint_finder.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/services/nearby/public/cpp/mock_nearby_connections.h"
#include "chromeos/ash/services/secure_channel/public/mojom/nearby_connector.mojom-shared.h"
#include "chromeos/ash/services/secure_channel/public/mojom/nearby_connector.mojom.h"
#include "chromeos/ash/services/secure_channel/public/mojom/secure_channel_types.mojom.h"
#include "chromeos/ash/services/secure_channel/util/histogram_util.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/shared_remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace secure_channel {
namespace {
using ::nearby::connections::mojom::BytesPayload;
using ::nearby::connections::mojom::ConnectionInfo;
using ::nearby::connections::mojom::ConnectionLifecycleListener;
using ::nearby::connections::mojom::ConnectionOptionsPtr;
using ::nearby::connections::mojom::DiscoveredEndpointInfo;
using ::nearby::connections::mojom::FilePayload;
using ::nearby::connections::mojom::Payload;
using ::nearby::connections::mojom::PayloadContent;
using ::nearby::connections::mojom::PayloadListener;
using ::nearby::connections::mojom::PayloadPtr;
using ::nearby::connections::mojom::PayloadStatus;
using ::nearby::connections::mojom::PayloadTransferUpdate;
using ::nearby::connections::mojom::PayloadTransferUpdatePtr;
using ::nearby::connections::mojom::Status;
using ::testing::_;
using ::testing::Invoke;
const char kEndpointId[] = "endpointId";
const std::vector<uint8_t>& GetEid() {
static const std::vector<uint8_t> eid{0, 1};
return eid;
}
const std::vector<uint8_t>& GetBluetoothAddress() {
static const std::vector<uint8_t> address{0, 1, 2, 3, 4, 5};
return address;
}
const std::vector<uint8_t>& GetEndpointInfo() {
static const std::vector<uint8_t> info{6, 7, 8, 9, 10};
return info;
}
} // namespace
class NearbyConnectionBrokerImplTest
: public testing::Test,
public mojom::NearbyMessageReceiver,
public mojom::NearbyConnectionStateListener,
public mojom::FilePayloadListener {
protected:
NearbyConnectionBrokerImplTest() = default;
~NearbyConnectionBrokerImplTest() override = default;
// testing::Test:
void SetUp() override {
auto mock_timer = std::make_unique<base::MockOneShotTimer>();
mock_timer_ = mock_timer.get();
broker_ = NearbyConnectionBrokerImpl::Factory::Create(
GetBluetoothAddress(), GetEid(), &fake_endpoint_finder_,
message_sender_.BindNewPipeAndPassReceiver(),
file_payload_handler_.BindNewPipeAndPassReceiver(),
message_receiver_.BindNewPipeAndPassRemote(),
nearby_connection_state_listener_.BindNewPipeAndPassRemote(),
mock_nearby_connections_.shared_remote(),
base::BindOnce(&NearbyConnectionBrokerImplTest::OnConnected,
base::Unretained(this)),
base::BindOnce(&NearbyConnectionBrokerImplTest::OnDisconnected,
base::Unretained(this)),
std::move(mock_timer));
EXPECT_EQ(GetBluetoothAddress(),
fake_endpoint_finder_.remote_device_bluetooth_address());
}
void DiscoverEndpoint() {
base::RunLoop run_loop;
EXPECT_CALL(mock_nearby_connections_, RequestConnection(_, _, _, _, _, _))
.WillOnce(Invoke(
[&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) {
request_connection_callback_ = std::move(callback);
connection_lifecycle_listener_.Bind(std::move(listener));
run_loop.Quit();
}));
fake_endpoint_finder_.NotifyEndpointFound(
kEndpointId,
DiscoveredEndpointInfo::New(GetEndpointInfo(), mojom::kServiceId));
run_loop.Run();
}
void FailDiscovery() {
base::RunLoop run_loop;
on_disconnected_closure_ = run_loop.QuitClosure();
fake_endpoint_finder_.NotifyEndpointDiscoveryFailure(
::nearby::connections::mojom::Status::kAlreadyDiscovering);
run_loop.Run();
}
void InvokeRequestConnectionCallback(Status status) {
if (status != Status::kSuccess) {
base::RunLoop run_loop;
on_disconnected_closure_ = run_loop.QuitClosure();
std::move(request_connection_callback_).Run(status);
run_loop.Run();
return;
}
std::move(request_connection_callback_).Run(status);
// Ensure that callback result is received; cannot use external event
// because the success callback only updates internal state.
base::RunLoop().RunUntilIdle();
}
void NotifyConnectionInitiated() {
base::RunLoop run_loop;
EXPECT_CALL(mock_nearby_connections_, AcceptConnection(_, _, _, _))
.WillOnce(Invoke(
[&](const std::string& service_id, const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener,
NearbyConnectionsMojom::AcceptConnectionCallback callback) {
accept_connection_callback_ = std::move(callback);
payload_listener_.Bind(std::move(listener));
run_loop.Quit();
}));
connection_lifecycle_listener_->OnConnectionInitiated(
kEndpointId, ConnectionInfo::New());
run_loop.Run();
}
void InvokeAcceptConnectionCallback(Status status) {
if (status != Status::kSuccess) {
base::RunLoop run_loop;
ExpectDisconnectFromEndpoint(run_loop.QuitClosure());
std::move(accept_connection_callback_).Run(status);
run_loop.Run();
return;
}
std::move(accept_connection_callback_).Run(status);
// Ensure that callback result is received; cannot use external event
// because the success callback only updates internal state.
base::RunLoop().RunUntilIdle();
}
void NotifyConnectionAccepted() {
base::RunLoop run_loop;
on_connected_closure_ = run_loop.QuitClosure();
connection_lifecycle_listener_->OnConnectionAccepted(kEndpointId);
run_loop.Run();
}
void SetUpFullConnection() {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kSuccess);
NotifyConnectionInitiated();
InvokeAcceptConnectionCallback(Status::kSuccess);
NotifyConnectionAccepted();
}
void InvokeDisconnectedCallback() {
base::RunLoop disconnect_run_loop;
on_disconnected_closure_ = disconnect_run_loop.QuitClosure();
connection_lifecycle_listener_->OnDisconnected(kEndpointId);
disconnect_run_loop.Run();
}
void SendMessage(const std::string& message, bool success) {
base::RunLoop send_message_run_loop;
base::RunLoop send_message_response_run_loop;
NearbyConnectionsMojom::SendPayloadCallback send_payload_callback;
std::string sent_message;
EXPECT_CALL(mock_nearby_connections_, SendPayload(_, _, _, _))
.WillOnce(
Invoke([&](const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
PayloadPtr payload,
NearbyConnectionsMojom::SendPayloadCallback callback) {
send_payload_callback = std::move(callback);
const std::vector<uint8_t>& payload_bytes =
payload->content->get_bytes()->bytes;
sent_message =
std::string(payload_bytes.begin(), payload_bytes.end());
send_message_run_loop.Quit();
}));
message_sender_->SendMessage(
message, base::BindLambdaForTesting([&](bool did_send_succeeed) {
EXPECT_EQ(success, did_send_succeeed);
send_message_response_run_loop.Quit();
}));
send_message_run_loop.Run();
EXPECT_EQ(message, sent_message);
if (success) {
std::move(send_payload_callback).Run(Status::kSuccess);
send_message_response_run_loop.Run();
return;
}
// Failure to send should disconnect the ongoing connection.
base::RunLoop disconnect_run_loop;
ExpectDisconnectFromEndpoint(disconnect_run_loop.QuitClosure());
std::move(send_payload_callback).Run(Status::kError);
send_message_response_run_loop.Run();
disconnect_run_loop.Run();
}
void ReceiveMessage(const std::string& message) {
static int64_t next_payload_id = 0;
base::RunLoop receive_run_loop;
on_message_received_closure_ = receive_run_loop.QuitClosure();
std::vector<uint8_t> message_as_bytes(message.begin(), message.end());
payload_listener_->OnPayloadReceived(
kEndpointId, Payload::New(next_payload_id++,
PayloadContent::NewBytes(
BytesPayload::New(message_as_bytes))));
receive_run_loop.Run();
EXPECT_EQ(received_messages_.back(), message);
}
void ReceiveFilePayload(int64_t payload_id, const base::FilePath& file_path) {
// Create fake file to receive.
const std::vector<uint8_t> kFakeFileContent{0x01, 0x02, 0x03};
base::File output_file(file_path, base::File::Flags::FLAG_CREATE_ALWAYS |
base::File::Flags::FLAG_WRITE);
output_file.WriteAndCheck(
/*offset=*/0, base::span(kFakeFileContent));
output_file.Flush();
output_file.Close();
base::File input_file(
file_path, base::File::Flags::FLAG_OPEN | base::File::Flags::FLAG_READ);
payload_listener_->OnPayloadReceived(
kEndpointId,
Payload::New(payload_id, PayloadContent::NewFile(
FilePayload::New(std::move(input_file)))));
}
void RegisterPayloadFile(int64_t payload_id,
const base::FilePath& file_path,
bool expect_success) {
base::File input_file(
file_path, base::File::Flags::FLAG_OPEN | base::File::Flags::FLAG_READ);
base::File output_file(file_path, base::File::Flags::FLAG_CREATE_ALWAYS |
base::File::Flags::FLAG_WRITE);
base::RunLoop nearby_connections_run_loop;
base::RunLoop file_payload_handler_run_loop;
NearbyConnectionsMojom::RegisterPayloadFileCallback
register_payload_file_callback;
EXPECT_CALL(mock_nearby_connections_, RegisterPayloadFile(_, _, _, _, _))
.WillOnce(Invoke(
[&](const std::string& service_id, int64_t payload_id,
const base::File& input_file, const base::File& output_file,
NearbyConnectionsMojom::RegisterPayloadFileCallback callback) {
register_payload_file_callback = std::move(callback);
nearby_connections_run_loop.Quit();
}));
file_payload_handler_->RegisterPayloadFile(
payload_id,
mojom::PayloadFiles::New(std::move(input_file), std::move(output_file)),
file_payload_listener_.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) {
EXPECT_EQ(expect_success, success);
file_payload_handler_run_loop.Quit();
}));
nearby_connections_run_loop.Run();
std::move(register_payload_file_callback)
.Run(expect_success ? Status::kSuccess : Status::kError);
file_payload_handler_run_loop.Run();
}
void ReceiveFileTransferUpdate(PayloadTransferUpdatePtr update) {
payload_listener_->OnPayloadTransferUpdate(kEndpointId, std::move(update));
payload_listener_.FlushForTesting();
}
void DisconnectMojoBindings(bool expected_to_disconnect) {
if (!expected_to_disconnect) {
base::RunLoop disconnect_run_loop;
on_disconnected_closure_ = disconnect_run_loop.QuitClosure();
EXPECT_CALL(mock_nearby_connections_, DisconnectFromEndpoint(_, _, _))
.Times(0);
message_sender_.reset();
file_payload_handler_.reset();
disconnect_run_loop.Run();
return;
}
base::RunLoop disconnect_from_endpoint_run_loop;
ExpectDisconnectFromEndpoint(
disconnect_from_endpoint_run_loop.QuitClosure());
message_sender_.reset();
file_payload_handler_.reset();
disconnect_from_endpoint_run_loop.Run();
}
void InvokeDisconnectedFromEndpointCallback(bool success) {
if (success) {
std::move(disconnect_from_endpoint_callback_).Run(Status::kSuccess);
// Ensure that callback result is received; cannot use external event
// because the success callback only updates internal state.
base::RunLoop().RunUntilIdle();
return;
}
base::RunLoop disconnect_run_loop;
on_disconnected_closure_ = disconnect_run_loop.QuitClosure();
std::move(disconnect_from_endpoint_callback_).Run(Status::kError);
disconnect_run_loop.Run();
}
void SimulateTimeout(bool expected_to_disconnect) {
if (!expected_to_disconnect) {
base::RunLoop disconnect_run_loop;
on_disconnected_closure_ = disconnect_run_loop.QuitClosure();
EXPECT_CALL(mock_nearby_connections_, DisconnectFromEndpoint(_, _, _))
.Times(0);
mock_timer_->Fire();
disconnect_run_loop.Run();
return;
}
base::RunLoop disconnect_from_endpoint_run_loop;
ExpectDisconnectFromEndpoint(
disconnect_from_endpoint_run_loop.QuitClosure());
mock_timer_->Fire();
disconnect_from_endpoint_run_loop.Run();
}
void ExpectDisconnectFromEndpoint(
base::OnceClosure on_disconnect_from_endpoint_closure) {
on_disconnect_from_endpoint_closure_ =
std::move(on_disconnect_from_endpoint_closure);
EXPECT_CALL(mock_nearby_connections_, DisconnectFromEndpoint(_, _, _))
.WillOnce(Invoke(
[&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback
callback) {
disconnect_from_endpoint_callback_ = std::move(callback);
std::move(on_disconnect_from_endpoint_closure_).Run();
}));
}
bool IsTimerRunning() const { return mock_timer_->IsRunning(); }
mojo::Receiver<mojom::FilePayloadListener>& file_payload_listener() {
return file_payload_listener_;
}
const std::vector<mojom::FileTransferUpdatePtr>& file_transfer_updates() {
return file_transfer_updates_;
}
NearbyConnectionsMojom::RequestConnectionCallback
request_connection_callback_;
NearbyConnectionsMojom::AcceptConnectionCallback accept_connection_callback_;
NearbyConnectionsMojom::DisconnectFromEndpointCallback
disconnect_from_endpoint_callback_;
base::HistogramTester histogram_tester_;
private:
// mojom::NearbyMessageReceiver:
void OnMessageReceived(const std::string& message) override {
received_messages_.push_back(message);
std::move(on_message_received_closure_).Run();
}
// mojom::NearbyConnectionStateListener:
void OnNearbyConnectionStateChanged(
mojom::NearbyConnectionStep nearby_connection_step,
mojom::NearbyConnectionStepResult result) override {
nearby_connection_step_ = nearby_connection_step;
nearby_connection_step_result_ = result;
}
// mojom::FilePayloadListener:
void OnFileTransferUpdate(mojom::FileTransferUpdatePtr update) override {
file_transfer_updates_.push_back(std::move(update));
}
void OnConnected() { std::move(on_connected_closure_).Run(); }
void OnDisconnected() { std::move(on_disconnected_closure_).Run(); }
base::test::TaskEnvironment task_environment_;
nearby::MockNearbyConnections mock_nearby_connections_;
FakeNearbyEndpointFinder fake_endpoint_finder_;
mojo::Remote<mojom::NearbyMessageSender> message_sender_;
mojo::Remote<mojom::NearbyFilePayloadHandler> file_payload_handler_;
mojo::Receiver<mojom::NearbyMessageReceiver> message_receiver_{this};
mojo::Receiver<mojom::NearbyConnectionStateListener>
nearby_connection_state_listener_{this};
mojo::Receiver<mojom::FilePayloadListener> file_payload_listener_{this};
std::unique_ptr<NearbyConnectionBroker> broker_;
raw_ptr<base::MockOneShotTimer> mock_timer_ = nullptr;
base::OnceClosure on_connected_closure_;
base::OnceClosure on_disconnected_closure_;
base::OnceClosure on_message_received_closure_;
base::OnceClosure on_disconnect_from_endpoint_closure_;
mojo::Remote<ConnectionLifecycleListener> connection_lifecycle_listener_;
mojo::Remote<PayloadListener> payload_listener_;
std::vector<std::string> received_messages_;
std::vector<mojom::FileTransferUpdatePtr> file_transfer_updates_;
mojom::NearbyConnectionStep nearby_connection_step_;
mojom::NearbyConnectionStepResult nearby_connection_step_result_;
};
TEST_F(NearbyConnectionBrokerImplTest, SendAndReceive) {
SetUpFullConnection();
SendMessage("test1", /*success=*/true);
SendMessage("test2", /*success=*/true);
ReceiveMessage("test3");
ReceiveMessage("test4");
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest, FailToDisconnect) {
SetUpFullConnection();
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/false);
}
TEST_F(NearbyConnectionBrokerImplTest, FailToDisconnect_Timeout) {
SetUpFullConnection();
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
SimulateTimeout(/*expected_to_disconnect=*/false);
}
TEST_F(NearbyConnectionBrokerImplTest, DisconnectsUnexpectedly) {
SetUpFullConnection();
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest,
DisconnectAfterReceivingFilePayloadWhenFeatureUnsupported) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
base::RunLoop disconnect_from_endpoint_run_loop;
ExpectDisconnectFromEndpoint(disconnect_from_endpoint_run_loop.QuitClosure());
base::FilePath path;
base::CreateTemporaryFile(&path);
ReceiveFilePayload(/*payload_id=*/1234, path);
disconnect_from_endpoint_run_loop.Run();
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest,
DisconnectAfterReceivingUnregisteredFilePayload) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
base::RunLoop disconnect_from_endpoint_run_loop;
ExpectDisconnectFromEndpoint(disconnect_from_endpoint_run_loop.QuitClosure());
base::FilePath path;
base::CreateTemporaryFile(&path);
RegisterPayloadFile(/*payload_id=*/1234, path, /*expect_success=*/false);
ReceiveFilePayload(/*payload_id=*/1234, path);
disconnect_from_endpoint_run_loop.Run();
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.OperationResult.RegisterPayloadFiles",
Status::kError,
/*expected_count=*/1);
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.FileAction",
util::FileAction::kUnexpectedFileReceived,
/*expected_count=*/1);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest, FileTransferUpdateForRegisteredPayload) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
int64_t payload_id = 1234;
base::FilePath path;
base::CreateTemporaryFile(&path);
RegisterPayloadFile(payload_id, path, /*expect_success=*/true);
ReceiveFilePayload(payload_id, path);
ReceiveFileTransferUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
ReceiveFileTransferUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kSuccess,
/*total_bytes=*/1000,
/*bytes_transferred=*/1000));
file_payload_listener().FlushForTesting();
EXPECT_EQ(2u, file_transfer_updates().size());
EXPECT_EQ(file_transfer_updates().at(0),
mojom::FileTransferUpdate::New(
payload_id, mojom::FileTransferStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
EXPECT_EQ(file_transfer_updates().at(1),
mojom::FileTransferUpdate::New(payload_id,
mojom::FileTransferStatus::kSuccess,
/*total_bytes=*/1000,
/*bytes_transferred=*/1000));
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.OperationResult.RegisterPayloadFiles",
Status::kSuccess,
/*expected_count=*/1);
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.FileAction",
util::FileAction::kRegisteredFileReceived,
/*expected_count=*/1);
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.FileTransferResult",
util::FileTransferResult::kFileTransferSuccess,
/*expected_count=*/1);
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest, FileTransferUpdateForCompletedPayload) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
int64_t payload_id = 1234;
base::FilePath path;
base::CreateTemporaryFile(&path);
RegisterPayloadFile(payload_id, path, /*expect_success=*/true);
ReceiveFilePayload(payload_id, path);
ReceiveFileTransferUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kFailure,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
// This is not supposed to trigger a FileTransferUpdate callback as this
// payload has already been completed and is now untracked.
ReceiveFileTransferUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/200));
file_payload_listener().FlushForTesting();
EXPECT_EQ(1u, file_transfer_updates().size());
EXPECT_EQ(file_transfer_updates().at(0),
mojom::FileTransferUpdate::New(payload_id,
mojom::FileTransferStatus::kFailure,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.FileTransferResult",
util::FileTransferResult::kFileTransferFailure,
/*expected_count=*/1);
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest,
FileTransferUpdateForUnregisteredPayload) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
base::FilePath path;
base::CreateTemporaryFile(&path);
RegisterPayloadFile(/*payload_id=*/1234, path, /*expect_success=*/true);
ReceiveFileTransferUpdate(PayloadTransferUpdate::New(
/*payload_id=*/5678, PayloadStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
file_payload_listener().FlushForTesting();
EXPECT_TRUE(file_transfer_updates().empty());
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest, FileTransferCanceledOnDisconnect) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
int64_t payload_id = 1234;
base::FilePath path;
base::CreateTemporaryFile(&path);
RegisterPayloadFile(payload_id, path, /*expect_success=*/true);
ReceiveFilePayload(payload_id, path);
ReceiveFileTransferUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
// Disconnect before the transfer is complete.
InvokeDisconnectedCallback();
file_payload_listener().FlushForTesting();
EXPECT_EQ(2u, file_transfer_updates().size());
EXPECT_EQ(file_transfer_updates().at(0),
mojom::FileTransferUpdate::New(
payload_id, mojom::FileTransferStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
EXPECT_EQ(file_transfer_updates().at(1),
mojom::FileTransferUpdate::New(payload_id,
mojom::FileTransferStatus::kCanceled,
/*total_bytes=*/0,
/*bytes_transferred=*/0));
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.FileTransferResult",
util::FileTransferResult::kFileTransferCanceled,
/*expected_count=*/1);
}
TEST_F(NearbyConnectionBrokerImplTest, FileTransferCanceledOnMojoDisconnect) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kPhoneHubCameraRoll);
SetUpFullConnection();
int64_t payload_id = 1234;
base::FilePath path;
base::CreateTemporaryFile(&path);
RegisterPayloadFile(payload_id, path, /*expect_success=*/true);
ReceiveFilePayload(payload_id, path);
ReceiveFileTransferUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
// Disconnect before the transfer is complete.
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
file_payload_listener().FlushForTesting();
EXPECT_EQ(2u, file_transfer_updates().size());
EXPECT_EQ(file_transfer_updates().at(0),
mojom::FileTransferUpdate::New(
payload_id, mojom::FileTransferStatus::kInProgress,
/*total_bytes=*/1000,
/*bytes_transferred=*/100));
EXPECT_EQ(file_transfer_updates().at(1),
mojom::FileTransferUpdate::New(payload_id,
mojom::FileTransferStatus::kCanceled,
/*total_bytes=*/0,
/*bytes_transferred=*/0));
histogram_tester_.ExpectBucketCount(
"MultiDevice.SecureChannel.Nearby.FileTransferResult",
util::FileTransferResult::kFileTransferCanceled,
/*expected_count=*/1);
}
TEST_F(NearbyConnectionBrokerImplTest, FailToSend) {
SetUpFullConnection();
SendMessage("test", /*success=*/false);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest, FailDiscovery) {
FailDiscovery();
}
TEST_F(NearbyConnectionBrokerImplTest, FailDiscovery_Timeout) {
SimulateTimeout(/*expected_to_disconnect=*/false);
}
TEST_F(NearbyConnectionBrokerImplTest, MojoDisconnectionBeforeDiscovery) {
DisconnectMojoBindings(/*expected_to_disconnect=*/false);
}
TEST_F(NearbyConnectionBrokerImplTest, MojoDisconnectionAfterDiscovery) {
DiscoverEndpoint();
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
// Run callback to prevent DCHECK() crash that ensures all Mojo callbacks are
// invoked.
std::move(request_connection_callback_).Run(Status::kError);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_AlreadyConnectedToEndpoint) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kAlreadyConnectedToEndpoint);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_PayloadUnknown) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kPayloadUnknown);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_NotConnectedToEndpoint) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kNotConnectedToEndpoint);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_AlreadyAdvertising) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kAlreadyAdvertising);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_AlreadyHaveActiveStrategy) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kAlreadyHaveActiveStrategy);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_AlreadyListening) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kAlreadyListening);
}
TEST_F(NearbyConnectionBrokerImplTest, FailRequestingConnection_Unknown) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kUnknown);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_EndpointIOError) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kEndpointIOError);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_EndpointUnknown) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kEndpointUnknown);
}
TEST_F(NearbyConnectionBrokerImplTest, FailRequestingConnection_BleError) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kBleError);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_BluetoothError) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kBluetoothError);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_OutOfOrderApiCall) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kOutOfOrderApiCall);
}
TEST_F(NearbyConnectionBrokerImplTest, FailRequestingConnection_WifiLanError) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kWifiLanError);
}
TEST_F(NearbyConnectionBrokerImplTest, FailRequestingConnection_Reset) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kReset);
}
TEST_F(NearbyConnectionBrokerImplTest,
FailRequestingConnection_NearbyConnectionTimeout) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kTimeout);
}
TEST_F(NearbyConnectionBrokerImplTest, FailRequestingConnection_Timeout) {
DiscoverEndpoint();
SimulateTimeout(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
TEST_F(NearbyConnectionBrokerImplTest,
MojoDisconnectionAfterRequestConnection) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kSuccess);
NotifyConnectionInitiated();
DisconnectMojoBindings(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
// Run callback to prevent DCHECK() crash that ensures all Mojo callbacks are
// invoked.
std::move(accept_connection_callback_).Run(Status::kError);
}
TEST_F(NearbyConnectionBrokerImplTest, FailAcceptingConnection) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kSuccess);
NotifyConnectionInitiated();
InvokeAcceptConnectionCallback(Status::kConnectionRejected);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
// Regression test for https://crbug.com/1175489.
TEST_F(NearbyConnectionBrokerImplTest, OnAcceptedBeforeAcceptCallback) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kSuccess);
NotifyConnectionInitiated();
// Invoke OnConnectionAccepted() callback before the AcceptConnection()
// call returns a value. Generally we expect AcceptConnection() to return
// before OnConnectionAccepted(), but we've seen in practice that this is
// sometimes not the case.
NotifyConnectionAccepted();
InvokeAcceptConnectionCallback(Status::kSuccess);
// The connection is now considered complete, so there should be no further
// timeout.
EXPECT_FALSE(IsTimerRunning());
}
TEST_F(NearbyConnectionBrokerImplTest, FailAcceptingConnection_Timeout) {
DiscoverEndpoint();
InvokeRequestConnectionCallback(Status::kSuccess);
NotifyConnectionInitiated();
SimulateTimeout(/*expected_to_disconnect=*/true);
InvokeDisconnectedFromEndpointCallback(/*success=*/true);
InvokeDisconnectedCallback();
}
} // namespace secure_channel
} // namespace ash
|