1 /*
2 * Copyright (c) 2024 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 "leak_detector_module_test.h"
17
18 #include <queue>
19 #include "test_util.h"
20 #include "parameters.h"
21 #include "fault_detector_util.h"
22
23 #include "hiview_logger.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 DEFINE_LOG_TAG("LeakDetectorModuleTest");
28 using namespace std;
29 using namespace testing::ext;
30
31 constexpr int OVERTIME_TO_GENERATE_FILES = 60; // 60s
32 constexpr int OVERTIME_TO_START_PROCESS = 5; // 5s
33 constexpr int OVERTIME_TO_LEAK = 30; // 30s
34 const string MY_PROCESS = "LeakDetectorModuleTest";
35 const string THRESHOLD_LINE = "DEFAULT 1";
36 const string NATIVE_THRESHOLD_PATH = "/system/etc/hiview/memory_leak_threshold";
37 const string NATIVE_THRESHOLD_BAK_PATH = "/data/local/tmp/memory_leak_threshold_bak";
38
SetUpTestCase(void)39 void LeakDetectorModuleTest::SetUpTestCase(void)
40 {
41 system::SetParameter("hiview.memleak.test", "enable");
42 TestUtil::CopyFile(NATIVE_THRESHOLD_PATH, NATIVE_THRESHOLD_BAK_PATH);
43 TestUtil::WriteFile(NATIVE_THRESHOLD_PATH, THRESHOLD_LINE);
44 TestUtil::KillProcess("hiview");
45 }
46
TearDownTestCase(void)47 void LeakDetectorModuleTest::TearDownTestCase(void)
48 {
49 TestUtil::CopyFile(NATIVE_THRESHOLD_BAK_PATH, NATIVE_THRESHOLD_PATH);
50 TestUtil::DeleteFile(NATIVE_THRESHOLD_BAK_PATH);
51 system::SetParameter("hiview.memleak.test", "disable");
52 TestUtil::KillProcess("hiview");
53 }
54
SetUp(void)55 void LeakDetectorModuleTest::SetUp(void)
56 {
57 TestUtil::ClearDir(MEMORY_LEAK_PATH);
58 }
59
TearDown(void)60 void LeakDetectorModuleTest::TearDown(void)
61 {
62 }
63
64 /**
65 * @tc.name: LeakDetectorModuleTest001
66 * @tc.desc: check whether the sample files generated.
67 * @tc.type: FUNC
68 * @tc.require:
69 * @tc.author: wuzhenyang
70 */
71 HWTEST_F(LeakDetectorModuleTest, LeakDetectorModuleTest001, TestSize.Level1)
72 {
73 string sampleFile = TestUtil::GetSampleFile(MY_PROCESS);
74 HIVIEW_LOGI("sampleFile:%{public}s", sampleFile.c_str());
75 ASSERT_FALSE(sampleFile.empty());
76
77 LeakDetectorModuleTest::WaitFile(sampleFile);
78 HIVIEW_LOGI("start assert FileExists sampleFile");
79 ASSERT_TRUE(TestUtil::FileExists(sampleFile));
80 }
81
WaitFile(string file)82 void LeakDetectorModuleTest::WaitFile(string file)
83 {
84 int checkCnt = 0;
85 while (checkCnt++ < OVERTIME_TO_GENERATE_FILES) {
86 if (TestUtil::FileExists(file)) {
87 HIVIEW_LOGI("WaitFile success, file:%{public}s", file.c_str());
88 return;
89 }
90 sleep(1);
91 }
92 HIVIEW_LOGI("WaitFile failed, checkCnt:%{public}d", checkCnt);
93 }
94
95 } // namespace HiviewDFX
96 } // namespace OHOS
97