• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dfx_map.h"
22 
23 using namespace OHOS::HiviewDFX;
24 using namespace testing::ext;
25 using namespace std;
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class DfxHapTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
SetUp()33     void SetUp() {}
TearDown()34     void TearDown() {}
35 };
36 
37 /**
38  * @tc.name: DfxHapTest001
39  * @tc.desc: test DfxHap functions ParseHapInfo exception
40  * @tc.type: FUNC
41  */
42 HWTEST_F(DfxHapTest, DfxHapTest001, TestSize.Level0)
43 {
44     GTEST_LOG_(INFO) << "DfxHapTest001: start.";
45     DfxHap dfxHap;
46     pid_t pid = 1;
47     uint64_t pc = 1;
48     auto map = std::make_shared<DfxMap>();
49     JsFunction jsFunction;
50 
51     bool res = dfxHap.ParseHapInfo(pid, pc, map, nullptr);
52     ASSERT_EQ(res, false);
53     res = dfxHap.ParseHapInfo(pid, pc, map, &jsFunction);
54     ASSERT_EQ(res, false);
55     map->name = "test.hap";
56     res = dfxHap.ParseHapInfo(pid, pc, 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  */
66 HWTEST_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  */
90 HWTEST_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  */
112 HWTEST_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     auto map = std::make_shared<DfxMap>();
119     JsFunction jsFunction;
120     auto res = dfxHap.ParseHapMemInfo(pid, pc, map, nullptr);
121     ASSERT_EQ(res, false);
122     res = dfxHap.ParseHapMemInfo(pid, pc, map, &jsFunction);
123     ASSERT_EQ(res, false);
124     GTEST_LOG_(INFO) << "DfxHapTest004: end.";
125 }
126 
127 /**
128  * @tc.name: DfxHapTest005
129  * @tc.desc: test DfxHap ParseHapFileInfo exception
130  * @tc.type: FUNC
131  */
132 HWTEST_F(DfxHapTest, DfxHapTest005, TestSize.Level2)
133 {
134     GTEST_LOG_(INFO) << "DfxHapTest005: start.";
135     DfxHap dfxHap;
136     std::shared_ptr<DfxMap> map = nullptr;
137     dfxHap.ParseHapFileInfo(0, map, nullptr);
138     JsFunction jsFunction;
139     dfxHap.ParseHapFileInfo(0, map, &jsFunction);
140     auto map1 = std::make_shared<DfxMap>();
141     bool ret = dfxHap.ParseHapFileInfo(0, map1, &jsFunction);
142     ASSERT_EQ(ret, false);
143     GTEST_LOG_(INFO) << "DfxHapTest005: end.";
144 }
145 } // namespace HiviewDFX
146 } // namespace OHOS