low_energy_client_unittest.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // Copyright 2015 Google, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at:
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include <base/macros.h>
  17. #include <gmock/gmock.h>
  18. #include <gtest/gtest.h>
  19. #include "service/adapter.h"
  20. #include "service/hal/fake_bluetooth_gatt_interface.h"
  21. #include "service/low_energy_client.h"
  22. #include "stack/include/bt_types.h"
  23. #include "stack/include/hcidefs.h"
  24. #include "test/mock_adapter.h"
  25. using ::testing::_;
  26. using ::testing::Return;
  27. using ::testing::Pointee;
  28. using ::testing::DoAll;
  29. using ::testing::Invoke;
  30. namespace bluetooth {
  31. namespace {
  32. class MockGattHandler
  33. : public hal::FakeBluetoothGattInterface::TestClientHandler {
  34. public:
  35. MockGattHandler(){};
  36. ~MockGattHandler() override = default;
  37. MOCK_METHOD1(RegisterClient, bt_status_t(const bluetooth::Uuid&));
  38. MOCK_METHOD1(UnregisterClient, bt_status_t(int));
  39. MOCK_METHOD4(Connect, bt_status_t(int, const RawAddress&, bool, int));
  40. MOCK_METHOD3(Disconnect, bt_status_t(int, const RawAddress&, int));
  41. private:
  42. DISALLOW_COPY_AND_ASSIGN(MockGattHandler);
  43. };
  44. class TestDelegate : public LowEnergyClient::Delegate {
  45. public:
  46. TestDelegate() : connection_state_count_(0), last_mtu_(0) {}
  47. ~TestDelegate() override = default;
  48. int connection_state_count() const { return connection_state_count_; }
  49. void OnConnectionState(LowEnergyClient* client, int status,
  50. const char* address, bool connected) {
  51. ASSERT_TRUE(client);
  52. connection_state_count_++;
  53. }
  54. void OnMtuChanged(LowEnergyClient* client, int status, const char* address,
  55. int mtu) {
  56. ASSERT_TRUE(client);
  57. last_mtu_ = mtu;
  58. }
  59. private:
  60. int connection_state_count_;
  61. int last_mtu_;
  62. DISALLOW_COPY_AND_ASSIGN(TestDelegate);
  63. };
  64. class LowEnergyClientTest : public ::testing::Test {
  65. public:
  66. LowEnergyClientTest() = default;
  67. ~LowEnergyClientTest() override = default;
  68. void SetUp() override {
  69. // Only set |mock_handler_| if a test hasn't set it.
  70. if (!mock_handler_) mock_handler_.reset(new MockGattHandler());
  71. fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface(
  72. nullptr, nullptr,
  73. std::static_pointer_cast<
  74. hal::FakeBluetoothGattInterface::TestClientHandler>(mock_handler_),
  75. nullptr);
  76. hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_);
  77. ble_factory_.reset(new LowEnergyClientFactory(mock_adapter_));
  78. }
  79. void TearDown() override {
  80. ble_factory_.reset();
  81. hal::BluetoothGattInterface::CleanUp();
  82. }
  83. protected:
  84. hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_;
  85. testing::MockAdapter mock_adapter_;
  86. std::shared_ptr<MockGattHandler> mock_handler_;
  87. std::unique_ptr<LowEnergyClientFactory> ble_factory_;
  88. private:
  89. DISALLOW_COPY_AND_ASSIGN(LowEnergyClientTest);
  90. };
  91. // Used for tests that operate on a pre-registered client.
  92. class LowEnergyClientPostRegisterTest : public LowEnergyClientTest {
  93. public:
  94. LowEnergyClientPostRegisterTest() : next_client_id_(0) {}
  95. ~LowEnergyClientPostRegisterTest() override = default;
  96. void SetUp() override {
  97. LowEnergyClientTest::SetUp();
  98. auto callback = [&](std::unique_ptr<LowEnergyClient> client) {
  99. le_client_ = std::move(client);
  100. };
  101. RegisterTestClient(callback);
  102. }
  103. void TearDown() override {
  104. EXPECT_CALL(*mock_handler_, UnregisterClient(_))
  105. .Times(1)
  106. .WillOnce(Return(BT_STATUS_SUCCESS));
  107. le_client_.reset();
  108. LowEnergyClientTest::TearDown();
  109. }
  110. void RegisterTestClient(
  111. const std::function<void(std::unique_ptr<LowEnergyClient> client)>
  112. callback) {
  113. Uuid uuid = Uuid::GetRandom();
  114. auto api_callback = [&](BLEStatus status, const Uuid& in_uuid,
  115. std::unique_ptr<BluetoothInstance> in_client) {
  116. CHECK(in_uuid == uuid);
  117. CHECK(in_client.get());
  118. CHECK(status == BLE_STATUS_SUCCESS);
  119. callback(std::unique_ptr<LowEnergyClient>(
  120. static_cast<LowEnergyClient*>(in_client.release())));
  121. };
  122. EXPECT_CALL(*mock_handler_, RegisterClient(_))
  123. .Times(1)
  124. .WillOnce(Return(BT_STATUS_SUCCESS));
  125. ble_factory_->RegisterInstance(uuid, api_callback);
  126. fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, next_client_id_++,
  127. uuid);
  128. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  129. }
  130. protected:
  131. std::unique_ptr<LowEnergyClient> le_client_;
  132. private:
  133. int next_client_id_;
  134. DISALLOW_COPY_AND_ASSIGN(LowEnergyClientPostRegisterTest);
  135. };
  136. TEST_F(LowEnergyClientTest, RegisterInstance) {
  137. EXPECT_CALL(*mock_handler_, RegisterClient(_))
  138. .Times(2)
  139. .WillOnce(Return(BT_STATUS_FAIL))
  140. .WillOnce(Return(BT_STATUS_SUCCESS));
  141. // These will be asynchronously populated with a result when the callback
  142. // executes.
  143. BLEStatus status = BLE_STATUS_SUCCESS;
  144. Uuid cb_uuid;
  145. std::unique_ptr<LowEnergyClient> client;
  146. int callback_count = 0;
  147. auto callback = [&](BLEStatus in_status, const Uuid& uuid,
  148. std::unique_ptr<BluetoothInstance> in_client) {
  149. status = in_status;
  150. cb_uuid = uuid;
  151. client = std::unique_ptr<LowEnergyClient>(
  152. static_cast<LowEnergyClient*>(in_client.release()));
  153. callback_count++;
  154. };
  155. Uuid uuid0 = Uuid::GetRandom();
  156. // HAL returns failure.
  157. EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback));
  158. EXPECT_EQ(0, callback_count);
  159. // HAL returns success.
  160. EXPECT_TRUE(ble_factory_->RegisterInstance(uuid0, callback));
  161. EXPECT_EQ(0, callback_count);
  162. // Calling twice with the same Uuid should fail with no additional call into
  163. // the stack.
  164. EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback));
  165. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  166. // Call with a different Uuid while one is pending.
  167. Uuid uuid1 = Uuid::GetRandom();
  168. EXPECT_CALL(*mock_handler_, RegisterClient(_))
  169. .Times(1)
  170. .WillOnce(Return(BT_STATUS_SUCCESS));
  171. EXPECT_TRUE(ble_factory_->RegisterInstance(uuid1, callback));
  172. // Trigger callback with an unknown Uuid. This should get ignored.
  173. Uuid uuid2 = Uuid::GetRandom();
  174. fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, uuid2);
  175. EXPECT_EQ(0, callback_count);
  176. // |uuid0| succeeds.
  177. int client_if0 = 2; // Pick something that's not 0.
  178. fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_SUCCESS,
  179. client_if0, uuid0);
  180. EXPECT_EQ(1, callback_count);
  181. ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error
  182. EXPECT_EQ(BLE_STATUS_SUCCESS, status);
  183. EXPECT_EQ(client_if0, client->GetInstanceId());
  184. EXPECT_EQ(uuid0, client->GetAppIdentifier());
  185. EXPECT_EQ(uuid0, cb_uuid);
  186. // The client should unregister itself when deleted.
  187. EXPECT_CALL(*mock_handler_, UnregisterClient(client_if0))
  188. .Times(1)
  189. .WillOnce(Return(BT_STATUS_SUCCESS));
  190. client.reset();
  191. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  192. // |uuid1| fails.
  193. int client_if1 = 3;
  194. fake_hal_gatt_iface_->NotifyRegisterClientCallback(BT_STATUS_FAIL, client_if1,
  195. uuid1);
  196. EXPECT_EQ(2, callback_count);
  197. ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error
  198. EXPECT_EQ(BLE_STATUS_FAILURE, status);
  199. EXPECT_EQ(uuid1, cb_uuid);
  200. }
  201. MATCHER_P(BitEq, x, std::string(negation ? "isn't" : "is") +
  202. " bitwise equal to " + ::testing::PrintToString(x)) {
  203. static_assert(sizeof(x) == sizeof(arg), "Size mismatch");
  204. return std::memcmp(&arg, &x, sizeof(x)) == 0;
  205. }
  206. TEST_F(LowEnergyClientPostRegisterTest, Connect) {
  207. const RawAddress kTestAddress = {{0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C}};
  208. const char kTestAddressStr[] = "01:02:03:0A:0B:0C";
  209. const bool kTestDirect = false;
  210. const int connId = 12;
  211. TestDelegate delegate;
  212. le_client_->SetDelegate(&delegate);
  213. // TODO(jpawlowski): NotifyConnectCallback should be called after returning
  214. // success, fix it when it becomes important.
  215. // These should succeed and result in a HAL call
  216. EXPECT_CALL(*mock_handler_,
  217. Connect(le_client_->GetInstanceId(), BitEq(kTestAddress),
  218. kTestDirect, BT_TRANSPORT_LE))
  219. .Times(1)
  220. .WillOnce(DoAll(Invoke([&](int client_id, const RawAddress& bd_addr,
  221. bool is_direct, int transport) {
  222. fake_hal_gatt_iface_->NotifyConnectCallback(
  223. connId, BT_STATUS_SUCCESS, client_id, bd_addr);
  224. }),
  225. Return(BT_STATUS_SUCCESS)));
  226. EXPECT_TRUE(le_client_->Connect(kTestAddressStr, kTestDirect));
  227. EXPECT_EQ(1, delegate.connection_state_count());
  228. // TODO(jpawlowski): same as above
  229. // These should succeed and result in a HAL call
  230. EXPECT_CALL(*mock_handler_, Disconnect(le_client_->GetInstanceId(),
  231. BitEq(kTestAddress), connId))
  232. .Times(1)
  233. .WillOnce(DoAll(
  234. Invoke([&](int client_id, const RawAddress& bd_addr, int connId) {
  235. fake_hal_gatt_iface_->NotifyDisconnectCallback(
  236. connId, BT_STATUS_SUCCESS, client_id, bd_addr);
  237. }),
  238. Return(BT_STATUS_SUCCESS)));
  239. EXPECT_TRUE(le_client_->Disconnect(kTestAddressStr));
  240. EXPECT_EQ(2, delegate.connection_state_count());
  241. le_client_->SetDelegate(nullptr);
  242. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  243. }
  244. } // namespace
  245. } // namespace bluetooth