/* * Copyright 2024 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 #include "log.h" #include "model/controller/dual_mode_controller.h" namespace rootcanal { using namespace bluetooth::hci; class InvalidPacketHandlerTest : public ::testing::Test { public: InvalidPacketHandlerTest() = default; ~InvalidPacketHandlerTest() override = default; protected: DualModeController controller_; }; // Set Event Mask command with missing parameters. const std::vector kInvalidCommandPacket = {0x01, 0x0C, 0x03, 0xff, 0xff, 0xff}; // Hardware Error event with code 0x43. const std::vector kHardwareErrorEvent = {0x10, 0x01, 0x43}; TEST_F(InvalidPacketHandlerTest, DefaultHandler) { // Validate that the default invalid packet handler causes // an abort when an invalid packet is received. ASSERT_DEATH(controller_.HandleCommand(std::make_shared>( kInvalidCommandPacket)), ""); } TEST_F(InvalidPacketHandlerTest, RegisteredHandler) { static struct { uint32_t id; InvalidPacketReason reason; std::vector bytes; } invalid_packet; static std::vector hci_event; // Validate that the registered invalid packet handler is correctly // invoked when an invalid packet is received. controller_.RegisterInvalidPacketHandler( [&](uint32_t id, InvalidPacketReason reason, std::string, std::vector const& bytes) { invalid_packet.id = id; invalid_packet.reason = reason; invalid_packet.bytes = bytes; }); controller_.RegisterEventChannel( [&](std::shared_ptr> packet) { hci_event = std::vector(*packet); }); controller_.HandleCommand( std::make_shared>(kInvalidCommandPacket)); ASSERT_EQ(invalid_packet.id, controller_.id_); ASSERT_EQ(invalid_packet.reason, InvalidPacketReason::kParseError); ASSERT_EQ(invalid_packet.bytes, kInvalidCommandPacket); ASSERT_EQ(hci_event, kHardwareErrorEvent); } } // namespace rootcanal