1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <array>
20 
21 #include "bta/dm/bta_dm_int.h"
22 #include "bta/hh/bta_hh_int.h"
23 #include "bta/include/bta_hh_api.h"
24 #include "bta/include/bta_le_audio_api.h"
25 #include "osi/include/allocator.h"
26 #include "test/common/mock_functions.h"
27 #include "test/mock/mock_osi_allocator.h"
28 
29 namespace {
30 std::array<uint8_t, 32> data32 = {
31         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
32         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
33         0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
34 };
35 }
36 
37 class BtaHhTest : public ::testing::Test {
38 protected:
SetUp()39   void SetUp() override {
40     reset_mock_function_count_map();
41     test::mock::osi_allocator::osi_malloc.body = [](size_t size) { return malloc(size); };
42     test::mock::osi_allocator::osi_calloc.body = [](size_t size) { return calloc(1UL, size); };
43     test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
44     test::mock::osi_allocator::osi_free_and_reset.body = [](void** ptr) {
45       free(*ptr);
46       *ptr = nullptr;
47     };
48   }
49 
TearDown()50   void TearDown() override {
51     bta_hh_cb.p_cback = nullptr;
52 
53     test::mock::osi_allocator::osi_malloc = {};
54     test::mock::osi_allocator::osi_calloc = {};
55     test::mock::osi_allocator::osi_free = {};
56     test::mock::osi_allocator::osi_free_and_reset = {};
57   }
58 };
59 
TEST_F(BtaHhTest,simple)60 TEST_F(BtaHhTest, simple) {}
61 
TEST_F(BtaHhTest,bta_hh_ctrl_dat_act__BTA_HH_GET_RPT_EVT)62 TEST_F(BtaHhTest, bta_hh_ctrl_dat_act__BTA_HH_GET_RPT_EVT) {
63   tBTA_HH_DEV_CB cb = {
64           .w4_evt = BTA_HH_GET_RPT_EVT,
65   };
66 
67   tBTA_HH_DATA data = {
68           .hid_cback =
69                   {
70                           .hdr =
71                                   {
72                                           .event = 0,
73                                           .len = 0,
74                                           .offset = 0,
75                                           .layer_specific = 0,
76                                   },
77                           .link_spec.addrt.bda = RawAddress::kEmpty,
78                           .link_spec.addrt.type = BLE_ADDR_PUBLIC,
79                           .link_spec.transport = BT_TRANSPORT_AUTO,
80                           .data = 32,
81                           .p_data = static_cast<BT_HDR*>(osi_calloc(32 + sizeof(BT_HDR))),
82                   },
83   };
84 
85   data.hid_cback.p_data->len = static_cast<uint16_t>(data32.size());
86   uint8_t* p_data = (uint8_t*)(data.hid_cback.p_data + 1);
87   int i = 0;
88   for (const auto& byte : data32) {
89     p_data[i++] = byte;
90   }
91 
92   bta_hh_cb.p_cback = [](tBTA_HH_EVT event, tBTA_HH* p_data) {
93     tBTA_HH_HSDATA& hs_data = p_data->hs_data;
94     uint8_t* data = (uint8_t*)(hs_data.rsp_data.p_rpt_data + 1);
95     ASSERT_EQ(BTA_HH_GET_RPT_EVT, event);
96     int i = 0;
97     for (const auto& byte : data32) {
98       ASSERT_EQ(byte, data[i++]);
99     }
100   };
101 
102   bta_hh_ctrl_dat_act(&cb, &data);
103   ASSERT_EQ(cb.w4_evt, BTA_HH_EMPTY_EVT);
104 }
105