11401458bSopenharmony_ci/*
21401458bSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
31401458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41401458bSopenharmony_ci * you may not use this file except in compliance with the License.
51401458bSopenharmony_ci * You may obtain a copy of the License at
61401458bSopenharmony_ci *
71401458bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81401458bSopenharmony_ci *
91401458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101401458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111401458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121401458bSopenharmony_ci * See the License for the specific language governing permissions and
131401458bSopenharmony_ci * limitations under the License.
141401458bSopenharmony_ci */
151401458bSopenharmony_ci
161401458bSopenharmony_ci#include <gtest/gtest.h>
171401458bSopenharmony_ci
181401458bSopenharmony_ci#include <limits>
191401458bSopenharmony_ci#include <memory>
201401458bSopenharmony_ci
211401458bSopenharmony_ci#include "gtest/gtest-message.h"
221401458bSopenharmony_ci#include "gtest/gtest-test-part.h"
231401458bSopenharmony_ci#include "gtest/hwext/gtest-ext.h"
241401458bSopenharmony_ci#include "gtest/hwext/gtest-tag.h"
251401458bSopenharmony_ci
261401458bSopenharmony_ci#include "encoded_param.h"
271401458bSopenharmony_ci#include "hisysevent.h"
281401458bSopenharmony_ci#include "raw_data_base_def.h"
291401458bSopenharmony_ci#include "raw_data_encoder.h"
301401458bSopenharmony_ci#include "raw_data.h"
311401458bSopenharmony_ci#include "transport.h"
321401458bSopenharmony_ci
331401458bSopenharmony_ciusing namespace testing::ext;
341401458bSopenharmony_ciusing namespace OHOS::HiviewDFX;
351401458bSopenharmony_ciusing namespace OHOS::HiviewDFX::Encoded;
361401458bSopenharmony_ci
371401458bSopenharmony_ciclass HiSysEventEncodedTest : public testing::Test {
381401458bSopenharmony_cipublic:
391401458bSopenharmony_ci    static void SetUpTestCase(void);
401401458bSopenharmony_ci    static void TearDownTestCase(void);
411401458bSopenharmony_ci    void SetUp();
421401458bSopenharmony_ci    void TearDown();
431401458bSopenharmony_ci};
441401458bSopenharmony_ci
451401458bSopenharmony_civoid HiSysEventEncodedTest::SetUpTestCase(void)
461401458bSopenharmony_ci{
471401458bSopenharmony_ci}
481401458bSopenharmony_ci
491401458bSopenharmony_civoid HiSysEventEncodedTest::TearDownTestCase(void)
501401458bSopenharmony_ci{
511401458bSopenharmony_ci}
521401458bSopenharmony_ci
531401458bSopenharmony_civoid HiSysEventEncodedTest::SetUp(void)
541401458bSopenharmony_ci{
551401458bSopenharmony_ci}
561401458bSopenharmony_ci
571401458bSopenharmony_civoid HiSysEventEncodedTest::TearDown(void)
581401458bSopenharmony_ci{
591401458bSopenharmony_ci}
601401458bSopenharmony_ci
611401458bSopenharmony_ci/**
621401458bSopenharmony_ci * @tc.name: EncodeParamTest001
631401458bSopenharmony_ci * @tc.desc: EncodeParam api interfaces test
641401458bSopenharmony_ci * @tc.type: FUNC
651401458bSopenharmony_ci * @tc.require: issueI7E737
661401458bSopenharmony_ci */
671401458bSopenharmony_ciHWTEST_F(HiSysEventEncodedTest, EncodeParamTest001, TestSize.Level1)
681401458bSopenharmony_ci{
691401458bSopenharmony_ci    uint64_t val = 39912344; // a random numeber
701401458bSopenharmony_ci    std::shared_ptr<EncodedParam> param = std::make_shared<UnsignedVarintEncodedParam<uint64_t>>("KEY", val);
711401458bSopenharmony_ci    auto data = param->GetRawData();
721401458bSopenharmony_ci    ASSERT_EQ(data, nullptr);
731401458bSopenharmony_ci    ASSERT_TRUE(!param->Encode());
741401458bSopenharmony_ci    auto rawData = std::make_shared<Encoded::RawData>();
751401458bSopenharmony_ci    param->SetRawData(rawData);
761401458bSopenharmony_ci    ASSERT_TRUE(param->Encode());
771401458bSopenharmony_ci    data = param->GetRawData();
781401458bSopenharmony_ci    ASSERT_NE(data, nullptr);
791401458bSopenharmony_ci    ASSERT_NE(data->GetData(), nullptr);
801401458bSopenharmony_ci    ASSERT_GT(data->GetDataLength(), 0);
811401458bSopenharmony_ci}
821401458bSopenharmony_ci
831401458bSopenharmony_ci/**
841401458bSopenharmony_ci * @tc.name: RawDatabaseDefTest001
851401458bSopenharmony_ci * @tc.desc: Some api interfaces of raw data base definition test
861401458bSopenharmony_ci * @tc.type: FUNC
871401458bSopenharmony_ci * @tc.require: issueI7E737
881401458bSopenharmony_ci */
891401458bSopenharmony_ciHWTEST_F(HiSysEventEncodedTest, RawDatabaseDefTest001, TestSize.Level1)
901401458bSopenharmony_ci{
911401458bSopenharmony_ci    auto tzIndex = ParseTimeZone(3600); // 3600 is a valid timezone value
921401458bSopenharmony_ci    ASSERT_EQ(tzIndex, 0); // reference to ALL_TIME_ZONES defined in raw_data_base_def.cpp
931401458bSopenharmony_ci    tzIndex = ParseTimeZone(15); // 15 is an invalid timezone value
941401458bSopenharmony_ci    ASSERT_EQ(tzIndex, 14); // default index
951401458bSopenharmony_ci}
961401458bSopenharmony_ci
971401458bSopenharmony_ci/**
981401458bSopenharmony_ci * @tc.name: RawDataTest001
991401458bSopenharmony_ci * @tc.desc: Construction and destruction of RawData
1001401458bSopenharmony_ci * @tc.type: FUNC
1011401458bSopenharmony_ci * @tc.require: issueI8YWH1
1021401458bSopenharmony_ci */
1031401458bSopenharmony_ciHWTEST_F(HiSysEventEncodedTest, RawDataTest001, TestSize.Level1)
1041401458bSopenharmony_ci{
1051401458bSopenharmony_ci    Encoded::RawData rawData1(nullptr, 0);
1061401458bSopenharmony_ci    ASSERT_TRUE(rawData1.IsEmpty());
1071401458bSopenharmony_ci    Encoded::RawData rawData2 = rawData1;
1081401458bSopenharmony_ci    ASSERT_TRUE(rawData2.IsEmpty());
1091401458bSopenharmony_ci    uint64_t val = 2323232; // 2323232 is a random test numeber
1101401458bSopenharmony_ci    std::shared_ptr<EncodedParam> param = std::make_shared<UnsignedVarintEncodedParam<uint64_t>>("KEY1", val);
1111401458bSopenharmony_ci    auto rawData3 = std::make_shared<Encoded::RawData>(nullptr, 0);
1121401458bSopenharmony_ci    param->SetRawData(rawData3);
1131401458bSopenharmony_ci    auto appendRawData = param->GetRawData();
1141401458bSopenharmony_ci    rawData1 = *appendRawData;
1151401458bSopenharmony_ci    ASSERT_TRUE(!rawData1.IsEmpty());
1161401458bSopenharmony_ci    Encoded::RawData rawData4 = rawData1;
1171401458bSopenharmony_ci    ASSERT_EQ(rawData1.GetDataLength(), rawData4.GetDataLength());
1181401458bSopenharmony_ci    Encoded::RawData rawData5(appendRawData->GetData(), appendRawData->GetDataLength());
1191401458bSopenharmony_ci    ASSERT_EQ(appendRawData->GetDataLength(), rawData5.GetDataLength());
1201401458bSopenharmony_ci}
1211401458bSopenharmony_ci
1221401458bSopenharmony_ci/**
1231401458bSopenharmony_ci * @tc.name: TransportTest001
1241401458bSopenharmony_ci * @tc.desc: Send raw data
1251401458bSopenharmony_ci * @tc.type: FUNC
1261401458bSopenharmony_ci * @tc.require: issueI8YWH1
1271401458bSopenharmony_ci */
1281401458bSopenharmony_ciHWTEST_F(HiSysEventEncodedTest, TransportTest001, TestSize.Level1)
1291401458bSopenharmony_ci{
1301401458bSopenharmony_ci    auto rawData1 = std::make_shared<Encoded::RawData>();
1311401458bSopenharmony_ci    ASSERT_TRUE(rawData1->IsEmpty());
1321401458bSopenharmony_ci    ASSERT_EQ(Transport::GetInstance().SendData(*rawData1), ERR_EMPTY_EVENT);
1331401458bSopenharmony_ci    uint64_t val = 2323232; // 2323232 is a random test numeber
1341401458bSopenharmony_ci    std::shared_ptr<EncodedParam> param = std::make_shared<UnsignedVarintEncodedParam<uint64_t>>("KEY1", val);
1351401458bSopenharmony_ci    ASSERT_NE(param, nullptr);
1361401458bSopenharmony_ci    param->SetRawData(rawData1);
1371401458bSopenharmony_ci    param->Encode();
1381401458bSopenharmony_ci    auto rawData2 = param->GetRawData();
1391401458bSopenharmony_ci    ASSERT_TRUE(!rawData2->IsEmpty());
1401401458bSopenharmony_ci    ASSERT_EQ(Transport::GetInstance().SendData(*rawData2), SUCCESS);
1411401458bSopenharmony_ci}
142