ipc_linux_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Copyright 2015 Google, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at:
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include <memory>
  17. #include <sys/socket.h>
  18. #include <sys/un.h>
  19. #include <base/at_exit.h>
  20. #include <base/command_line.h>
  21. #include <base/files/scoped_file.h>
  22. #include <base/macros.h>
  23. #include <base/run_loop.h>
  24. #include <base/strings/stringprintf.h>
  25. #include <gtest/gtest.h>
  26. #include "service/adapter.h"
  27. #include "service/hal/fake_bluetooth_gatt_interface.h"
  28. #include "service/hal/fake_bluetooth_interface.h"
  29. #include "service/ipc/ipc_manager.h"
  30. #include "service/settings.h"
  31. #include "service/test/mock_daemon.h"
  32. namespace {
  33. using testing::Return;
  34. const char kTestSocketPath[] = "test_socket_path";
  35. class IPCLinuxTest : public ::testing::Test {
  36. public:
  37. IPCLinuxTest() = default;
  38. ~IPCLinuxTest() override = default;
  39. void SetUp() override {
  40. SetUpCommandLine();
  41. ASSERT_TRUE(settings_.Init());
  42. auto mock_daemon = new bluetooth::testing::MockDaemon();
  43. ON_CALL(*mock_daemon, GetSettings()).WillByDefault(Return(&settings_));
  44. ON_CALL(*mock_daemon, GetMessageLoop())
  45. .WillByDefault(Return(&message_loop_));
  46. bluetooth::Daemon::InitializeForTesting(mock_daemon);
  47. bluetooth::hal::BluetoothInterface::InitializeForTesting(
  48. new bluetooth::hal::FakeBluetoothInterface());
  49. bluetooth::hal::BluetoothGattInterface::InitializeForTesting(
  50. new bluetooth::hal::FakeBluetoothGattInterface(nullptr, nullptr,
  51. nullptr, nullptr));
  52. adapter_ = bluetooth::Adapter::Create();
  53. ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
  54. }
  55. void TearDown() override {
  56. client_fd_.reset();
  57. ipc_manager_.reset();
  58. adapter_.reset();
  59. bluetooth::hal::BluetoothGattInterface::CleanUp();
  60. bluetooth::hal::BluetoothInterface::CleanUp();
  61. bluetooth::Daemon::ShutDown();
  62. base::CommandLine::Reset();
  63. }
  64. virtual void SetUpCommandLine() {
  65. std::string ipc_socket_arg =
  66. base::StringPrintf("--create-ipc-socket=%s", kTestSocketPath);
  67. const base::CommandLine::CharType* argv[] = {
  68. "program", ipc_socket_arg.c_str(),
  69. };
  70. base::CommandLine::Init(arraysize(argv), argv);
  71. }
  72. void ConnectToTestSocket() {
  73. client_fd_.reset(socket(PF_UNIX, SOCK_SEQPACKET, 0));
  74. ASSERT_TRUE(client_fd_.is_valid());
  75. struct sockaddr_un address;
  76. memset(&address, 0, sizeof(address));
  77. address.sun_family = AF_UNIX;
  78. strncpy(address.sun_path, kTestSocketPath, sizeof(address.sun_path) - 1);
  79. int status =
  80. connect(client_fd_.get(), (struct sockaddr*)&address, sizeof(address));
  81. EXPECT_EQ(0, status);
  82. }
  83. protected:
  84. base::AtExitManager exit_manager_;
  85. base::MessageLoop message_loop_;
  86. bluetooth::Settings settings_;
  87. std::unique_ptr<bluetooth::Adapter> adapter_;
  88. std::unique_ptr<ipc::IPCManager> ipc_manager_;
  89. base::ScopedFD client_fd_;
  90. DISALLOW_COPY_AND_ASSIGN(IPCLinuxTest);
  91. };
  92. class IPCLinuxTestDisabled : public IPCLinuxTest {
  93. public:
  94. IPCLinuxTestDisabled() = default;
  95. ~IPCLinuxTestDisabled() override = default;
  96. void SetUpCommandLine() override {
  97. // Set up with no --ipc-socket-path
  98. const base::CommandLine::CharType* argv[] = {"program"};
  99. base::CommandLine::Init(arraysize(argv), argv);
  100. }
  101. private:
  102. DISALLOW_COPY_AND_ASSIGN(IPCLinuxTestDisabled);
  103. };
  104. class TestDelegate : public ipc::IPCManager::Delegate,
  105. public base::SupportsWeakPtr<TestDelegate> {
  106. public:
  107. TestDelegate() : started_count_(0), stopped_count_(0) {}
  108. void OnIPCHandlerStarted(ipc::IPCManager::Type type) override {
  109. ASSERT_EQ(ipc::IPCManager::TYPE_LINUX, type);
  110. started_count_++;
  111. base::MessageLoop::current()->QuitWhenIdle();
  112. }
  113. void OnIPCHandlerStopped(ipc::IPCManager::Type type) override {
  114. ASSERT_EQ(ipc::IPCManager::TYPE_LINUX, type);
  115. stopped_count_++;
  116. base::MessageLoop::current()->QuitWhenIdle();
  117. }
  118. int started_count() const { return started_count_; }
  119. int stopped_count() const { return stopped_count_; }
  120. private:
  121. int started_count_;
  122. int stopped_count_;
  123. DISALLOW_COPY_AND_ASSIGN(TestDelegate);
  124. };
  125. TEST_F(IPCLinuxTestDisabled, StartWithNoSocketPath) {
  126. TestDelegate delegate;
  127. EXPECT_FALSE(ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, &delegate));
  128. EXPECT_FALSE(ipc_manager_->LinuxStarted());
  129. EXPECT_EQ(0, delegate.started_count());
  130. EXPECT_EQ(0, delegate.stopped_count());
  131. }
  132. TEST_F(IPCLinuxTest, BasicStartAndExit) {
  133. TestDelegate delegate;
  134. EXPECT_TRUE(ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, &delegate));
  135. EXPECT_TRUE(ipc_manager_->LinuxStarted());
  136. // Run the message loop. We will stop the loop when we receive a delegate
  137. // event.
  138. base::RunLoop().Run();
  139. // We should have received the started event.
  140. EXPECT_EQ(1, delegate.started_count());
  141. EXPECT_EQ(0, delegate.stopped_count());
  142. // At this point the thread is blocking on accept and listening for incoming
  143. // connections. TearDown should gracefully clean up the thread and the test
  144. // should succeed without hanging.
  145. ipc_manager_.reset();
  146. base::RunLoop().Run();
  147. EXPECT_EQ(1, delegate.stopped_count());
  148. }
  149. TEST_F(IPCLinuxTest, BasicStartAndConnect) {
  150. TestDelegate delegate;
  151. EXPECT_TRUE(ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, &delegate));
  152. EXPECT_TRUE(ipc_manager_->LinuxStarted());
  153. // Run the message loop. We will stop the loop when we receive a delegate
  154. // event.
  155. base::RunLoop().Run();
  156. // We should have received the started event.
  157. EXPECT_EQ(1, delegate.started_count());
  158. EXPECT_EQ(0, delegate.stopped_count());
  159. // IPC successfully started. Now attempt to connect to the socket.
  160. ConnectToTestSocket();
  161. // TODO(armansito): Test that the IPC event loop shuts down cleanly while a
  162. // client is connected. Currently this will fail and the fix is to use
  163. // MessageLoopForIO rather than a custom event loop.
  164. }
  165. } // namespace