OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[android-x86/system-bt.git] / service / test / ipc_unix_unittest.cpp
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_interface.h"
31 #include "service/ipc/ipc_manager.h"
32 #include "service/settings.h"
33 #include "service/test/mock_daemon.h"
34
35
36 namespace {
37
38 using testing::Return;
39
40 const char kTestSocketPath[] = "test_socket_path";
41
42 class IPCUnixTest : public ::testing::Test {
43  public:
44   IPCUnixTest() = default;
45   ~IPCUnixTest() override = default;
46
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
61     adapter_.reset(new bluetooth::Adapter());
62     ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
63   }
64
65   void TearDown() override {
66     client_fd_.reset();
67     ipc_manager_.reset();
68     adapter_.reset();
69     bluetooth::hal::BluetoothInterface::CleanUp();
70     bluetooth::Daemon::ShutDown();
71     base::CommandLine::Reset();
72   }
73
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(),
79     };
80     base::CommandLine::Init(arraysize(argv), argv);
81   }
82
83   void ConnectToTestSocket() {
84     client_fd_.reset(socket(PF_UNIX, SOCK_SEQPACKET, 0));
85     ASSERT_TRUE(client_fd_.is_valid());
86
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);
91
92     int status = connect(client_fd_.get(), (struct sockaddr *)&address,
93                          sizeof(address));
94     EXPECT_EQ(0, status);
95   }
96
97  protected:
98   base::AtExitManager exit_manager_;
99   base::MessageLoop message_loop_;
100   bluetooth::Settings settings_;
101
102   std::unique_ptr<bluetooth::Adapter> adapter_;
103   std::unique_ptr<ipc::IPCManager> ipc_manager_;
104   base::ScopedFD client_fd_;
105
106   DISALLOW_COPY_AND_ASSIGN(IPCUnixTest);
107 };
108
109 class IPCUnixTestDisabled : public IPCUnixTest {
110  public:
111   IPCUnixTestDisabled() = default;
112   ~IPCUnixTestDisabled() override = default;
113
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);
118   }
119
120  private:
121   DISALLOW_COPY_AND_ASSIGN(IPCUnixTestDisabled);
122 };
123
124 class TestDelegate : public ipc::IPCManager::Delegate,
125                      public base::SupportsWeakPtr<TestDelegate> {
126  public:
127   TestDelegate() : started_count_(0), stopped_count_(0) {
128   }
129
130   void OnIPCHandlerStarted(ipc::IPCManager::Type type) override {
131     ASSERT_EQ(ipc::IPCManager::TYPE_UNIX, type);
132     started_count_++;
133     base::MessageLoop::current()->Quit();
134   }
135
136   void OnIPCHandlerStopped(ipc::IPCManager::Type type) override {
137     ASSERT_EQ(ipc::IPCManager::TYPE_UNIX, type);
138     stopped_count_++;
139     base::MessageLoop::current()->Quit();
140   }
141
142   int started_count() const { return started_count_; }
143   int stopped_count() const { return stopped_count_; }
144
145  private:
146   int started_count_;
147   int stopped_count_;
148
149   DISALLOW_COPY_AND_ASSIGN(TestDelegate);
150 };
151
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());
158 }
159
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());
164
165   // Run the message loop. We will stop the loop when we receive a delegate
166   // event.
167   message_loop_.Run();
168
169   // We should have received the started event.
170   EXPECT_EQ(1, delegate.started_count());
171   EXPECT_EQ(0, delegate.stopped_count());
172
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();
177   message_loop_.Run();
178   EXPECT_EQ(1, delegate.stopped_count());
179 }
180
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());
185
186   // Run the message loop. We will stop the loop when we receive a delegate
187   // event.
188   message_loop_.Run();
189
190   // We should have received the started event.
191   EXPECT_EQ(1, delegate.started_count());
192   EXPECT_EQ(0, delegate.stopped_count());
193
194   // IPC successfully started. Now attempt to connect to the socket.
195   ConnectToTestSocket();
196
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.
200 }
201
202 }  // namespace