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