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

Commit 5f20bff6 authored by Myles Watson's avatar Myles Watson Committed by Automerger Merge Worker
Browse files

Merge "btif_core_test: Add Rfcomm listen tests" into main am: 1b7f7838 am: a9a3fc9e

parents 83ae76dc a9a3fc9e
Loading
Loading
Loading
Loading
+91 −7
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
#include <gtest/gtest.h>
#include <sys/socket.h>

#include <future>
#include <future>  // NOLINT
#include <map>
#include <memory>
#include <string>
@@ -30,28 +30,31 @@
#include "bta/include/bta_hh_api.h"
#include "btcore/include/module.h"
#include "btif/include/btif_api.h"
#include "btif/include/btif_bqr.h"
#include "btif/include/btif_common.h"
#include "btif/include/btif_jni_task.h"
#include "btif/include/btif_sock.h"
#include "btif/include/btif_util.h"
#include "btif_bqr.h"
#include "btif_jni_task.h"
#include "common/bind.h"
#include "common/contextual_callback.h"
#include "common/postable_context.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "hci/controller_interface_mock.h"
#include "hci/hci_layer_mock.h"
#include "include/hardware/bluetooth.h"
#include "include/hardware/bt_av.h"
#include "main_thread.h"
#include "packet/base_packet_builder.h"
#include "packet/bit_inserter.h"
#include "packet/packet_view.h"
#include "packet/raw_builder.h"
#include "stack/include/bt_uuid16.h"
#include "stack/include/main_thread.h"
#include "test/common/core_interface.h"
#include "test/fake/fake_osi.h"
#include "test/mock/mock_main_shim_entry.h"
#include "test/mock/mock_osi_properties.h"
#include "test/mock/mock_osi_thread.h"
#include "test/mock/mock_stack_btm_sec.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"

namespace bluetooth::testing {
@@ -65,6 +68,7 @@ void bta_dm_acl_up(const RawAddress& bd_addr, tBT_TRANSPORT transport, uint16_t

const tBTA_AG_RES_DATA tBTA_AG_RES_DATA::kEmpty = {};

using bluetooth::Uuid;
using bluetooth::common::BindOnce;
using bluetooth::common::ContextualCallback;
using bluetooth::common::ContextualOnceCallback;
@@ -851,7 +855,6 @@ protected:
  }
  bluetooth::hci::testing::MockHciLayer hci_;
  ContextualCallback<void(VendorSpecificEventView)> vse_callback_;
  PostableContext* context;
};

TEST_F(BtifCoreWithVendorSupportTest, configure_bqr_test) {}
@@ -1017,3 +1020,84 @@ TEST_F(BtifCoreVseWithSocketTest, debug_dump_a2dp_choppy) {
          std::move(reading_promise)));
  EXPECT_EQ(std::future_status::ready, reading_done.wait_for(std::chrono::seconds(1)));
}

class BtifCoreSocketTest : public BtifCoreWithControllerTest {
protected:
  void SetUp() override {
    BtifCoreWithControllerTest::SetUp();
    fake_osi_ = std::make_unique<test::fake::FakeOsi>();
    uid_set = uid_set_create();
    thread_t* kThreadPtr = reinterpret_cast<thread_t*>(0xbadbadbad);
    test::mock::osi_thread::thread_new.body = [kThreadPtr](const char* name) -> thread_t* {
      bluetooth::log::info("Explicitly not starting thread {}", name);
      return kThreadPtr;
    };
    test::mock::osi_thread::thread_free.body = [kThreadPtr](thread_t* ptr_to_free) {
      ASSERT_EQ(ptr_to_free, kThreadPtr);
    };
    btif_sock_init(uid_set);
  }

  void TearDown() override {
    test::mock::osi_thread::thread_new = {};
    test::mock::osi_thread::thread_free = {};
    btif_sock_cleanup();
    uid_set_destroy(uid_set);
    BtifCoreWithControllerTest::TearDown();
  }

  std::unique_ptr<test::fake::FakeOsi> fake_osi_;
  uid_set_t* uid_set;
};

TEST_F(BtifCoreSocketTest, empty_test) {}

TEST_F(BtifCoreSocketTest, CreateRfcommServerSocket) {
  static constexpr int kChannelOne = 1;
  static constexpr int kFlags = 2;
  static constexpr int kAppUid = 3;
  const Uuid server_uuid = Uuid::From16Bit(UUID_SERVCLASS_SERIAL_PORT);
  int socket_number = 0;
  ASSERT_EQ(BT_STATUS_SUCCESS,
            btif_sock_get_interface()->listen(BTSOCK_RFCOMM, "TestService", &server_uuid,
                                              kChannelOne, &socket_number, kFlags, kAppUid));
}

TEST_F(BtifCoreSocketTest, CreateTwoRfcommServerSockets) {
  static constexpr int kChannelOne = 1;
  static constexpr int kFlags = 2;
  static constexpr int kAppUid = 3;
  const Uuid server_uuid = Uuid::From16Bit(UUID_SERVCLASS_SERIAL_PORT);
  int socket_number = 0;
  ASSERT_EQ(BT_STATUS_SUCCESS,
            btif_sock_get_interface()->listen(BTSOCK_RFCOMM, "TestService", &server_uuid,
                                              kChannelOne, &socket_number, kFlags, kAppUid));
  static constexpr int kChannelTwo = 2;
  static constexpr int kFlagsTwo = 4;
  static constexpr int kAppUidTwo = 6;
  const Uuid server_uuid_two = Uuid::FromString("12345678-1234-2345-3456-456789123456");
  int socket_number_two = 1;
  ASSERT_EQ(BT_STATUS_SUCCESS, btif_sock_get_interface()->listen(
                                       BTSOCK_RFCOMM, "ServiceTwo", &server_uuid_two, kChannelTwo,
                                       &socket_number_two, kFlagsTwo, kAppUidTwo));
}

TEST_F(BtifCoreSocketTest, CreateManyRfcommServerSockets) {
  char server_uuid_str[] = "____5678-1234-2345-3456-456789123456";
  int number_of_sockets = 20;
  for (int i = 0; i < number_of_sockets; i++) {
    int channel = 11;
    int flags = 0;
    int app_uuid = i + 3;
    int socket_number = 0;
    server_uuid_str[3] = i % 10 + '0';
    server_uuid_str[2] = (i / 10) % 10 + '0';
    server_uuid_str[1] = (i / 100) % 10 + '0';
    server_uuid_str[0] = (i / 1000) % 10 + '0';
    Uuid server_uuid = Uuid::FromString(server_uuid_str);
    ASSERT_EQ(BT_STATUS_SUCCESS,
              btif_sock_get_interface()->listen(BTSOCK_RFCOMM, "TestService", &server_uuid, channel,
                                                &socket_number, flags, app_uuid));
    ASSERT_EQ(0, close(socket_number));
  }
}
+0 −1
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@
#include <map>
#include <string>

#include "os/log.h"
#include "osi/include/alarm.h"
#include "osi/include/allocator.h"
#include "osi/include/config.h"