1 /*
2  * Copyright (C) 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 "bta/le_audio/gmap_server.h"
18 
19 #include <bluetooth/log.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <hardware/bluetooth.h>
23 
24 #include "bta/le_audio/le_audio_types.h"
25 #include "bta_gatt_api_mock.h"
26 #include "test/common/mock_functions.h"
27 
28 using ::testing::_;
29 using ::testing::AnyNumber;
30 using ::testing::DoAll;
31 using ::testing::DoDefault;
32 using ::testing::Invoke;
33 using ::testing::Mock;
34 using ::testing::NotNull;
35 using ::testing::Return;
36 using ::testing::SaveArg;
37 using ::testing::Sequence;
38 using ::testing::SetArgPointee;
39 using ::testing::WithArg;
40 
41 using ::testing::NiceMock;
42 
43 using bluetooth::Uuid;
44 using namespace bluetooth;
45 using bluetooth::le_audio::GmapCharacteristic;
46 using bluetooth::le_audio::GmapServer;
47 
48 class GmapServerTest : public ::testing::Test {
49 public:
50   RawAddress addr = RawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
51   NiceMock<gatt::MockBtaGattServerInterface> gatt_server_interface;
52   uint8_t role = 0b1;
53   uint8_t UGG_feature = 0b111;
54 
SetUp(void)55   void SetUp(void) override {
56     reset_mock_function_count_map();
57     gatt::SetMockBtaGattServerInterface(&gatt_server_interface);
58     EXPECT_CALL(gatt_server_interface, AppRegister(_, _, _)).Times(1);
59     GmapServer::Initialize(role, UGG_feature);
60   }
61 };
62 
TEST_F(GmapServerTest,test_get_role)63 TEST_F(GmapServerTest, test_get_role) { ASSERT_EQ(GmapServer::GetRole(), role); }
64 
TEST_F(GmapServerTest,test_get_UGG_feature)65 TEST_F(GmapServerTest, test_get_UGG_feature) {
66   ASSERT_EQ(GmapServer::GetUGGFeature(), UGG_feature);
67 }
68 
TEST_F(GmapServerTest,test_add_service)69 TEST_F(GmapServerTest, test_add_service) {
70   tBTA_GATTS gatts_cb_data;
71   uint8_t server_if = 10;
72   gatts_cb_data.reg_oper.status = GATT_SUCCESS;
73   gatts_cb_data.reg_oper.server_if = server_if;
74 
75   EXPECT_CALL(gatt_server_interface, AddService(_, _, _)).Times(1);
76   GmapServer::GattsCallback(BTA_GATTS_REG_EVT, &gatts_cb_data);
77 }
78 
TEST_F(GmapServerTest,test_app_deregister)79 TEST_F(GmapServerTest, test_app_deregister) {
80   tBTA_GATTS gatts_cb_data;
81   EXPECT_CALL(gatt_server_interface, AppDeregister(_)).Times(1);
82   GmapServer::GattsCallback(BTA_GATTS_DEREG_EVT, &gatts_cb_data);
83 }
84 
TEST_F(GmapServerTest,test_read_invalid_characteristic)85 TEST_F(GmapServerTest, test_read_invalid_characteristic) {
86   uint16_t handle = 10;
87   tGATTS_DATA gatts_data;
88   gatts_data.read_req.handle = handle;
89   tBTA_GATTS gatts_cb_data;
90   gatts_cb_data.req_data.p_data = &gatts_data;
91 
92   EXPECT_CALL(gatt_server_interface, SendRsp(_, _, GATT_INVALID_HANDLE, _)).Times(1);
93   GmapServer::GattsCallback(BTA_GATTS_READ_CHARACTERISTIC_EVT, &gatts_cb_data);
94 }
95 
TEST_F(GmapServerTest,test_read_invalid_role_characteristic)96 TEST_F(GmapServerTest, test_read_invalid_role_characteristic) {
97   uint16_t handle = 10;
98   GmapCharacteristic invalidGmapCharacteristic{
99           .uuid_ = bluetooth::le_audio::uuid::kTelephonyMediaAudioProfileRoleCharacteristicUuid,
100           .attribute_handle_ = handle};
101   GmapServer::GetCharacteristics()[handle] = invalidGmapCharacteristic;
102 
103   tGATTS_DATA gatts_data;
104   gatts_data.read_req.handle = handle;
105   tBTA_GATTS gatts_cb_data;
106   gatts_cb_data.req_data.p_data = &gatts_data;
107 
108   EXPECT_CALL(gatt_server_interface, SendRsp(_, _, GATT_ILLEGAL_PARAMETER, _)).Times(1);
109   GmapServer::GattsCallback(BTA_GATTS_READ_CHARACTERISTIC_EVT, &gatts_cb_data);
110 }
111 
TEST_F(GmapServerTest,test_read_valid_role_characteristic)112 TEST_F(GmapServerTest, test_read_valid_role_characteristic) {
113   uint16_t handle = 10;
114   GmapCharacteristic gmapCharacteristic{.uuid_ = bluetooth::le_audio::uuid::kRoleCharacteristicUuid,
115                                         .attribute_handle_ = handle};
116   GmapServer::GetCharacteristics()[handle] = gmapCharacteristic;
117 
118   tGATTS_DATA gatts_data;
119   gatts_data.read_req.handle = handle;
120   tBTA_GATTS gatts_cb_data;
121   gatts_cb_data.req_data.p_data = &gatts_data;
122 
123   EXPECT_CALL(gatt_server_interface, SendRsp(_, _, GATT_SUCCESS, _)).Times(1);
124   GmapServer::GattsCallback(BTA_GATTS_READ_CHARACTERISTIC_EVT, &gatts_cb_data);
125 }
126 
TEST_F(GmapServerTest,test_read_valid_ugg_feature_characteristic)127 TEST_F(GmapServerTest, test_read_valid_ugg_feature_characteristic) {
128   uint16_t handle = 10;
129   GmapCharacteristic gmapCharacteristic{
130           .uuid_ = bluetooth::le_audio::uuid::kUnicastGameGatewayCharacteristicUuid,
131           .attribute_handle_ = handle};
132   GmapServer::GetCharacteristics()[handle] = gmapCharacteristic;
133 
134   tGATTS_DATA gatts_data;
135   gatts_data.read_req.handle = handle;
136   tBTA_GATTS gatts_cb_data;
137   gatts_cb_data.req_data.p_data = &gatts_data;
138 
139   EXPECT_CALL(gatt_server_interface, SendRsp(_, _, GATT_SUCCESS, _)).Times(1);
140   GmapServer::GattsCallback(BTA_GATTS_READ_CHARACTERISTIC_EVT, &gatts_cb_data);
141 }
142 
TEST_F(GmapServerTest,test_get_UGG_feature_handle)143 TEST_F(GmapServerTest, test_get_UGG_feature_handle) {
144   uint16_t handle = 10;
145   GmapCharacteristic gmapCharacteristic{
146           .uuid_ = bluetooth::le_audio::uuid::kUnicastGameGatewayCharacteristicUuid,
147           .attribute_handle_ = handle};
148   GmapServer::GetCharacteristics()[handle] = gmapCharacteristic;
149 
150   ASSERT_EQ(GmapServer::GetUGGFeatureHandle(), handle);
151 }
152 
TEST_F(GmapServerTest,test_read_invalid_UGG_feature_handle)153 TEST_F(GmapServerTest, test_read_invalid_UGG_feature_handle) {
154   uint16_t handle = 10;
155   GmapServer::GetCharacteristics().clear();
156 
157   ASSERT_NE(GmapServer::GetUGGFeatureHandle(), handle);
158 }
159 
TEST_F(GmapServerTest,test_get_role_handle)160 TEST_F(GmapServerTest, test_get_role_handle) {
161   uint16_t handle = 10;
162   GmapCharacteristic gmapCharacteristic{.uuid_ = bluetooth::le_audio::uuid::kRoleCharacteristicUuid,
163                                         .attribute_handle_ = handle};
164   GmapServer::GetCharacteristics()[handle] = gmapCharacteristic;
165 
166   ASSERT_EQ(GmapServer::GetRoleHandle(), handle);
167 }
168