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 
17 #include <memory>
18 
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 
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/run_loop.h>
27 #include <base/strings/stringprintf.h>
28 #include <gtest/gtest.h>
29 
30 #include "abstract_message_loop"
31 #include "array_utils.h"
32 #include "service/adapter.h"
33 #include "service/hal/fake_bluetooth_gatt_interface.h"
34 #include "service/hal/fake_bluetooth_interface.h"
35 #include "service/ipc/ipc_manager.h"
36 #include "service/settings.h"
37 #include "service/test/mock_daemon.h"
38 
39 namespace {
40 
41 using testing::Return;
42 
43 const char kTestSocketPath[] = "test_socket_path";
44 
45 class IPCLinuxTest : public ::testing::Test {
46  public:
47   IPCLinuxTest() = default;
48   ~IPCLinuxTest() override = default;
49 
SetUp()50   void SetUp() override {
51     SetUpCommandLine();
52     ASSERT_TRUE(settings_.Init());
53 
54     auto mock_daemon = new bluetooth::testing::MockDaemon();
55 
56     ON_CALL(*mock_daemon, GetSettings()).WillByDefault(Return(&settings_));
57     ON_CALL(*mock_daemon, GetMessageLoop())
58         .WillByDefault(Return(&message_loop_));
59 
60     bluetooth::Daemon::InitializeForTesting(mock_daemon);
61     bluetooth::hal::BluetoothInterface::InitializeForTesting(
62         new bluetooth::hal::FakeBluetoothInterface());
63     bluetooth::hal::BluetoothGattInterface::InitializeForTesting(
64         new bluetooth::hal::FakeBluetoothGattInterface(nullptr, nullptr,
65                                                        nullptr, nullptr));
66 
67     adapter_ = bluetooth::Adapter::Create();
68     ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
69   }
70 
TearDown()71   void TearDown() override {
72     client_fd_.reset();
73     ipc_manager_.reset();
74     adapter_.reset();
75     bluetooth::hal::BluetoothGattInterface::CleanUp();
76     bluetooth::hal::BluetoothInterface::CleanUp();
77     bluetooth::Daemon::ShutDown();
78     base::CommandLine::Reset();
79   }
80 
SetUpCommandLine()81   virtual void SetUpCommandLine() {
82     std::string ipc_socket_arg =
83         base::StringPrintf("--create-ipc-socket=%s", kTestSocketPath);
84     const base::CommandLine::CharType* argv[] = {
85         "program", ipc_socket_arg.c_str(),
86     };
87     base::CommandLine::Init(ARRAY_SIZE(argv), argv);
88   }
89 
ConnectToTestSocket()90   void ConnectToTestSocket() {
91     client_fd_.reset(socket(PF_UNIX, SOCK_SEQPACKET, 0));
92     ASSERT_TRUE(client_fd_.is_valid());
93 
94     struct sockaddr_un address;
95     memset(&address, 0, sizeof(address));
96     address.sun_family = AF_UNIX;
97     strncpy(address.sun_path, kTestSocketPath, sizeof(address.sun_path) - 1);
98 
99     int status =
100         connect(client_fd_.get(), (struct sockaddr*)&address, sizeof(address));
101     EXPECT_EQ(0, status);
102   }
103 
104  protected:
105   base::AtExitManager exit_manager_;
106   DEFINE_TEST_TASK_ENV(message_loop_);
107   bluetooth::Settings settings_;
108 
109   std::unique_ptr<bluetooth::Adapter> adapter_;
110   std::unique_ptr<ipc::IPCManager> ipc_manager_;
111   base::ScopedFD client_fd_;
112 
113   DISALLOW_COPY_AND_ASSIGN(IPCLinuxTest);
114 };
115 
116 class IPCLinuxTestDisabled : public IPCLinuxTest {
117  public:
118   IPCLinuxTestDisabled() = default;
119   ~IPCLinuxTestDisabled() override = default;
120 
SetUpCommandLine()121   void SetUpCommandLine() override {
122     // Set up with no --ipc-socket-path
123     const base::CommandLine::CharType* argv[] = {"program"};
124     base::CommandLine::Init(ARRAY_SIZE(argv), argv);
125   }
126 
127  private:
128   DISALLOW_COPY_AND_ASSIGN(IPCLinuxTestDisabled);
129 };
130 
131 class TestDelegate : public ipc::IPCManager::Delegate,
132                      public base::SupportsWeakPtr<TestDelegate> {
133  public:
TestDelegate()134   TestDelegate() : started_count_(0), stopped_count_(0) {}
135 
OnIPCHandlerStarted(ipc::IPCManager::Type type)136   void OnIPCHandlerStarted(ipc::IPCManager::Type type) override {
137     ASSERT_EQ(ipc::IPCManager::TYPE_LINUX, type);
138     started_count_++;
139     btbase::AbstractTestMessageLoop::currentIO()->QuitWhenIdle();
140   }
141 
OnIPCHandlerStopped(ipc::IPCManager::Type type)142   void OnIPCHandlerStopped(ipc::IPCManager::Type type) override {
143     ASSERT_EQ(ipc::IPCManager::TYPE_LINUX, type);
144     stopped_count_++;
145     btbase::AbstractTestMessageLoop::currentIO()->QuitWhenIdle();
146   }
147 
started_count() const148   int started_count() const { return started_count_; }
stopped_count() const149   int stopped_count() const { return stopped_count_; }
150 
151  private:
152   int started_count_;
153   int stopped_count_;
154 
155   DISALLOW_COPY_AND_ASSIGN(TestDelegate);
156 };
157 
TEST_F(IPCLinuxTestDisabled,StartWithNoSocketPath)158 TEST_F(IPCLinuxTestDisabled, StartWithNoSocketPath) {
159   TestDelegate delegate;
160   EXPECT_FALSE(ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, &delegate));
161   EXPECT_FALSE(ipc_manager_->LinuxStarted());
162   EXPECT_EQ(0, delegate.started_count());
163   EXPECT_EQ(0, delegate.stopped_count());
164 }
165 
TEST_F(IPCLinuxTest,BasicStartAndExit)166 TEST_F(IPCLinuxTest, BasicStartAndExit) {
167   TestDelegate delegate;
168   EXPECT_TRUE(ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, &delegate));
169   EXPECT_TRUE(ipc_manager_->LinuxStarted());
170 
171   // Run the message loop. We will stop the loop when we receive a delegate
172   // event.
173   base::RunLoop().Run();
174 
175   // We should have received the started event.
176   EXPECT_EQ(1, delegate.started_count());
177   EXPECT_EQ(0, delegate.stopped_count());
178 
179   // At this point the thread is blocking on accept and listening for incoming
180   // connections. TearDown should gracefully clean up the thread and the test
181   // should succeed without hanging.
182   ipc_manager_.reset();
183   base::RunLoop().Run();
184   EXPECT_EQ(1, delegate.stopped_count());
185 }
186 
TEST_F(IPCLinuxTest,BasicStartAndConnect)187 TEST_F(IPCLinuxTest, BasicStartAndConnect) {
188   TestDelegate delegate;
189   EXPECT_TRUE(ipc_manager_->Start(ipc::IPCManager::TYPE_LINUX, &delegate));
190   EXPECT_TRUE(ipc_manager_->LinuxStarted());
191 
192   // Run the message loop. We will stop the loop when we receive a delegate
193   // event.
194   base::RunLoop().Run();
195 
196   // We should have received the started event.
197   EXPECT_EQ(1, delegate.started_count());
198   EXPECT_EQ(0, delegate.stopped_count());
199 
200   // IPC successfully started. Now attempt to connect to the socket.
201   ConnectToTestSocket();
202 
203   // TODO(armansito): Test that the IPC event loop shuts down cleanly while a
204   // client is connected. Currently this will fail and the fix is to use
205   // MessageLoopForIO rather than a custom event loop.
206 }
207 
208 }  // namespace
209