1 /*
2 * Copyright 2024 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 <base/strings/stringprintf.h>
18 #include <gtest/gtest.h>
19 #include <stdlib.h>
20
21 #include "hci_error_code.h"
22 #include "stack/btm/btm_int_types.h"
23 #include "stack/include/inq_hci_link_interface.h"
24 #include "stack/test/btm/btm_test_fixtures.h"
25 #include "test/fake/fake_looper.h"
26 #include "test/mock/mock_osi_allocator.h"
27 #include "test/mock/mock_osi_thread.h"
28 #include "types/raw_address.h"
29
30 extern tBTM_CB btm_cb;
31
32 namespace {
33 const RawAddress kRawAddress = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
34 const RawAddress kRawAddress2 =
35 RawAddress({0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc});
36 const BD_NAME kBdName = {'A', ' ', 'B', 'd', ' ', 'N', 'a', 'm', 'e', '\0'};
37 const BD_NAME kEmptyName = "";
38
39 tBTM_REMOTE_DEV_NAME gBTM_REMOTE_DEV_NAME{};
40 bool gBTM_REMOTE_DEV_NAME_sent{false};
41
42 } // namespace
43
44 class BtmInqTest : public BtmWithMocksTest {
45 protected:
SetUp()46 void SetUp() override {
47 BtmWithMocksTest::SetUp();
48 btm_cb = {};
49 }
50
TearDown()51 void TearDown() override { BtmWithMocksTest::TearDown(); }
52 };
53
54 class BtmInqActiveTest : public BtmInqTest {
55 protected:
SetUp()56 void SetUp() override {
57 BtmInqTest::SetUp();
58 gBTM_REMOTE_DEV_NAME = {};
59 gBTM_REMOTE_DEV_NAME_sent = false;
60
61 btm_cb.btm_inq_vars.remname_active = true;
62 btm_cb.btm_inq_vars.remname_bda = kRawAddress;
63 btm_cb.btm_inq_vars.remname_dev_type = BT_DEVICE_TYPE_BREDR;
64 btm_cb.btm_inq_vars.p_remname_cmpl_cb =
65 [](const tBTM_REMOTE_DEV_NAME* name) {
66 gBTM_REMOTE_DEV_NAME = *name;
67 gBTM_REMOTE_DEV_NAME_sent = true;
68 };
69 }
70
TearDown()71 void TearDown() override { BtmInqTest::TearDown(); }
72 };
73
TEST_F(BtmInqActiveTest,btm_process_remote_name__typical)74 TEST_F(BtmInqActiveTest, btm_process_remote_name__typical) {
75 btm_process_remote_name(&kRawAddress, kBdName, 0, HCI_SUCCESS);
76 ASSERT_FALSE(btm_cb.btm_inq_vars.p_remname_cmpl_cb);
77 ASSERT_FALSE(btm_cb.btm_inq_vars.remname_active);
78 ASSERT_EQ(btm_cb.btm_inq_vars.remname_bda, RawAddress::kEmpty);
79 ASSERT_EQ(btm_cb.btm_inq_vars.remname_dev_type, BT_DEVICE_TYPE_UNKNOWN);
80 ASSERT_EQ(1, get_func_call_count("alarm_cancel"));
81
82 ASSERT_TRUE(gBTM_REMOTE_DEV_NAME_sent);
83 ASSERT_EQ(BTM_SUCCESS, gBTM_REMOTE_DEV_NAME.status);
84 ASSERT_EQ(HCI_SUCCESS, gBTM_REMOTE_DEV_NAME.hci_status);
85 ASSERT_EQ(kRawAddress, gBTM_REMOTE_DEV_NAME.bd_addr);
86 ASSERT_STREQ((char*)kBdName, (char*)gBTM_REMOTE_DEV_NAME.remote_bd_name);
87 }
88
TEST_F(BtmInqActiveTest,btm_process_remote_name__no_name)89 TEST_F(BtmInqActiveTest, btm_process_remote_name__no_name) {
90 btm_process_remote_name(&kRawAddress, nullptr, 0, HCI_SUCCESS);
91 ASSERT_FALSE(btm_cb.btm_inq_vars.p_remname_cmpl_cb);
92 ASSERT_FALSE(btm_cb.btm_inq_vars.remname_active);
93 ASSERT_EQ(btm_cb.btm_inq_vars.remname_bda, RawAddress::kEmpty);
94 ASSERT_EQ(btm_cb.btm_inq_vars.remname_dev_type, BT_DEVICE_TYPE_UNKNOWN);
95 ASSERT_EQ(1, get_func_call_count("alarm_cancel"));
96
97 ASSERT_TRUE(gBTM_REMOTE_DEV_NAME_sent);
98 ASSERT_EQ(BTM_SUCCESS, gBTM_REMOTE_DEV_NAME.status);
99 ASSERT_EQ(HCI_SUCCESS, gBTM_REMOTE_DEV_NAME.hci_status);
100 ASSERT_EQ(kRawAddress, gBTM_REMOTE_DEV_NAME.bd_addr);
101 ASSERT_STREQ((char*)kEmptyName, (char*)gBTM_REMOTE_DEV_NAME.remote_bd_name);
102 }
103
TEST_F(BtmInqActiveTest,btm_process_remote_name__bad_status)104 TEST_F(BtmInqActiveTest, btm_process_remote_name__bad_status) {
105 btm_process_remote_name(&kRawAddress, kBdName, 0, HCI_ERR_PAGE_TIMEOUT);
106 ASSERT_FALSE(btm_cb.btm_inq_vars.p_remname_cmpl_cb);
107 ASSERT_FALSE(btm_cb.btm_inq_vars.remname_active);
108 ASSERT_EQ(btm_cb.btm_inq_vars.remname_bda, RawAddress::kEmpty);
109 ASSERT_EQ(btm_cb.btm_inq_vars.remname_dev_type, BT_DEVICE_TYPE_UNKNOWN);
110 ASSERT_EQ(1, get_func_call_count("alarm_cancel"));
111
112 ASSERT_TRUE(gBTM_REMOTE_DEV_NAME_sent);
113 ASSERT_EQ(BTM_BAD_VALUE_RET, gBTM_REMOTE_DEV_NAME.status);
114 ASSERT_EQ(HCI_ERR_PAGE_TIMEOUT, gBTM_REMOTE_DEV_NAME.hci_status);
115 ASSERT_EQ(kRawAddress, gBTM_REMOTE_DEV_NAME.bd_addr);
116 ASSERT_STREQ((char*)kEmptyName, (char*)gBTM_REMOTE_DEV_NAME.remote_bd_name);
117 }
118
TEST_F(BtmInqActiveTest,btm_process_remote_name__no_address)119 TEST_F(BtmInqActiveTest, btm_process_remote_name__no_address) {
120 btm_process_remote_name(nullptr, kBdName, 0, HCI_SUCCESS);
121 ASSERT_FALSE(btm_cb.btm_inq_vars.p_remname_cmpl_cb);
122 ASSERT_FALSE(btm_cb.btm_inq_vars.remname_active);
123 ASSERT_EQ(btm_cb.btm_inq_vars.remname_bda, RawAddress::kEmpty);
124 ASSERT_EQ(btm_cb.btm_inq_vars.remname_dev_type, BT_DEVICE_TYPE_UNKNOWN);
125 ASSERT_EQ(1, get_func_call_count("alarm_cancel"));
126
127 ASSERT_TRUE(gBTM_REMOTE_DEV_NAME_sent);
128 ASSERT_EQ(BTM_SUCCESS, gBTM_REMOTE_DEV_NAME.status);
129 ASSERT_EQ(HCI_SUCCESS, gBTM_REMOTE_DEV_NAME.hci_status);
130 ASSERT_EQ(RawAddress::kEmpty, gBTM_REMOTE_DEV_NAME.bd_addr);
131 ASSERT_STREQ((char*)kBdName, (char*)gBTM_REMOTE_DEV_NAME.remote_bd_name);
132 }
133
TEST_F(BtmInqActiveTest,btm_process_remote_name__different_address)134 TEST_F(BtmInqActiveTest, btm_process_remote_name__different_address) {
135 btm_cb.btm_inq_vars.remname_bda = kRawAddress2;
136 btm_process_remote_name(&kRawAddress, kBdName, 0, HCI_SUCCESS);
137 ASSERT_TRUE(btm_cb.btm_inq_vars.p_remname_cmpl_cb);
138 ASSERT_TRUE(btm_cb.btm_inq_vars.remname_active);
139 ASSERT_NE(btm_cb.btm_inq_vars.remname_bda, RawAddress::kEmpty);
140 ASSERT_NE(btm_cb.btm_inq_vars.remname_dev_type, BT_DEVICE_TYPE_UNKNOWN);
141 ASSERT_EQ(0, get_func_call_count("alarm_cancel"));
142
143 ASSERT_FALSE(gBTM_REMOTE_DEV_NAME_sent);
144 }
145