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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <stdlib.h>
20
21 #include <cstddef>
22 #include <cstdint>
23 #include <memory>
24
25 #include "btif/include/btif_common.h"
26 #include "osi/include/allocator.h"
27 #include "stack/include/sdp_api.h"
28 #include "stack/sdp/sdpint.h"
29 #include "test/fake/fake_osi.h"
30 #include "test/mock/mock_osi_allocator.h"
31 #include "test/mock/mock_stack_l2cap_interface.h"
32
33 #ifndef BT_DEFAULT_BUFFER_SIZE
34 #define BT_DEFAULT_BUFFER_SIZE (4096 + 16)
35 #endif
36
37 using ::testing::_;
38 using ::testing::DoAll;
39 using ::testing::Invoke;
40 using ::testing::Return;
41 using ::testing::ReturnArg;
42 using ::testing::SaveArg;
43
44 namespace {
45 constexpr uint8_t kSDP_MAX_CONNECTIONS = static_cast<uint8_t>(SDP_MAX_CONNECTIONS);
46
47 const RawAddress kRawAddress = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6});
48 int L2CA_ConnectReqWithSecurity_cid = 0x42;
49
50 class StackSdpWithMocksTest : public ::testing::Test {
51 protected:
SetUp()52 void SetUp() override {
53 fake_osi_ = std::make_unique<::test::fake::FakeOsi>();
54 bluetooth::testing::stack::l2cap::set_interface(&mock_stack_l2cap_interface_);
55
56 EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_RegisterWithSecurity(_, _, _, _, _, _, _))
57 .WillOnce(DoAll(SaveArg<1>(&l2cap_callbacks_), ::testing::ReturnArg<0>()));
58 EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_Deregister(_));
59 }
60
TearDown()61 void TearDown() override {
62 bluetooth::testing::stack::l2cap::reset_interface();
63 fake_osi_.reset();
64 }
65
66 tL2CAP_APPL_INFO l2cap_callbacks_{};
67 bluetooth::testing::stack::l2cap::Mock mock_stack_l2cap_interface_;
68 std::unique_ptr<test::fake::FakeOsi> fake_osi_;
69 };
70
71 class StackSdpApiTest : public StackSdpWithMocksTest {
72 protected:
SetUp()73 void SetUp() override {
74 StackSdpWithMocksTest::SetUp();
75 sdp_init();
76 }
77
TearDown()78 void TearDown() override {
79 sdp_free();
80 StackSdpWithMocksTest::TearDown();
81 }
82 };
83
84 } // namespace
85
TEST_F(StackSdpApiTest,nop)86 TEST_F(StackSdpApiTest, nop) {}
87
TEST_F(StackSdpApiTest,SDP_ServiceSearchRequest)88 TEST_F(StackSdpApiTest, SDP_ServiceSearchRequest) {
89 EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _))
90 .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */,
91 uint16_t /* sec_level */) -> uint16_t {
92 return L2CA_ConnectReqWithSecurity_cid;
93 }));
94 for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) {
95 RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i});
96 ASSERT_NE(nullptr, sdp_conn_originate(bd_addr));
97 }
98 tSDP_DISCOVERY_DB db;
99 ASSERT_FALSE(bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api()
100 ->service.SDP_ServiceSearchRequest(
101 kRawAddress, &db,
102 [](const RawAddress& /* bd_addr */, tSDP_RESULT /* result */) {}));
103 }
104
TEST_F(StackSdpApiTest,SDP_ServiceSearchAttributeRequest)105 TEST_F(StackSdpApiTest, SDP_ServiceSearchAttributeRequest) {
106 EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _))
107 .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */,
108 uint16_t /* sec_level */) -> uint16_t {
109 return L2CA_ConnectReqWithSecurity_cid;
110 }));
111 for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) {
112 RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i});
113 ASSERT_NE(nullptr, sdp_conn_originate(bd_addr));
114 }
115 tSDP_DISCOVERY_DB db;
116 ASSERT_FALSE(bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api()
117 ->service.SDP_ServiceSearchAttributeRequest(
118 kRawAddress, &db,
119 [](const RawAddress& /* bd_addr */, tSDP_RESULT /* result */) {}));
120 }
121
TEST_F(StackSdpApiTest,SDP_ServiceSearchAttributeRequest2)122 TEST_F(StackSdpApiTest, SDP_ServiceSearchAttributeRequest2) {
123 EXPECT_CALL(mock_stack_l2cap_interface_, L2CA_ConnectReqWithSecurity(_, _, _))
124 .WillRepeatedly(Invoke([](uint16_t /* psm */, const RawAddress& /* p_bd_addr */,
125 uint16_t /* sec_level */) -> uint16_t {
126 return L2CA_ConnectReqWithSecurity_cid;
127 }));
128 for (uint8_t i = 0; i < kSDP_MAX_CONNECTIONS; i++) {
129 RawAddress bd_addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, i});
130 ASSERT_NE(nullptr, sdp_conn_originate(bd_addr));
131 }
132 tSDP_DISCOVERY_DB db;
133 ASSERT_FALSE(bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api()
134 ->service.SDP_ServiceSearchAttributeRequest2(
135 kRawAddress, &db,
136 base::BindRepeating([](const RawAddress& /* bd_addr */,
137 tSDP_RESULT /* result */) {})));
138 }
139