blob: 053851b6b586acc5fcc0e23761759bab92f119c7 [file] [edit]
// 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/debug/debug_agent/failing_write_stream.h"
#include <memory>
#include <utility>
#include <gtest/gtest.h>
#include "src/developer/debug/debug_agent/mock_process.h"
#include "src/developer/debug/debug_agent/mock_system_interface.h"
#include "src/developer/debug/ipc/protocol.h"
#include "src/developer/debug/shared/logging/logging.h"
#include "src/developer/debug/shared/message_loop.h"
namespace debug_agent {
bool FailingWriteStream::Start() {
valid_ = true;
return true;
}
bool FailingWriteStream::Stop() {
valid_ = false;
return true;
}
bool FailingWriteStream::IsValid() { return valid_; }
size_t FailingWriteStream::ConsumeStreamBufferData(const char* data, size_t len) {
++write_calls_;
if (error_callback()) {
error_callback()();
}
return 0;
}
void FailingWriteStream::ResetInternal() { valid_ = false; }
FailingWriteAgentSetup::FailingWriteAgentSetup()
: agent(std::make_unique<DebugAgent>(MockSystemInterface::CreateWithData())) {
auto owned_stream = std::make_unique<FailingWriteStream>();
stream = owned_stream.get();
agent->TakeAndConnectRemoteAPIStream(std::move(owned_stream));
debug_ipc::HelloRequest hello_request;
hello_request.version = debug_ipc::kCurrentProtocolVersion;
debug_ipc::HelloReply hello_reply;
agent->OnHello(hello_request, &hello_reply);
}
FailingWriteAgentSetup::~FailingWriteAgentSetup() {
if (agent) {
debug::LogBackend::Unset();
}
}
MockProcess* FailingWriteAgentSetup::AddProcess(zx_koid_t process_koid) const {
auto process = std::make_unique<MockProcess>(agent.get(), process_koid);
MockProcess* result = process.get();
agent->InjectProcessForTest(std::move(process));
return result;
}
void ExpectDeferredQuitNow(debug::MessageLoop& loop) {
auto sentinel_ran = std::make_shared<bool>(false);
loop.PostTask(FROM_HERE, [&loop, sentinel_ran] {
*sentinel_ran = true;
loop.QuitNow();
});
loop.Run();
EXPECT_FALSE(*sentinel_ran);
// Drain the trailing sentinel task so subsequent loop.Run() calls on the same loop do not run a
// stale QuitNow() before newly posted work.
if (!*sentinel_ran) {
loop.Run();
EXPECT_TRUE(*sentinel_ran);
}
}
} // namespace debug_agent