1/*
2 * Copyright (c) 2023 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#define private public
19#include "dfx_hap.h"
20#undef private
21
22using namespace OHOS::HiviewDFX;
23using namespace testing::ext;
24using namespace std;
25
26namespace OHOS {
27namespace HiviewDFX {
28class DfxHapTest : public testing::Test {
29public:
30    static void SetUpTestCase(void) {}
31    static void TearDownTestCase(void) {}
32    void SetUp() {}
33    void TearDown() {}
34};
35
36/**
37 * @tc.name: DfxHapTest001
38 * @tc.desc: test DfxHap functions ParseHapInfo exception
39 * @tc.type: FUNC
40 */
41HWTEST_F(DfxHapTest, DfxHapTest001, TestSize.Level2)
42{
43    GTEST_LOG_(INFO) << "DfxHapTest001: start.";
44    DfxHap dfxHap;
45    pid_t pid = 1;
46    uint64_t pc = 1;
47    uintptr_t methodid = 1;
48    auto map = std::make_shared<DfxMap>();
49    JsFunction jsFunction;
50
51    bool res = dfxHap.ParseHapInfo(pid, pc, methodid, map, nullptr);
52    ASSERT_EQ(res, false);
53    res = dfxHap.ParseHapInfo(pid, pc, methodid, map, &jsFunction);
54    ASSERT_EQ(res, false);
55    map->name = "test.hap";
56    res = dfxHap.ParseHapInfo(pid, pc, methodid, map, &jsFunction);
57    ASSERT_EQ(res, false);
58    GTEST_LOG_(INFO) << "DfxHapTest001: end.";
59}
60
61/**
62 * @tc.name: DfxHapTest002
63 * @tc.desc: test DfxHap ParseHapMemData exception
64 * @tc.type: FUNC
65 */
66HWTEST_F(DfxHapTest, DfxHapTest002, TestSize.Level2)
67{
68    DfxHap dfxHap;
69    GTEST_LOG_(INFO) << "DfxHapTest002: start.";
70    pid_t pid = 1;
71    auto map = std::make_shared<DfxMap>();
72    map->begin = 10001; // 10001 : simulate the begin value of DfxMap
73    map->end = 10101; // 10101 : simulate the end value of DfxMap
74    auto res = dfxHap.ParseHapMemData(pid, nullptr);
75    ASSERT_EQ(res, false);
76    res = dfxHap.ParseHapMemData(pid, map);
77    ASSERT_EQ(res, false);
78    size_t abcDataSize = map->end - map->begin;
79    dfxHap.abcDataPtr_ = std::make_unique<uint8_t[]>(abcDataSize);
80    res = dfxHap.ParseHapMemData(pid, map);
81    ASSERT_EQ(res, true);
82    GTEST_LOG_(INFO) << "DfxHapTest002: end.";
83}
84
85/**
86 * @tc.name: DfxHapTest003
87 * @tc.desc: test DfxHap ParseHapFileData exception
88 * @tc.type: FUNC
89 */
90HWTEST_F(DfxHapTest, DfxHapTest003, TestSize.Level2)
91{
92    DfxHap dfxHap;
93    GTEST_LOG_(INFO) << "DfxHapTest003: start.";
94    std::string name = "";
95    auto res = dfxHap.ParseHapFileData(name);
96    ASSERT_EQ(res, false);
97    name = "test";
98    res = dfxHap.ParseHapFileData(name);
99    ASSERT_EQ(res, false);
100    dfxHap.abcDataPtr_ = std::make_unique<uint8_t[]>(1);
101    res = dfxHap.ParseHapFileData(name);
102    ASSERT_EQ(res, true);
103
104    GTEST_LOG_(INFO) << "DfxHapTest003: end.";
105}
106
107/**
108 * @tc.name: DfxHapTest004
109 * @tc.desc: test DfxHap ParseHapMemInfo exception
110 * @tc.type: FUNC
111 */
112HWTEST_F(DfxHapTest, DfxHapTest004, TestSize.Level2)
113{
114    DfxHap dfxHap;
115    GTEST_LOG_(INFO) << "DfxHapTest004: start.";
116    pid_t pid = 1;
117    uint64_t pc = 1;
118    uintptr_t methodid = 1;
119    auto map = std::make_shared<DfxMap>();
120    JsFunction jsFunction;
121    auto res = dfxHap.ParseHapMemInfo(pid, pc, methodid, map, nullptr);
122    ASSERT_EQ(res, false);
123    res = dfxHap.ParseHapMemInfo(pid, pc, methodid, map, &jsFunction);
124    ASSERT_EQ(res, false);
125    GTEST_LOG_(INFO) << "DfxHapTest004: end.";
126}
127
128
129} // namespace HiviewDFX
130} // namespace OHOS