1341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker/* 2341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * Copyright 2018 The Android Open Source Project 3341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * 4341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * Licensed under the Apache License, Version 2.0 (the "License"); 5341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * you may not use this file except in compliance with the License. 6341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * You may obtain a copy of the License at 7341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * 8341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * http://www.apache.org/licenses/LICENSE-2.0 9341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * 10341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * Unless required by applicable law or agreed to in writing, software 11341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * distributed under the License is distributed on an "AS IS" BASIS, 12341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * See the License for the specific language governing permissions and 14341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker * limitations under the License. 15341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker */ 16341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 17341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker#include <gtest/gtest.h> 18341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 19341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker#include "avrcp_packet.h" 20341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker#include "avrcp_test_packets.h" 21341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker#include "packet_test_helper.h" 22341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 23341ab2befa059e859cdc67655d83bac14980ea9eAjay Panickernamespace bluetooth { 24341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 25341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker// A helper class that has public accessors to protected methods 26341ab2befa059e859cdc67655d83bac14980ea9eAjay Panickerclass TestPacketBuilder : public PacketBuilder { 27341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker public: 28341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker static std::unique_ptr<TestPacketBuilder> MakeBuilder( 29341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker std::vector<uint8_t> data) { 30341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker std::unique_ptr<TestPacketBuilder> builder(new TestPacketBuilder(data)); 31341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker return builder; 32341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker }; 33341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 34341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker // Make all the utility functions public 35341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::ReserveSpace; 36341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::AddPayloadOctets1; 37341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::AddPayloadOctets2; 38341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::AddPayloadOctets3; 39341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::AddPayloadOctets4; 40341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::AddPayloadOctets6; 41341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker using PacketBuilder::AddPayloadOctets8; 42341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 43341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker size_t size() const override { return data_.size(); }; 44341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 45341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker bool Serialize(const std::shared_ptr<Packet>& pkt) override { 46341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ReserveSpace(pkt, size()); 47341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 48341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker for (uint8_t byte : data_) { 49341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker AddPayloadOctets1(pkt, byte); 50341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker } 51341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 52341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker return true; 53341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker } 54341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 55341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker TestPacketBuilder(std::vector<uint8_t> data) : data_(data){}; 56341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 57341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker std::vector<uint8_t> data_; 58341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker}; 59341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 60341ab2befa059e859cdc67655d83bac14980ea9eAjay Panickernamespace avrcp { 61341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 62341ab2befa059e859cdc67655d83bac14980ea9eAjay Panickerusing TestAvrcpPacket = TestPacketType<Packet>; 63341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 64341ab2befa059e859cdc67655d83bac14980ea9eAjay PanickerTEST(AvrcpPacketBuilderTest, buildPacketTest) { 65341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker std::vector<uint8_t> get_capabilities_request_payload = { 66341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 0x00, 0x19, 0x58, 0x10, 0x00, 0x00, 0x01, 0x03}; 67341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto cap_req_builder = 68341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker TestPacketBuilder::MakeBuilder(get_capabilities_request_payload); 69341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 70341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto builder = PacketBuilder::MakeBuilder( 71341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker CType::STATUS, 0x09, 0x00, Opcode::VENDOR, std::move(cap_req_builder)); 72341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 73341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(builder->size(), get_capabilities_request.size()); 74341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 75341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto test_packet = TestAvrcpPacket::Make(); 76341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker builder->Serialize(test_packet); 77341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_packet->GetData(), get_capabilities_request); 78341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker} 79341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 80341ab2befa059e859cdc67655d83bac14980ea9eAjay PanickerTEST(AvrcpPacketTest, getterTests) { 81341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto test_avrcp_packet = TestAvrcpPacket::Make(get_capabilities_request); 82341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 83341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetCType(), CType::STATUS); 84341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetSubunitType(), 0x09); 85341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetSubunitId(), 0x00); 86341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetOpcode(), Opcode::VENDOR); 87341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker} 88341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 89341ab2befa059e859cdc67655d83bac14980ea9eAjay PanickerTEST(AvrcpPacketTest, getterMaskTests) { 90341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto bad_get_cap_data = get_capabilities_request; 91341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker bad_get_cap_data[0] = 0xFF; // CType 92341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker bad_get_cap_data[1] = 0xFF; // Subunit Type & ID 93341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 94341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto test_avrcp_packet = TestAvrcpPacket::Make(bad_get_cap_data); 95341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 96341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetCType(), CType::INTERIM); 97341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetSubunitType(), 0b00011111); 98341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(test_avrcp_packet->GetSubunitId(), 0b00000111); 99341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker} 100341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 101341ab2befa059e859cdc67655d83bac14980ea9eAjay PanickerTEST(AvrcpPacketTest, payloadBoundsTest) { 102341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto test_avrcp_packet = TestAvrcpPacket::Make(get_capabilities_request); 103341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 104341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker std::vector<uint8_t> get_cap_payload_data = {0x00, 0x19, 0x58, 0x10, 105341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 0x00, 0x00, 0x01, 0x03}; 1068f7377353db29efe0a506123deec03d70935957aAjay Panicker 107341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker auto get_cap_payload_packet = TestAvrcpPacket::Make(test_avrcp_packet); 108341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 109341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker // We are unable to do a direct vector compare here as one of the packets is 110341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker // a larger vector that only has a segment of data currently visible. 111341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker for (size_t i = 0; i < get_cap_payload_data.size(); i++) { 112341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker ASSERT_EQ(get_cap_payload_data[i], (*get_cap_payload_packet)[i]); 113341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker } 114341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker} 115341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker 116341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker} // namespace avrcp 117341ab2befa059e859cdc67655d83bac14980ea9eAjay Panicker} // namespace bluetooth