mojo_ipc_test_util.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/host/mojo_ipc/mojo_ipc_test_util.h"
  5. #include <inttypes.h>
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/logging.h"
  9. #include "base/rand_util.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "build/build_config.h"
  12. namespace remoting {
  13. namespace test {
  14. mojo::NamedPlatformChannel::ServerName GenerateRandomServerName() {
  15. std::string temp_path;
  16. #if BUILDFLAG(IS_POSIX)
  17. // Posix server names should start with the temp directory path. Otherwise the
  18. // socket file will be created under the current working directory.
  19. base::FilePath temp_file_path;
  20. if (base::GetTempDir(&temp_file_path)) {
  21. temp_path = temp_file_path.AsEndingWithSeparator().value();
  22. } else {
  23. LOG(ERROR) << "Failed to retrieve temporary directory.";
  24. }
  25. #endif
  26. return mojo::NamedPlatformChannel::ServerNameFromUTF8(
  27. base::StringPrintf("%sremoting_mojo_test_server.%" PRIu64,
  28. temp_path.c_str(), base::RandUint64()));
  29. }
  30. } // namespace test
  31. } // namespace remoting