#include "mojo/core/channel.h"
#include <stdint.h>
#include "base/functional/callback_helpers.h"
#include "base/message_loop/message_pump_type.h"
#include "base/no_destructor.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h"
#include "build/build_config.h"
#include "mojo/core/connection_params.h"
#include "mojo/core/fuzzing_utils.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, size_t size) {
static base::NoDestructor<mojo::core::Environment> environment;
mojo::PlatformChannel channel;
mojo::core::FakeChannelDelegate receiver_delegate{
false};
auto receiver = mojo::core::Channel::Create(
&receiver_delegate,
mojo::core::ConnectionParams(channel.TakeLocalEndpoint()),
mojo::core::Channel::HandlePolicy::kRejectHandles,
environment->main_thread_task_executor.task_runner());
#if BUILDFLAG(IS_WIN)
receiver->set_remote_process(base::Process::Current());
#endif
receiver->Start();
mojo::core::FakeChannelDelegate sender_delegate{false};
auto sender = mojo::core::Channel::Create(
&sender_delegate,
mojo::core::ConnectionParams(channel.TakeRemoteEndpoint()),
mojo::core::Channel::HandlePolicy::kRejectHandles,
environment->main_thread_task_executor.task_runner());
sender->Start();
auto payload = UNSAFE_BUFFERS(base::span(data, size));
sender->Write(mojo::core::Channel::Message::CreateRawForFuzzing(payload));
base::RunLoop().RunUntilIdle();
sender->ShutDown();
sender.reset();
receiver->ShutDown();
receiver.reset();
base::RunLoop().RunUntilIdle();
return 0;
}