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

Commit 7529fe2c authored by Jack He's avatar Jack He
Browse files

TEST: Fix net_test_stock_rfcomm unit test

* Properly mock updated L2CAP registeration method by including the
  filtered btsnoop option
* Properly mock btsnoop module by creating a separate file to link with
  the test executable

Fixes: 123361801
Test: atest net_test_stack_rfcomm
Change-Id: I89ca875cbcc941feddd7c501fa1b7e14f8d14794
parent 36cca14f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -257,6 +257,7 @@ cc_test {
        "rfcomm/rfc_ts_frames.cc",
        "rfcomm/rfc_utils.cc",
        "test/common/mock_btm_layer.cc",
        "test/common/mock_btsnoop_module.cc",
        "test/common/mock_btu_layer.cc",
        "test/common/mock_l2cap_layer.cc",
        "test/common/stack_test_packet_utils.cc",
+42 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2019 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 "hci/include/btsnoop.h"

static void capture(const BT_HDR*, bool) { /* do nothing */
}

static void whitelist_l2c_channel(uint16_t, uint16_t,
                                  uint16_t) { /* do nothing */
}

static void whitelist_rfc_dlci(uint16_t, uint8_t) { /* do nothing */
}

static void add_rfc_l2c_channel(uint16_t, uint16_t, uint16_t) { /* do nothing */
}

static void clear_l2cap_whitelist(uint16_t, uint16_t,
                                  uint16_t) { /* do nothing */
}

static const btsnoop_t fake_snoop = {capture, whitelist_l2c_channel,
                                     whitelist_rfc_dlci, add_rfc_l2c_channel,
                                     clear_l2cap_whitelist};

const btsnoop_t* btsnoop_get_interface() { return &fake_snoop; }
+5 −3
Original line number Diff line number Diff line
@@ -24,9 +24,11 @@ void bluetooth::l2cap::SetMockInterface(
  l2cap_interface = mock_l2cap_interface;
}

uint16_t L2CA_Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info) {
  VLOG(1) << __func__ << ": psm=" << psm << ", p_cb_info=" << p_cb_info;
  return l2cap_interface->Register(psm, p_cb_info);
uint16_t L2CA_Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info,
                       bool enable_snoop) {
  VLOG(1) << __func__ << ": psm=" << psm << ", p_cb_info=" << p_cb_info
          << ", enable_snoop=" << enable_snoop;
  return l2cap_interface->Register(psm, p_cb_info, enable_snoop);
}

uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& bd_addr) {
+4 −2
Original line number Diff line number Diff line
@@ -26,7 +26,8 @@ namespace l2cap {

class L2capInterface {
 public:
  virtual uint16_t Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info) = 0;
  virtual uint16_t Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info,
                            bool enable_snoop) = 0;
  virtual uint16_t ConnectRequest(uint16_t psm, const RawAddress& bd_addr) = 0;
  virtual bool ConnectResponse(const RawAddress& bd_addr, uint8_t id,
                               uint16_t lcid, uint16_t result,
@@ -41,7 +42,8 @@ class L2capInterface {

class MockL2capInterface : public L2capInterface {
 public:
  MOCK_METHOD2(Register, uint16_t(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info));
  MOCK_METHOD3(Register, uint16_t(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info,
                                  bool enable_snoop));
  MOCK_METHOD2(ConnectRequest,
               uint16_t(uint16_t psm, const RawAddress& bd_addr));
  MOCK_METHOD5(ConnectResponse,
+1 −21
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@
#include "port_api.h"

#include "btm_int.h"
#include "hci/include/btsnoop.h"
#include "rfc_int.h"

#include "mock_btm_layer.h"
@@ -35,23 +34,6 @@
#include "stack_rfcomm_test_utils.h"
#include "stack_test_packet_utils.h"

static void capture(const BT_HDR*, bool) { /* do nothing */
}
static void whitelist_l2c_channel(uint16_t, uint16_t,
                                  uint16_t) { /* do nothing */
}
static void whitelist_rfc_dlci(uint16_t, uint8_t) { /* do nothing */
}
static void add_rfc_l2c_channel(uint16_t, uint16_t, uint16_t) { /* do nothing */
}
static void clear_l2cap_whitelist(uint16_t, uint16_t,
                                  uint16_t) { /* do nothing */
}
static const btsnoop_t fake_snoop = {capture, whitelist_l2c_channel,
                                     whitelist_rfc_dlci, add_rfc_l2c_channel,
                                     clear_l2cap_whitelist};
const btsnoop_t* btsnoop_get_interface() { return &fake_snoop; }

std::string DumpByteBufferToString(uint8_t* p_data, size_t len) {
  std::stringstream str;
  str.setf(std::ios_base::hex, std::ios::basefield);
@@ -64,8 +46,6 @@ std::string DumpByteBufferToString(uint8_t* p_data, size_t len) {
  return str.str();
}

uint16_t L2CA_Register(unsigned short, tL2CAP_APPL_INFO*, bool) { return 0; }

std::string DumpBtHdrToString(BT_HDR* p_hdr) {
  uint8_t* p_hdr_data = p_hdr->data + p_hdr->offset;
  return DumpByteBufferToString(p_hdr_data, p_hdr->len);
@@ -473,7 +453,7 @@ class StackRfcommTest : public Test {
        &btm_security_internal_interface_);
    bluetooth::l2cap::SetMockInterface(&l2cap_interface_);
    rfcomm_callback = &rfcomm_callback_;
    EXPECT_CALL(l2cap_interface_, Register(BT_PSM_RFCOMM, _))
    EXPECT_CALL(l2cap_interface_, Register(BT_PSM_RFCOMM, _, _))
        .WillOnce(
            DoAll(SaveArgPointee<1>(&l2cap_appl_info_), Return(BT_PSM_RFCOMM)));
    RFCOMM_Init();