chromium/mojo/public/cpp/system/tests/invitation_unittest.cc

// Copyright 2018 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 "mojo/public/cpp/system/invitation.h"

#include <optional>
#include <string_view>
#include <utility>

#include "base/base_paths.h"
#include "base/base_switches.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/multiprocess_test.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/test/test_switches.h"
#include "mojo/public/c/system/invitation.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "mojo/public/cpp/system/wait.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"

#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
#include "mojo/public/cpp/platform/named_platform_channel.h"
#endif

#if BUILDFLAG(IS_WIN)
#include <windows.h>

#include "base/win/access_token.h"
#endif

namespace mojo {
namespace {

enum class InvitationType {};

enum class TransportType {};

// Switches and values to tell clients of parameterized test runs what mode they
// should be testing against.
const char kTransportTypeSwitch[] =;
const char kTransportTypeChannel[] =;
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS)
const char kTransportTypeChannelServer[] =;
#endif

// TODO(crbug.com/40900578): Flaky on Tsan.
#if defined(THREAD_SANITIZER)
#define MAYBE_InvitationCppTest
#else
#define MAYBE_InvitationCppTest
#endif
class MAYBE_InvitationCppTest
    : public testing::Test,
      public testing::WithParamInterface<TransportType> {};

class TestClientBase : public MAYBE_InvitationCppTest {};

#define DEFINE_TEST_CLIENT(name)

const char kTestMessage1[] =;
const char kTestMessage2[] =;

TEST_P(MAYBE_InvitationCppTest, Send) {}

DEFINE_TEST_CLIENT(CppSendClient) {}

TEST_P(MAYBE_InvitationCppTest, SendIsolated) {}

DEFINE_TEST_CLIENT(CppSendIsolatedClient) {}

#if BUILDFLAG(IS_WIN)
TEST_P(MAYBE_InvitationCppTest, SendElevated) {
  ScopedMessagePipeHandle pipe;
  LaunchChildTestClient("CppSendElevatedClient", &pipe, 1,
                        InvitationType::kElevated, GetParam());
  WriteMessage(pipe, kTestMessage1);
  WaitForChildExit();
}

DEFINE_TEST_CLIENT(CppSendElevatedClient) {
  auto invitation = AcceptInvitation(MOJO_ACCEPT_INVITATION_FLAG_ELEVATED);
  auto pipe = invitation.ExtractMessagePipe(0);
  CHECK_EQ(kTestMessage1, ReadMessage(pipe));
}
#endif  // BUILDFLAG(IS_WIN)

TEST_P(MAYBE_InvitationCppTest, SendWithMultiplePipes) {}

DEFINE_TEST_CLIENT(CppSendWithMultiplePipesClient) {}

TEST(MAYBE_InvitationCppTest_NoParam, SendIsolatedInvitationWithDuplicateName) {}

const char kErrorMessage[] =;
const char kDisconnectMessage[] =;

// Flakily times out on Android under ASAN.
// crbug.com/1011494
#if BUILDFLAG(IS_ANDROID) && defined(ADDRESS_SANITIZER)
#define MAYBE_ProcessErrors
#else
#define MAYBE_ProcessErrors
#endif

TEST_P(MAYBE_InvitationCppTest, MAYBE_ProcessErrors) {}

DEFINE_TEST_CLIENT(CppProcessErrorsClient) {}

INSTANTIATE_TEST_SUITE_P();

}  // namespace
}  // namespace mojo