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