Home | History | Annotate | Download | only in test
      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 
     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 
     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 
     64     adapter_ = bluetooth::Adapter::Create();
     65     ipc_manager_.reset(new ipc::IPCManager(adapter_.get()));
     66   }
     67 
     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 
     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 
     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 = connect(client_fd_.get(), (struct sockaddr *)&address,
     97                          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 
    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:
    131   TestDelegate() : started_count_(0), stopped_count_(0) {
    132   }
    133 
    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 
    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 
    146   int started_count() const { return started_count_; }
    147   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 
    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 
    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   message_loop_.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   message_loop_.Run();
    182   EXPECT_EQ(1, delegate.stopped_count());
    183 }
    184 
    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   message_loop_.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