1/******************************************************************************
2 *
3 *  Copyright (C) 2015 Google, Inc.
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19#include "adapter/bluetooth_test.h"
20#include "btcore/include/property.h"
21#include "stack/include/bt_types.h"
22
23namespace {
24
25// Each iteration of the test takes about 2 seconds to run, so choose a value
26// that matches your time constraints. For example, 5 iterations would take
27// about 10 seconds to run
28const int kTestRepeatCount = 5;
29
30}  // namespace
31
32namespace bttest {
33
34TEST_F(BluetoothTest, AdapterEnableDisable) {
35  EXPECT_EQ(GetState(), BT_STATE_OFF)
36      << "Test should be run with Adapter disabled";
37
38  EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
39  semaphore_wait(adapter_state_changed_callback_sem_);
40  EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
41
42  EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
43  semaphore_wait(adapter_state_changed_callback_sem_);
44  EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
45}
46
47TEST_F(BluetoothTest, AdapterRepeatedEnableDisable) {
48  EXPECT_EQ(GetState(), BT_STATE_OFF)
49      << "Test should be run with Adapter disabled";
50
51  for (int i = 0; i < kTestRepeatCount; ++i) {
52    EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
53    semaphore_wait(adapter_state_changed_callback_sem_);
54    EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
55
56    EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
57    semaphore_wait(adapter_state_changed_callback_sem_);
58    EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
59  }
60}
61
62TEST_F(BluetoothTest, AdapterSetGetName) {
63  bt_property_t* new_name = property_new_name("BluetoothTestName1");
64
65  EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
66  semaphore_wait(adapter_state_changed_callback_sem_);
67  EXPECT_EQ(GetState(), BT_STATE_ON)
68      << "Test should be run with Adapter enabled";
69
70  // Enabling the interface will call the properties callback twice before
71  // ever reaching this point.
72  ClearSemaphore(adapter_properties_callback_sem_);
73
74  EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME),
75            BT_STATUS_SUCCESS);
76  semaphore_wait(adapter_properties_callback_sem_);
77  EXPECT_GT(GetPropertiesChangedCount(), 0)
78      << "Expected at least one adapter property to change";
79  bt_property_t* name_property = GetProperty(BT_PROPERTY_BDNAME);
80  EXPECT_NE(name_property, nullptr);
81  if (property_equals(name_property, new_name)) {
82    property_free(new_name);
83    new_name = property_new_name("BluetoothTestName2");
84  }
85  std::string old_name((const char*)property_as_name(name_property)->name);
86
87  EXPECT_EQ(bt_interface()->set_adapter_property(new_name), BT_STATUS_SUCCESS);
88  semaphore_wait(adapter_properties_callback_sem_);
89  EXPECT_GT(GetPropertiesChangedCount(), 0)
90      << "Expected at least one adapter property to change";
91  EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME))
92      << "The Bluetooth name property did not change.";
93  EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), new_name))
94      << "Bluetooth name "
95      << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
96      << " does not match test value " << property_as_name(new_name)->name;
97
98  bt_property_t* old_name_property = property_new_name(old_name.c_str());
99  EXPECT_EQ(bt_interface()->set_adapter_property(old_name_property),
100            BT_STATUS_SUCCESS);
101  semaphore_wait(adapter_properties_callback_sem_);
102  EXPECT_TRUE(
103      property_equals(GetProperty(BT_PROPERTY_BDNAME), old_name_property))
104      << "Bluetooth name "
105      << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
106      << " does not match original name" << old_name;
107
108  EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
109  semaphore_wait(adapter_state_changed_callback_sem_);
110  EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
111  property_free(new_name);
112  property_free(old_name_property);
113}
114
115TEST_F(BluetoothTest, AdapterStartDiscovery) {
116  EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
117  semaphore_wait(adapter_state_changed_callback_sem_);
118  EXPECT_EQ(GetState(), BT_STATE_ON)
119      << "Test should be run with Adapter enabled";
120
121  EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
122  semaphore_wait(discovery_state_changed_callback_sem_);
123  EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STARTED)
124      << "Unable to start discovery.";
125
126  EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
127  semaphore_wait(adapter_state_changed_callback_sem_);
128  EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
129}
130
131TEST_F(BluetoothTest, AdapterCancelDiscovery) {
132  EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
133  semaphore_wait(adapter_state_changed_callback_sem_);
134  EXPECT_EQ(GetState(), BT_STATE_ON)
135      << "Test should be run with Adapter enabled";
136
137  EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
138  semaphore_wait(discovery_state_changed_callback_sem_);
139  EXPECT_EQ(bt_interface()->cancel_discovery(), BT_STATUS_SUCCESS);
140  semaphore_wait(discovery_state_changed_callback_sem_);
141
142  EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STOPPED)
143      << "Unable to stop discovery.";
144
145  EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
146  semaphore_wait(adapter_state_changed_callback_sem_);
147  EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
148}
149
150TEST_F(BluetoothTest, AdapterDisableDuringBonding) {
151  EXPECT_EQ(GetState(), BT_STATE_OFF)
152      << "Test should be run with Adapter disabled";
153
154  RawAddress bdaddr = {{0x22, 0x22, 0x22, 0x22, 0x22, 0x22}};
155
156  for (int i = 0; i < kTestRepeatCount; ++i) {
157    EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
158    semaphore_wait(adapter_state_changed_callback_sem_);
159    EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
160
161    EXPECT_EQ(bt_interface()->create_bond(&bdaddr, BT_TRANSPORT_BR_EDR),
162              BT_STATUS_SUCCESS);
163
164    EXPECT_EQ(bt_interface()->cancel_bond(&bdaddr), BT_STATUS_SUCCESS);
165
166    EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
167    semaphore_wait(adapter_state_changed_callback_sem_);
168    EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
169  }
170}
171
172TEST_F(BluetoothTest, AdapterCleanupDuringDiscovery) {
173  EXPECT_EQ(GetState(), BT_STATE_OFF)
174      << "Test should be run with Adapter disabled";
175
176  bt_callbacks_t* bt_callbacks =
177      bluetooth::hal::BluetoothInterface::Get()->GetHALCallbacks();
178  ASSERT_TRUE(bt_callbacks != nullptr);
179
180  for (int i = 0; i < kTestRepeatCount; ++i) {
181    bt_interface()->init(bt_callbacks);
182    EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
183    semaphore_wait(adapter_state_changed_callback_sem_);
184    EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
185
186    EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
187
188    EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
189    semaphore_wait(adapter_state_changed_callback_sem_);
190    EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
191    bt_interface()->cleanup();
192  }
193}
194
195}  // bttest
196