server_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 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 "components/exo/wayland/server.h"
  5. #include <stdlib.h>
  6. #include <wayland-client-core.h>
  7. #include <memory>
  8. #include "base/atomic_sequence_num.h"
  9. #include "base/bind.h"
  10. #include "base/files/file_enumerator.h"
  11. #include "base/files/file_util.h"
  12. #include "base/process/process_handle.h"
  13. #include "base/run_loop.h"
  14. #include "base/test/bind.h"
  15. #include "components/exo/display.h"
  16. #include "components/exo/security_delegate.h"
  17. #include "components/exo/wayland/server_util.h"
  18. #include "components/exo/wayland/test/wayland_server_test_base.h"
  19. #include "testing/gmock/include/gmock/gmock.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. namespace exo {
  22. namespace wayland {
  23. using ServerTest = test::WaylandServerTestBase;
  24. TEST_F(ServerTest, AddSocket) {
  25. auto server = CreateServer(SecurityDelegate::GetDefaultSecurityDelegate());
  26. // Check that calling AddSocket() with a unique socket name succeeds.
  27. bool rv = server->AddSocket(GetUniqueSocketName());
  28. EXPECT_TRUE(rv);
  29. }
  30. TEST_F(ServerTest, GetFileDescriptor) {
  31. auto server = CreateServer(SecurityDelegate::GetDefaultSecurityDelegate());
  32. bool rv = server->AddSocket(GetUniqueSocketName());
  33. EXPECT_TRUE(rv);
  34. // Check that the returned file descriptor is valid.
  35. int fd = server->GetFileDescriptor();
  36. DCHECK_GE(fd, 0);
  37. }
  38. TEST_F(ServerTest, SecurityDelegateAssociation) {
  39. std::unique_ptr<SecurityDelegate> security_delegate =
  40. SecurityDelegate::GetDefaultSecurityDelegate();
  41. SecurityDelegate* security_delegate_ptr = security_delegate.get();
  42. auto server = CreateServer(std::move(security_delegate));
  43. EXPECT_EQ(GetSecurityDelegate(server->GetWaylandDisplayForTesting()),
  44. security_delegate_ptr);
  45. }
  46. TEST_F(ServerTest, CreateAsync) {
  47. using MockServerFunction =
  48. testing::MockFunction<void(bool, const base::FilePath&)>;
  49. base::ScopedTempDir non_xdg_dir;
  50. ASSERT_TRUE(non_xdg_dir.CreateUniqueTempDir());
  51. base::RunLoop run_loop;
  52. base::FilePath server_socket;
  53. MockServerFunction server_callback;
  54. EXPECT_CALL(server_callback, Call(testing::_, testing::_))
  55. .WillOnce(testing::Invoke([&run_loop, &server_socket](
  56. bool success, const base::FilePath& path) {
  57. EXPECT_TRUE(success);
  58. server_socket = path;
  59. run_loop.Quit();
  60. }));
  61. auto server = CreateServer();
  62. server->StartAsync(base::BindOnce(&MockServerFunction::Call,
  63. base::Unretained(&server_callback)));
  64. run_loop.Run();
  65. // Should create a directory for the server.
  66. EXPECT_TRUE(base::DirectoryExists(server_socket.DirName()));
  67. // Must not be a child of the XDG dir.
  68. EXPECT_TRUE(base::IsDirectoryEmpty(xdg_temp_dir_.GetPath()));
  69. // Must be deleted when the helper is removed.
  70. server.reset();
  71. EXPECT_FALSE(base::PathExists(server_socket));
  72. }
  73. TEST_F(ServerTest, Dispatch) {
  74. auto server = CreateServer(SecurityDelegate::GetDefaultSecurityDelegate());
  75. std::string socket_name = GetUniqueSocketName();
  76. bool rv = server->AddSocket(socket_name);
  77. EXPECT_TRUE(rv);
  78. test::WaylandClientRunner client(server.get(), "client-" + socket_name);
  79. wl_display* client_display;
  80. // Post a task that connects server on the created thread.
  81. bool connected_to_server = false;
  82. client.RunAndWait(base::BindLambdaForTesting([&]() {
  83. client_display = wl_display_connect(socket_name.c_str());
  84. connected_to_server = !!client_display;
  85. }));
  86. EXPECT_TRUE(connected_to_server);
  87. client.RunAndWait(base::BindLambdaForTesting(
  88. [&]() { wl_display_disconnect(client_display); }));
  89. }
  90. TEST_F(ServerTest, Flush) {
  91. auto server = CreateServer(SecurityDelegate::GetDefaultSecurityDelegate());
  92. bool rv = server->AddSocket(GetUniqueSocketName());
  93. EXPECT_TRUE(rv);
  94. // Just call Flush to check that it doesn't have any bad side-effects.
  95. server->Flush();
  96. }
  97. } // namespace wayland
  98. } // namespace exo