1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#define private public
19800b99b8Sopenharmony_ci#include "dfx_hap.h"
20800b99b8Sopenharmony_ci#undef private
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX;
23800b99b8Sopenharmony_ciusing namespace testing::ext;
24800b99b8Sopenharmony_ciusing namespace std;
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_cinamespace OHOS {
27800b99b8Sopenharmony_cinamespace HiviewDFX {
28800b99b8Sopenharmony_ciclass DfxHapTest : public testing::Test {
29800b99b8Sopenharmony_cipublic:
30800b99b8Sopenharmony_ci    static void SetUpTestCase(void) {}
31800b99b8Sopenharmony_ci    static void TearDownTestCase(void) {}
32800b99b8Sopenharmony_ci    void SetUp() {}
33800b99b8Sopenharmony_ci    void TearDown() {}
34800b99b8Sopenharmony_ci};
35800b99b8Sopenharmony_ci
36800b99b8Sopenharmony_ci/**
37800b99b8Sopenharmony_ci * @tc.name: DfxHapTest001
38800b99b8Sopenharmony_ci * @tc.desc: test DfxHap functions ParseHapInfo exception
39800b99b8Sopenharmony_ci * @tc.type: FUNC
40800b99b8Sopenharmony_ci */
41800b99b8Sopenharmony_ciHWTEST_F(DfxHapTest, DfxHapTest001, TestSize.Level2)
42800b99b8Sopenharmony_ci{
43800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest001: start.";
44800b99b8Sopenharmony_ci    DfxHap dfxHap;
45800b99b8Sopenharmony_ci    pid_t pid = 1;
46800b99b8Sopenharmony_ci    uint64_t pc = 1;
47800b99b8Sopenharmony_ci    uintptr_t methodid = 1;
48800b99b8Sopenharmony_ci    auto map = std::make_shared<DfxMap>();
49800b99b8Sopenharmony_ci    JsFunction jsFunction;
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_ci    bool res = dfxHap.ParseHapInfo(pid, pc, methodid, map, nullptr);
52800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
53800b99b8Sopenharmony_ci    res = dfxHap.ParseHapInfo(pid, pc, methodid, map, &jsFunction);
54800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
55800b99b8Sopenharmony_ci    map->name = "test.hap";
56800b99b8Sopenharmony_ci    res = dfxHap.ParseHapInfo(pid, pc, methodid, map, &jsFunction);
57800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
58800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest001: end.";
59800b99b8Sopenharmony_ci}
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ci/**
62800b99b8Sopenharmony_ci * @tc.name: DfxHapTest002
63800b99b8Sopenharmony_ci * @tc.desc: test DfxHap ParseHapMemData exception
64800b99b8Sopenharmony_ci * @tc.type: FUNC
65800b99b8Sopenharmony_ci */
66800b99b8Sopenharmony_ciHWTEST_F(DfxHapTest, DfxHapTest002, TestSize.Level2)
67800b99b8Sopenharmony_ci{
68800b99b8Sopenharmony_ci    DfxHap dfxHap;
69800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest002: start.";
70800b99b8Sopenharmony_ci    pid_t pid = 1;
71800b99b8Sopenharmony_ci    auto map = std::make_shared<DfxMap>();
72800b99b8Sopenharmony_ci    map->begin = 10001; // 10001 : simulate the begin value of DfxMap
73800b99b8Sopenharmony_ci    map->end = 10101; // 10101 : simulate the end value of DfxMap
74800b99b8Sopenharmony_ci    auto res = dfxHap.ParseHapMemData(pid, nullptr);
75800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
76800b99b8Sopenharmony_ci    res = dfxHap.ParseHapMemData(pid, map);
77800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
78800b99b8Sopenharmony_ci    size_t abcDataSize = map->end - map->begin;
79800b99b8Sopenharmony_ci    dfxHap.abcDataPtr_ = std::make_unique<uint8_t[]>(abcDataSize);
80800b99b8Sopenharmony_ci    res = dfxHap.ParseHapMemData(pid, map);
81800b99b8Sopenharmony_ci    ASSERT_EQ(res, true);
82800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest002: end.";
83800b99b8Sopenharmony_ci}
84800b99b8Sopenharmony_ci
85800b99b8Sopenharmony_ci/**
86800b99b8Sopenharmony_ci * @tc.name: DfxHapTest003
87800b99b8Sopenharmony_ci * @tc.desc: test DfxHap ParseHapFileData exception
88800b99b8Sopenharmony_ci * @tc.type: FUNC
89800b99b8Sopenharmony_ci */
90800b99b8Sopenharmony_ciHWTEST_F(DfxHapTest, DfxHapTest003, TestSize.Level2)
91800b99b8Sopenharmony_ci{
92800b99b8Sopenharmony_ci    DfxHap dfxHap;
93800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest003: start.";
94800b99b8Sopenharmony_ci    std::string name = "";
95800b99b8Sopenharmony_ci    auto res = dfxHap.ParseHapFileData(name);
96800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
97800b99b8Sopenharmony_ci    name = "test";
98800b99b8Sopenharmony_ci    res = dfxHap.ParseHapFileData(name);
99800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
100800b99b8Sopenharmony_ci    dfxHap.abcDataPtr_ = std::make_unique<uint8_t[]>(1);
101800b99b8Sopenharmony_ci    res = dfxHap.ParseHapFileData(name);
102800b99b8Sopenharmony_ci    ASSERT_EQ(res, true);
103800b99b8Sopenharmony_ci
104800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest003: end.";
105800b99b8Sopenharmony_ci}
106800b99b8Sopenharmony_ci
107800b99b8Sopenharmony_ci/**
108800b99b8Sopenharmony_ci * @tc.name: DfxHapTest004
109800b99b8Sopenharmony_ci * @tc.desc: test DfxHap ParseHapMemInfo exception
110800b99b8Sopenharmony_ci * @tc.type: FUNC
111800b99b8Sopenharmony_ci */
112800b99b8Sopenharmony_ciHWTEST_F(DfxHapTest, DfxHapTest004, TestSize.Level2)
113800b99b8Sopenharmony_ci{
114800b99b8Sopenharmony_ci    DfxHap dfxHap;
115800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest004: start.";
116800b99b8Sopenharmony_ci    pid_t pid = 1;
117800b99b8Sopenharmony_ci    uint64_t pc = 1;
118800b99b8Sopenharmony_ci    uintptr_t methodid = 1;
119800b99b8Sopenharmony_ci    auto map = std::make_shared<DfxMap>();
120800b99b8Sopenharmony_ci    JsFunction jsFunction;
121800b99b8Sopenharmony_ci    auto res = dfxHap.ParseHapMemInfo(pid, pc, methodid, map, nullptr);
122800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
123800b99b8Sopenharmony_ci    res = dfxHap.ParseHapMemInfo(pid, pc, methodid, map, &jsFunction);
124800b99b8Sopenharmony_ci    ASSERT_EQ(res, false);
125800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxHapTest004: end.";
126800b99b8Sopenharmony_ci}
127800b99b8Sopenharmony_ci
128800b99b8Sopenharmony_ci
129800b99b8Sopenharmony_ci} // namespace HiviewDFX
130800b99b8Sopenharmony_ci} // namespace OHOS