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

Commit cbfa2222 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

L2CAP: Simplify parameters for segmenter and reassembler

Currently remote_cid and queue_down_end can be fetched from ChannelImpl.
Remove unnecessary parameters for better cohesion.

To make it work, we cannot mock AllocateFixedChannel() in Link.
Since Link needs to allocate a fixed channel for signalling manager
during ctor, it doesn't make sense to make AllocateFixedChannel()
virtual.
Since the coupling between Link and Signalling Manager is too tight, we
are unable to test signalling manager by itself. Instead use cert test.

Bug: 144503952
Bug: 141557006
Test: run_cert.sh and bluetooth_test_gd
Change-Id: I4be323bc7a9810f5f6b500b4f0f0fc4fcad12b6c
parent 304726c3
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -60,8 +60,8 @@ void Link::Disconnect() {

std::shared_ptr<FixedChannelImpl> Link::AllocateFixedChannel(Cid cid, SecurityPolicy security_policy) {
  auto channel = fixed_channel_allocator_.AllocateChannel(cid, security_policy);
  scheduler_->AttachChannel(cid, channel->GetQueueDownEnd(), cid, nullptr);
  reassembler_.AttachChannel(cid, channel->GetQueueDownEnd(), nullptr);
  scheduler_->AttachChannel(cid, channel);
  reassembler_.AttachChannel(cid, channel);
  return channel;
}

@@ -95,8 +95,8 @@ std::shared_ptr<DynamicChannelImpl> Link::AllocateDynamicChannel(Psm psm, Cid re
                                                                 SecurityPolicy security_policy) {
  auto channel = dynamic_channel_allocator_.AllocateChannel(psm, remote_cid, security_policy);
  if (channel != nullptr) {
    scheduler_->AttachChannel(channel->GetCid(), channel->GetQueueDownEnd(), channel->GetRemoteCid(), channel);
    reassembler_.AttachChannel(channel->GetCid(), channel->GetQueueDownEnd(), channel);
    scheduler_->AttachChannel(channel->GetCid(), channel);
    reassembler_.AttachChannel(channel->GetCid(), channel);
  }
  channel->local_initiated_ = false;
  return channel;
@@ -106,8 +106,8 @@ std::shared_ptr<DynamicChannelImpl> Link::AllocateReservedDynamicChannel(Cid res
                                                                         SecurityPolicy security_policy) {
  auto channel = dynamic_channel_allocator_.AllocateReservedChannel(reserved_cid, psm, remote_cid, security_policy);
  if (channel != nullptr) {
    scheduler_->AttachChannel(channel->GetCid(), channel->GetQueueDownEnd(), channel->GetRemoteCid(), channel);
    reassembler_.AttachChannel(channel->GetCid(), channel->GetQueueDownEnd(), channel);
    scheduler_->AttachChannel(channel->GetCid(), channel);
    reassembler_.AttachChannel(channel->GetCid(), channel);
  }
  channel->local_initiated_ = true;
  return channel;
+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ class Link {

  // FixedChannel methods

  virtual std::shared_ptr<FixedChannelImpl> AllocateFixedChannel(Cid cid, SecurityPolicy security_policy);
  std::shared_ptr<FixedChannelImpl> AllocateFixedChannel(Cid cid, SecurityPolicy security_policy);

  virtual bool IsFixedChannelAllocated(Cid cid);

+0 −2
Original line number Diff line number Diff line
@@ -43,8 +43,6 @@ class MockLink : public Link {
  MOCK_METHOD(hci::Address, GetDevice, (), (override));
  MOCK_METHOD(void, OnAclDisconnected, (hci::ErrorCode status), (override));
  MOCK_METHOD(void, Disconnect, (), (override));
  MOCK_METHOD(std::shared_ptr<FixedChannelImpl>, AllocateFixedChannel, (Cid cid, SecurityPolicy security_policy),
              (override));
  MOCK_METHOD(std::shared_ptr<DynamicChannelImpl>, AllocateDynamicChannel,
              (Psm psm, Cid cid, SecurityPolicy security_policy), (override));
  MOCK_METHOD(bool, IsFixedChannelAllocated, (Cid cid), (override));
+1 −30
Original line number Diff line number Diff line
@@ -58,36 +58,7 @@ class L2capClassicSignallingManagerTest : public ::testing::Test {
  os::Handler* l2cap_handler_ = nullptr;
};

PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
  auto bytes = std::make_shared<std::vector<uint8_t>>();
  BitInserter i(*bytes);
  bytes->reserve(packet->size());
  packet->Serialize(i);
  return packet::PacketView<packet::kLittleEndian>(bytes);
}

TEST_F(L2capClassicSignallingManagerTest, handle_connection_request) {
  l2cap::internal::testing::MockParameterProvider parameter_provider;
  testing::MockDynamicChannelServiceManagerImpl dynamic_service_manager_;
  testing::MockFixedChannelServiceManagerImpl fixed_service_manager_;
  testing::MockLink link{l2cap_handler_, &parameter_provider};
  std::shared_ptr<FixedChannelImpl> signalling_channel = std::make_shared<FixedChannelImpl>(1, &link, l2cap_handler_);
  EXPECT_CALL(link, AllocateFixedChannel(_, _)).WillRepeatedly(Return(signalling_channel));
  auto service_psm = 0x1;
  EXPECT_CALL(dynamic_service_manager_, IsServiceRegistered(service_psm)).WillRepeatedly(Return(true));
  DynamicChannelAllocator channel_allocator{&link, l2cap_handler_};
  ClassicSignallingManager signalling_manager{l2cap_handler_, &link, &dynamic_service_manager_, &channel_allocator,
                                              &fixed_service_manager_};
  auto* down_end = signalling_channel->GetQueueDownEnd();
  os::EnqueueBuffer<packet::PacketView<kLittleEndian>> enqueue_buffer{down_end};
  auto dcid = 0x101;
  auto builder = ConnectionRequestBuilder::Create(1, service_psm, dcid);
  enqueue_buffer.Enqueue(std::make_unique<PacketView<kLittleEndian>>(GetPacketView(std::move(builder))),
                         l2cap_handler_);
  SyncHandler(l2cap_handler_);
  EXPECT_CALL(link, AllocateDynamicChannel(_, dcid, _));
  SyncHandler(l2cap_handler_);
}
TEST_F(L2capClassicSignallingManagerTest, precondition) {}

}  // namespace
}  // namespace internal
+44 −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.
 */

#pragma once

#include "l2cap/internal/channel_impl.h"

#include <gmock/gmock.h>

// Unit test interfaces
namespace bluetooth {
namespace l2cap {
namespace internal {
namespace testing {

class MockChannelImpl : public ChannelImpl {
 public:
  MOCK_METHOD((common::BidiQueueEnd<packet::BasePacketBuilder, packet::PacketView<packet::kLittleEndian>>*),
              GetQueueUpEnd, (), (override));
  MOCK_METHOD((common::BidiQueueEnd<packet::PacketView<packet::kLittleEndian>, packet::BasePacketBuilder>*),
              GetQueueDownEnd, (), (override));
  MOCK_METHOD(Cid, GetCid, (), (const, override));
  MOCK_METHOD(Cid, GetRemoteCid, (), (const, override));
  MOCK_METHOD(RetransmissionAndFlowControlModeOption, GetChannelMode, (), (const, override));
  MOCK_METHOD(void, SetChannelMode, (RetransmissionAndFlowControlModeOption), (override));
};

}  // namespace testing
}  // namespace internal
}  // namespace l2cap
}  // namespace bluetooth
Loading