Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dc06c004 authored by Chris Manton's avatar Chris Manton
Browse files

testing: Refactor bta unit tests

Bug: 307835034
Test: atest bt_host_test_bta

Change-Id: I8afb02b28e090edc4ce3060cd576f53d78700ec6
parent e650ef6e
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gmock/gmock.h>

#include "btm_client_interface.h"
#include "test/common/mock_functions.h"
#include "test/fake/fake_osi.h"
#include "test/mock/mock_stack_btm_interface.h"

class BtaBaseTest : public testing::Test {
 protected:
  void SetUp() override {
    reset_mock_function_count_map();
    reset_mock_btm_client_interface();
    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
    ASSERT_NE(get_btm_client_interface().lifecycle.btm_init, nullptr);
    ASSERT_NE(get_btm_client_interface().lifecycle.btm_free, nullptr);
  }

  void TearDown() override {}

  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
};
+11 −14
Original line number Diff line number Diff line
@@ -18,43 +18,40 @@

#include <gtest/gtest.h>

#include <memory>

#include "bta/dm/bta_dm_int.h"
#include "bta/test/bta_base_test.h"
#include "osi/include/allocator.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/btm_status.h"
#include "test/fake/fake_osi.h"
#include "test/mock/mock_stack_btm_interface.h"
#include "types/bluetooth/uuid.h"

using bluetooth::Uuid;

