1/*
2 * Copyright (C) 2021 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
18#include "bluetooth_gatt_service.h"
19#include "uuid.h"
20
21using namespace testing::ext;
22
23namespace OHOS {
24namespace Bluetooth {
25class GattServiceTest : public testing::Test {
26public:
27    GattServiceTest()
28    {}
29    ~GattServiceTest()
30    {}
31
32    int tempData_ = 0;
33    static void SetUpTestCase(void);
34    static void TearDownTestCase(void);
35    void SetUp();
36    void TearDown();
37};
38
39void GattServiceTest::SetUpTestCase(void)
40{}
41void GattServiceTest::TearDownTestCase(void)
42{}
43void GattServiceTest::SetUp()
44{
45    tempData_ = 0;
46}
47
48void GattServiceTest::TearDown()
49{}
50
51/*
52 * @tc.number: GattService001
53 * @tc.name: IsPrimary
54*/
55HWTEST_F(GattServiceTest, GattService_UnitTest_IsPrimary, TestSize.Level1)
56{
57    UUID uuid_ = UUID::RandomUUID();
58    GattServiceType serviceType_ = GattServiceType::PRIMARY;
59    EXPECT_EQ(true, GattService(uuid_, serviceType_).IsPrimary());
60    GTEST_LOG_(INFO) << "GattService::GattService ends";
61    GTEST_LOG_(INFO) << "GattService::IsPrimary ends";
62}
63
64/*
65 * @tc.number: GattService001
66 * @tc.name: IsPrimary
67*/
68HWTEST_F(GattServiceTest, GattService_UnitTest_GetHandle_, TestSize.Level1)
69{
70    UUID uuid_;
71    uuid_ = UUID::FromString("00001820-0000-1000-8000-00805F9B34FB");
72    GattServiceType serviceType_ = GattServiceType::PRIMARY;
73    uint16_t handle_ = 0b0000000000010000;
74    uint16_t endHandle_ = 0b0000000000010001;
75    uint16_t result = GattService(uuid_, handle_, endHandle_, serviceType_).GetHandle();
76    EXPECT_EQ(16, result);
77    GTEST_LOG_(INFO) << "GattService::GattService ends";
78    GTEST_LOG_(INFO) << "GattService::GetHandle ends";
79}
80
81/*
82 * @tc.number: GattService001
83 * @tc.name: IsPrimary
84*/
85HWTEST_F(GattServiceTest, GattService_UnitTest_GetUuid, TestSize.Level1)
86{
87    UUID uuid_ = UUID::RandomUUID();
88    GattServiceType serviceType_ = GattServiceType::PRIMARY;
89    uint16_t handle_ = 0b0000000000010000;
90    uint16_t endHandle_ = 0b0000000000010001;
91    const UUID result = GattService(uuid_, handle_, endHandle_, serviceType_).GetUuid();
92    bool cmp;
93    if (uuid_.ToString() == result.ToString()) {
94        cmp = true;
95    } else {
96        cmp = false;
97    }
98    EXPECT_EQ(true, cmp);
99    GTEST_LOG_(INFO) << "GattService::GattService ends";
100    GTEST_LOG_(INFO) << "GattService::GetUuid ends";
101}
102
103/*
104 * @tc.number: GattService001
105 * @tc.name: IsPrimary
106*/
107HWTEST_F(GattServiceTest, GattService_UnitTest_AddService, TestSize.Level1)
108{
109    UUID uuid_ = UUID::RandomUUID();
110    GattServiceType serviceType_ = GattServiceType::PRIMARY;
111    uint16_t handle_ = 0b0000000000010000;
112    uint16_t endHandle_ = 0b0000000000010001;
113    std::vector<std::reference_wrapper<GattService>> includedservices;
114    UUID uuid_test;
115    uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
116    GattService addservice = GattService(uuid_test, serviceType_);
117    GattService service = GattService(uuid_, handle_, endHandle_, serviceType_);
118    service.AddService(addservice);
119    includedservices = service.GetIncludedServices();
120    bool result = false;
121    if (includedservices.size() == 0) {
122        result = true;
123    }
124    EXPECT_EQ(false, result);
125    GTEST_LOG_(INFO) << "GattService::GattService ends";
126    GTEST_LOG_(INFO) << "GattService::AddService ends";
127}
128
129/*
130 * @tc.number: GattService002
131 * @tc.name: GetIncludedServices
132*/
133HWTEST_F(GattServiceTest, GattService_UnitTest_GetIncludedServices, TestSize.Level1)
134{
135    UUID uuid_ = UUID::RandomUUID();
136    GattServiceType serviceType_ = GattServiceType::PRIMARY;
137    uint16_t handle_ = 0b0000000000010000;
138    uint16_t endHandle_ = 0b0000000000010001;
139    std::vector<std::reference_wrapper<GattService>> includedservices;
140    UUID uuid_test;
141    uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
142    GattService addservice = GattService(uuid_test, serviceType_);
143    GattService service = GattService(uuid_, handle_, endHandle_, serviceType_);
144    service.AddService(addservice);
145    includedservices = service.GetIncludedServices();
146    bool result = false;
147    if (includedservices.size() == 1) {
148        result = true;
149    }
150    EXPECT_EQ(true, result);
151    GTEST_LOG_(INFO) << "GattService::GattService ends";
152    GTEST_LOG_(INFO) << "GattService::GetIncludedServices ends";
153}
154
155/*
156 * @tc.number: GattService003
157 * @tc.name: AddCharacteristic
158*/
159HWTEST_F(GattServiceTest, GattService_UnitTest_AddCharacteristic, TestSize.Level1)
160{
161    UUID uuid_ = UUID::RandomUUID();
162    GattServiceType serviceType_ = GattServiceType::PRIMARY;
163    uint16_t handle_ = 0b0000000000010000;
164    uint16_t endHandle_ = 0b0000000000010001;
165    int permissions = 27;
166    int properties = 26;
167    UUID uuid_test;
168    uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
169    GattCharacteristic characteristic = GattCharacteristic(uuid_test, permissions, properties);
170
171    GattService addcharacter = GattService(uuid_, handle_, endHandle_, serviceType_);
172    addcharacter.AddCharacteristic(characteristic);
173    auto test = addcharacter.GetCharacteristic(uuid_test);
174    bool result = false;
175    if (test == nullptr) {
176        result = true;
177    }
178    EXPECT_EQ(false, result);
179    GTEST_LOG_(INFO) << "GattService::GattService ends";
180    GTEST_LOG_(INFO) << "GattService::AddCharacteristic ends";
181}
182
183/*
184 * @tc.number: GattService004
185 * @tc.name: GetCharacteristic
186*/
187HWTEST_F(GattServiceTest, GattService_UnitTest_GetCharacteristic, TestSize.Level1)
188{
189    UUID uuid_ = UUID::RandomUUID();
190    UUID uuid_test;
191    uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
192    int permissions = 27;
193    int properties = 26;
194    GattServiceType serviceType_ = GattServiceType::PRIMARY;
195    uint16_t handle_ = 0b0000000000010000;
196    uint16_t endHandle_ = 0b0000000000010001;
197    GattCharacteristic characteristic = GattCharacteristic(uuid_test, permissions, properties);
198    GattService getcharacter = GattService(uuid_, handle_, endHandle_, serviceType_);
199    getcharacter.AddCharacteristic(characteristic);
200    auto test = getcharacter.GetCharacteristic(uuid_test);
201    bool result = false;
202    if (test == nullptr) {
203        result = true;
204    }
205    EXPECT_EQ(false, result);
206
207    GTEST_LOG_(INFO) << "GattService::GattService ends";
208    GTEST_LOG_(INFO) << "GattService::GetCharacteristic ends";
209}
210
211/*
212 * @tc.number: GattService005
213 * @tc.name: GetCharacteristics
214*/
215HWTEST_F(GattServiceTest, GattService_UnitTest_GetCharacteristics, TestSize.Level1)
216{
217    UUID uuid_ = UUID::RandomUUID();
218    GattServiceType serviceType_ = GattServiceType::PRIMARY;
219    uint16_t handle_ = 0b0000000000010000;
220    uint16_t endHandle_ = 0b0000000000010001;
221    UUID uuid_test;
222    uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
223    int permissions = 27;
224    int properties = 26;
225    GattCharacteristic characteristic = GattCharacteristic(uuid_test, permissions, properties);
226
227    GattService getcharacters = GattService(uuid_, handle_, endHandle_, serviceType_);
228    getcharacters.AddCharacteristic(characteristic);
229
230    std::vector<GattCharacteristic> res;
231    res = getcharacters.GetCharacteristics();
232    bool result = false;
233    if (res.size() == 0) {
234        result = true;
235    }
236    EXPECT_EQ(false, result);
237    GTEST_LOG_(INFO) << "GattService::GattService ends";
238    GTEST_LOG_(INFO) << "GattService::GetCharacteristics ends";
239}
240
241}  // namespace Bluetooth
242}  // namespace OHOS
243