| // Copyright 2026 The Fuchsia 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 "src/developer/adb/testing/client/adb_client.h" | |
| #include <fidl/fuchsia.hardware.usb.endpoint/cpp/fidl.h> | |
| #include <fidl/fuchsia.hardware.usb.request/cpp/fidl.h> | |
| #include <fidl/fuchsia.testing.adb/cpp/fidl.h> | |
| #include <lib/fidl/cpp/channel.h> | |
| #include <lib/zx/vmo.h> | |
| #include <cstdint> | |
| #include <cstring> | |
| #include <deque> | |
| #include <optional> | |
| #include <span> | |
| #include <string> | |
| #include <string_view> | |
| #include <vector> | |
| #include <gtest/gtest.h> | |
| #include "src/developer/adb/third_party/adb/adb-protocol.h" | |
| #include "src/lib/testing/loop_fixture/real_loop_fixture.h" | |
| namespace { | |
| namespace fendpoint = fuchsia_hardware_usb_endpoint; | |
| namespace frequest = fuchsia_hardware_usb_request; | |
| std::vector<uint8_t> MakeHeader(uint32_t command, uint32_t arg0, uint32_t arg1, | |
| uint32_t data_length) { | |
| amessage msg{}; | |
| msg.command = command; | |
| msg.arg0 = arg0; | |
| msg.arg1 = arg1; | |
| msg.data_length = data_length; | |
| msg.data_check = 0; | |
| msg.magic = command ^ 0xffffffffu; | |
| std::vector<uint8_t> out(sizeof(msg)); | |
| memcpy(out.data(), &msg, sizeof(msg)); | |
| return out; | |
| } | |
| std::vector<uint8_t> ToBytes(std::string_view text) { | |
| return std::vector<uint8_t>(text.begin(), text.end()); | |
| } | |
| class FakeEndpoint : public fidl::Server<fendpoint::Endpoint> { | |
| public: | |
| void Bind(async_dispatcher_t* dispatcher, fidl::ServerEnd<fendpoint::Endpoint> server_end) { | |
| binding_.emplace(dispatcher, std::move(server_end), this, fidl::kIgnoreBindingClosure); | |
| } | |
| void Close() { binding_.reset(); } | |
| void GetInfo(GetInfoCompleter::Sync& completer) override { | |
| completer.Reply(fit::as_error(ZX_ERR_NOT_SUPPORTED)); | |
| } | |
| void GetAlignmentInfo(GetAlignmentInfoCompleter::Sync& completer) override { | |
| completer.Reply(fit::as_error(ZX_ERR_NOT_SUPPORTED)); | |
| } | |
| void RegisterVmos(RegisterVmosRequest& request, RegisterVmosCompleter::Sync& completer) override { | |
| completer.Reply({{}}); | |
| } | |
| void UnregisterVmos(UnregisterVmosRequest& request, | |
| UnregisterVmosCompleter::Sync& completer) override { | |
| completer.Reply({{}, {}}); | |
| } | |
| void QueueRequests(QueueRequestsRequest& request, | |
| QueueRequestsCompleter::Sync& completer) override { | |
| for (auto& req : request.req()) { | |
| queued_.push_back(std::move(req)); | |
| } | |
| } | |
| void CancelAll(CancelAllCompleter::Sync& completer) override { completer.Reply(fit::ok()); } | |
| void CompleteBatch(std::vector<fendpoint::Completion> completions) { | |
| ASSERT_TRUE(binding_.has_value()); | |
| ASSERT_TRUE(fidl::SendEvent(*binding_)->OnCompletion(std::move(completions)).is_ok()); | |
| } | |
| frequest::Request PopRequest() { | |
| EXPECT_FALSE(queued_.empty()); | |
| frequest::Request req = std::move(queued_.front()); | |
| queued_.pop_front(); | |
| return req; | |
| } | |
| bool empty() const { return queued_.empty(); } | |
| size_t queued_count() const { return queued_.size(); } | |
| private: | |
| std::optional<fidl::ServerBinding<fendpoint::Endpoint>> binding_; | |
| std::deque<frequest::Request> queued_; | |
| }; | |
| class AdbClientTest : public gtest::RealLoopFixture { | |
| protected: | |
| static constexpr size_t kVmoSize = 16384; | |
| void SetUp() override { | |
| auto client_endpoints = fidl::Endpoints<fuchsia_testing_adb::Client>::Create(); | |
| auto in_endpoints = fidl::Endpoints<fendpoint::Endpoint>::Create(); | |
| auto out_endpoints = fidl::Endpoints<fendpoint::Endpoint>::Create(); | |
| ASSERT_EQ(zx::vmo::create(kVmoSize, 0, &bulk_in_vmo_), ZX_OK); | |
| zx::vmo client_vmo; | |
| ASSERT_EQ(bulk_in_vmo_.duplicate(ZX_RIGHT_SAME_RIGHTS, &client_vmo), ZX_OK); | |
| ASSERT_EQ(zx::vmo::create(kVmoSize, 0, &bulk_out_vmo_), ZX_OK); | |
| zx::vmo client_write_vmo; | |
| ASSERT_EQ(bulk_out_vmo_.duplicate(ZX_RIGHT_SAME_RIGHTS, &client_write_vmo), ZX_OK); | |
| bulk_in_server_.Bind(dispatcher(), std::move(in_endpoints.server)); | |
| bulk_out_server_.Bind(dispatcher(), std::move(out_endpoints.server)); | |
| impl_ = std::make_unique<AdbClientImpl>(dispatcher()); | |
| impl_->SetEndpointsForTesting(std::move(in_endpoints.client), std::move(out_endpoints.client), | |
| std::move(client_vmo), std::move(client_write_vmo)); | |
| impl_binding_.emplace(dispatcher(), std::move(client_endpoints.server), impl_.get(), | |
| fidl::kIgnoreBindingClosure); | |
| client_.Bind(std::move(client_endpoints.client), dispatcher()); | |
| } | |
| void CompleteNextReadWithVmo(std::span<const uint8_t> bytes, size_t expected_read_size) { | |
| RunLoopUntil([&]() { return !bulk_in_server_.empty(); }); | |
| frequest::Request req = bulk_in_server_.PopRequest(); | |
| ASSERT_TRUE(req.data().has_value()); | |
| ASSERT_EQ(req.data()->size(), 1u); | |
| const auto& region = (*req.data())[0]; | |
| ASSERT_TRUE(region.buffer().has_value()); | |
| ASSERT_TRUE(region.buffer()->vmo_id().has_value()); | |
| EXPECT_EQ(region.size(), expected_read_size); | |
| ASSERT_LE(bytes.size(), kVmoSize); | |
| if (!bytes.empty()) { | |
| ASSERT_EQ(bulk_in_vmo_.write(bytes.data(), 0, bytes.size()), ZX_OK); | |
| } | |
| std::vector<fendpoint::Completion> completions; | |
| completions.emplace_back(std::move( | |
| fendpoint::Completion().request(std::move(req)).status(ZX_OK).transfer_size(bytes.size()))); | |
| bulk_in_server_.CompleteBatch(std::move(completions)); | |
| } | |
| amessage PopOutPacketHeader(std::string* out_payload = nullptr, bool* out_short = nullptr) { | |
| RunLoopUntil([&]() { return !bulk_out_server_.empty(); }); | |
| frequest::Request req = bulk_out_server_.PopRequest(); | |
| if (out_short != nullptr) { | |
| *out_short = req.short_().value_or(false); | |
| } | |
| EXPECT_TRUE(req.data().has_value()); | |
| EXPECT_EQ(req.data()->size(), 1u); | |
| const auto& region = (*req.data())[0]; | |
| EXPECT_TRUE(region.buffer().has_value()); | |
| EXPECT_TRUE(region.buffer()->vmo_id().has_value()); | |
| const uint64_t offset = region.offset().value_or(0); | |
| const uint64_t size = region.size().value_or(0); | |
| EXPECT_GE(size, sizeof(amessage)); | |
| EXPECT_LE(offset + sizeof(amessage), kVmoSize); | |
| amessage msg{}; | |
| EXPECT_EQ(bulk_out_vmo_.read(&msg, offset, sizeof(msg)), ZX_OK); | |
| EXPECT_EQ(size, sizeof(amessage) + msg.data_length); | |
| if (size > sizeof(amessage) && offset <= kVmoSize && size <= kVmoSize - offset) { | |
| std::vector<uint8_t> payload(size - sizeof(amessage)); | |
| EXPECT_EQ(bulk_out_vmo_.read(payload.data(), offset + sizeof(amessage), payload.size()), | |
| ZX_OK); | |
| uint32_t checksum = 0; | |
| for (uint8_t b : payload) { | |
| checksum += b; | |
| } | |
| EXPECT_EQ(msg.data_check, checksum); | |
| if (out_payload != nullptr) { | |
| out_payload->assign(reinterpret_cast<const char*>(payload.data()), payload.size()); | |
| } | |
| } else if (out_payload != nullptr) { | |
| out_payload->clear(); | |
| } | |
| std::vector<fendpoint::Completion> completions; | |
| completions.emplace_back(std::move( | |
| fendpoint::Completion().request(std::move(req)).status(ZX_OK).transfer_size(size))); | |
| bulk_out_server_.CompleteBatch(std::move(completions)); | |
| return msg; | |
| } | |
| std::string PerformHandshake(const std::string& banner = "device::", | |
| uint32_t max_payload = MAX_PAYLOAD) { | |
| bool connected = false; | |
| std::string received_banner; | |
| client_->Connect().ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_ok()); | |
| if (result.is_ok()) { | |
| received_banner = result->banner(); | |
| } | |
| connected = true; | |
| }); | |
| std::string cnxn_payload; | |
| amessage cnxn = PopOutPacketHeader(&cnxn_payload); | |
| EXPECT_EQ(cnxn.command, static_cast<uint32_t>(A_CNXN)); | |
| EXPECT_EQ(cnxn_payload, std::string("host::\0", 7)); | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_CNXN, A_VERSION, max_payload, static_cast<uint32_t>(banner.size())), kVmoSize); | |
| if (!banner.empty()) { | |
| CompleteNextReadWithVmo(ToBytes(banner), banner.size()); | |
| } | |
| RunLoopUntil([&]() { return connected; }); | |
| EXPECT_EQ(received_banner, banner); | |
| return received_banner; | |
| } | |
| FakeEndpoint bulk_in_server_; | |
| FakeEndpoint bulk_out_server_; | |
| zx::vmo bulk_in_vmo_; | |
| zx::vmo bulk_out_vmo_; | |
| std::unique_ptr<AdbClientImpl> impl_; | |
| std::optional<fidl::ServerBinding<fuchsia_testing_adb::Client>> impl_binding_; | |
| fidl::Client<fuchsia_testing_adb::Client> client_; | |
| }; | |
| TEST_F(AdbClientTest, MultiPacketConnectAndPacketAlignedWrite) { | |
| PerformHandshake("device::ro.product.name=test;"); | |
| bool done = false; | |
| std::string output; | |
| client_->ExecuteCommand({"echo test"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_ok()); | |
| if (result.is_ok()) { | |
| output = result->output(); | |
| } | |
| done = true; | |
| }); | |
| std::string open_payload; | |
| amessage open_msg = PopOutPacketHeader(&open_payload); | |
| EXPECT_EQ(open_msg.command, static_cast<uint32_t>(A_OPEN)); | |
| EXPECT_EQ(open_payload, std::string("shell:echo test\0", 16)); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 7; | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| // Send a 4096-byte (512-byte aligned) WRTE payload across separate header and payload reads. | |
| // QueueReadRequest must request exactly 4096 bytes so the transfer completes without a ZLP. | |
| constexpr size_t kAlignedPayloadSize = 4096; | |
| std::vector<uint8_t> payload(kAlignedPayloadSize, 'A'); | |
| CompleteNextReadWithVmo(MakeHeader(A_WRTE, kRemoteId, local_id, kAlignedPayloadSize), kVmoSize); | |
| CompleteNextReadWithVmo(payload, kAlignedPayloadSize); | |
| amessage ack = PopOutPacketHeader(); | |
| EXPECT_EQ(ack.command, static_cast<uint32_t>(A_OKAY)); | |
| EXPECT_EQ(ack.arg0, local_id); | |
| EXPECT_EQ(ack.arg1, kRemoteId); | |
| EXPECT_EQ(ack.data_length, 0u); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return done; }); | |
| EXPECT_EQ(output, std::string(kAlignedPayloadSize, 'A')); | |
| } | |
| TEST_F(AdbClientTest, MultiChunkWriteExceeding16KiB) { | |
| PerformHandshake(); | |
| bool done = false; | |
| std::string output; | |
| client_->ExecuteCommand({"big"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_ok()); | |
| if (result.is_ok()) { | |
| output = result->output(); | |
| } | |
| done = true; | |
| }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 9; | |
| constexpr size_t kTotalSize = 20480; | |
| CompleteNextReadWithVmo(MakeHeader(A_WRTE, kRemoteId, local_id, kTotalSize), kVmoSize); | |
| CompleteNextReadWithVmo(std::vector<uint8_t>(16384, 'B'), 16384); | |
| CompleteNextReadWithVmo(std::vector<uint8_t>(4096, 'C'), 4096); | |
| amessage ack = PopOutPacketHeader(); | |
| EXPECT_EQ(ack.command, static_cast<uint32_t>(A_OKAY)); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return done; }); | |
| EXPECT_EQ(output, std::string(16384, 'B') + std::string(4096, 'C')); | |
| } | |
| TEST_F(AdbClientTest, FailedOkayResetsSessionAndFailsCommand) { | |
| PerformHandshake(); | |
| bool done = false; | |
| zx_status_t err_status = ZX_OK; | |
| client_->ExecuteCommand({"fail-ack"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| err_status = result.error_value().domain_error(); | |
| } | |
| done = true; | |
| }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 3; | |
| // Unbind bulk OUT before the WRTE arrives so SendOkay() fails synchronously in OnCompletion. | |
| impl_->UnbindBulkOutForTesting(); | |
| std::vector<uint8_t> pkt = MakeHeader(A_WRTE, kRemoteId, local_id, 4); | |
| pkt.insert(pkt.end(), {'t', 'e', 's', 't'}); | |
| CompleteNextReadWithVmo(pkt, kVmoSize); | |
| RunLoopUntil([&]() { return done; }); | |
| EXPECT_EQ(err_status, ZX_ERR_BAD_STATE); | |
| RunLoopUntilIdle(); | |
| EXPECT_TRUE(bulk_in_server_.empty()); | |
| // Handshake state was reset, so a subsequent ExecuteCommand is rejected with ZX_ERR_BAD_STATE. | |
| bool second_done = false; | |
| client_->ExecuteCommand({"after-error"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| EXPECT_EQ(result.error_value().domain_error(), ZX_ERR_BAD_STATE); | |
| } | |
| second_done = true; | |
| }); | |
| RunLoopUntil([&]() { return second_done; }); | |
| } | |
| TEST_F(AdbClientTest, BulkOutPeerClosedResetsSessionAndFailsCommand) { | |
| PerformHandshake(); | |
| bool done = false; | |
| zx_status_t err_status = ZX_OK; | |
| client_->ExecuteCommand({"peer-closed"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| err_status = result.error_value().domain_error(); | |
| } | |
| done = true; | |
| }); | |
| (void)PopOutPacketHeader(); | |
| bulk_out_server_.Close(); | |
| RunLoopUntil([&]() { return done; }); | |
| EXPECT_EQ(err_status, ZX_ERR_PEER_CLOSED); | |
| bool second_done = false; | |
| client_->ExecuteCommand({"after-close"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| EXPECT_EQ(result.error_value().domain_error(), ZX_ERR_BAD_STATE); | |
| } | |
| second_done = true; | |
| }); | |
| RunLoopUntil([&]() { return second_done; }); | |
| } | |
| TEST_F(AdbClientTest, BulkOutCompletionErrorResetsSessionAndFailsCommand) { | |
| PerformHandshake(); | |
| bool done = false; | |
| zx_status_t err_status = ZX_OK; | |
| client_->ExecuteCommand({"out-io-error"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| err_status = result.error_value().domain_error(); | |
| } | |
| done = true; | |
| }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 4; | |
| std::vector<uint8_t> pkt = MakeHeader(A_WRTE, kRemoteId, local_id, 4); | |
| pkt.insert(pkt.end(), {'t', 'e', 's', 't'}); | |
| CompleteNextReadWithVmo(pkt, kVmoSize); | |
| RunLoopUntil([&]() { return !bulk_out_server_.empty(); }); | |
| frequest::Request ack_req = bulk_out_server_.PopRequest(); | |
| std::vector<fendpoint::Completion> completions; | |
| completions.emplace_back(std::move( | |
| fendpoint::Completion().request(std::move(ack_req)).status(ZX_ERR_IO).transfer_size(0))); | |
| bulk_out_server_.CompleteBatch(std::move(completions)); | |
| RunLoopUntil([&]() { return done; }); | |
| EXPECT_EQ(err_status, ZX_ERR_IO); | |
| bool second_done = false; | |
| client_->ExecuteCommand({"after-out-error"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| EXPECT_EQ(result.error_value().domain_error(), ZX_ERR_BAD_STATE); | |
| } | |
| second_done = true; | |
| }); | |
| RunLoopUntil([&]() { return second_done; }); | |
| // Verify the failed bulk OUT request was returned to write_reqs_ before returning early. | |
| PerformHandshake(); | |
| } | |
| TEST_F(AdbClientTest, BulkOutPoolExhaustionReturnsShouldWaitAndRecovers) { | |
| PerformHandshake(); | |
| bool done = false; | |
| zx_status_t err_status = ZX_OK; | |
| client_->ExecuteCommand({"exhaust"}).Then([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| err_status = result.error_value().domain_error(); | |
| } | |
| done = true; | |
| }); | |
| // Hold the single A_OPEN write request in flight on bulk_out_server_ so write_reqs_ is empty. | |
| RunLoopUntil([&]() { return !bulk_out_server_.empty(); }); | |
| frequest::Request open_req = bulk_out_server_.PopRequest(); | |
| ASSERT_TRUE(open_req.data().has_value()); | |
| ASSERT_EQ(open_req.data()->size(), 1u); | |
| const auto& open_region = (*open_req.data())[0]; | |
| const uint64_t open_offset = open_region.offset().value_or(0); | |
| const uint64_t open_size = open_region.size().value_or(0); | |
| ASSERT_LE(open_offset + sizeof(amessage), kVmoSize); | |
| amessage open_msg{}; | |
| ASSERT_EQ(bulk_out_vmo_.read(&open_msg, open_offset, sizeof(open_msg)), ZX_OK); | |
| EXPECT_EQ(open_msg.command, static_cast<uint32_t>(A_OPEN)); | |
| // Delivering A_WRTE triggers SendOkay(), which must fail with ZX_ERR_SHOULD_WAIT. | |
| std::vector<uint8_t> wrte = MakeHeader(A_WRTE, 12, open_msg.arg0, 4); | |
| wrte.insert(wrte.end(), {'t', 'e', 's', 't'}); | |
| CompleteNextReadWithVmo(wrte, kVmoSize); | |
| RunLoopUntil([&]() { return done; }); | |
| EXPECT_EQ(err_status, ZX_ERR_SHOULD_WAIT); | |
| EXPECT_TRUE(bulk_out_server_.empty()); | |
| // Completing the in-flight A_OPEN request returns it to write_reqs_, allowing a new handshake. | |
| std::vector<fendpoint::Completion> completions; | |
| completions.emplace_back(std::move( | |
| fendpoint::Completion().request(std::move(open_req)).status(ZX_OK).transfer_size(open_size))); | |
| bulk_out_server_.CompleteBatch(std::move(completions)); | |
| RunLoopUntilIdle(); | |
| PerformHandshake(); | |
| } | |
| TEST_F(AdbClientTest, UnsolicitedWriteAfterCloseIsDiscarded) { | |
| PerformHandshake(); | |
| bool done1 = false; | |
| std::string out1; | |
| client_->ExecuteCommand({"first"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_ok()); | |
| if (result.is_ok()) { | |
| out1 = result->output(); | |
| } | |
| done1 = true; | |
| }); | |
| amessage open1 = PopOutPacketHeader(); | |
| const uint32_t local1 = open1.arg0; | |
| constexpr uint32_t kRemote1 = 5; | |
| std::vector<uint8_t> wrte1 = MakeHeader(A_WRTE, kRemote1, local1, 2); | |
| wrte1.insert(wrte1.end(), {'o', 'k'}); | |
| CompleteNextReadWithVmo(wrte1, kVmoSize); | |
| EXPECT_EQ(PopOutPacketHeader().command, static_cast<uint32_t>(A_OKAY)); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemote1, local1, 0), kVmoSize); | |
| RunLoopUntil([&]() { return done1; }); | |
| EXPECT_EQ(out1, "ok"); | |
| // Deliver a late WRTE for the closed stream across two packets; it must be discarded. | |
| CompleteNextReadWithVmo(MakeHeader(A_WRTE, kRemote1, local1, 6), kVmoSize); | |
| CompleteNextReadWithVmo(std::vector<uint8_t>{'s', 't', 'a', 'l', 'e', '!'}, 6); | |
| RunLoopUntilIdle(); | |
| EXPECT_TRUE(bulk_out_server_.empty()); | |
| // Next command should receive only its own payload. | |
| bool done2 = false; | |
| std::string out2; | |
| client_->ExecuteCommand({"second"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_ok()); | |
| if (result.is_ok()) { | |
| out2 = result->output(); | |
| } | |
| done2 = true; | |
| }); | |
| amessage open2 = PopOutPacketHeader(); | |
| std::vector<uint8_t> wrte2 = MakeHeader(A_WRTE, 6, open2.arg0, 3); | |
| wrte2.insert(wrte2.end(), {'n', 'e', 'w'}); | |
| CompleteNextReadWithVmo(wrte2, kVmoSize); | |
| EXPECT_EQ(PopOutPacketHeader().command, static_cast<uint32_t>(A_OKAY)); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, 6, open2.arg0, 0), kVmoSize); | |
| RunLoopUntil([&]() { return done2; }); | |
| EXPECT_EQ(out2, "new"); | |
| } | |
| TEST_F(AdbClientTest, ConnectWhileCommandActiveReturnsShouldWait) { | |
| PerformHandshake(); | |
| bool cmd_done = false; | |
| std::string output; | |
| client_->ExecuteCommand({"busy"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_ok()); | |
| if (result.is_ok()) { | |
| output = result->output(); | |
| } | |
| cmd_done = true; | |
| }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| bool connect_done = false; | |
| client_->Connect().ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| EXPECT_EQ(result.error_value().domain_error(), ZX_ERR_SHOULD_WAIT); | |
| } | |
| connect_done = true; | |
| }); | |
| RunLoopUntil([&]() { return connect_done; }); | |
| EXPECT_FALSE(cmd_done); | |
| // The active command must still complete cleanly. | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, 11, open_msg.arg0, 0), kVmoSize); | |
| RunLoopUntil([&]() { return cmd_done; }); | |
| EXPECT_TRUE(output.empty()); | |
| } | |
| TEST_F(AdbClientTest, FatalCompletionErrorResetsSessionAndStopsBatch) { | |
| PerformHandshake(); | |
| bool cmd_done = false; | |
| client_->ExecuteCommand({"malformed"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| EXPECT_EQ(result.error_value().domain_error(), ZX_ERR_IO); | |
| } | |
| cmd_done = true; | |
| }); | |
| (void)PopOutPacketHeader(); | |
| RunLoopUntil([&]() { return !bulk_in_server_.empty(); }); | |
| frequest::Request req1 = bulk_in_server_.PopRequest(); | |
| // Send a batch whose first completion has an out-of-bounds VMO transfer_size followed by a | |
| // second completion. OnCompletion must reset session state and return early without re-queuing. | |
| std::vector<fendpoint::Completion> completions; | |
| completions.emplace_back(std::move( | |
| fendpoint::Completion().request(std::move(req1)).status(ZX_OK).transfer_size(kVmoSize + 1))); | |
| completions.emplace_back(std::move(fendpoint::Completion().status(ZX_OK).transfer_size(0))); | |
| bulk_in_server_.CompleteBatch(std::move(completions)); | |
| RunLoopUntil([&]() { return cmd_done; }); | |
| RunLoopUntilIdle(); | |
| EXPECT_TRUE(bulk_in_server_.empty()); | |
| // Handshake must be cleared so subsequent ExecuteCommand fails with ZX_ERR_BAD_STATE. | |
| bool next_done = false; | |
| client_->ExecuteCommand({"after-oob"}).ThenExactlyOnce([&](auto& result) { | |
| EXPECT_TRUE(result.is_error()); | |
| if (result.is_error() && result.error_value().is_domain_error()) { | |
| EXPECT_EQ(result.error_value().domain_error(), ZX_ERR_BAD_STATE); | |
| } | |
| next_done = true; | |
| }); | |
| RunLoopUntil([&]() { return next_done; }); | |
| } | |
| TEST_F(AdbClientTest, HeaderSplitAcrossShortTransfers) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| const std::string banner = "recovery::ro.product.name=test"; | |
| std::vector<uint8_t> header = | |
| MakeHeader(A_CNXN, A_VERSION, 4096, static_cast<uint32_t>(banner.size())); | |
| CompleteNextReadWithVmo(std::span<const uint8_t>(header).subspan(0, 10), kVmoSize); | |
| CompleteNextReadWithVmo(std::span<const uint8_t>(header).subspan(10), kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(banner), banner.size()); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_ok()); | |
| EXPECT_EQ((*connect_result)->banner(), banner); | |
| } | |
| TEST_F(AdbClientTest, InvalidMagicResetsSession) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| std::vector<uint8_t> bad = MakeHeader(A_CNXN, A_VERSION, 4096, 0); | |
| bad[offsetof(amessage, magic)] ^= 0x1; | |
| CompleteNextReadWithVmo(bad, kVmoSize); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_error()); | |
| ASSERT_TRUE(connect_result->error_value().is_domain_error()); | |
| EXPECT_EQ(connect_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| // Subsequent Connect() should recover cleanly. | |
| EXPECT_EQ(PerformHandshake("device::recovered", 4096), "device::recovered"); | |
| } | |
| TEST_F(AdbClientTest, OversizedDataLengthResetsSession) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| CompleteNextReadWithVmo(MakeHeader(A_CNXN, A_VERSION, 4096, MAX_PAYLOAD + 1), kVmoSize); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_error()); | |
| ASSERT_TRUE(connect_result->error_value().is_domain_error()); | |
| EXPECT_EQ(connect_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| } | |
| TEST_F(AdbClientTest, ConcurrentConnectReturnsShouldWait) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> first_result; | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> second_result; | |
| client_->Connect().Then([&](auto& result) { first_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| client_->Connect().Then([&](auto& result) { second_result = std::move(result); }); | |
| RunLoopUntil([&]() { return second_result.has_value(); }); | |
| ASSERT_TRUE(second_result->is_error()); | |
| ASSERT_TRUE(second_result->error_value().is_domain_error()); | |
| EXPECT_EQ(second_result->error_value().domain_error(), ZX_ERR_SHOULD_WAIT); | |
| EXPECT_FALSE(first_result.has_value()); | |
| const std::string banner = "device::"; | |
| CompleteNextReadWithVmo(MakeHeader(A_CNXN, A_VERSION, 4096, static_cast<uint32_t>(banner.size())), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(banner), banner.size()); | |
| RunLoopUntil([&]() { return first_result.has_value(); }); | |
| ASSERT_TRUE(first_result->is_ok()); | |
| EXPECT_EQ((*first_result)->banner(), banner); | |
| } | |
| TEST_F(AdbClientTest, AuthReturnsNotSupported) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| const std::vector<uint8_t> token(20, 0xaa); | |
| CompleteNextReadWithVmo(MakeHeader(A_AUTH, 1, 0, static_cast<uint32_t>(token.size())), kVmoSize); | |
| CompleteNextReadWithVmo(token, token.size()); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_error()); | |
| ASSERT_TRUE(connect_result->error_value().is_domain_error()); | |
| EXPECT_EQ(connect_result->error_value().domain_error(), ZX_ERR_NOT_SUPPORTED); | |
| } | |
| TEST_F(AdbClientTest, ConnectWithZeroMaxPayloadResetsSession) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| CompleteNextReadWithVmo(MakeHeader(A_CNXN, A_VERSION, 0, 0), kVmoSize); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_error()); | |
| ASSERT_TRUE(connect_result->error_value().is_domain_error()); | |
| EXPECT_EQ(connect_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| } | |
| TEST_F(AdbClientTest, CloseWithUnexpectedRemoteIdFailsStream) { | |
| PerformHandshake(); | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"shell:id", {}}).Then([&](auto& result) { run_result = std::move(result); }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| ASSERT_EQ(open_msg.command, static_cast<uint32_t>(A_OPEN)); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 42; | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId + 1, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_error()); | |
| ASSERT_TRUE(run_result->error_value().is_domain_error()); | |
| EXPECT_EQ(run_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| } | |
| TEST_F(AdbClientTest, RejectedStreamReturnsNotSupported) { | |
| PerformHandshake(); | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"unsupported:", {}}).Then([&](auto& result) { | |
| run_result = std::move(result); | |
| }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, 0, open_msg.arg0, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_error()); | |
| ASSERT_TRUE(run_result->error_value().is_domain_error()); | |
| EXPECT_EQ(run_result->error_value().domain_error(), ZX_ERR_NOT_SUPPORTED); | |
| } | |
| TEST_F(AdbClientTest, SendPacketSetsShortFlag) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| bool cnxn_short = false; | |
| EXPECT_EQ(PopOutPacketHeader(nullptr, &cnxn_short).command, static_cast<uint32_t>(A_CNXN)); | |
| EXPECT_TRUE(cnxn_short); | |
| const std::string banner = "device::"; | |
| CompleteNextReadWithVmo(MakeHeader(A_CNXN, A_VERSION, 4096, static_cast<uint32_t>(banner.size())), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(banner), banner.size()); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_ok()); | |
| // 488-byte payload + 24-byte header produces an exact 512-byte-multiple bulk OUT transfer. | |
| const std::string long_service = "shell:" + std::string(1500, 'a'); | |
| const std::vector<uint8_t> input(488, 0x55); | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({long_service, input}).Then([&](auto& result) { | |
| run_result = std::move(result); | |
| }); | |
| bool open_short = false; | |
| std::string open_payload; | |
| amessage open_msg = PopOutPacketHeader(&open_payload, &open_short); | |
| EXPECT_EQ(open_msg.command, static_cast<uint32_t>(A_OPEN)); | |
| EXPECT_TRUE(open_short); | |
| EXPECT_EQ(open_payload, long_service + '\0'); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 7; | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| bool wrte_short = false; | |
| std::string wrte_payload; | |
| amessage wrte_msg = PopOutPacketHeader(&wrte_payload, &wrte_short); | |
| EXPECT_EQ(wrte_msg.command, static_cast<uint32_t>(A_WRTE)); | |
| EXPECT_TRUE(wrte_short); | |
| EXPECT_EQ(wrte_payload, std::string(488, '\x55')); | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| EXPECT_TRUE(run_result->is_ok()); | |
| } | |
| TEST_F(AdbClientTest, RunServiceFlowControlAndReadAccumulation) { | |
| PerformHandshake("device::", /*max_payload=*/512); | |
| // 800 bytes splits into 512 + 288 when max_payload is 512. | |
| std::vector<uint8_t> input(800); | |
| for (size_t i = 0; i < input.size(); ++i) { | |
| input[i] = static_cast<uint8_t>(i & 0xff); | |
| } | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"sync:", input}).Then([&](auto& result) { run_result = std::move(result); }); | |
| amessage open_msg = PopOutPacketHeader(); | |
| const uint32_t local_id = open_msg.arg0; | |
| constexpr uint32_t kRemoteId = 11; | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| // Only the first 512-byte WRTE should be sent before the device ACKs it. | |
| RunLoopUntil([&]() { return !bulk_out_server_.empty(); }); | |
| RunLoopUntilIdle(); | |
| ASSERT_EQ(bulk_out_server_.queued_count(), 1u); | |
| std::string chunk1; | |
| EXPECT_EQ(PopOutPacketHeader(&chunk1).command, static_cast<uint32_t>(A_WRTE)); | |
| EXPECT_EQ(chunk1.size(), 512u); | |
| // ACK first chunk -> second 288-byte WRTE is sent. | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return !bulk_out_server_.empty(); }); | |
| RunLoopUntilIdle(); | |
| ASSERT_EQ(bulk_out_server_.queued_count(), 1u); | |
| std::string chunk2; | |
| EXPECT_EQ(PopOutPacketHeader(&chunk2).command, static_cast<uint32_t>(A_WRTE)); | |
| EXPECT_EQ(chunk2.size(), 288u); | |
| // ACK second chunk, then send two inbound WRTEs and verify each triggers an A_OKAY. | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| const std::string part1 = "hello "; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_WRTE, kRemoteId, local_id, static_cast<uint32_t>(part1.size())), kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(part1), part1.size()); | |
| amessage ack1 = PopOutPacketHeader(); | |
| EXPECT_EQ(ack1.command, static_cast<uint32_t>(A_OKAY)); | |
| EXPECT_EQ(ack1.arg0, local_id); | |
| EXPECT_EQ(ack1.arg1, kRemoteId); | |
| const std::string part2 = "world"; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_WRTE, kRemoteId, local_id, static_cast<uint32_t>(part2.size())), kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(part2), part2.size()); | |
| EXPECT_EQ(PopOutPacketHeader().command, static_cast<uint32_t>(A_OKAY)); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_ok()); | |
| EXPECT_EQ((*run_result)->output(), ToBytes("hello world")); | |
| } | |
| TEST_F(AdbClientTest, StaleLocalIdPacketsAreIgnored) { | |
| PerformHandshake(); | |
| // First stream: open and close to obtain a previous local_id. | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> first_result; | |
| client_->RunService({"shell:first", {}}).Then([&](auto& result) { | |
| first_result = std::move(result); | |
| }); | |
| const uint32_t stale_local_id = PopOutPacketHeader().arg0; | |
| constexpr uint32_t kRemoteId = 20; | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, stale_local_id, 0), kVmoSize); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, stale_local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return first_result.has_value(); }); | |
| ASSERT_TRUE(first_result->is_ok()); | |
| // Second stream: stale WRTE and CLSE targeting stale_local_id must be ignored. | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> second_result; | |
| client_->RunService({"shell:second", {}}).Then([&](auto& result) { | |
| second_result = std::move(result); | |
| }); | |
| const uint32_t active_local_id = PopOutPacketHeader().arg0; | |
| ASSERT_NE(active_local_id, stale_local_id); | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, active_local_id, 0), kVmoSize); | |
| const std::string stale_data = "stale"; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_WRTE, kRemoteId, stale_local_id, static_cast<uint32_t>(stale_data.size())), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(stale_data), stale_data.size()); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, stale_local_id, 0), kVmoSize); | |
| RunLoopUntilIdle(); | |
| EXPECT_FALSE(second_result.has_value()); | |
| EXPECT_TRUE(bulk_out_server_.empty()); | |
| const std::string live_data = "live"; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_WRTE, kRemoteId, active_local_id, static_cast<uint32_t>(live_data.size())), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(ToBytes(live_data), live_data.size()); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, active_local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return second_result.has_value(); }); | |
| ASSERT_TRUE(second_result->is_ok()); | |
| EXPECT_EQ((*second_result)->output(), ToBytes(live_data)); | |
| } | |
| TEST_F(AdbClientTest, ReconnectSendsNewHandshake) { | |
| EXPECT_EQ(PerformHandshake("device::first"), "device::first"); | |
| EXPECT_EQ(PerformHandshake("recovery::second"), "recovery::second"); | |
| } | |
| TEST_F(AdbClientTest, RunServiceBeforeConnectReturnsBadState) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"shell:id", {}}).Then([&](auto& result) { run_result = std::move(result); }); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_error()); | |
| ASSERT_TRUE(run_result->error_value().is_domain_error()); | |
| EXPECT_EQ(run_result->error_value().domain_error(), ZX_ERR_BAD_STATE); | |
| } | |
| TEST_F(AdbClientTest, RunServiceOversizedDestinationReturnsOutOfRange) { | |
| PerformHandshake("device::", /*max_payload=*/32); | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({std::string(32, 'a'), {}}).Then([&](auto& result) { | |
| run_result = std::move(result); | |
| }); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_error()); | |
| ASSERT_TRUE(run_result->error_value().is_domain_error()); | |
| EXPECT_EQ(run_result->error_value().domain_error(), ZX_ERR_OUT_OF_RANGE); | |
| } | |
| TEST_F(AdbClientTest, CloseWithZeroRemoteIdAfterOpenFailsStream) { | |
| PerformHandshake(); | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"shell:id", {}}).Then([&](auto& result) { run_result = std::move(result); }); | |
| const uint32_t local_id = PopOutPacketHeader().arg0; | |
| constexpr uint32_t kRemoteId = 42; | |
| CompleteNextReadWithVmo(MakeHeader(A_OKAY, kRemoteId, local_id, 0), kVmoSize); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, 0, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_error()); | |
| ASSERT_TRUE(run_result->error_value().is_domain_error()); | |
| EXPECT_EQ(run_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| } | |
| TEST_F(AdbClientTest, NonUtf8BannerAndExecuteOutputReturnDataIntegrity) { | |
| // Invalid UTF-8 in A_CNXN banner must return a clean domain error without closing the channel. | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::Connect>> connect_result; | |
| client_->Connect().Then([&](auto& result) { connect_result = std::move(result); }); | |
| (void)PopOutPacketHeader(); | |
| const std::vector<uint8_t> bad_utf8 = {'d', 'e', 'v', 0xff, 0xfe}; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_CNXN, A_VERSION, 4096, static_cast<uint32_t>(bad_utf8.size())), kVmoSize); | |
| CompleteNextReadWithVmo(bad_utf8, bad_utf8.size()); | |
| RunLoopUntil([&]() { return connect_result.has_value(); }); | |
| ASSERT_TRUE(connect_result->is_error()); | |
| ASSERT_TRUE(connect_result->error_value().is_domain_error()); | |
| EXPECT_EQ(connect_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| // Channel stays open and a valid handshake succeeds. | |
| PerformHandshake(); | |
| // Invalid UTF-8 in ExecuteCommand output returns ZX_ERR_IO_DATA_INTEGRITY without closing. | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::ExecuteCommand>> exec_result; | |
| client_->ExecuteCommand({"cat /bin/data"}).Then([&](auto& result) { | |
| exec_result = std::move(result); | |
| }); | |
| const uint32_t exec_local_id = PopOutPacketHeader().arg0; | |
| constexpr uint32_t kRemoteId = 15; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_WRTE, kRemoteId, exec_local_id, static_cast<uint32_t>(bad_utf8.size())), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(bad_utf8, bad_utf8.size()); | |
| EXPECT_EQ(PopOutPacketHeader().command, static_cast<uint32_t>(A_OKAY)); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, exec_local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return exec_result.has_value(); }); | |
| ASSERT_TRUE(exec_result->is_error()); | |
| ASSERT_TRUE(exec_result->error_value().is_domain_error()); | |
| EXPECT_EQ(exec_result->error_value().domain_error(), ZX_ERR_IO_DATA_INTEGRITY); | |
| // RunService returns arbitrary non-UTF-8 bytes intact on the same session. | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"sync:", {}}).Then([&](auto& result) { run_result = std::move(result); }); | |
| const uint32_t svc_local_id = PopOutPacketHeader().arg0; | |
| CompleteNextReadWithVmo( | |
| MakeHeader(A_WRTE, kRemoteId, svc_local_id, static_cast<uint32_t>(bad_utf8.size())), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(bad_utf8, bad_utf8.size()); | |
| EXPECT_EQ(PopOutPacketHeader().command, static_cast<uint32_t>(A_OKAY)); | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, svc_local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_ok()); | |
| EXPECT_EQ((*run_result)->output(), bad_utf8); | |
| } | |
| TEST_F(AdbClientTest, OutputExceedingFidlLimitReturnsBufferTooSmall) { | |
| PerformHandshake(); | |
| constexpr uint32_t kRemoteId = 31; | |
| auto send_stream_output = [&](size_t total_bytes) { | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"sync:", {}}).Then([&](auto& result) { run_result = std::move(result); }); | |
| const uint32_t local_id = PopOutPacketHeader().arg0; | |
| size_t sent = 0; | |
| while (sent < total_bytes) { | |
| const size_t chunk = std::min(kVmoSize, total_bytes - sent); | |
| CompleteNextReadWithVmo(MakeHeader(A_WRTE, kRemoteId, local_id, static_cast<uint32_t>(chunk)), | |
| kVmoSize); | |
| CompleteNextReadWithVmo(std::vector<uint8_t>(chunk, 'z'), chunk); | |
| EXPECT_EQ(PopOutPacketHeader().command, static_cast<uint32_t>(A_OKAY)); | |
| sent += chunk; | |
| } | |
| CompleteNextReadWithVmo(MakeHeader(A_CLSE, kRemoteId, local_id, 0), kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| return std::move(*run_result); | |
| }; | |
| // Exactly kMaxReplyPayloadBytes succeeds. | |
| auto ok_result = send_stream_output(AdbClientImpl::kMaxReplyPayloadBytes); | |
| ASSERT_TRUE(ok_result.is_ok()); | |
| EXPECT_EQ(ok_result->output().size(), AdbClientImpl::kMaxReplyPayloadBytes); | |
| // Pushing 1 byte past the limit drains to CLSE and returns ZX_ERR_BUFFER_TOO_SMALL. | |
| auto overflow_result = send_stream_output(AdbClientImpl::kMaxReplyPayloadBytes + 1); | |
| ASSERT_TRUE(overflow_result.is_error()); | |
| ASSERT_TRUE(overflow_result.error_value().is_domain_error()); | |
| EXPECT_EQ(overflow_result.error_value().domain_error(), ZX_ERR_BUFFER_TOO_SMALL); | |
| // Session remains healthy for subsequent streams. | |
| auto recovery_result = send_stream_output(16); | |
| ASSERT_TRUE(recovery_result.is_ok()); | |
| EXPECT_EQ(recovery_result->output().size(), 16u); | |
| } | |
| TEST_F(AdbClientTest, SendNextWriteFailureResetsSessionAndStopsBatch) { | |
| PerformHandshake(); | |
| std::optional<fidl::Result<fuchsia_testing_adb::Client::RunService>> run_result; | |
| client_->RunService({"sync:", ToBytes("payload")}).Then([&](auto& result) { | |
| run_result = std::move(result); | |
| }); | |
| const uint32_t local_id = PopOutPacketHeader().arg0; | |
| // Unbind bulk OUT before A_OKAY so SendNextWrite() fails when opening the stream. | |
| impl_->UnbindBulkOutForTesting(); | |
| // Pack A_OKAY followed by A_CLSE in the same read; OnCompletion must reset and return early. | |
| std::vector<uint8_t> combined = MakeHeader(A_OKAY, 9, local_id, 0); | |
| std::vector<uint8_t> clse = MakeHeader(A_CLSE, 9, local_id, 0); | |
| combined.insert(combined.end(), clse.begin(), clse.end()); | |
| CompleteNextReadWithVmo(combined, kVmoSize); | |
| RunLoopUntil([&]() { return run_result.has_value(); }); | |
| ASSERT_TRUE(run_result->is_error()); | |
| ASSERT_TRUE(run_result->error_value().is_domain_error()); | |
| EXPECT_EQ(run_result->error_value().domain_error(), ZX_ERR_BAD_STATE); | |
| RunLoopUntilIdle(); | |
| EXPECT_TRUE(bulk_in_server_.empty()); | |
| } | |
| } // namespace |