• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 namespace {
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
28 const int kTestRepeatCount = 5;
29 
30 }  // namespace
31 
32 namespace bttest {
33 
TEST_F(BluetoothTest,AdapterEnableDisable)34 TEST_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(), 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 
TEST_F(BluetoothTest,AdapterRepeatedEnableDisable)47 TEST_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(), 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 
TEST_F(BluetoothTest,AdapterSetGetName)62 TEST_F(BluetoothTest, AdapterSetGetName) {
63   bt_property_t* new_name = property_new_name("BluetoothTestName1");
64 
65   EXPECT_EQ(bt_interface()->enable(), 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                        name_property->len);
87 
88   EXPECT_EQ(bt_interface()->set_adapter_property(new_name), BT_STATUS_SUCCESS);
89   semaphore_wait(adapter_properties_callback_sem_);
90   EXPECT_GT(GetPropertiesChangedCount(), 0)
91       << "Expected at least one adapter property to change";
92   EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME))
93       << "The Bluetooth name property did not change.";
94   EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), new_name))
95       << "Bluetooth name "
96       << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
97       << " does not match test value " << property_as_name(new_name)->name;
98 
99   bt_property_t* old_name_property = property_new_name(old_name.c_str());
100   EXPECT_EQ(bt_interface()->set_adapter_property(old_name_property),
101             BT_STATUS_SUCCESS);
102   semaphore_wait(adapter_properties_callback_sem_);
103   EXPECT_TRUE(
104       property_equals(GetProperty(BT_PROPERTY_BDNAME), old_name_property))
105       << "Bluetooth name "
106       << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
107       << " does not match original name" << old_name;
108 
109   EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
110   semaphore_wait(adapter_state_changed_callback_sem_);
111   EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
112   property_free(new_name);
113   property_free(old_name_property);
114 }
115 
TEST_F(BluetoothTest,AdapterStartDiscovery)116 TEST_F(BluetoothTest, AdapterStartDiscovery) {
117   EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
118   semaphore_wait(adapter_state_changed_callback_sem_);
119   EXPECT_EQ(GetState(), BT_STATE_ON)
120       << "Test should be run with Adapter enabled";
121 
122   EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
123   semaphore_wait(discovery_state_changed_callback_sem_);
124   EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STARTED)
125       << "Unable to start discovery.";
126 
127   EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
128   semaphore_wait(adapter_state_changed_callback_sem_);
129   EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
130 }
131 
TEST_F(BluetoothTest,AdapterCancelDiscovery)132 TEST_F(BluetoothTest, AdapterCancelDiscovery) {
133   EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
134   semaphore_wait(adapter_state_changed_callback_sem_);
135   EXPECT_EQ(GetState(), BT_STATE_ON)
136       << "Test should be run with Adapter enabled";
137 
138   EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
139   semaphore_wait(discovery_state_changed_callback_sem_);
140   EXPECT_EQ(bt_interface()->cancel_discovery(), BT_STATUS_SUCCESS);
141   semaphore_wait(discovery_state_changed_callback_sem_);
142 
143   EXPECT_EQ(GetDiscoveryState(), BT_DISCOVERY_STOPPED)
144       << "Unable to stop discovery.";
145 
146   EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
147   semaphore_wait(adapter_state_changed_callback_sem_);
148   EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
149 }
150 
TEST_F(BluetoothTest,AdapterDisableDuringBonding)151 TEST_F(BluetoothTest, AdapterDisableDuringBonding) {
152   EXPECT_EQ(GetState(), BT_STATE_OFF)
153       << "Test should be run with Adapter disabled";
154 
155   RawAddress bdaddr = {{0x22, 0x22, 0x22, 0x22, 0x22, 0x22}};
156 
157   for (int i = 0; i < kTestRepeatCount; ++i) {
158     EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
159     semaphore_wait(adapter_state_changed_callback_sem_);
160     EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
161 
162     EXPECT_EQ(bt_interface()->create_bond(&bdaddr, BT_TRANSPORT_BR_EDR),
163               BT_STATUS_SUCCESS);
164 
165     EXPECT_EQ(bt_interface()->cancel_bond(&bdaddr), BT_STATUS_SUCCESS);
166 
167     EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
168     semaphore_wait(adapter_state_changed_callback_sem_);
169     EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
170   }
171 }
172 
TEST_F(BluetoothTest,AdapterCleanupDuringDiscovery)173 TEST_F(BluetoothTest, AdapterCleanupDuringDiscovery) {
174   EXPECT_EQ(GetState(), BT_STATE_OFF)
175       << "Test should be run with Adapter disabled";
176 
177   bt_callbacks_t* bt_callbacks =
178       bluetooth::hal::BluetoothInterface::Get()->GetHALCallbacks();
179   ASSERT_TRUE(bt_callbacks != nullptr);
180 
181   for (int i = 0; i < kTestRepeatCount; ++i) {
182     bt_interface()->init(bt_callbacks, false, false);
183     EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
184     semaphore_wait(adapter_state_changed_callback_sem_);
185     EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
186 
187     EXPECT_EQ(bt_interface()->start_discovery(), BT_STATUS_SUCCESS);
188 
189     EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
190     semaphore_wait(adapter_state_changed_callback_sem_);
191     EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
192     bt_interface()->cleanup();
193   }
194 }
195 
196 }  // bttest
197