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 <base/macros.h>
18 #include <gtest/gtest.h>
19 
20 #include "service/adapter.h"
21 #include "service/common/bluetooth/util/address_helper.h"
22 #include "service/hal/fake_bluetooth_gatt_interface.h"
23 #include "service/hal/fake_bluetooth_interface.h"
24 
25 namespace bluetooth {
26 namespace {
27 
28 class AdapterTest : public ::testing::Test {
29  public:
30   AdapterTest() = default;
31   ~AdapterTest() override = default;
32 
SetUp()33   void SetUp() override {
34     fake_hal_manager_ = hal::FakeBluetoothInterface::GetManager();
35     fake_hal_iface_ = new hal::FakeBluetoothInterface();
36     hal::BluetoothInterface::InitializeForTesting(fake_hal_iface_);
37 
38     // Initialize GATT interface with default handlers.
39     hal::BluetoothGattInterface::InitializeForTesting(
40         new hal::FakeBluetoothGattInterface(nullptr, nullptr));
41 
42     adapter_ = Adapter::Create();
43   }
44 
TearDown()45   void TearDown() override {
46     adapter_.reset();
47     hal::BluetoothGattInterface::CleanUp();
48     hal::BluetoothInterface::CleanUp();
49   }
50 
51  protected:
52   hal::FakeBluetoothInterface* fake_hal_iface_;
53   hal::FakeBluetoothInterface::Manager* fake_hal_manager_;
54   std::unique_ptr<Adapter> adapter_;
55 
56  private:
57   DISALLOW_COPY_AND_ASSIGN(AdapterTest);
58 };
59 
60 class TestObserver final : public bluetooth::Adapter::Observer {
61  public:
TestObserver(bluetooth::Adapter * adapter)62   TestObserver(bluetooth::Adapter* adapter)
63       : adapter_(adapter),
64         prev_state_(bluetooth::ADAPTER_STATE_INVALID),
65         cur_state_(bluetooth::ADAPTER_STATE_INVALID),
66         last_device_connected_state_(false) {
67     CHECK(adapter_);
68     adapter_->AddObserver(this);
69   }
70 
~TestObserver()71   ~TestObserver() override {
72     adapter_->RemoveObserver(this);
73   }
74 
prev_state() const75   bluetooth::AdapterState prev_state() const { return prev_state_; }
cur_state() const76   bluetooth::AdapterState cur_state() const { return cur_state_; }
77 
last_connection_state_address() const78   std::string last_connection_state_address() const {
79     return last_connection_state_address_;
80   }
81 
last_device_connected_state() const82   bool last_device_connected_state() const {
83     return last_device_connected_state_;
84   }
85 
86   // bluetooth::Adapter::Observer override:
OnAdapterStateChanged(bluetooth::Adapter * adapter,bluetooth::AdapterState prev_state,bluetooth::AdapterState new_state)87   void OnAdapterStateChanged(bluetooth::Adapter* adapter,
88                              bluetooth::AdapterState prev_state,
89                              bluetooth::AdapterState new_state) override {
90     ASSERT_EQ(adapter_, adapter);
91     prev_state_ = prev_state;
92     cur_state_ = new_state;
93   }
94 
OnDeviceConnectionStateChanged(Adapter * adapter,const std::string & device_address,bool connected)95   void OnDeviceConnectionStateChanged(
96       Adapter* adapter,
97       const std::string& device_address,
98       bool connected) override {
99     ASSERT_EQ(adapter_, adapter);
100     last_connection_state_address_ = device_address;
101     last_device_connected_state_ = connected;
102   }
103 
104  private:
105   bluetooth::Adapter* adapter_;
106   bluetooth::AdapterState prev_state_, cur_state_;
107   std::string last_connection_state_address_;
108   bool last_device_connected_state_;
109 
110   DISALLOW_COPY_AND_ASSIGN(TestObserver);
111 };
112 
TEST_F(AdapterTest,IsEnabled)113 TEST_F(AdapterTest, IsEnabled) {
114   EXPECT_FALSE(adapter_->IsEnabled());
115 
116   fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
117   EXPECT_TRUE(adapter_->IsEnabled());
118 
119   fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_OFF);
120   EXPECT_FALSE(adapter_->IsEnabled());
121 }
122 
TEST_F(AdapterTest,Enable)123 TEST_F(AdapterTest, Enable) {
124   TestObserver observer(adapter_.get());
125 
126   EXPECT_FALSE(adapter_->IsEnabled());
127   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
128 
129   // Enable fails at HAL level
130   EXPECT_FALSE(adapter_->Enable(false));
131   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
132 
133   // Enable success
134   fake_hal_manager_->enable_succeed = true;
135   EXPECT_TRUE(adapter_->Enable(false));
136 
137   // Should have received a state update.
138   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.prev_state());
139   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, observer.cur_state());
140 
141   // Enable fails because not disabled
142   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, adapter_->GetState());
143   EXPECT_FALSE(adapter_->Enable(false));
144 
145   // Adapter state updates properly
146   fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
147   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, adapter_->GetState());
148 
149   // Should have received a state update.
150   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_ON, observer.prev_state());
151   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
152 
153   // Enable fails because already enabled
154   EXPECT_FALSE(adapter_->Enable(false));
155 }
156 
TEST_F(AdapterTest,Disable)157 TEST_F(AdapterTest, Disable) {
158   TestObserver observer(adapter_.get());
159 
160   fake_hal_manager_->disable_succeed = true;
161   EXPECT_FALSE(adapter_->IsEnabled());
162   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
163 
164   // Disable fails because already disabled
165   EXPECT_FALSE(adapter_->Disable());
166   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, adapter_->GetState());
167 
168   // Disable success
169   fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
170 
171   // Should have received a state update.
172   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.prev_state());
173   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
174 
175   EXPECT_TRUE(adapter_->Disable());
176 
177   // Should have received a state update.
178   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.prev_state());
179   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.cur_state());
180 
181   // Disable fails because not enabled
182   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, adapter_->GetState());
183   EXPECT_FALSE(adapter_->Disable());
184 
185   fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_ON);
186   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, adapter_->GetState());
187 
188   // Should have received a state update.
189   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.prev_state());
190   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
191 
192   // Disable fails at HAL level
193   fake_hal_manager_->disable_succeed = false;
194   EXPECT_FALSE(adapter_->Disable());
195 
196   // Should have received a state update. In this case we will receive two
197   // updates: one going from OFF to TURNING_OFF, and one going from TURNING_OFF
198   // back to ON since we failed to initiate the disable operation.
199   EXPECT_EQ(bluetooth::ADAPTER_STATE_TURNING_OFF, observer.prev_state());
200   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.cur_state());
201 
202   // Update state to OFF. Should receive a state update.
203   fake_hal_iface_->NotifyAdapterStateChanged(BT_STATE_OFF);
204   EXPECT_EQ(bluetooth::ADAPTER_STATE_ON, observer.prev_state());
205   EXPECT_EQ(bluetooth::ADAPTER_STATE_OFF, observer.cur_state());
206 }
207 
TEST_F(AdapterTest,GetName)208 TEST_F(AdapterTest, GetName) {
209   EXPECT_EQ(bluetooth::Adapter::kDefaultName, adapter_->GetName());
210 
211   const char kTestAdapterName[] = "Test Adapter Name";
212 
213   fake_hal_iface_->NotifyAdapterNamePropertyChanged(kTestAdapterName);
214   EXPECT_EQ(kTestAdapterName, adapter_->GetName());
215 }
216 
TEST_F(AdapterTest,SetName)217 TEST_F(AdapterTest, SetName) {
218   bt_bdname_t hal_name;
219 
220   // Name too large.
221   EXPECT_FALSE(adapter_->SetName(std::string(sizeof(hal_name.name), 'a')));
222 
223   // Valid length.
224   EXPECT_FALSE(adapter_->SetName("Test Name"));
225   fake_hal_manager_->set_property_succeed = true;
226   EXPECT_TRUE(adapter_->SetName("Test Name"));
227 }
228 
TEST_F(AdapterTest,GetAddress)229 TEST_F(AdapterTest, GetAddress) {
230   EXPECT_EQ(bluetooth::Adapter::kDefaultAddress, adapter_->GetAddress());
231 
232   const bt_bdaddr_t kTestAdapterInput = {
233     { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc }
234   };
235   const char kTestAdapterAddressOutput[] = "12:34:56:78:9A:BC";
236 
237   fake_hal_iface_->NotifyAdapterAddressPropertyChanged(&kTestAdapterInput);
238   EXPECT_EQ(kTestAdapterAddressOutput, adapter_->GetAddress());
239 }
240 
TEST_F(AdapterTest,IsMultiAdvertisementSupported)241 TEST_F(AdapterTest, IsMultiAdvertisementSupported) {
242   EXPECT_FALSE(adapter_->IsMultiAdvertisementSupported());
243 
244   bt_local_le_features_t features;
245   memset(&features, 0, sizeof(features));
246 
247   features.max_adv_instance = 10;  // Some high number.
248   fake_hal_iface_->NotifyAdapterLocalLeFeaturesPropertyChanged(&features);
249   EXPECT_TRUE(adapter_->IsMultiAdvertisementSupported());
250 
251   features.max_adv_instance = 0;  // Low number.
252   fake_hal_iface_->NotifyAdapterLocalLeFeaturesPropertyChanged(&features);
253   EXPECT_FALSE(adapter_->IsMultiAdvertisementSupported());
254 }
255 
TEST_F(AdapterTest,IsDeviceConnected)256 TEST_F(AdapterTest, IsDeviceConnected) {
257   const char kDeviceAddr[] = "12:34:56:78:9A:BC";
258   TestObserver observer(adapter_.get());
259 
260   EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
261 
262   bt_bdaddr_t hal_addr;
263   ASSERT_TRUE(util::BdAddrFromString(kDeviceAddr, &hal_addr));
264 
265   // status != BT_STATUS_SUCCESS should be ignored
266   fake_hal_iface_->NotifyAclStateChangedCallback(
267       BT_STATUS_FAIL, hal_addr, BT_ACL_STATE_CONNECTED);
268   EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
269   EXPECT_TRUE(observer.last_connection_state_address().empty());
270   EXPECT_FALSE(observer.last_device_connected_state());
271 
272   // Connected
273   fake_hal_iface_->NotifyAclStateChangedCallback(
274       BT_STATUS_SUCCESS, hal_addr, BT_ACL_STATE_CONNECTED);
275   EXPECT_TRUE(adapter_->IsDeviceConnected(kDeviceAddr));
276   EXPECT_EQ(kDeviceAddr, observer.last_connection_state_address());
277   EXPECT_TRUE(observer.last_device_connected_state());
278 
279   // Disconnected
280   fake_hal_iface_->NotifyAclStateChangedCallback(
281       BT_STATUS_SUCCESS, hal_addr, BT_ACL_STATE_DISCONNECTED);
282   EXPECT_FALSE(adapter_->IsDeviceConnected(kDeviceAddr));
283   EXPECT_EQ(kDeviceAddr, observer.last_connection_state_address());
284   EXPECT_FALSE(observer.last_device_connected_state());
285 }
286 
287 }  // namespace
288 }  // namespace bluetooth
289