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
21 extern "C" {
22 #include "btcore/include/property.h"
23 #include "stack/include/bt_types.h"
24 }
25
26 namespace {
27
28 // Each iteration of the test takes about 2 seconds to run, so choose a value
29 // that matches your time constraints. For example, 5 iterations would take
30 // about 10 seconds to run
31 const int kTestRepeatCount = 5;
32
33 } // namespace
34
35 namespace bttest {
36
TEST_F(BluetoothTest,AdapterEnableDisable)37 TEST_F(BluetoothTest, AdapterEnableDisable) {
38 EXPECT_EQ(GetState(), BT_STATE_OFF) <<
39 "Test should be run with Adapter disabled";
40
41 EXPECT_EQ(bt_interface()->enable(false), 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)
52 << "Test should be run with Adapter disabled";
53
54 for (int i = 0; i < kTestRepeatCount; ++i) {
55 EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
56 semaphore_wait(adapter_state_changed_callback_sem_);
57 EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
58
59 EXPECT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
60 semaphore_wait(adapter_state_changed_callback_sem_);
61 EXPECT_EQ(GetState(), BT_STATE_OFF) << "Adapter did not turn off.";
62 }
63 }
64
TEST_F(BluetoothTest,AdapterSetGetName)65 TEST_F(BluetoothTest, AdapterSetGetName) {
66 bt_property_t *new_name = property_new_name("BluetoothTestName1");
67
68 EXPECT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
69 semaphore_wait(adapter_state_changed_callback_sem_);
70 EXPECT_EQ(GetState(), BT_STATE_ON)
71 << "Test should be run with Adapter enabled";
72
73 // Enabling the interface will call the properties callback twice before
74 // ever reaching this point.
75 ClearSemaphore(adapter_properties_callback_sem_);
76
77 EXPECT_EQ(bt_interface()->get_adapter_property(BT_PROPERTY_BDNAME),
78 BT_STATUS_SUCCESS);
79 semaphore_wait(adapter_properties_callback_sem_);
80 EXPECT_GT(GetPropertiesChangedCount(), 0)
81 << "Expected at least one adapter property to change";
82 bt_property_t *name_property = GetProperty(BT_PROPERTY_BDNAME);
83 EXPECT_NE(name_property, nullptr);
84 if (property_equals(name_property, new_name)) {
85 property_free(new_name);
86 new_name = property_new_name("BluetoothTestName2");
87 }
88 std::string old_name((const char *)property_as_name(name_property)->name);
89
90 EXPECT_EQ(bt_interface()->set_adapter_property(new_name), BT_STATUS_SUCCESS);
91 semaphore_wait(adapter_properties_callback_sem_);
92 EXPECT_GT(GetPropertiesChangedCount(), 0)
93 << "Expected at least one adapter property to change";
94 EXPECT_TRUE(GetProperty(BT_PROPERTY_BDNAME))
95 << "The Bluetooth name property did not change.";
96 EXPECT_TRUE(property_equals(GetProperty(BT_PROPERTY_BDNAME), new_name))
97 << "Bluetooth name "
98 << property_as_name(GetProperty(BT_PROPERTY_BDNAME))->name
99 << " does not match test value " << property_as_name(new_name)->name;
100
101
102 bt_property_t *old_name_property = property_new_name(old_name.c_str());
103 EXPECT_EQ(bt_interface()->set_adapter_property(old_name_property), BT_STATUS_SUCCESS);
104 semaphore_wait(adapter_properties_callback_sem_);
105 EXPECT_TRUE(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(false), 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(false), 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 bt_bdaddr_t bdaddr = { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 } };
157
158 for (int i = 0; i < kTestRepeatCount; ++i) {
159 EXPECT_EQ(bt_interface()->enable(false), 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
174 } // bttest
175