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_descriptor.h"
19 #include "uuid.h"
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 class GattDescriptorTest : public testing::Test {
25 public:
GattDescriptorTest()26     GattDescriptorTest()
27     {}
~GattDescriptorTest()28     ~GattDescriptorTest()
29     {}
30 
31     int tempData_ = 0;
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36 };
SetUpTestCase(void)37 void GattDescriptorTest::SetUpTestCase(void)
38 {}
TearDownTestCase(void)39 void GattDescriptorTest::TearDownTestCase(void)
40 {}
SetUp()41 void GattDescriptorTest::SetUp()
42 {
43     tempData_ = 0;
44 }
TearDown()45 void GattDescriptorTest::TearDown()
46 {}
47 
48 /*
49  * @tc.number: GattDescriptor001
50  * @tc.name: GetPermissions
51 */
52 HWTEST_F(GattDescriptorTest, GattDescriptor_UnitTest_GetPermissions, TestSize.Level1)
53 {
54     UUID uuid_ = UUID::RandomUUID();
55     int permissions = 27;
56     EXPECT_EQ(permissions, GattDescriptor(uuid_, permissions).GetPermissions());
57     GTEST_LOG_(INFO) << "GattDescriptor::GattDescriptor ends";
58     GTEST_LOG_(INFO) << "GattDescriptor::GetPermissions ends";
59 }
60 
61 /*
62  * @tc.number: GattDescriptor002
63  * @tc.name: GetHandle
64 */
65 HWTEST_F(GattDescriptorTest, GattDescriptor_UnitTest_GetHandle, TestSize.Level1)
66 {
67     UUID uuid_ = UUID::RandomUUID();
68     int permissions = 27;
69     uint16_t handle = GattDescriptor(uuid_, permissions).GetHandle();
70     EXPECT_EQ(handle, 0);
71     GTEST_LOG_(INFO) << "GattDescriptor::GattDescriptor ends";
72     GTEST_LOG_(INFO) << "GattDescriptor::GetHandle ends";
73 }
74 
75 /*
76  * @tc.number: GattDescriptor003
77  * @tc.name: GetUuid
78 */
79 HWTEST_F(GattDescriptorTest, GattDescriptor_UnitTest_GetUuid, TestSize.Level1)
80 {
81     int permissions = 27;
82     uint16_t handle_ = 0b0000000000010000;
83     UUID uuid_;
84     uuid_ = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
85     UUID uuid_test;
86     uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
87     const UUID result = GattDescriptor(uuid_, handle_, permissions).GetUuid();
88     bool cmp;
89     if (uuid_test.ToString() == result.ToString()) {
90         cmp = true;
91     } else {
92         cmp = false;
93     }
94     EXPECT_EQ(true, cmp);
95     GTEST_LOG_(INFO) << "GattDescriptor::GattDescriptor ends";
96     GTEST_LOG_(INFO) << "GattDescriptor::GetUuid ends";
97 }
98 
99 /*
100  * @tc.number: GattDescriptor004
101  * @tc.name: GetCharacteristic
102 */
103 HWTEST_F(GattDescriptorTest, GattDescriptor_UnitTest_GetCharacteristic, TestSize.Level1)
104 {
105     UUID uuid_ = UUID::RandomUUID();
106     int permissions = 27;
107     uint16_t handle_ = 0b0000000000010000;
108     auto res = GattDescriptor(uuid_, handle_, permissions).GetCharacteristic();
109     bool result = false;
110     if (res) {
111         result = true;
112     }
113     EXPECT_EQ(false, result);
114     GTEST_LOG_(INFO) << "GattDescriptor::GattDescriptor ends";
115     GTEST_LOG_(INFO) << "GattDescriptor::GetCharacteristic ends";
116 }
117 
118 /*
119  * @tc.number: GattDescriptor005
120  * @tc.name: GetValue
121 */
122 HWTEST_F(GattDescriptorTest, GattDescriptor_UnitTest_GetValue, TestSize.Level1)
123 {
124     UUID uuid_ = UUID::RandomUUID();
125     int permissions = 27;
126     uint16_t handle_ = 0b0000000000010000;
127     size_t size = 1256;
128     uint8_t *test;
129     uint16_t handle_test = 0b0000000000010001;
130     UUID uuid_test;
131     uuid_test = UUID::FromString("00001810-0000-1000-8000-00805F9B34FB");
132     size_t size_test = 1212;
133     GattDescriptor getvalue_test = GattDescriptor(uuid_test, handle_test, permissions);
134     test = getvalue_test.GetValue(&size_test).get();
135     uint8_t *res;
136     GattDescriptor getvalue = GattDescriptor(uuid_, handle_, permissions);
137     res = getvalue.GetValue(&size).get();
138     bool result = false;
139     if (res == test) {
140         result = true;
141     }
142     EXPECT_EQ(true, result);
143     GTEST_LOG_(INFO) << "GattDescriptor::GattDescriptor ends";
144     GTEST_LOG_(INFO) << "GattDescriptor::GetValue ends";
145 }
146 
147 /*
148  * @tc.number: GattDescriptor006
149  * @tc.name: SetValue
150 */
151 HWTEST_F(GattDescriptorTest, GattDescriptor_UnitTest_SetValue, TestSize.Level1)
152 {
153     UUID uuid_ = UUID::RandomUUID();
154     int permissions = 27;
155     uint16_t handle_ = 0b0000000000010000;
156     const uint8_t values = 17;
157     size_t length = 16;
158     GattDescriptor SetValue = GattDescriptor(uuid_, handle_, permissions);
159     SetValue.SetValue(&values, length);
160     uint8_t *res = SetValue.GetValue(&length).get();
161     bool result = false;
162     if (res != nullptr) {
163         result = true;
164     }
165     EXPECT_EQ(true, result);
166     GTEST_LOG_(INFO) << "GattDescriptor::GattDescriptor ends";
167     GTEST_LOG_(INFO) << "GattDescriptor::SetValue ends";
168 }
169 
170 }  // namespace Bluetooth
171 }  // namespace OHOS
172