2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/socket.h>
22 #include <base/at_exit.h>
23 #include <base/command_line.h>
24 #include <base/files/scoped_file.h>
25 #include <base/macros.h>
26 #include <base/strings/stringprintf.h>
27 #include <gtest/gtest.h>
29 #include "service/adapter.h"
30 #include "service/hal/fake_bluetooth_interface.h"
31 #include "service/ipc/ipc_manager.h"
32 #include "service/settings.h"
33 #include "service/test/mock_daemon.h"
38 using testing::Return;
40 const char kTestSocketPath[] = "test_socket_path";
42 class IPCUnixTest : public ::testing::Test {
44 IPCUnixTest() = default;
45 ~IPCUnixTest() override = default;
47 void SetUp() override {
49 ASSERT_TRUE(settings_.Init());
51 auto mock_daemon = new bluetooth::testing::MockDaemon();
53 ON_CALL(*mock_daemon, GetSettings()).WillByDefault(Return(&settings_));
54 ON_CALL(*mock_daemon, GetMessageLoop())
55 .WillByDefault(Return(&message_loop_));
57 bluetooth::Daemon::InitializeForTesting(mock_daemon);
58 bluetooth::hal::BluetoothInterface::InitializeForTesting(
59 new bluetooth::hal::FakeBluetoothInterface());
61 adapter_.reset(new bluetooth::Adapter());
62 ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
65 void TearDown() override {
69 bluetooth::hal::BluetoothInterface::CleanUp();
70 bluetooth::Daemon::ShutDown();
71 base::CommandLine::Reset();
74 virtual void SetUpCommandLine() {
75 std::string ipc_socket_arg =
76 base::StringPrintf("--create-ipc-socket=%s", kTestSocketPath);
77 const base::CommandLine::CharType* argv[] = {
78 "program", ipc_socket_arg.c_str(),
80 base::CommandLine::Init(arraysize(argv), argv);
83 void ConnectToTestSocket() {
84 client_fd_.reset(socket(PF_UNIX, SOCK_SEQPACKET, 0));
85 ASSERT_TRUE(client_fd_.is_valid());
87 struct sockaddr_un address;
88 memset(&address, 0, sizeof(address));
89 address.sun_family = AF_UNIX;
90 strncpy(address.sun_path, kTestSocketPath, sizeof(address.sun_path) - 1);
92 int status = connect(client_fd_.get(), (struct sockaddr *)&address,
98 base::AtExitManager exit_manager_;
99 base::MessageLoop message_loop_;
100 bluetooth::Settings settings_;
102 std::unique_ptr<bluetooth::Adapter> adapter_;
103 std::unique_ptr<ipc::IPCManager> ipc_manager_;
104 base::ScopedFD client_fd_;
106 DISALLOW_COPY_AND_ASSIGN(IPCUnixTest);
109 class IPCUnixTestDisabled : public IPCUnixTest {
111 IPCUnixTestDisabled() = default;
112 ~IPCUnixTestDisabled() override = default;
114 void SetUpCommandLine() override {
115 // Set up with no --ipc-socket-path
116 const base::CommandLine::CharType* argv[] = { "program" };
117 base::CommandLine::Init(arraysize(argv), argv);
121 DISALLOW_COPY_AND_ASSIGN(IPCUnixTestDisabled);
124 class TestDelegate : public ipc::IPCManager::Delegate,
125 public base::SupportsWeakPtr<TestDelegate> {
127 TestDelegate() : started_count_(0), stopped_count_(0) {
130 void OnIPCHandlerStarted(ipc::IPCManager::Type type) override {
131 ASSERT_EQ(ipc::IPCManager::TYPE_UNIX, type);
133 base::MessageLoop::current()->Quit();
136 void OnIPCHandlerStopped(ipc::IPCManager::Type type) override {
137 ASSERT_EQ(ipc::IPCManager::TYPE_UNIX, type);
139 base::MessageLoop::current()->Quit();
142 int started_count() const { return started_count_; }
143 int stopped_count() const { return stopped_count_; }
149 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
152 TEST_F(IPCUnixTestDisabled, StartWithNoSocketPath) {
153 TestDelegate delegate;
154 EXPECT_FALSE(ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX, &delegate));
155 EXPECT_FALSE(ipc_manager_->UnixStarted());
156 EXPECT_EQ(0, delegate.started_count());
157 EXPECT_EQ(0, delegate.stopped_count());
160 TEST_F(IPCUnixTest, BasicStartAndExit) {
161 TestDelegate delegate;
162 EXPECT_TRUE(ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX, &delegate));
163 EXPECT_TRUE(ipc_manager_->UnixStarted());
165 // Run the message loop. We will stop the loop when we receive a delegate
169 // We should have received the started event.
170 EXPECT_EQ(1, delegate.started_count());
171 EXPECT_EQ(0, delegate.stopped_count());
173 // At this point the thread is blocking on accept and listening for incoming
174 // connections. TearDown should gracefully clean up the thread and the test
175 // should succeed without hanging.
176 ipc_manager_.reset();
178 EXPECT_EQ(1, delegate.stopped_count());
181 TEST_F(IPCUnixTest, BasicStartAndConnect) {
182 TestDelegate delegate;
183 EXPECT_TRUE(ipc_manager_->Start(ipc::IPCManager::TYPE_UNIX, &delegate));
184 EXPECT_TRUE(ipc_manager_->UnixStarted());
186 // Run the message loop. We will stop the loop when we receive a delegate
190 // We should have received the started event.
191 EXPECT_EQ(1, delegate.started_count());
192 EXPECT_EQ(0, delegate.stopped_count());
194 // IPC successfully started. Now attempt to connect to the socket.
195 ConnectToTestSocket();
197 // TODO(armansito): Test that the IPC event loop shuts down cleanly while a
198 // client is connected. Currently this will fail and the fix is to use
199 // MessageLoopForIO rather than a custom event loop.