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) {
42       return malloc(size);
43     };
44     test::mock::osi_allocator::osi_calloc.body = [](size_t size) {
45       return calloc(1UL, size);
46     };
47     test::mock::osi_allocator::osi_free.body = [](void* ptr) { free(ptr); };
48     test::mock::osi_allocator::osi_free_and_reset.body = [](void** ptr) {
49       free(*ptr);
50       *ptr = nullptr;
51     };
52   }
53 
TearDown()54   void TearDown() override {
55     bta_hh_cb.p_cback = nullptr;
56 
57     test::mock::osi_allocator::osi_malloc = {};
58     test::mock::osi_allocator::osi_calloc = {};
59     test::mock::osi_allocator::osi_free = {};
60     test::mock::osi_allocator::osi_free_and_reset = {};
61   }
62 };
63 
TEST_F(BtaHhTest,simple)64 TEST_F(BtaHhTest, simple) {}
65 
TEST_F(BtaHhTest,bta_hh_ctrl_dat_act__BTA_HH_GET_RPT_EVT)66 TEST_F(BtaHhTest, bta_hh_ctrl_dat_act__BTA_HH_GET_RPT_EVT) {
67   tBTA_HH_DEV_CB cb = {
68       .w4_evt = BTA_HH_GET_RPT_EVT,
69   };
70 
71   tBTA_HH_DATA data = {
72       .hid_cback =
73           {
74               .hdr =
75                   {
76                       .event = 0,
77                       .len = 0,
78                       .offset = 0,
79                       .layer_specific = 0,
80                   },
81               .link_spec.addrt.bda = RawAddress::kEmpty,
82               .link_spec.addrt.type = BLE_ADDR_PUBLIC,
83               .link_spec.transport = BT_TRANSPORT_AUTO,
84               .data = 32,
85               .p_data = static_cast<BT_HDR*>(osi_calloc(32 + sizeof(BT_HDR))),
86           },
87   };
88 
89   data.hid_cback.p_data->len = static_cast<uint16_t>(data32.size());
90   uint8_t* p_data = (uint8_t*)(data.hid_cback.p_data + 1);
91   int i = 0;
92   for (const auto& byte : data32) {
93     p_data[i++] = byte;
94   }
95 
96   bta_hh_cb.p_cback = [](tBTA_HH_EVT event, tBTA_HH* p_data) {
97     tBTA_HH_HSDATA& hs_data = p_data->hs_data;
98     uint8_t* data = (uint8_t*)(hs_data.rsp_data.p_rpt_data + 1);
99     ASSERT_EQ(BTA_HH_GET_RPT_EVT, event);
100     int i = 0;
101     for (const auto& byte : data32) {
102       ASSERT_EQ(byte, data[i++]);
103     }
104   };
105 
106   bta_hh_ctrl_dat_act(&cb, &data);
107   ASSERT_EQ(cb.w4_evt, BTA_HH_EMPTY_EVT);
108 }
109