119e95205Sopenharmony_ci/*
219e95205Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
319e95205Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
419e95205Sopenharmony_ci * you may not use this file except in compliance with the License.
519e95205Sopenharmony_ci * You may obtain a copy of the License at
619e95205Sopenharmony_ci *
719e95205Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
819e95205Sopenharmony_ci *
919e95205Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1019e95205Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1119e95205Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1219e95205Sopenharmony_ci * See the License for the specific language governing permissions and
1319e95205Sopenharmony_ci * limitations under the License.
1419e95205Sopenharmony_ci */
1519e95205Sopenharmony_ci
1619e95205Sopenharmony_ci#include <gmock/gmock.h>
1719e95205Sopenharmony_ci#include <gtest/gtest.h>
1819e95205Sopenharmony_ci#include "securec.h"
1919e95205Sopenharmony_ci#include "bluetooth_gatt_characteristic.h"
2019e95205Sopenharmony_ci#include "bluetooth_gatt_descriptor.h"
2119e95205Sopenharmony_ci#include "uuid.h"
2219e95205Sopenharmony_ci#include "bluetooth_def.h"
2319e95205Sopenharmony_ci#include <memory>
2419e95205Sopenharmony_ci
2519e95205Sopenharmony_ciusing namespace testing;
2619e95205Sopenharmony_ciusing namespace testing::ext;
2719e95205Sopenharmony_ci
2819e95205Sopenharmony_cinamespace OHOS {
2919e95205Sopenharmony_cinamespace Bluetooth {
3019e95205Sopenharmony_ciclass GattCharacteristicTest : public testing::Test {
3119e95205Sopenharmony_cipublic:
3219e95205Sopenharmony_ci    GattCharacteristicTest()
3319e95205Sopenharmony_ci    {}
3419e95205Sopenharmony_ci    ~GattCharacteristicTest()
3519e95205Sopenharmony_ci    {}
3619e95205Sopenharmony_ci
3719e95205Sopenharmony_ci    GattCharacteristic *pGattCharacteristic = nullptr;
3819e95205Sopenharmony_ci    static void SetUpTestCase(void);
3919e95205Sopenharmony_ci    static void TearDownTestCase(void);
4019e95205Sopenharmony_ci    void SetUp();
4119e95205Sopenharmony_ci    void TearDown();
4219e95205Sopenharmony_cipublic:
4319e95205Sopenharmony_ci    UUID uuid_;
4419e95205Sopenharmony_ci    uint16_t handle_ = 1;
4519e95205Sopenharmony_ci    const int permissions_ = 100;
4619e95205Sopenharmony_ci    const int properties_ = 200;
4719e95205Sopenharmony_ci};
4819e95205Sopenharmony_ci
4919e95205Sopenharmony_civoid GattCharacteristicTest::SetUpTestCase(void)
5019e95205Sopenharmony_ci{}
5119e95205Sopenharmony_civoid GattCharacteristicTest::TearDownTestCase(void)
5219e95205Sopenharmony_ci{}
5319e95205Sopenharmony_civoid GattCharacteristicTest::SetUp()
5419e95205Sopenharmony_ci{
5519e95205Sopenharmony_ci    uuid_ = UUID::RandomUUID();
5619e95205Sopenharmony_ci}
5719e95205Sopenharmony_ci
5819e95205Sopenharmony_civoid GattCharacteristicTest::TearDown()
5919e95205Sopenharmony_ci{
6019e95205Sopenharmony_ci
6119e95205Sopenharmony_ci}
6219e95205Sopenharmony_ci
6319e95205Sopenharmony_ci/*
6419e95205Sopenharmony_ci * @tc.number: GattClient001
6519e95205Sopenharmony_ci * @tc.name: GattCharacteristic4
6619e95205Sopenharmony_ci*/
6719e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GattCharacteristic4, TestSize.Level1)
6819e95205Sopenharmony_ci{
6919e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GattCharacteristic(4 parameters) start";
7019e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
7119e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GattCharacteristic(4 parameters) end";
7219e95205Sopenharmony_ci}
7319e95205Sopenharmony_ci
7419e95205Sopenharmony_ci/*
7519e95205Sopenharmony_ci * @tc.number: GattClient002
7619e95205Sopenharmony_ci * @tc.name: GattCharacteristic3
7719e95205Sopenharmony_ci*/
7819e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GattCharacteristic3, TestSize.Level1)
7919e95205Sopenharmony_ci{
8019e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GattCharacteristic(3 parameters) start";
8119e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GattCharacteristic(3 parameters) end";
8219e95205Sopenharmony_ci}
8319e95205Sopenharmony_ci
8419e95205Sopenharmony_ci/*
8519e95205Sopenharmony_ci * @tc.number: GattClient003
8619e95205Sopenharmony_ci * @tc.name: AddDescriptor
8719e95205Sopenharmony_ci*/
8819e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_AddDescriptor, TestSize.Level1)
8919e95205Sopenharmony_ci{
9019e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_AddDescriptor start";
9119e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
9219e95205Sopenharmony_ci    GattDescriptor *pdescriptor = new GattDescriptor(uuid_, permissions_);
9319e95205Sopenharmony_ci    pGattCharacteristic->AddDescriptor(*pdescriptor);
9419e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_AddDescriptor end";
9519e95205Sopenharmony_ci}
9619e95205Sopenharmony_ci
9719e95205Sopenharmony_ci/*
9819e95205Sopenharmony_ci * @tc.number: GattClient004
9919e95205Sopenharmony_ci * @tc.name: GetDescriptor
10019e95205Sopenharmony_ci*/
10119e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetDescriptor, TestSize.Level1)
10219e95205Sopenharmony_ci{
10319e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetDescriptor start";
10419e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
10519e95205Sopenharmony_ci    GattDescriptor *pdescriptor = new GattDescriptor(uuid_, permissions_);
10619e95205Sopenharmony_ci    pGattCharacteristic->AddDescriptor(*pdescriptor);
10719e95205Sopenharmony_ci
10819e95205Sopenharmony_ci    GattDescriptor *pdescriptorTmp = pGattCharacteristic->GetDescriptor(uuid_);
10919e95205Sopenharmony_ci    if (pdescriptorTmp->GetUuid().Equals(uuid_)) {
11019e95205Sopenharmony_ci        EXPECT_TRUE(true);
11119e95205Sopenharmony_ci        GTEST_LOG_(INFO) << "GattCharacteristic_GetDescriptor returns correctly";
11219e95205Sopenharmony_ci    }
11319e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetDescriptor end";
11419e95205Sopenharmony_ci}
11519e95205Sopenharmony_ci
11619e95205Sopenharmony_ci/*
11719e95205Sopenharmony_ci * @tc.number: GattClient005
11819e95205Sopenharmony_ci * @tc.name: GetHandle
11919e95205Sopenharmony_ci*/
12019e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetHandle, TestSize.Level1)
12119e95205Sopenharmony_ci{
12219e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetHandle start";
12319e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
12419e95205Sopenharmony_ci    uint16_t tempHandle = pGattCharacteristic->GetHandle();
12519e95205Sopenharmony_ci    EXPECT_EQ(handle_, tempHandle);
12619e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetHandle end";
12719e95205Sopenharmony_ci}
12819e95205Sopenharmony_ci
12919e95205Sopenharmony_ci/*
13019e95205Sopenharmony_ci * @tc.number: GattClient006
13119e95205Sopenharmony_ci * @tc.name: GetPermissions
13219e95205Sopenharmony_ci*/
13319e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetPermissions, TestSize.Level1)
13419e95205Sopenharmony_ci{
13519e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetPermissions start";
13619e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
13719e95205Sopenharmony_ci    int tempPer = pGattCharacteristic->GetPermissions();
13819e95205Sopenharmony_ci    EXPECT_EQ(permissions_, tempPer);
13919e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetPermissions end";
14019e95205Sopenharmony_ci}
14119e95205Sopenharmony_ci
14219e95205Sopenharmony_ci/*
14319e95205Sopenharmony_ci * @tc.number: GattClient007
14419e95205Sopenharmony_ci * @tc.name: GetProperties
14519e95205Sopenharmony_ci*/
14619e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetProperties, TestSize.Level1)
14719e95205Sopenharmony_ci{
14819e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetProperties start";
14919e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
15019e95205Sopenharmony_ci    int tempPro = pGattCharacteristic->GetProperties();
15119e95205Sopenharmony_ci    EXPECT_EQ(properties_, tempPro);
15219e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetProperties end";
15319e95205Sopenharmony_ci}
15419e95205Sopenharmony_ci
15519e95205Sopenharmony_ci/*
15619e95205Sopenharmony_ci * @tc.number: GattClient008
15719e95205Sopenharmony_ci * @tc.name: GetService
15819e95205Sopenharmony_ci*/
15919e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetService, TestSize.Level1)
16019e95205Sopenharmony_ci{
16119e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetService start";
16219e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
16319e95205Sopenharmony_ci    GattService *tempGattService = pGattCharacteristic->GetService();
16419e95205Sopenharmony_ci    if (nullptr == tempGattService) {
16519e95205Sopenharmony_ci        EXPECT_TRUE(true);
16619e95205Sopenharmony_ci        GTEST_LOG_(INFO) << "GattCharacteristic_GetService returns correctly";
16719e95205Sopenharmony_ci    }
16819e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetService end";
16919e95205Sopenharmony_ci}
17019e95205Sopenharmony_ci
17119e95205Sopenharmony_ci/*
17219e95205Sopenharmony_ci * @tc.number: GattClient009
17319e95205Sopenharmony_ci * @tc.name: GetUuid
17419e95205Sopenharmony_ci*/
17519e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetUuid, TestSize.Level1)
17619e95205Sopenharmony_ci{
17719e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetUuid start";
17819e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
17919e95205Sopenharmony_ci    UUID tempUuid = pGattCharacteristic->GetUuid();
18019e95205Sopenharmony_ci    if (tempUuid.Equals(uuid_)) {
18119e95205Sopenharmony_ci        EXPECT_TRUE(true);
18219e95205Sopenharmony_ci        GTEST_LOG_(INFO) << "GattCharacteristic_GetUuid returns correctly";
18319e95205Sopenharmony_ci    }
18419e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetUuid end";
18519e95205Sopenharmony_ci}
18619e95205Sopenharmony_ci
18719e95205Sopenharmony_ci/*
18819e95205Sopenharmony_ci * @tc.number: GattClient010
18919e95205Sopenharmony_ci * @tc.name: SetValue
19019e95205Sopenharmony_ci*/
19119e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_SetValue, TestSize.Level1)
19219e95205Sopenharmony_ci{
19319e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_SetValue start";
19419e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
19519e95205Sopenharmony_ci    size_t length_Set = 1;
19619e95205Sopenharmony_ci    uint8_t values_Set[1] = {'a'};
19719e95205Sopenharmony_ci    pGattCharacteristic->SetValue(values_Set, length_Set);
19819e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_SetValue end";
19919e95205Sopenharmony_ci}
20019e95205Sopenharmony_ci
20119e95205Sopenharmony_ci/*
20219e95205Sopenharmony_ci * @tc.number: GattClient011
20319e95205Sopenharmony_ci * @tc.name: GetValue
20419e95205Sopenharmony_ci*/
20519e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetValue, TestSize.Level1)
20619e95205Sopenharmony_ci{
20719e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetValue start";
20819e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
20919e95205Sopenharmony_ci    size_t length_Set = 1;
21019e95205Sopenharmony_ci    size_t length_Get = 1;
21119e95205Sopenharmony_ci    size_t *plength_Get = &length_Get;
21219e95205Sopenharmony_ci    uint8_t values_Set[1] = {'a'};
21319e95205Sopenharmony_ci    pGattCharacteristic->SetValue(values_Set, length_Set);
21419e95205Sopenharmony_ci
21519e95205Sopenharmony_ci    std::unique_ptr<uint8_t[]> values_Get = std::make_unique<uint8_t[]>(length_Get);
21619e95205Sopenharmony_ci    (void)memcpy_s(values_Get.get(), length_Get, pGattCharacteristic->GetValue(plength_Get).get(), length_Get);
21719e95205Sopenharmony_ci    const uint8_t *values = static_cast<uint8_t *>(values_Get.get());
21819e95205Sopenharmony_ci    uint8_t resultValuesArray = values[0];
21919e95205Sopenharmony_ci    uint8_t resultValuesPointer = *values;
22019e95205Sopenharmony_ci
22119e95205Sopenharmony_ci    if ('a' == resultValuesArray) {
22219e95205Sopenharmony_ci        EXPECT_TRUE(true);
22319e95205Sopenharmony_ci        GTEST_LOG_(INFO) << "GattCharacteristic_GetValue returns correctly";
22419e95205Sopenharmony_ci    }
22519e95205Sopenharmony_ci
22619e95205Sopenharmony_ci    if ('a' == resultValuesPointer) {
22719e95205Sopenharmony_ci        EXPECT_TRUE(true);
22819e95205Sopenharmony_ci        GTEST_LOG_(INFO) << "GattCharacteristic_GetValue returns correctly";
22919e95205Sopenharmony_ci    }
23019e95205Sopenharmony_ci
23119e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetValue end";
23219e95205Sopenharmony_ci}
23319e95205Sopenharmony_ci
23419e95205Sopenharmony_ci/*
23519e95205Sopenharmony_ci * @tc.number: GattClient012
23619e95205Sopenharmony_ci * @tc.name: SetWriteType
23719e95205Sopenharmony_ci*/
23819e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_SetWriteType, TestSize.Level1)
23919e95205Sopenharmony_ci{
24019e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_SetWriteType start";
24119e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
24219e95205Sopenharmony_ci    GattCharacteristic::WriteType typeTemp = GattCharacteristic::WriteType::DEFAULT;
24319e95205Sopenharmony_ci    int returnCode = 0;
24419e95205Sopenharmony_ci    returnCode = pGattCharacteristic->SetWriteType(typeTemp);
24519e95205Sopenharmony_ci    EXPECT_EQ(returnCode, 0);
24619e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_SetWriteType end";
24719e95205Sopenharmony_ci}
24819e95205Sopenharmony_ci
24919e95205Sopenharmony_ci/*
25019e95205Sopenharmony_ci * @tc.number: GattClient013
25119e95205Sopenharmony_ci * @tc.name: GetWriteType
25219e95205Sopenharmony_ci*/
25319e95205Sopenharmony_ciHWTEST_F(GattCharacteristicTest, GattCharacteristic_UnitTest_GetWriteType, TestSize.Level1)
25419e95205Sopenharmony_ci{
25519e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetWriteType start";
25619e95205Sopenharmony_ci    pGattCharacteristic = new GattCharacteristic(uuid_, handle_, permissions_, properties_);
25719e95205Sopenharmony_ci    GattCharacteristic::WriteType typeTemp = GattCharacteristic::WriteType::DEFAULT;
25819e95205Sopenharmony_ci    int returnCode = 0;
25919e95205Sopenharmony_ci    returnCode = pGattCharacteristic->SetWriteType(typeTemp);
26019e95205Sopenharmony_ci    EXPECT_EQ(returnCode, 0);
26119e95205Sopenharmony_ci
26219e95205Sopenharmony_ci    int resultType = pGattCharacteristic->GetWriteType();
26319e95205Sopenharmony_ci    if (typeTemp == resultType) {
26419e95205Sopenharmony_ci        EXPECT_TRUE(true);
26519e95205Sopenharmony_ci        GTEST_LOG_(INFO) << "GattCharacteristic_GetWriteType returns correctly";
26619e95205Sopenharmony_ci    }
26719e95205Sopenharmony_ci    GTEST_LOG_(INFO) << "GattCharacteristic_GetWriteType end";
26819e95205Sopenharmony_ci}
26919e95205Sopenharmony_ci
27019e95205Sopenharmony_ci}  // namespace Bluetooth
27119e95205Sopenharmony_ci}  // namespace OHOS
272