1 /*
2 * Copyright (c) 2025 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 <algorithm>
17 #include <gtest/gtest.h>
18
19 #define private public
20 #define protected public
21 #include "application_cleaner.h"
22 #include "context_impl.h"
23 #undef private
24 #undef protected
25
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AppExecFwk;
29 using namespace OHOS::AbilityRuntime;
30
31 namespace OHOS {
32 namespace AppExecFwk {
33 class ApplicationCleanerSecondTest : public testing::Test {
34 public:
ApplicationCleanerSecondTest()35 ApplicationCleanerSecondTest()
36 {}
~ApplicationCleanerSecondTest()37 ~ApplicationCleanerSecondTest()
38 {}
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase(void)45 void ApplicationCleanerSecondTest::SetUpTestCase(void)
46 {}
47
TearDownTestCase(void)48 void ApplicationCleanerSecondTest::TearDownTestCase(void)
49 {}
50
SetUp(void)51 void ApplicationCleanerSecondTest::SetUp(void)
52 {}
53
TearDown(void)54 void ApplicationCleanerSecondTest::TearDown(void)
55 {}
56
57 /**
58 * @tc.number: CheckFileSize_0100
59 * @tc.name: CheckFileSize
60 * @tc.desc: Test whether CheckFileSize and are called normally.
61 */
62 HWTEST_F(ApplicationCleanerSecondTest, CheckFileSize_0100, Function | MediumTest | Level1)
63 {
64 GTEST_LOG_(INFO) << "CheckFileSize_0100 start";
65 auto cleaner = ApplicationCleaner::GetInstance();
66 std::vector<std::string> bundlePath{};
67 std::string pathStr = "https://cn.bing.com/searchsearchsearchsearchsearch";
68 bundlePath.clear();
69 bundlePath.push_back(pathStr);
70 bool ret = cleaner->CheckFileSize(bundlePath);
71 EXPECT_TRUE(ret);
72 GTEST_LOG_(INFO) << "CheckFileSize_0100 end";
73 }
74
75 /**
76 * @tc.number: CheckFileSize_0200
77 * @tc.name: CheckFileSize
78 * @tc.desc: Test whether CheckFileSize and are called normally.
79 */
80 HWTEST_F(ApplicationCleanerSecondTest, CheckFileSize_0200, Function | MediumTest | Level1)
81 {
82 GTEST_LOG_(INFO) << "CheckFileSize_0200 start";
83 auto cleaner = ApplicationCleaner::GetInstance();
84 std::vector<std::string> bundlePath{};
85 std::string pathStr = "https://cn.bing.com/searchsearchsearchsearchsearch";
86 bundlePath.clear();
87 for (int i = 0; i < 20000; i++) {
88 bundlePath.push_back(pathStr);
89 }
90 bool ret = cleaner->CheckFileSize(bundlePath);
91 EXPECT_TRUE(ret);
92 GTEST_LOG_(INFO) << "CheckFileSize_0200 end";
93 }
94
95 /**
96 * @tc.number: ClearTempData_0100
97 * @tc.name: ClearTempData
98 * @tc.desc: Test whether ClearTempData and are called normally.
99 */
100 HWTEST_F(ApplicationCleanerSecondTest, ClearTempData_0100, Function | MediumTest | Level1)
101 {
102 GTEST_LOG_(INFO) << "ClearTempData_0100 start";
103 auto cleaner = ApplicationCleaner::GetInstance();
104 EXPECT_NE(cleaner, nullptr);
105 cleaner->ClearTempData();
106 GTEST_LOG_(INFO) << "ClearTempData_0100 end";
107 }
108
109 /**
110 * @tc.number: RenameTempData_0100
111 * @tc.name: RenameTempData
112 * @tc.desc: Test whether RenameTempData and are called normally.
113 */
114 HWTEST_F(ApplicationCleanerSecondTest, RenameTempData_0100, Function | MediumTest | Level1)
115 {
116 GTEST_LOG_(INFO) << "RenameTempData_0100 start";
117 auto cleaner = ApplicationCleaner::GetInstance();
118 std::vector<std::string> tempDir1{};
119 cleaner->context_ = AbilityRuntime::ApplicationContext::GetInstance();
120 cleaner->context_->GetAllTempDir(tempDir1);
121 cleaner->RenameTempData();
122 std::vector<std::string> tempDir2{};
123 cleaner->context_->GetAllTempDir(tempDir2);
124 bool res = std::equal(tempDir1.begin(), tempDir1.end(), tempDir2.begin());
125 EXPECT_EQ(res, true);
126 GTEST_LOG_(INFO) << "RenameTempData_0100 end";
127 }
128
129 /**
130 * @tc.number: GetObsoleteBundleTempPath_0100
131 * @tc.name: GetObsoleteBundleTempPath
132 * @tc.desc: Test whether GetObsoleteBundleTempPath and are called normally.
133 */
134 HWTEST_F(ApplicationCleanerSecondTest, GetObsoleteBundleTempPath_0100, Function | MediumTest | Level1)
135 {
136 GTEST_LOG_(INFO) << "GetObsoleteBundleTempPath_0100 start";
137 auto cleaner = ApplicationCleaner::GetInstance();
138 std::string path = "https://cn.bing.com/searchsearchsearchsearchsearch";
139 std::vector<std::string> rootPath{};
140 rootPath.push_back(path);
141 std::vector<std::string> tempPath{};
142 int res = cleaner->GetObsoleteBundleTempPath(rootPath, tempPath);
143 EXPECT_EQ(res, 0);
144 GTEST_LOG_(INFO) << "GetObsoleteBundleTempPath_0100 end";
145 }
146
147 /**
148 * @tc.number: GetObsoleteBundleTempPath_0200
149 * @tc.name: GetObsoleteBundleTempPath
150 * @tc.desc: Test whether GetObsoleteBundleTempPath and are called normally.
151 */
152 HWTEST_F(ApplicationCleanerSecondTest, GetObsoleteBundleTempPath_0200, Function | MediumTest | Level1)
153 {
154 GTEST_LOG_(INFO) << "GetObsoleteBundleTempPath_0200 start";
155 auto cleaner = ApplicationCleaner::GetInstance();
156 std::vector<std::string> tempPath{};
157 std::vector<std::string> rootPath{};
158 int res = cleaner->GetObsoleteBundleTempPath(rootPath, tempPath);
159 EXPECT_EQ(res, -1);
160 GTEST_LOG_(INFO) << "GetObsoleteBundleTempPath_0200 end";
161 }
162
163 /**
164 * @tc.number: RemoveDir_0100
165 * @tc.name: RemoveDir
166 * @tc.desc: Test whether RemoveDir and are called normally.
167 */
168 HWTEST_F(ApplicationCleanerSecondTest, RemoveDir_0100, Function | MediumTest | Level1)
169 {
170 GTEST_LOG_(INFO) << "RemoveDir_0100 start";
171 auto cleaner = ApplicationCleaner::GetInstance();
172 std::string currentPath = "https://cn.bing.com/searchsearchsearchsearchsearch";
173 bool res = cleaner->RemoveDir(currentPath);
174 EXPECT_EQ(res, false);
175 GTEST_LOG_(INFO) << "RemoveDir_0100 end";
176 }
177
178 /**
179 * @tc.number: RemoveDir_0200
180 * @tc.name: RemoveDir
181 * @tc.desc: Test whether RemoveDir and are called normally.
182 */
183 HWTEST_F(ApplicationCleanerSecondTest, RemoveDir_0200, Function | MediumTest | Level1)
184 {
185 GTEST_LOG_(INFO) << "RemoveDir_0200 start";
186 auto cleaner = ApplicationCleaner::GetInstance();
187 std::string currentPath = "";
188 bool res = cleaner->RemoveDir(currentPath);
189 EXPECT_FALSE(res);
190 GTEST_LOG_(INFO) << "RemoveDir_0200 end";
191 }
192
193 /**
194 * @tc.number: GetRootPath_0100
195 * @tc.name: GetRootPath
196 * @tc.desc: Test whether GetRootPath and are called normally.
197 */
198 HWTEST_F(ApplicationCleanerSecondTest, GetRootPath_0100, Function | MediumTest | Level1)
199 {
200 GTEST_LOG_(INFO) << "GetRootPath_0100 start";
201 auto cleaner = ApplicationCleaner::GetInstance();
202 std::shared_ptr<AbilityRuntime::ApplicationContext> abilityRuntimeContext = nullptr;
203 std::vector<std::string> bundlePath{};
204 std::string pathStr = "https://cn.bing.com/searchsearchsearchsearchsearch";
205 bundlePath.clear();
206 bundlePath.push_back(pathStr);
207 cleaner->SetRuntimeContext(abilityRuntimeContext);
208 int ret = cleaner->GetRootPath(bundlePath);
209 EXPECT_EQ(ret, -1);
210 GTEST_LOG_(INFO) << "GetRootPath_0100 end";
211 }
212 }
213 }