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

Commit a42d5604 authored by William Escande's avatar William Escande Committed by Automerger Merge Worker
Browse files
parents 4e98aacb f185b5b5
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
@@ -1380,7 +1380,12 @@ cc_test {
// Bluetooth stack connection multiplexing
cc_test {
    name: "net_test_stack_sdp",
    defaults: ["fluoride_defaults"],
    test_suites: ["device-tests"],
    host_supported: true,
    defaults: [
        "fluoride_defaults",
        "mts_defaults",
    ],
    local_include_dirs: [
        "include",
        "test/common",
@@ -1394,10 +1399,18 @@ cc_test {
    ],
    srcs: [
        ":TestCommonMockFunctions",
        ":TestMockBtif",
        ":TestMockOsi",
        ":TestMockStackL2cap",
        ":TestMockStackMetrics",
        "sdp/sdp_api.cc",
        "sdp/sdp_discovery.cc",
        "sdp/sdp_server.cc",
        "sdp/sdp_db.cc",
        "sdp/sdp_main.cc",
        "sdp/sdp_utils.cc",
        "test/common/mock_btif_config.cc",
        "test/stack_sdp_utils_test.cc",
        "test/sdp/stack_sdp_test.cc",
        "test/sdp/stack_sdp_utils_test.cc",
    ],
    shared_libs: [
        "libcutils",
+0 −37
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2022 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 "mock_btif_config.h"

static bluetooth::manager::MockBtifConfigInterface* btif_config_interface =
    nullptr;

void bluetooth::manager::SetMockBtifConfigInterface(
    MockBtifConfigInterface* mock_btif_config_interface) {
  btif_config_interface = mock_btif_config_interface;
}

bool btif_config_get_bin(const std::string& section, const std::string& key,
                         uint8_t* value, size_t* length) {
  return btif_config_interface->GetBin(section, key, value, length);
}

size_t btif_config_get_bin_length(const std::string& section,
                                  const std::string& key) {
  return btif_config_interface->GetBinLength(section, key);
}
+97 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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 <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdlib.h>

#include <cstddef>

#include "stack/include/sdp_api.h"
#include "stack/sdp/sdpint.h"
#include "test/mock/mock_osi_allocator.h"
#include "test/mock/mock_stack_l2cap_api.h"

#ifndef BT_DEFAULT_BUFFER_SIZE
#define BT_DEFAULT_BUFFER_SIZE (4096 + 16)
#endif

static int L2CA_ConnectReq2_cid = 0x42;
static RawAddress addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6});
static tSDP_DISCOVERY_DB* sdp_db = nullptr;

class StackSdpMainTest : public ::testing::Test {
 protected:
  void SetUp() override {
    sdp_init();
    test::mock::stack_l2cap_api::L2CA_ConnectReq2.body =
        [](uint16_t psm, const RawAddress& p_bd_addr, uint16_t sec_level) {
          return ++L2CA_ConnectReq2_cid;
        };
    test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t cid,
                                                          BT_HDR* p_data) {
      osi_free_and_reset((void**)&p_data);
      return 0;
    };
    test::mock::stack_l2cap_api::L2CA_DisconnectReq.body = [](uint16_t cid) {
      return true;
    };
    test::mock::stack_l2cap_api::L2CA_Register2.body =
        [](uint16_t psm, const tL2CAP_APPL_INFO& p_cb_info, bool enable_snoop,
           tL2CAP_ERTM_INFO* p_ertm_info, uint16_t my_mtu,
           uint16_t required_remote_mtu, uint16_t sec_level) {
          return 42;  // return non zero
        };
    test::mock::osi_allocator::osi_malloc.body = [](size_t size) {
      return malloc(size);
    };
    test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
    test::mock::osi_allocator::osi_free_and_reset.body = [](void** ptr) {
      free(*ptr);
      *ptr = nullptr;
    };
    sdp_db = (tSDP_DISCOVERY_DB*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
  }

  void TearDown() override {
    osi_free(sdp_db);
    test::mock::stack_l2cap_api::L2CA_ConnectReq2 = {};
    test::mock::stack_l2cap_api::L2CA_Register2 = {};
    test::mock::stack_l2cap_api::L2CA_DataWrite = {};
    test::mock::stack_l2cap_api::L2CA_DisconnectReq = {};
    test::mock::osi_allocator::osi_malloc = {};
    test::mock::osi_allocator::osi_free = {};
    test::mock::osi_allocator::osi_free_and_reset = {};
  }
};

TEST_F(StackSdpMainTest, sdp_service_search_request) {
  ASSERT_TRUE(SDP_ServiceSearchRequest(addr, sdp_db, nullptr));
  int cid = L2CA_ConnectReq2_cid;
  tCONN_CB* p_ccb = sdpu_find_ccb_by_cid(cid);
  ASSERT_NE(p_ccb, nullptr);
  ASSERT_EQ(p_ccb->con_state, SDP_STATE_CONN_SETUP);

  tL2CAP_CFG_INFO cfg;
  sdp_cb.reg_info.pL2CA_ConfigCfm_Cb(p_ccb->connection_id, 0, &cfg);

  ASSERT_EQ(p_ccb->con_state, SDP_STATE_CONNECTED);

  sdp_disconnect(p_ccb, SDP_SUCCESS);
  sdp_cb.reg_info.pL2CA_DisconnectCfm_Cb(p_ccb->connection_id, 0);

  ASSERT_EQ(p_ccb->con_state, SDP_STATE_IDLE);
}
+18 −4
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@
#include "stack/include/avrc_defs.h"
#include "stack/include/sdp_api.h"
#include "stack/sdp/sdpint.h"
#include "test/mock/mock_btif_config.h"
#include "test/mock/mock_osi_properties.h"

using testing::_;
using testing::DoAll;
@@ -51,8 +53,6 @@ bool interop_match_addr(const interop_feature_t feature,
  return localIopMock->InteropMatchAddr(feature, addr);
}

bool osi_property_get_bool(const char* key, bool default_value) { return true; }

uint8_t avrc_value[8] = {
    ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE),  // data_element
    6,                                                    // data_len
@@ -88,14 +88,28 @@ uint16_t get_avrc_target_version(tSDP_ATTRIBUTE* p_attr) {
class StackSdpUtilsTest : public ::testing::Test {
 protected:
  void SetUp() override {
    bluetooth::manager::SetMockBtifConfigInterface(&btif_config_interface_);
    test::mock::btif_config::btif_config_get_bin.body =
        [this](const std::string& section, const std::string& key,
               uint8_t* value, size_t* length) {
          return btif_config_interface_.GetBin(section, key, value, length);
        };
    test::mock::btif_config::btif_config_get_bin_length.body =
        [this](const std::string& section, const std::string& key) {
          return btif_config_interface_.GetBinLength(section, key);
        };
    test::mock::osi_properties::osi_property_get_bool.body =
        [](const char* key, bool default_value) { return true; };

    localIopMock = std::make_unique<IopMock>();
    set_avrcp_attr(8, ATTR_ID_BT_PROFILE_DESC_LIST,
                   UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
  }

  void TearDown() override {
    bluetooth::manager::SetMockBtifConfigInterface(nullptr);
    test::mock::btif_config::btif_config_get_bin_length = {};
    test::mock::btif_config::btif_config_get_bin = {};
    test::mock::osi_properties::osi_property_get_bool = {};

    localIopMock.reset();
  }
  bluetooth::manager::MockBtifConfigInterface btif_config_interface_;
+13 −0
Original line number Diff line number Diff line
@@ -112,6 +112,19 @@ void log_manufacturer_info(const RawAddress& address,
      address, source_type, source_name, manufacturer, model, hardware_version,
      software_version);
}
void log_manufacturer_info(const RawAddress& address,
                           android::bluetooth::AddressTypeEnum address_type,
                           android::bluetooth::DeviceInfoSrcEnum source_type,
                           const std::string& source_name,
                           const std::string& manufacturer,
                           const std::string& model,
                           const std::string& hardware_version,
                           const std::string& software_version) {
  mock_function_count_map[__func__]++;
  test::mock::stack_metrics_logging::log_manufacturer_info(
      address, address_type, source_type, source_name, manufacturer, model,
      hardware_version, software_version);
}

void log_counter_metrics(android::bluetooth::CodePathCounterKeyEnum key,
                         int64_t value) {
Loading