1 /*
2  * Copyright 2021 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 <com_android_bluetooth_flags.h>
18 #include <gtest/gtest.h>
19 
20 #include <cstdint>
21 
22 #include "stack/include/btu_hcif.h"
23 #include "stack/include/hci_error_code.h"
24 #include "stack/include/hcidefs.h"
25 #include "test/common/mock_functions.h"
26 
27 /* Function for test provided by btu_hcif.cc */
28 void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status, const uint8_t* p_cmd);
29 
30 class StackBtuTest : public ::testing::Test {
31 protected:
SetUp()32   void SetUp() override {
33     reset_mock_function_count_map();
34     com::android::bluetooth::flags::provider_->reset_flags();
35   }
36 };
37 
TEST_F(StackBtuTest,post_on_main)38 TEST_F(StackBtuTest, post_on_main) {}
39 
TEST_F(StackBtuTest,btm_sco_connection_failed_called)40 TEST_F(StackBtuTest, btm_sco_connection_failed_called) {
41   // TODO b/358573137  - remove when flag is removing
42   com::android::bluetooth::flags::provider_->fix_sco_command_status_handling(false);
43   uint8_t p_cmd[10];  // garbage data for testing
44   bluetooth::legacy::testing::btu_hcif_hdl_command_status(HCI_SETUP_ESCO_CONNECTION,
45                                                           HCI_ERR_UNSPECIFIED, p_cmd);
46   ASSERT_EQ(1, get_func_call_count("btm_sco_connection_failed"));
47 }
48 
TEST_F(StackBtuTest,btm_sco_connection_failed_called_fixed)49 TEST_F(StackBtuTest, btm_sco_connection_failed_called_fixed) {
50   com::android::bluetooth::flags::provider_->fix_sco_command_status_handling(true);
51   uint8_t test_data[18];  // garbage data for testing
52   bluetooth::legacy::testing::btu_hcif_hdl_command_status(HCI_SETUP_ESCO_CONNECTION,
53                                                           HCI_ERR_UNSPECIFIED, test_data);
54   ASSERT_EQ(0, get_func_call_count("btm_sco_connection_failed"));
55 
56   // prepare sco complete event with an error
57   BT_HDR* esco_command_complete_ev = (BT_HDR*)malloc(sizeof(BT_HDR) + sizeof(test_data));
58   esco_command_complete_ev->event = HCI_ESCO_CONNECTION_COMP_EVT,
59   esco_command_complete_ev->len = sizeof(test_data);
60   esco_command_complete_ev->offset = 0,
61 
62   // Event code
63           esco_command_complete_ev->data[0] = HCI_ESCO_CONNECTION_COMP_EVT;
64   // Event len
65   esco_command_complete_ev->data[1] = 17;
66   // Event status - error
67   esco_command_complete_ev->data[2] = 0x0a;
68 
69   bluetooth::legacy::testing::btu_hcif_process_event(0, esco_command_complete_ev);
70   ASSERT_EQ(1, get_func_call_count("btm_sco_connection_failed"));
71 }
72 
TEST_F(StackBtuTest,btm_sco_create_connection_status_failed_called)73 TEST_F(StackBtuTest, btm_sco_create_connection_status_failed_called) {
74   com::android::bluetooth::flags::provider_->fix_sco_command_status_handling(true);
75   uint8_t test_data[10];  // garbage data for testing
76   bluetooth::legacy::testing::btu_hcif_hdl_command_status(HCI_SETUP_ESCO_CONNECTION,
77                                                           HCI_ERR_UNSPECIFIED, test_data);
78   ASSERT_EQ(1, get_func_call_count("btm_sco_create_command_status_failed"));
79 }
80