chromium/services/service_manager/tests/service_process_launcher_unittest.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/service_manager/service_process_launcher.h"

#include <memory>
#include <optional>
#include <utility>

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "sandbox/policy/mojom/sandbox.mojom.h"
#include "services/service_manager/public/mojom/service.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace service_manager {
namespace {

const char kTestServiceName[] =;

#if BUILDFLAG(IS_WIN)
const base::FilePath::CharType kServiceExtension[] =
    FILE_PATH_LITERAL(".service.exe");
#else
const base::FilePath::CharType kServiceExtension[] =);
#endif

void ProcessReadyCallbackAdapter(bool expect_process_id_valid,
                                 base::OnceClosure callback,
                                 base::ProcessId process_id) {}

class ServiceProcessLauncherDelegateImpl
    : public ServiceProcessLauncherDelegate {};

// TODO(qsr): Multiprocess service manager tests are not supported on android.
// TODO(crbug.com/40817146): Flakes on all platforms.
TEST(ServiceProcessLauncherTest, DISABLED_StartJoin) {}

#if !BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_MAC)
// Verify that if ServiceProcessLauncher cannot launch a process running the
// service from the specified path, then we are able to clean up without e.g.
// double-freeing the platform-channel handle reserved for the peer.
// This test won't work as-is on POSIX platforms, where we use fork()+exec() to
// launch child processes, since we won't fail until exec(), therefore the test
// will see a valid child process-Id. We use posix_spawn() on Mac OS X.
TEST(ServiceProcessLauncherTest, FailToLaunchProcess) {
  base::test::TaskEnvironment task_environment;

  // Pick a service path that could not possibly ever exist.
  base::FilePath test_service_path(FILE_PATH_LITERAL("rockot@_rules.service"));

  ServiceProcessLauncherDelegateImpl service_process_launcher_delegate;
  std::optional<ServiceProcessLauncher> launcher(
      std::in_place, &service_process_launcher_delegate, test_service_path);
  base::RunLoop run_loop;
  launcher->Start(Identity(), sandbox::mojom::Sandbox::kNoSandbox,
                  base::BindOnce(&ProcessReadyCallbackAdapter,
                                 false /*expect_process_id_valid*/,
                                 run_loop.QuitClosure()));
  run_loop.Run();

  launcher.reset();
  task_environment.RunUntilIdle();
}
#endif  //  !BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_MAC)

}  // namespace
}  // namespace service_manager