1 /******************************************************************************
2 *
3 * Copyright 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 <bluetooth/log.h>
20
21 #include "adapter/bluetooth_test.h"
22 #include "osi/include/allocator.h"
23 #include "osi/include/compat.h"
24 #include "types/bt_transport.h"
25 #include "types/raw_address.h"
26
27 namespace {
28
29 // Each iteration of the test takes about 2 seconds to run, so choose a value
30 // that matches your time constraints. For example, 5 iterations would take
31 // about 10 seconds to run
32 const int kTestRepeatCount = 5;
33
34 } // namespace
35
36 namespace bttest {
37
TEST_F(BluetoothTest,AdapterEnableDisable)38 TEST_F(BluetoothTest, AdapterEnableDisable) {
39 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Test should be run with Adapter disabled";
40
41 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
42 semaphore_wait(adapter_state_changed_callback_sem_);
43 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
44
45 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
46 semaphore_wait(adapter_state_changed_callback_sem_);
47 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
48 }
49
TEST_F(BluetoothTest,AdapterRepeatedEnableDisable)50 TEST_F(BluetoothTest, AdapterRepeatedEnableDisable) {
51 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Test should be run with Adapter disabled";
52
53 for (int i = 0; i < kTestRepeatCount; ++i) {
54 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
55 semaphore_wait(adapter_state_changed_callback_sem_);
56 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
57
58 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
59 semaphore_wait(adapter_state_changed_callback_sem_);
60 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
61 }
62 }
63
property_new_name(const char * name)64 static bt_property_t* property_new_name(const char* name) {
65 bluetooth::log::assert_that(name != NULL, "assert failed: name != NULL");
66 bt_property_t* property = static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t)));
67
68 property->val = osi_calloc(sizeof(bt_bdname_t) + 1);
69 osi_strlcpy((char*)property->val, name, sizeof(bt_bdname_t));
70
71 property->type = BT_PROPERTY_BDNAME;
72 property->len = sizeof(bt_bdname_t);
73
74 return property;
75 }
76
property_free(bt_property_t * property)77 static void property_free(bt_property_t* property) {
78 if (property == NULL) {
79 return;
80 }
81
82 osi_free(property->val);
83 osi_free(property);
84 }
85
property_as_name(const bt_property_t * property)86 static const bt_bdname_t* property_as_name(const bt_property_t* property) {
87 bluetooth::log::assert_that(property->type == BT_PROPERTY_BDNAME,
88 "assert failed: property_is_name(property)");
89 return (const bt_bdname_t*)property->val;
90 }
91
property_equals(const bt_property_t * p1,const bt_property_t * p2)92 static bool property_equals(const bt_property_t* p1, const bt_property_t* p2) {
93 if (!p1 || !p2 || p1->type != p2->type) {
94 return false;
95 }
96
97 if (p1->type == BT_PROPERTY_BDNAME && p1->len != p2->len) {
98 const bt_property_t *shorter = p1, *longer = p2;
99 if (p1->len > p2->len) {
100 shorter = p2;
101 longer = p1;
102 }
103 return strlen((const char*)longer->val) == (size_t)shorter->len &&
104 !memcmp(longer->val, shorter->val, shorter->len);
105 }
106
107 return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len);
108 }
109
TEST_F(BluetoothTest,AdapterSetGetName)110 TEST_F(BluetoothTest, AdapterSetGetName) {
111 bt_property_t* new_name = property_new_name("BluetoothTestName1");
112
113 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
114 semaphore_wait(adapter_state_changed_callback_sem_);
115 EXPECT_EQ(GetState(), BT_STATE_ON) << "Test should be run with Adapter enabled";
116
117 // Enabling the interface will call the properties callback twice before
118 // ever reaching this point.
119 ClearSemaphore(adapter_properties_callback_sem_);
120
121 EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME), BT_STATUS_SUCCESS);
122 semaphore_wait(adapter_properties_callback_sem_);
123 EXPECT_GT(GetPropertiesChangedCount(), 0) << "Expected at least one adapter property to change";
124 bt_property_t* name_property = GetProperty(BT_PROPERTY_BDNAME);
125 EXPECT_NE(name_property, nullptr);
126 if (property_equals(name_property, new_name)) {
127 property_free(new_name);
128 new_name = property_new_name("BluetoothTestName2");
129 }
130 std::string old_name((const char*)property_as_name(name_property)->name, name_property->len);
131
132 EXPECT_EQ(bt_interface()->set_adapter_property(new_name), BT_STATUS_SUCCESS);
133 semaphore_wait(adapter_properties_callback_sem_);
134 EXPECT_GT(GetPropertiesChangedCount(), 0) << "Expected at least one adapter property to change";
135 EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME)) << "The Bluetooth name property did not change.";
136 EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), new_name))
137 << "Bluetooth name " << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
138 << " does not match test value " << property_as_name(new_name)->name;
139
140 bt_property_t* old_name_property = property_new_name(old_name.c_str());
141 EXPECT_EQ(bt_interface()->set_adapter_property(old_name_property), BT_STATUS_SUCCESS);
142 semaphore_wait(adapter_properties_callback_sem_);
143 EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), old_name_property))
144 << "Bluetooth name " << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
145 << " does not match original name" << old_name;
146
147 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
148 semaphore_wait(adapter_state_changed_callback_sem_);
149 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
150 property_free(new_name);
151 property_free(old_name_property);
152 }
153
TEST_F(BluetoothTest,AdapterStartDiscovery)154 TEST_F(BluetoothTest, AdapterStartDiscovery) {
155 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
156 semaphore_wait(adapter_state_changed_callback_sem_);
157 EXPECT_EQ(GetState(), BT_STATE_ON) << "Test should be run with Adapter enabled";
158
159 EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
160 semaphore_wait(discovery_state_changed_callback_sem_);
161 EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STARTED) << "Unable to start discovery.";
162
163 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
164 semaphore_wait(adapter_state_changed_callback_sem_);
165 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
166 }
167
TEST_F(BluetoothTest,AdapterCancelDiscovery)168 TEST_F(BluetoothTest, AdapterCancelDiscovery) {
169 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
170 semaphore_wait(adapter_state_changed_callback_sem_);
171 EXPECT_EQ(GetState(), BT_STATE_ON) << "Test should be run with Adapter enabled";
172
173 EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
174 semaphore_wait(discovery_state_changed_callback_sem_);
175 EXPECT_EQ(bt_interface()->cancel_discovery(), BT_STATUS_SUCCESS);
176 semaphore_wait(discovery_state_changed_callback_sem_);
177
178 EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STOPPED) << "Unable to stop discovery.";
179
180 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
181 semaphore_wait(adapter_state_changed_callback_sem_);
182 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
183 }
184
TEST_F(BluetoothTest,AdapterDisableDuringBonding)185 TEST_F(BluetoothTest, AdapterDisableDuringBonding) {
186 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Test should be run with Adapter disabled";
187
188 RawAddress bdaddr = {{0x22, 0x22, 0x22, 0x22, 0x22, 0x22}};
189
190 for (int i = 0; i < kTestRepeatCount; ++i) {
191 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
192 semaphore_wait(adapter_state_changed_callback_sem_);
193 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
194
195 EXPECT_EQ(bt_interface()->create_bond(&bdaddr, BT_TRANSPORT_BR_EDR), BT_STATUS_SUCCESS);
196
197 EXPECT_EQ(bt_interface()->cancel_bond(&bdaddr), BT_STATUS_SUCCESS);
198
199 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
200 semaphore_wait(adapter_state_changed_callback_sem_);
201 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
202 }
203 }
204
TEST_F(BluetoothTest,AdapterCleanupDuringDiscovery)205 TEST_F(BluetoothTest, AdapterCleanupDuringDiscovery) {
206 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Test should be run with Adapter disabled";
207
208 bt_callbacks_t* callbacks = bt_callbacks();
209 ASSERT_TRUE(callbacks != nullptr);
210
211 for (int i = 0; i < kTestRepeatCount; ++i) {
212 bt_interface()->init(callbacks, false, false, 0, false);
213 EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
214 semaphore_wait(adapter_state_changed_callback_sem_);
215 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
216
217 EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
218
219 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
220 semaphore_wait(adapter_state_changed_callback_sem_);
221 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
222 bt_interface()->cleanup();
223 }
224 }
225
226 } // namespace bttest
227