chromium/mojo/core/message_unittest.cc

// Copyright 2017 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/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <memory>
#include <string_view>
#include <utility>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/numerics/safe_math.h"
#include "base/rand_util.h"
#include "base/ranges/algorithm.h"
#include "build/blink_buildflags.h"
#include "build/build_config.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/ipcz_driver/mojo_message.h"
#include "mojo/core/test/mojo_test_base.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/buffer.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/public/cpp/system/platform_handle.h"

#if BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)
#include "mojo/core/user_message_impl.h"
#endif

namespace mojo::core {
namespace {

MessageTest;

#if BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)
constexpr uint32_t kLegacyMinimumPayloadBufferSize = kMinimumPayloadBufferSize;
#else
constexpr uint32_t kLegacyMinimumPayloadBufferSize =;
#endif

// Helper class which provides a base implementation for an unserialized user
// message context and helpers to go between these objects and opaque message
// handles.
class TestMessageBase {};

class NeverSerializedMessage : public TestMessageBase {};

class SimpleMessage : public TestMessageBase {};

TEST_F(MessageTest, InvalidMessageObjects) {}

TEST_F(MessageTest, SendLocalMessageWithContext) {}

TEST_F(MessageTest, DestroyMessageWithContext) {}

const char kTestMessageWithContext1[] =;

#if BUILDFLAG(USE_BLINK)

const char kTestMessageWithContext2[] =;
const char kTestMessageWithContext3[] =;
const char kTestMessageWithContext4[] =;
const char kTestQuitMessage[] =;

DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveMessageNoHandles, MessageTest, h) {}

#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_SerializeSimpleMessageNoHandlesWithContext
#else
#define MAYBE_SerializeSimpleMessageNoHandlesWithContext
#endif  // BUILDFLAG(IS_IOS)
TEST_F(MessageTest, MAYBE_SerializeSimpleMessageNoHandlesWithContext) {}

#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_SerializeDynamicallySizedMessage
#else
#define MAYBE_SerializeDynamicallySizedMessage
#endif  // BUILDFLAG(IS_IOS)
TEST_F(MessageTest, MAYBE_SerializeDynamicallySizedMessage) {}

DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveMessageOneHandle, MessageTest, h) {}

#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_SerializeSimpleMessageOneHandleWithContext
#else
#define MAYBE_SerializeSimpleMessageOneHandleWithContext
#endif  // BUILDFLAG(IS_IOS)
TEST_F(MessageTest, MAYBE_SerializeSimpleMessageOneHandleWithContext) {}

DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveMessageWithHandles, MessageTest, h) {}

#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_SerializeSimpleMessageWithHandlesWithContext
#else
#define MAYBE_SerializeSimpleMessageWithHandlesWithContext
#endif  // BUILDFLAG(IS_IOS)
TEST_F(MessageTest, MAYBE_SerializeSimpleMessageWithHandlesWithContext) {}

#endif  // BUILDFLAG(USE_BLINK)

TEST_F(MessageTest, SendLocalSimpleMessageWithHandlesWithContext) {}

TEST_F(MessageTest, DropUnreadLocalMessageWithContext) {}

TEST_F(MessageTest, GetMessageDataWithHandles) {}

TEST_F(MessageTest, ReadMessageWithContextAsSerializedMessage) {}

TEST_F(MessageTest, ReadSerializedMessageAsMessageWithContext) {}

TEST_F(MessageTest, ForceSerializeMessageWithContext) {}

TEST_F(MessageTest, DoubleSerialize) {}

TEST_F(MessageTest, ExtendMessagePayload) {}

TEST_F(MessageTest, PreallocateEnoughMemoryForMessage) {}

TEST_F(MessageTest, PreallocateNotEnoughMemoryForMessage) {}

TEST_F(MessageTest, ExtendMessageWithHandlesPayload) {}

TEST_F(MessageTest, ExtendMessagePayloadLarge) {}

TEST_F(MessageTest, CorrectPayloadBufferBoundaries) {}

#if BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)
TEST_F(MessageTest, CommitInvalidMessageContents) {
  // Regression test for https://crbug.com/755127. Ensures that we don't crash
  // if we attempt to commit the contents of an unserialized message.
  MojoMessageHandle message;
  EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessage(nullptr, &message));
  EXPECT_EQ(MOJO_RESULT_OK, MojoAppendMessageData(message, 0, nullptr, 0,
                                                  nullptr, nullptr, nullptr));
  MojoHandle a, b;
  CreateMessagePipe(&a, &b);
  EXPECT_EQ(MOJO_RESULT_OK, MojoAppendMessageData(message, 0, &a, 1, nullptr,
                                                  nullptr, nullptr));

  UserMessageImpl::FailHandleSerializationForTesting(true);
  MojoAppendMessageDataOptions options;
  options.struct_size = sizeof(options);
  options.flags = MOJO_APPEND_MESSAGE_DATA_FLAG_COMMIT_SIZE;
  EXPECT_EQ(MOJO_RESULT_OK, MojoAppendMessageData(message, 0, nullptr, 0,
                                                  nullptr, nullptr, nullptr));
  UserMessageImpl::FailHandleSerializationForTesting(false);
  EXPECT_EQ(MOJO_RESULT_OK, MojoDestroyMessage(message));
  EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
#endif  // BUILDFLAG(MOJO_SUPPORT_LEGACY_CORE)

#if BUILDFLAG(USE_BLINK)

#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_ExtendPayloadWithHandlesAttached
#else
#define MAYBE_ExtendPayloadWithHandlesAttached
#endif  // BUILDFLAG(IS_IOS)
TEST_F(MessageTest, MAYBE_ExtendPayloadWithHandlesAttached) {}

DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadAndIgnoreMessage, MessageTest, h) {}

#if BUILDFLAG(IS_IOS)
// TODO(crbug.com/40257752): Test currently fails on iOS.
#define MAYBE_ExtendPayloadWithHandlesAttachedViaExtension
#else
#define MAYBE_ExtendPayloadWithHandlesAttachedViaExtension
#endif  // BUILDFLAG(IS_IOS)
TEST_F(MessageTest, MAYBE_ExtendPayloadWithHandlesAttachedViaExtension) {}

DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadMessageAndCheckPipe, MessageTest, h) {}

#endif  // BUILDFLAG(USE_BLINK)

TEST_F(MessageTest, PartiallySerializedMessagesDontLeakHandles) {}

}  // namespace
}  // namespace mojo::core