1/*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <gtest/gtest.h>
17#include "bluetooth_errorcode.h"
18#include "bluetooth_gatt_server.h"
19#include "bluetooth_host.h"
20#include "bluetooth_remote_device.h"
21
22using namespace testing::ext;
23
24namespace OHOS {
25namespace Bluetooth {
26const int TIME = 2;
27class BluetoothGattServerCallbackCommon : public GattServerCallback {
28public:
29    BluetoothGattServerCallbackCommon() = default;
30    virtual ~BluetoothGattServerCallbackCommon() = default;
31    void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) override
32    {}
33    void OnServiceAdded(GattService *Service, int ret) override
34    {}
35    void OnCharacteristicReadRequest(
36        const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId) override
37    {}
38    void OnCharacteristicWriteRequest(
39        const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId) override
40    {}
41    void OnDescriptorReadRequest(
42        const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId) override
43    {}
44    void OnDescriptorWriteRequest(
45        const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId) override
46    {}
47    void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) override
48    {}
49    void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int result) override
50    {}
51    void OnConnectionParameterChanged(
52        const BluetoothRemoteDevice &device, int interval, int latency, int timeout, int status) override
53    {}
54
55private:
56};
57static BluetoothGattServerCallbackCommon callback_;
58
59class GattServerTest : public testing::Test {
60public:
61    GattServerTest()
62    {}
63    ~GattServerTest()
64    {}
65
66    int tempData_ = 0;
67    static void SetUpTestCase(void);
68    static void TearDownTestCase(void);
69    void SetUp();
70    void TearDown();
71    BluetoothHost *host_;
72};
73
74void GattServerTest::SetUpTestCase(void)
75{}
76void GattServerTest::TearDownTestCase(void)
77{}
78void GattServerTest::SetUp()
79{
80    tempData_ = 0;
81    host_ = &BluetoothHost::GetDefaultHost();
82    host_->EnableBt();
83    host_->EnableBle();
84    sleep(TIME);
85}
86
87void GattServerTest::TearDown()
88{
89    host_->DisableBt();
90    host_->DisableBle();
91    host_ = nullptr;
92    sleep(TIME);
93}
94
95/*
96 * @tc.number: GattServer001
97 * @tc.name: AddService
98 * @tc.desc: Check the addservice interface.
99*/
100HWTEST_F(GattServerTest, GattServer_ModuleTest_AddService, TestSize.Level1)
101{
102    GTEST_LOG_(INFO) << "GattServer_ModuleTest_AddService start";
103    GattServer server(callback_);
104    UUID uuidSerPer;
105    uuidSerPer = UUID::FromString("00001810-0000-1000-8000-00805F9B34F7");
106    GattService serviceOne(uuidSerPer, GattServiceType::PRIMARY);
107    int ret = server.AddService(serviceOne);
108
109    EXPECT_EQ(ret, 0);
110    GTEST_LOG_(INFO) << "GattServer_ModuleTest_AddService end";
111}
112
113/*
114 * @tc.number: GattServer002
115 * @tc.name: CancelConnection
116 * @tc.desc: Check the CancelConnection interface.
117*/
118HWTEST_F(GattServerTest, GattServer_ModuleTest_CancelConnection, TestSize.Level1)
119{
120    GTEST_LOG_(INFO) << "GattServer_ModuleTest_CancelConnection start";
121    GattServer server(callback_);
122
123    BluetoothRemoteDevice deviceBle_;
124
125    server.CancelConnection(deviceBle_);
126    GTEST_LOG_(INFO) << "GattServer_ModuleTest_CancelConnection end";
127}
128
129/*
130 * @tc.number: GattServer003
131 * @tc.name: RemoveService
132 * @tc.desc: Check the RemoveService interface.
133*/
134HWTEST_F(GattServerTest, GattServer_ModuleTest_RemoveService, TestSize.Level1)
135{
136    GTEST_LOG_(INFO) << "GattServer_ModuleTest_RemoveService start";
137    GattServer server(callback_);
138
139    UUID uuidSerPer;
140    uuidSerPer = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
141    GattService serviceOne(uuidSerPer, 1, 1, GattServiceType::PRIMARY);
142    int result = server.AddService(serviceOne);
143    EXPECT_EQ(result, 0);
144    sleep(5);
145    std::list<GattService> &list = server.GetServices();
146    GTEST_LOG_(INFO) << list.size();
147    int ret = server.RemoveGattService(list.back());
148    EXPECT_EQ(ret, 0);
149    GTEST_LOG_(INFO) << "GattServer_ModuleTest_RemoveService end";
150}
151
152/*
153 * @tc.number: GattServer005
154 * @tc.name: ClearServices
155 * @tc.desc: Check the ClearServices interface.
156*/
157HWTEST_F(GattServerTest, GattServer_ModuleTest_ClearServices, TestSize.Level1)
158{
159    GTEST_LOG_(INFO) << "GattServer_ModuleTest_ClearServices start";
160    GattServer server(callback_);
161
162    std::list<GattService>& list = server.GetServices();
163    GTEST_LOG_(INFO) << (int)list.size();
164    server.ClearServices();
165    list = server.GetServices();
166    EXPECT_EQ((int)list.size(), 0);
167    GTEST_LOG_(INFO) << "GattServer_ModuleTest_ClearServices end";
168}
169
170/*
171 * @tc.number: GattServer006
172 * @tc.name: NotifyCharacteristicChanged
173 * @tc.desc: Check the NotifyCharacteristicChanged interface.
174*/
175HWTEST_F(GattServerTest, GattServer_ModuleTest_NotifyCharacteristicChanged, TestSize.Level1)
176{
177    GTEST_LOG_(INFO) << "GattServer_ModuleTest_Notify start";
178    GattServer server(callback_);
179
180    BluetoothRemoteDevice deviceBle_;
181    UUID uuidSerPer;
182    uuidSerPer = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
183    GattCharacteristic* aa = new GattCharacteristic(uuidSerPer, 1, 1);
184    int res = server.NotifyCharacteristicChanged(deviceBle_, *aa, false);
185    EXPECT_NE(res, NO_ERROR);
186
187    GTEST_LOG_(INFO) << "GattServer_ModuleTest_Notify end";
188}
189
190/*
191 * @tc.number: GattServer007
192 * @tc.name: SendResponse
193 * @tc.desc: Check the SendResponse interface.
194*/
195HWTEST_F(GattServerTest, GattServer_ModuleTest_SendResponse, TestSize.Level1)
196{
197    GTEST_LOG_(INFO) << "GattServer_ModuleTest_SendResponse start";
198    GattServer server(callback_);
199
200    BluetoothRemoteDevice deviceBle_;
201    string valueChrTwo = "2";
202    int ret = server.SendResponse(deviceBle_,
203        0,
204        (int)GattStatus::GATT_SUCCESS,
205        1,
206        (uint8_t *)valueChrTwo.c_str(),
207        valueChrTwo.size());
208    EXPECT_NE(ret, NO_ERROR);
209    GTEST_LOG_(INFO) << "GattServer_ModuleTest_SendResponse end";
210}
211
212/*
213 * @tc.number: GattServer008
214 * @tc.name: GetServices
215 * @tc.desc: Check the GetServices interface.
216*/
217HWTEST_F(GattServerTest, GattServer_ModuleTest_GetServices, TestSize.Level1)
218{
219    GTEST_LOG_(INFO) << "GattServer_ModuleTest_GetServices start";
220    GattServer server(callback_);
221
222    UUID uuidSerPer;
223    uuidSerPer = UUID::FromString("00001810-0000-1000-8000-00805F9B34F5");
224
225    GattService serviceOne(uuidSerPer, GattServiceType::PRIMARY);
226    int ret = server.AddService(serviceOne);
227
228    EXPECT_EQ(ret, 0);
229    sleep(TIME);
230    std::list<GattService> list = server.GetServices();
231    EXPECT_EQ((int)list.size(), 1);
232
233    GTEST_LOG_(INFO) << "GattServer_ModuleTest_GetServices end";
234}
235
236/*
237 * @tc.number: GattServer009
238 * @tc.name: GetService
239 * @tc.desc: Check the GetService interface.
240*/
241
242HWTEST_F(GattServerTest, GattServer_ModuleTest_GetService, TestSize.Level1)
243{
244    GTEST_LOG_(INFO) << "GattServer_ModuleTest_GetService start";
245    GattServer server(callback_);
246
247    UUID uuidSerPer;
248    uuidSerPer = UUID::FromString("00001810-0000-1000-8000-00805F9B34F9");
249
250    GattService serviceOne(uuidSerPer, GattServiceType::SECONDARY);
251    int ret = server.AddService(serviceOne);
252
253    EXPECT_EQ(ret, 0);
254    sleep(5);
255
256    std::optional<GattService> listSecondary = server.GetService(uuidSerPer, false);
257    EXPECT_FALSE(listSecondary->IsPrimary());
258    GTEST_LOG_(INFO) << "GattServer_ModuleTest_GetService end";
259}
260}  // namespace Bluetooth
261}  // namespace OHOS
262