class BtaCustUuid : public testing::Test {
class BtaCustUuid : public BtaBaseTest {
 protected:
  void SetUp() override {
    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
    BtaBaseTest::SetUp();
    bta_dm_cb = {};
    btm_client_interface = {};
    btm_client_interface.eir.BTM_WriteEIR = [](BT_HDR* p_buf) -> tBTM_STATUS {
    mock_btm_client_interface.eir.BTM_WriteEIR =
        [](BT_HDR* p_buf) -> tBTM_STATUS {
      osi_free(p_buf);
      return BTM_SUCCESS;
    };
    btm_client_interface.eir.BTM_GetEirSupportedServices =
    mock_btm_client_interface.eir.BTM_GetEirSupportedServices =
        [](uint32_t* p_eir_uuid, uint8_t** p, uint8_t max_num_uuid16,
           uint8_t* p_num_uuid16) -> uint8_t { return 0; };
    btm_client_interface.eir.BTM_WriteEIR = [](BT_HDR* p_buf) -> tBTM_STATUS {
    mock_btm_client_interface.eir.BTM_WriteEIR =
        [](BT_HDR* p_buf) -> tBTM_STATUS {
      osi_free(p_buf);
      return BTM_SUCCESS;
    };
    btm_client_interface.local.BTM_ReadLocalDeviceNameFromController =
    mock_btm_client_interface.local.BTM_ReadLocalDeviceNameFromController =
        [](tBTM_CMPL_CB* cb) -> tBTM_STATUS { return BTM_CMD_STARTED; };
    btm_client_interface.security.BTM_SecRegister =
    mock_btm_client_interface.security.BTM_SecRegister =
        [](const tBTM_APPL_INFO* p_cb_info) -> bool { return true; };
  }
  void TearDown() override { btm_client_interface = {}; }

  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
  void TearDown() override { BtaBaseTest::TearDown(); }
};

namespace {
+9 −13
Original line number Diff line number Diff line
@@ -19,20 +19,19 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <memory>
#include <string>

#include "bta/dm/bta_dm_int.h"
#include "bta/dm/bta_dm_sec_int.h"
#include "bta/hf_client/bta_hf_client_int.h"
#include "bta/include/bta_api.h"
#include "bta/test/bta_base_test.h"
#include "osi/include/compat.h"
#include "osi/include/osi.h"
#include "stack/include/bt_device_type.h"
#include "stack/include/btm_status.h"
#include "test/common/main_handler.h"
#include "test/common/mock_functions.h"
#include "test/fake/fake_osi.h"
#include "test/mock/mock_osi_alarm.h"
#include "test/mock/mock_osi_allocator.h"
#include "test/mock/mock_stack_acl.h"
@@ -73,12 +72,10 @@ void bta_dm_remote_name_cmpl(const tBTA_DM_MSG* p_data);
}  // namespace legacy
}  // namespace bluetooth

class BtaDmTest : public testing::Test {
class BtaDmTest : public BtaBaseTest {
 protected:
  void SetUp() override {
    reset_mock_function_count_map();
    fake_osi_ = std::make_unique<test::fake::FakeOsi>();

    BtaBaseTest::SetUp();
    main_thread_start_up();
    post_on_bt_main([]() { LOG_INFO("Main thread started up"); });

@@ -96,9 +93,8 @@ class BtaDmTest : public testing::Test {
    bluetooth::legacy::testing::bta_dm_deinit_cb();
    post_on_bt_main([]() { LOG_INFO("Main thread shutting down"); });
    main_thread_shut_down();
    BtaBaseTest::TearDown();
  }

  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
};

class BtaDmCustomAlarmTest : public BtaDmTest {
@@ -250,7 +246,7 @@ TEST_F(BtaDmTest, bta_dm_set_encryption) {
  device->p_encrypt_cback = nullptr;

  // Setup a device that fails encryption
  btm_client_interface.security.BTM_SetEncryption =
  mock_btm_client_interface.security.BTM_SetEncryption =
      [](const RawAddress& bd_addr, tBT_TRANSPORT transport,
         tBTM_SEC_CALLBACK* p_callback, void* p_ref_data,
         tBTM_BLE_SEC_ACT sec_act) -> tBTM_STATUS {
@@ -264,7 +260,7 @@ TEST_F(BtaDmTest, bta_dm_set_encryption) {
  device->p_encrypt_cback = nullptr;

  // Setup a device that successfully starts encryption
  btm_client_interface.security.BTM_SetEncryption =
  mock_btm_client_interface.security.BTM_SetEncryption =
      [](const RawAddress& bd_addr, tBT_TRANSPORT transport,
         tBTM_SEC_CALLBACK* p_callback, void* p_ref_data,
         tBTM_BLE_SEC_ACT sec_act) -> tBTM_STATUS {
@@ -385,7 +381,7 @@ TEST_F(BtaDmTest, bta_dm_remname_cback__typical) {
  strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
          strlen(kRemoteName));

  btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
  mock_btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
      [](tBTM_RMT_NAME_CALLBACK*) -> bool {
    inc_func_call_count("BTM_SecDeleteRmtNameNotifyCallback");
    return true;
@@ -414,7 +410,7 @@ TEST_F(BtaDmTest, bta_dm_remname_cback__wrong_address) {
  strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
          strlen(kRemoteName));

  btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
  mock_btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
      [](tBTM_RMT_NAME_CALLBACK*) -> bool {
    inc_func_call_count("BTM_SecDeleteRmtNameNotifyCallback");
    return true;
@@ -443,7 +439,7 @@ TEST_F(BtaDmTest, bta_dm_remname_cback__HCI_ERR_CONNECTION_EXISTS) {
  strlcpy(reinterpret_cast<char*>(&name.remote_bd_name), kRemoteName,
          strlen(kRemoteName));

  btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
  mock_btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
      [](tBTM_RMT_NAME_CALLBACK*) -> bool {
    inc_func_call_count("BTM_SecDeleteRmtNameNotifyCallback");
    return true;
+12 −13
Original line number Diff line number Diff line
@@ -17,13 +17,12 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <memory>
#include <string>

#include "bta/dm/bta_dm_int.h"
#include "bta/test/bta_base_test.h"
#include "osi/include/allocator.h"
#include "test/common/main_handler.h"
#include "test/fake/fake_osi.h"
#include "test/mock/mock_stack_btm_interface.h"
#include "test/mock/mock_stack_gatt_api.h"

@@ -44,24 +43,24 @@ void bta_dm_sdp_result(tBTA_DM_MSG* p_data);
}  // namespace legacy
}  // namespace bluetooth

class BtaSdpTest : public testing::Test {
class BtaSdpTest : public BtaBaseTest {
 protected:
  void SetUp() override {
    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
    BtaBaseTest::SetUp();
    test::mock::stack_gatt_api::GATT_Register.body =
        [](const bluetooth::Uuid& p_app_uuid128, const std::string name,
           tGATT_CBACK* p_cb_info, bool eatt_support) { return 5; };
    btm_client_interface = {};
    btm_client_interface.eir.BTM_GetEirSupportedServices =
    mock_btm_client_interface.eir.BTM_GetEirSupportedServices =
        [](uint32_t* p_eir_uuid, uint8_t** p, uint8_t max_num_uuid16,
           uint8_t* p_num_uuid16) -> uint8_t { return 0; };
    btm_client_interface.eir.BTM_WriteEIR = [](BT_HDR* p_buf) -> tBTM_STATUS {
    mock_btm_client_interface.eir.BTM_WriteEIR =
        [](BT_HDR* p_buf) -> tBTM_STATUS {
      osi_free(p_buf);
      return BTM_SUCCESS;
    };
    btm_client_interface.local.BTM_ReadLocalDeviceNameFromController =
    mock_btm_client_interface.local.BTM_ReadLocalDeviceNameFromController =
        [](tBTM_CMPL_CB* cb) -> tBTM_STATUS { return BTM_CMD_STARTED; };
    btm_client_interface.security.BTM_SecRegister =
    mock_btm_client_interface.security.BTM_SecRegister =
        [](const tBTM_APPL_INFO* p_cb_info) -> bool { return true; };

    main_thread_start_up();
@@ -77,9 +76,8 @@ class BtaSdpTest : public testing::Test {
    main_thread_shut_down();

    test::mock::stack_gatt_api::GATT_Register = {};
    btm_client_interface = {};
    BtaBaseTest::TearDown();
  }
  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
};

class BtaSdpRegisteredTest : public BtaSdpTest {
@@ -111,12 +109,13 @@ TEST_F(BtaSdpRegisteredTest, bta_dm_sdp_result_SDP_SUCCESS) {
  tBTA_DM_MSG msg = {
      .sdp_event =
          {
              .hdr = {},
              .sdp_result = SDP_SUCCESS,
          },
  };
  btm_client_interface.security.BTM_SecReadDevName =
  mock_btm_client_interface.security.BTM_SecReadDevName =
      [](const RawAddress& bd_addr) -> const char* { return kName; };
  btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
  mock_btm_client_interface.security.BTM_SecDeleteRmtNameNotifyCallback =
      [](tBTM_RMT_NAME_CALLBACK*) -> bool { return true; };
  bluetooth::legacy::testing::bta_dm_sdp_result(&msg);
}
+7 −6
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <sys/socket.h>

#include "bta/dm/bta_dm_sec_int.h"
#include "bta/test/bta_base_test.h"
#include "test/mock/mock_stack_btm_inq.h"
#include "test/mock/mock_stack_btm_interface.h"
#include "types/raw_address.h"
@@ -47,17 +48,17 @@ void btm_set_local_io_caps(uint8_t io_caps);
}  // namespace legacy
}  // namespace bluetooth

class BtaSecTest : public testing::Test {
class BtaSecTest : public BtaBaseTest {
 protected:
  void SetUp() override {}
  void SetUp() override { BtaBaseTest::SetUp(); }

  void TearDown() override {}
  void TearDown() override { BtaBaseTest::TearDown(); }
};

TEST_F(BtaSecTest, bta_dm_sp_cback__BTM_SP_CFM_REQ_EVT_WithName) {
  constexpr uint32_t kNumVal = 1234;
  static bool callback_sent = false;
  btm_client_interface.peer.BTM_ReadRemoteDeviceName =
  mock_btm_client_interface.peer.BTM_ReadRemoteDeviceName =
      [](const RawAddress& remote_bda, tBTM_NAME_CMPL_CB* p_cb,
         tBT_TRANSPORT transport) -> tBTM_STATUS { return BTM_CMD_STARTED; };

@@ -149,7 +150,7 @@ TEST_F(BtaSecTest, bta_dm_sp_cback__BTM_SP_CFM_REQ_EVT_WithoutName_RNRSuccess) {
TEST_F(BtaSecTest, bta_dm_sp_cback__BTM_SP_CFM_REQ_EVT_WithoutName_RNRFail) {
  constexpr uint32_t kNumVal = 1234;
  static bool callback_sent = false;
  btm_client_interface.peer.BTM_ReadRemoteDeviceName =
  mock_btm_client_interface.peer.BTM_ReadRemoteDeviceName =
      [](const RawAddress& remote_bda, tBTM_NAME_CMPL_CB* p_cb,
         tBT_TRANSPORT transport) -> tBTM_STATUS { return BTM_SUCCESS; };

@@ -198,7 +199,7 @@ TEST_F(BtaSecTest, bta_dm_sp_cback__BTM_SP_CFM_REQ_EVT_WithoutName_RNRFail) {
TEST_F(BtaSecTest, bta_dm_sp_cback__BTM_SP_KEY_NOTIF_EVT) {
  constexpr uint32_t kPassKey = 1234;
  static bool callback_sent = false;
  btm_client_interface.peer.BTM_ReadRemoteDeviceName =
  mock_btm_client_interface.peer.BTM_ReadRemoteDeviceName =
      [](const RawAddress& remote_bda, tBTM_NAME_CMPL_CB* p_cb,
         tBT_TRANSPORT transport) -> tBTM_STATUS { return BTM_CMD_STARTED; };

Loading