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