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 <iostream>
17 #include <sstream>
18 #include <string>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <vector>
22 #include <fstream>
23
24 #include <gtest/gtest.h>
25 #include "json/value.h"
26
27 #include "b_error/b_error.h"
28 #include "b_error/b_excep_utils.h"
29 #include "b_json/b_json_service_disposal_config.h"
30
31 namespace OHOS::FileManagement::Backup {
32 using namespace std;
33 using namespace testing;
34
35 namespace {
36 const string PATH = "/data/service/el2/100/backup/";
37 const string CONFIG_NAME = "RestoreDisposalConfig.json";
38 } // namespace
39
40 class BJsonServiceDisposalConfigTest : public testing::Test {
41 public:
42 // 所有测试用例执行之前执行
SetUpTestCase(void)43 static void SetUpTestCase(void) {};
44 // 所有测试用例执行之后执行
TearDownTestCase(void)45 static void TearDownTestCase(void) {};
46 // 每次测试用例执行之前执行
SetUp()47 void SetUp() {};
48 // 每次测试用例执行之后执行
49 void TearDown();
50 };
51
TearDown()52 void BJsonServiceDisposalConfigTest::TearDown()
53 {
54 BJsonDisposalConfig config;
55 config.DeleteConfigFile();
56 };
57
58 /**
59 * @tc.number: SUB_Disposal_Config_Test_0100
60 * @tc.name: Disposal_Config_Test_0100
61 * @tc.desc: 测试配置文件存在的情况
62 * @tc.size: MEDIUM
63 * @tc.type: FUNC
64 * @tc.level Level 1
65 * @tc.require: IAAMIK
66 */
67 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0100, testing::ext::TestSize.Level1)
68 {
69 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0100";
70 try {
71 string filePath = PATH + CONFIG_NAME;
72 ofstream outFile(filePath);
73 outFile.close();
74 BJsonDisposalConfig config;
75 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
76 } catch (...) {
77 EXPECT_TRUE(false);
78 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
79 }
80 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0100";
81 }
82
83 /**
84 * @tc.number: SUB_Disposal_Config_Test_0101
85 * @tc.name: Disposal_Config_Test_0101
86 * @tc.desc: 测试配置文件不存在的情况
87 * @tc.size: MEDIUM
88 * @tc.type: FUNC
89 * @tc.level Level 1
90 * @tc.require: IAAMIK
91 */
92 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0101, testing::ext::TestSize.Level1)
93 {
94 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0101";
95 try {
96 string filePath = PATH + CONFIG_NAME;
97 BJsonDisposalConfig config;
98 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
99 } catch (...) {
100 EXPECT_TRUE(false);
101 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
102 }
103 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0101";
104 }
105
106 /**
107 * @tc.number: SUB_Disposal_Config_Test_0200
108 * @tc.name: Disposal_Config_Test_0200
109 * @tc.desc: 测试写入成功的情况
110 * @tc.size: MEDIUM
111 * @tc.type: FUNC
112 * @tc.level Level 1
113 * @tc.require: IAAMIK
114 */
115 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0200, testing::ext::TestSize.Level1)
116 {
117 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0200";
118 try {
119 string filePath = PATH + CONFIG_NAME;
120 BJsonDisposalConfig config;
121 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
122 const string bundleName = "test1";
123 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
124 EXPECT_TRUE(retAdd);
125 } catch (...) {
126 EXPECT_TRUE(false);
127 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
128 }
129 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0200";
130 }
131
132 /**
133 * @tc.number: SUB_Disposal_Config_Test_0201
134 * @tc.name: Disposal_Config_Test_0201
135 * @tc.desc: 测试写入失败的情况
136 * @tc.size: MEDIUM
137 * @tc.type: FUNC
138 * @tc.level Level 1
139 * @tc.require: IAAMIK
140 */
141 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0201, testing::ext::TestSize.Level1)
142 {
143 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0201";
144 try {
145 string filePath = PATH + CONFIG_NAME;
146 BJsonDisposalConfig config;
147 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
148 bool retDele = config.DeleteConfigFile();
149 EXPECT_TRUE(retDele);
150 const string bundleName = "test1";
151 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
152 EXPECT_FALSE(retAdd);
153 } catch (...) {
154 EXPECT_TRUE(false);
155 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
156 }
157 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0201";
158 }
159
160 /**
161 * @tc.number: SUB_Disposal_Config_Test_0300
162 * @tc.name: Disposal_Config_Test_0300
163 * @tc.desc: 测试删除成功的情况
164 * @tc.size: MEDIUM
165 * @tc.type: FUNC
166 * @tc.level Level 1
167 * @tc.require: IAAMIK
168 */
169 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0300, testing::ext::TestSize.Level1)
170 {
171 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0300";
172 try {
173 string filePath = PATH + CONFIG_NAME;
174 BJsonDisposalConfig config;
175 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
176 const string bundleName = "test1";
177 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
178 EXPECT_TRUE(retAdd);
179 bool retDel = config.DeleteFromDisposalConfigFile(bundleName);
180 EXPECT_TRUE(retDel);
181 } catch (...) {
182 EXPECT_TRUE(false);
183 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
184 }
185 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0300";
186 }
187
188 /**
189 * @tc.number: SUB_Disposal_Config_Test_0301
190 * @tc.name: Disposal_Config_Test_0301
191 * @tc.desc: 测试删除失败的情况
192 * @tc.size: MEDIUM
193 * @tc.type: FUNC
194 * @tc.level Level 1
195 * @tc.require: IAAMIK
196 */
197 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0301, testing::ext::TestSize.Level1)
198 {
199 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0301";
200 try {
201 string filePath = PATH + CONFIG_NAME;
202 BJsonDisposalConfig config;
203 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
204 const string bundleName = "test1";
205 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
206 EXPECT_TRUE(retAdd);
207 bool retDele = config.DeleteConfigFile();
208 EXPECT_TRUE(retDele);
209 bool retDel = config.DeleteFromDisposalConfigFile(bundleName);
210 EXPECT_FALSE(retDel);
211 } catch (...) {
212 EXPECT_TRUE(false);
213 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
214 }
215 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0301";
216 }
217
218 /**
219 * @tc.number: SUB_Disposal_Config_Test_0400
220 * @tc.name: Disposal_Config_Test_0400
221 * @tc.desc: 测试判断成功的情况
222 * @tc.size: MEDIUM
223 * @tc.type: FUNC
224 * @tc.level Level 1
225 * @tc.require: IAAMIK
226 */
227 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0400, testing::ext::TestSize.Level1)
228 {
229 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0400";
230 try {
231 string filePath = PATH + CONFIG_NAME;
232 BJsonDisposalConfig config;
233 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
234 const string bundleName = "test1";
235 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
236 EXPECT_TRUE(retAdd);
237 bool retIf = config.IfBundleNameInDisposalConfigFile(bundleName);
238 EXPECT_TRUE(retIf);
239 } catch (...) {
240 EXPECT_TRUE(false);
241 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
242 }
243 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0400";
244 }
245
246 /**
247 * @tc.number: SUB_Disposal_Config_Test_0401
248 * @tc.name: Disposal_Config_Test_0401
249 * @tc.desc: 测试判断失败的情况
250 * @tc.size: MEDIUM
251 * @tc.type: FUNC
252 * @tc.level Level 1
253 * @tc.require: IAAMIK
254 */
255 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0401, testing::ext::TestSize.Level1)
256 {
257 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0401";
258 try {
259 string filePath = PATH + CONFIG_NAME;
260 BJsonDisposalConfig config;
261 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
262 const string bundleName = "test1";
263 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
264 EXPECT_TRUE(retAdd);
265 bool retDele = config.DeleteConfigFile();
266 EXPECT_TRUE(retDele);
267 bool retIf = config.IfBundleNameInDisposalConfigFile(bundleName);
268 EXPECT_FALSE(retIf);
269 } catch (...) {
270 EXPECT_TRUE(false);
271 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
272 }
273 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0401";
274 }
275
276 /**
277 * @tc.number: SUB_Disposal_Config_Test_0500
278 * @tc.name: Disposal_Config_Test_0500
279 * @tc.desc: 测试获取成功的情况
280 * @tc.size: MEDIUM
281 * @tc.type: FUNC
282 * @tc.level Level 1
283 * @tc.require: IAAMIK
284 */
285 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0500, testing::ext::TestSize.Level1)
286 {
287 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0500";
288 try {
289 string filePath = PATH + CONFIG_NAME;
290 BJsonDisposalConfig config;
291 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
292 const string bundleName = "test1";
293 bool retAdd = config.AppendIntoDisposalConfigFile(bundleName);
294 EXPECT_TRUE(retAdd);
295 vector<string> bundleNameList = config.GetBundleNameFromConfigFile();
296 bool result = false;
297 if (bundleNameList.size() == 1) {
298 result = true;
299 }
300 EXPECT_TRUE(result);
301 } catch (...) {
302 EXPECT_TRUE(false);
303 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
304 }
305 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0500";
306 }
307
308 /**
309 * @tc.number: SUB_Disposal_Config_Test_0501
310 * @tc.name: Disposal_Config_Test_0501
311 * @tc.desc: 测试获取失败的情况
312 * @tc.size: MEDIUM
313 * @tc.type: FUNC
314 * @tc.level Level 1
315 * @tc.require: IAAMIK
316 */
317 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0501, testing::ext::TestSize.Level1)
318 {
319 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0501";
320 try {
321 string filePath = PATH + CONFIG_NAME;
322 BJsonDisposalConfig config;
323 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
324 vector<string> bundleNameList = config.GetBundleNameFromConfigFile();
325 bool result = false;
326 if (bundleNameList.size() == 0) {
327 result = true;
328 }
329 EXPECT_TRUE(result);
330 } catch (...) {
331 EXPECT_TRUE(false);
332 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
333 }
334 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0501";
335 }
336
337 /**
338 * @tc.number: SUB_Disposal_Config_Test_0600
339 * @tc.name: Disposal_Config_Test_0600
340 * @tc.desc: 测试删除成功的情况
341 * @tc.size: MEDIUM
342 * @tc.type: FUNC
343 * @tc.level Level 1
344 * @tc.require: IAAMIK
345 */
346 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0600, testing::ext::TestSize.Level1)
347 {
348 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0600";
349 try {
350 string filePath = PATH + CONFIG_NAME;
351 BJsonDisposalConfig config;
352 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
353 bool retDel = config.DeleteConfigFile();
354 EXPECT_TRUE(retDel);
355 } catch (...) {
356 EXPECT_TRUE(false);
357 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
358 }
359 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0600";
360 }
361
362 /**
363 * @tc.number: SUB_Disposal_Config_Test_0601
364 * @tc.name: Disposal_Config_Test_0601
365 * @tc.desc: 测试删除失败的情况
366 * @tc.size: MEDIUM
367 * @tc.type: FUNC
368 * @tc.level Level 1
369 * @tc.require: IAAMIK
370 */
371 HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0601, testing::ext::TestSize.Level1)
372 {
373 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0601";
374 try {
375 string filePath = PATH + CONFIG_NAME;
376 BJsonDisposalConfig config;
377 EXPECT_EQ(access(filePath.c_str(), F_OK), 0);
378 bool retDele = config.DeleteConfigFile();
379 EXPECT_TRUE(retDele);
380 bool retDel = config.DeleteConfigFile();
381 EXPECT_FALSE(retDel);
382 } catch (...) {
383 EXPECT_TRUE(false);
384 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction.";
385 }
386 GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0601";
387 }
388 } // namespace OHOS::FileManagement::Backup