1 /*
2  * Copyright (c) 2022-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 <cstdio>
17 #include <gtest/gtest.h>
18 
19 #include "b_error/b_error.h"
20 
21 namespace OHOS::FileManagement::Backup {
22 class BErrorTest : public testing::Test {
23 public:
SetUpTestCase(void)24     static void SetUpTestCase(void) {};
TearDownTestCase()25     static void TearDownTestCase() {};
SetUp()26     void SetUp() {};
TearDown()27     void TearDown() {};
28 };
29 
30 /**
31  * @tc.number: SUB_backup_b_error_construction_0100
32  * @tc.name: b_error_construction_0100
33  * @tc.desc: Test function of construction interface for SUCCESS.
34  * @tc.size: MEDIUM
35  * @tc.type: FUNC
36  * @tc.level Level 0
37  * @tc.require: I6F3GV
38  */
39 HWTEST_F(BErrorTest, b_error_construction_0100, testing::ext::TestSize.Level0)
40 {
41     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0100";
42     try {
43         BError be(BError::Codes::OK);
44     } catch (...) {
45         EXPECT_TRUE(false);
46         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
47     }
48     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0100";
49 }
50 
51 /**
52  * @tc.number: SUB_backup_b_error_construction_0300
53  * @tc.name: b_error_construction_0300
54  * @tc.desc: Test function of construction interface for SUCCESS.
55  * @tc.size: MEDIUM
56  * @tc.type: FUNC
57  * @tc.level Level 0
58  * @tc.require: I6F3GV
59  */
60 HWTEST_F(BErrorTest, b_error_construction_0300, testing::ext::TestSize.Level0)
61 {
62     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0300";
63     try {
64         std::string_view extraMsg;
65         BError be(BError::Codes::OK, extraMsg);
66     } catch (...) {
67         EXPECT_TRUE(false);
68         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
69     }
70     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0300";
71 }
72 
73 /**
74  * @tc.number: SUB_backup_b_error_construction_0500
75  * @tc.name: b_error_construction_0500
76  * @tc.desc: Test function of construction interface for SUCCESS.
77  * @tc.size: MEDIUM
78  * @tc.type: FUNC
79  * @tc.level Level 0
80  * @tc.require: I6F3GV
81  */
82 HWTEST_F(BErrorTest, b_error_construction_0500, testing::ext::TestSize.Level0)
83 {
84     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0500";
85     try {
86         throw BError(BError::Codes::UTILS_INVAL_JSON_ENTITY);
87         EXPECT_TRUE(false);
88     } catch (const BError &e) {
89     } catch (...) {
90         EXPECT_TRUE(false);
91         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
92     }
93     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0500";
94 }
95 
96 /**
97  * @tc.number: SUB_backup_b_error_construction_0700
98  * @tc.name: b_error_construction_0700
99  * @tc.desc: Test function of construction interface for SUCCESS.
100  * @tc.size: MEDIUM
101  * @tc.type: FUNC
102  * @tc.level Level 0
103  * @tc.require: I6F3GV
104  */
105 HWTEST_F(BErrorTest, b_error_construction_0700, testing::ext::TestSize.Level0)
106 {
107     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0700";
108     try {
109         throw BError(BError::Codes::UTILS_INVAL_FILE_HANDLE);
110         EXPECT_TRUE(false);
111     } catch (const BError &e) {
112     } catch (...) {
113         EXPECT_TRUE(false);
114         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
115     }
116     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0700";
117 }
118 
119 /**
120  * @tc.number: SUB_backup_b_error_construction_0900
121  * @tc.name: b_error_construction_0900
122  * @tc.desc: Test function of construction interface for SUCCESS.
123  * @tc.size: MEDIUM
124  * @tc.type: FUNC
125  * @tc.level Level 0
126  * @tc.require: I6F3GV
127  */
128 HWTEST_F(BErrorTest, b_error_construction_0900, testing::ext::TestSize.Level0)
129 {
130     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0900";
131     try {
132         throw BError(BError::Codes::UTILS_INVAL_TARBALL_ARG);
133         EXPECT_TRUE(false);
134     } catch (const BError &e) {
135     } catch (...) {
136         EXPECT_TRUE(false);
137         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
138     }
139     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0900";
140 }
141 
142 /**
143  * @tc.number: SUB_backup_b_error_construction_0200
144  * @tc.name: b_error_construction_0200
145  * @tc.desc: Test function of construction interface for SUCCESS.
146  * @tc.size: MEDIUM
147  * @tc.type: FUNC
148  * @tc.level Level 0
149  * @tc.require: I6F3GV
150  */
151 HWTEST_F(BErrorTest, b_error_construction_0200, testing::ext::TestSize.Level0)
152 {
153     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0200";
154     try {
155         throw BError(BError::Codes::UTILS_INVAL_PROCESS_ARG);
156         EXPECT_TRUE(false);
157     } catch (const BError &e) {
158     } catch (...) {
159         EXPECT_TRUE(false);
160         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
161     }
162     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0200";
163 }
164 
165 /**
166  * @tc.number: SUB_backup_b_error_construction_0400
167  * @tc.name: b_error_construction_0400
168  * @tc.desc: Test function of construction interface for SUCCESS.
169  * @tc.size: MEDIUM
170  * @tc.type: FUNC
171  * @tc.level Level 0
172  * @tc.require: I6F3GV
173  */
174 HWTEST_F(BErrorTest, b_error_construction_0400, testing::ext::TestSize.Level0)
175 {
176     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_construction_0400";
177     try {
178         throw BError(BError::Codes::UTILS_INTERRUPTED_PROCESS);
179         EXPECT_TRUE(false);
180     } catch (const BError &e) {
181     } catch (...) {
182         EXPECT_TRUE(false);
183         GTEST_LOG_(INFO) << "BErrorTest-an exception occurred by construction.";
184     }
185     GTEST_LOG_(INFO) << "BErrorTest-end b_error_construction_0400";
186 }
187 
188 /**
189  * @tc.number: SUB_backup_b_error_GetCode_0100
190  * @tc.name: b_error_GetCode_0100
191  * @tc.desc: Test function of GetCode interface for SUCCESS.
192  * @tc.size: MEDIUM
193  * @tc.type: FUNC
194  * @tc.level Level 0
195  * @tc.require: I6F3GV
196  */
197 HWTEST_F(BErrorTest, b_error_GetCode_0100, testing::ext::TestSize.Level0)
198 {
199     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCode_0100";
200     BError be(BError::Codes::OK);
201     int result = be.GetCode();
202     EXPECT_EQ(result, 0);
203     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCode_0100";
204 }
205 
206 /**
207  * @tc.number: SUB_backup_b_error_GetCode_0200
208  * @tc.name: b_error_GetCode_0200
209  * @tc.desc: Test function of GetCode interface for SUCCESS.
210  * @tc.size: MEDIUM
211  * @tc.type: FUNC
212  * @tc.level Level 0
213  * @tc.require: I6F3GV
214  */
215 HWTEST_F(BErrorTest, b_error_GetCode_0200, testing::ext::TestSize.Level0)
216 {
217     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCode_0200";
218     BError be(BError::Codes::UTILS_INVAL_JSON_ENTITY);
219     int ret = be.GetCode();
220     EXPECT_EQ(ret, BError::BackupErrorCode::E_INVAL);
221     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCode_0200";
222 }
223 
224 /**
225  * @tc.number: SUB_backup_b_error_GetCode_0300
226  * @tc.name: b_error_GetCode_0300
227  * @tc.desc: Test function of GetCode interface for SUCCESS.
228  * @tc.size: MEDIUM
229  * @tc.type: FUNC
230  * @tc.level Level 0
231  * @tc.require: I6F3GV
232  */
233 HWTEST_F(BErrorTest, b_error_GetCode_0300, testing::ext::TestSize.Level0)
234 {
235     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCode_0300";
236     BError be(BError::Codes::UTILS_INVAL_FILE_HANDLE);
237     int ret = be.GetCode();
238     EXPECT_EQ(ret, BError::BackupErrorCode::E_INVAL);
239     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCode_0300";
240 }
241 
242 /**
243  * @tc.number: SUB_backup_b_error_GetCode_0400
244  * @tc.name: b_error_GetCode_0400
245  * @tc.desc: Test function of GetCode interface for SUCCESS.
246  * @tc.size: MEDIUM
247  * @tc.type: FUNC
248  * @tc.level Level 0
249  * @tc.require: I6F3GV
250  */
251 HWTEST_F(BErrorTest, b_error_GetCode_0400, testing::ext::TestSize.Level0)
252 {
253     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCode_0400";
254     BError be(BError::Codes::UTILS_INVAL_TARBALL_ARG);
255     int ret = be.GetCode();
256     EXPECT_EQ(ret, BError::BackupErrorCode::E_UKERR);
257     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCode_0400";
258 }
259 
260 /**
261  * @tc.number: SUB_backup_b_error_GetCode_0500
262  * @tc.name: b_error_GetCode_0500
263  * @tc.desc: Test function of GetCode interface for SUCCESS.
264  * @tc.size: MEDIUM
265  * @tc.type: FUNC
266  * @tc.level Level 0
267  * @tc.require: I6F3GV
268  */
269 HWTEST_F(BErrorTest, b_error_GetCode_0500, testing::ext::TestSize.Level0)
270 {
271     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCode_0500";
272     BError be(BError::Codes::UTILS_INVAL_PROCESS_ARG);
273     int ret = be.GetCode();
274     EXPECT_EQ(ret, BError::BackupErrorCode::E_UKERR);
275     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCode_0500";
276 }
277 
278 /**
279  * @tc.number: SUB_backup_b_error_GetCode_0600
280  * @tc.name: b_error_GetCode_0600
281  * @tc.desc: Test function of GetCode interface for SUCCESS.
282  * @tc.size: MEDIUM
283  * @tc.type: FUNC
284  * @tc.level Level 0
285  * @tc.require: I6F3GV
286  */
287 HWTEST_F(BErrorTest, b_error_GetCode_0600, testing::ext::TestSize.Level0)
288 {
289     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCode_0600";
290     BError be(BError::Codes::UTILS_INTERRUPTED_PROCESS);
291     int ret = be.GetCode();
292     EXPECT_EQ(ret, BError::BackupErrorCode::E_UKERR);
293     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCode_0600";
294 }
295 
296 /**
297  * @tc.number: SUB_backup_b_error_GetRawCode_0100
298  * @tc.name: b_error_GetRawCode_0100
299  * @tc.desc: Test function of GetRawCode interface for SUCCESS.
300  * @tc.size: MEDIUM
301  * @tc.type: FUNC
302  * @tc.level Level 0
303  * @tc.require: I6F3GV
304  */
305 HWTEST_F(BErrorTest, b_error_GetRawCode_0100, testing::ext::TestSize.Level0)
306 {
307     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetRawCode_0100";
308     BError be(BError::Codes::OK);
309     BError::Codes result = be.GetRawCode();
310     EXPECT_EQ(result, BError::Codes::OK);
311     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetRawCode_0100";
312 }
313 
314 /**
315  * @tc.number: SUB_backup_b_error_GetRawCode_0200
316  * @tc.name: b_error_GetRawCode_0200
317  * @tc.desc: Test function of GetRawCode interface for SUCCESS.
318  * @tc.size: MEDIUM
319  * @tc.type: FUNC
320  * @tc.level Level 0
321  * @tc.require: I6F3GV
322  */
323 HWTEST_F(BErrorTest, b_error_GetRawCode_0200, testing::ext::TestSize.Level0)
324 {
325     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetRawCode_0200";
326     BError be(BError::Codes::UTILS_INVAL_JSON_ENTITY);
327     BError::Codes result = be.GetRawCode();
328     EXPECT_EQ(result, BError::Codes::UTILS_INVAL_JSON_ENTITY);
329     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetRawCode_0200";
330 }
331 
332 /**
333  * @tc.number: SUB_backup_b_error_GetRawCode_0300
334  * @tc.name: b_error_GetRawCode_0300
335  * @tc.desc: Test function of GetRawCode interface for SUCCESS.
336  * @tc.size: MEDIUM
337  * @tc.type: FUNC
338  * @tc.level Level 0
339  * @tc.require: I6F3GV
340  */
341 HWTEST_F(BErrorTest, b_error_GetRawCode_0300, testing::ext::TestSize.Level0)
342 {
343     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetRawCode_0300";
344     BError be(BError::Codes::UTILS_INVAL_FILE_HANDLE);
345     BError::Codes result = be.GetRawCode();
346     EXPECT_EQ(result, BError::Codes::UTILS_INVAL_FILE_HANDLE);
347     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetRawCode_0300";
348 }
349 
350 /**
351  * @tc.number: SUB_backup_b_error_GetRawCode_0400
352  * @tc.name: b_error_GetRawCode_0400
353  * @tc.desc: Test function of GetRawCode interface for SUCCESS.
354  * @tc.size: MEDIUM
355  * @tc.type: FUNC
356  * @tc.level Level 0
357  * @tc.require: I6F3GV
358  */
359 HWTEST_F(BErrorTest, b_error_GetRawCode_0400, testing::ext::TestSize.Level0)
360 {
361     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetRawCode_0400";
362     BError be(BError::Codes::UTILS_INVAL_TARBALL_ARG);
363     BError::Codes result = be.GetRawCode();
364     EXPECT_EQ(result, BError::Codes::UTILS_INVAL_TARBALL_ARG);
365     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetRawCode_0400";
366 }
367 
368 /**
369  * @tc.number: SUB_backup_b_error_GetRawCode_0500
370  * @tc.name: b_error_GetRawCode_0500
371  * @tc.desc: Test function of GetRawCode interface for SUCCESS.
372  * @tc.size: MEDIUM
373  * @tc.type: FUNC
374  * @tc.level Level 0
375  * @tc.require: I6F3GV
376  */
377 HWTEST_F(BErrorTest, b_error_GetRawCode_0500, testing::ext::TestSize.Level0)
378 {
379     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetRawCode_0500";
380     BError be(BError::Codes::UTILS_INVAL_PROCESS_ARG);
381     BError::Codes result = be.GetRawCode();
382     EXPECT_EQ(result, BError::Codes::UTILS_INVAL_PROCESS_ARG);
383     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetRawCode_0500";
384 }
385 
386 /**
387  * @tc.number: SUB_backup_b_error_GetRawCode_0600
388  * @tc.name: b_error_GetRawCode_0600
389  * @tc.desc: Test function of GetRawCode interface for SUCCESS.
390  * @tc.size: MEDIUM
391  * @tc.type: FUNC
392  * @tc.level Level 0
393  * @tc.require: I6F3GV
394  */
395 HWTEST_F(BErrorTest, b_error_GetRawCode_0600, testing::ext::TestSize.Level0)
396 {
397     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetRawCode_0600";
398     BError be(BError::Codes::UTILS_INTERRUPTED_PROCESS);
399     BError::Codes result = be.GetRawCode();
400     EXPECT_EQ(result, BError::Codes::UTILS_INTERRUPTED_PROCESS);
401     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetRawCode_0600";
402 }
403 
404 /**
405  * @tc.number: SUB_backup_b_error_what_0100
406  * @tc.name: b_error_what_0100
407  * @tc.desc: Test function of what interface for SUCCESS.
408  * @tc.size: MEDIUM
409  * @tc.type: FUNC
410  * @tc.level Level 0
411  * @tc.require: I6F3GV
412  */
413 HWTEST_F(BErrorTest, b_error_what_0100, testing::ext::TestSize.Level0)
414 {
415     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_what_0100";
416     BError be(BError::Codes::OK);
417     auto result = be.what();
418     EXPECT_NE(result, nullptr);
419     GTEST_LOG_(INFO) << "BErrorTest-end b_error_what_0100";
420 }
421 
422 /**
423  * @tc.number: SUB_backup_b_error_bool_0100
424  * @tc.name: b_error_bool_0100
425  * @tc.desc: Test function of bool interface for SUCCESS.
426  * @tc.size: MEDIUM
427  * @tc.type: FUNC
428  * @tc.level Level 0
429  * @tc.require: I6F3GV
430  */
431 HWTEST_F(BErrorTest, b_error_bool_0100, testing::ext::TestSize.Level0)
432 {
433     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_bool_0100";
434     bool result = BError();
435     EXPECT_FALSE(result);
436     GTEST_LOG_(INFO) << "BErrorTest-end b_error_bool_0100";
437 }
438 
439 /**
440  * @tc.number: SUB_backup_b_error_int_0100
441  * @tc.name: b_error_int_0100
442  * @tc.desc: Test function of int interface for SUCCESS.
443  * @tc.size: MEDIUM
444  * @tc.type: FUNC
445  * @tc.level Level 0
446  * @tc.require: I6F3GV
447  */
448 HWTEST_F(BErrorTest, b_error_int_0100, testing::ext::TestSize.Level0)
449 {
450     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_int_0100";
451     int result = BError();
452     EXPECT_EQ(result, 0);
453     GTEST_LOG_(INFO) << "BErrorTest-end b_error_int_0100";
454 }
455 
456 /**
457  * @tc.number: SUB_backup_b_error_GetCodeByErrno_0100
458  * @tc.name: b_error_GetCodeByErrno_0100
459  * @tc.desc: Test function of int interface for SUCCESS.
460  * @tc.size: MEDIUM
461  * @tc.type: FUNC
462  * @tc.level Level 0
463  * @tc.require: I6F3GV
464  */
465 HWTEST_F(BErrorTest, b_error_GetCodeByErrno_0100, testing::ext::TestSize.Level0)
466 {
467     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCodeByErrno_0100";
468     int32_t errnoSys = 0;
469     int result = BError::GetCodeByErrno(errnoSys);
470     EXPECT_EQ(result, 0);
471     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCodeByErrno_0100";
472 }
473 
474 /**
475  * @tc.number: SUB_backup_b_error_GetCodeByErrno_0200
476  * @tc.name: b_error_GetCodeByErrno_0200
477  * @tc.desc: Test function of int interface for SUCCESS.
478  * @tc.size: MEDIUM
479  * @tc.type: FUNC
480  * @tc.level Level 0
481  * @tc.require: I6F3GV
482  */
483 HWTEST_F(BErrorTest, b_error_GetCodeByErrno_0200, testing::ext::TestSize.Level0)
484 {
485     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCodeByErrno_0200";
486     int32_t errnoSys = EPERM;
487     int result = BError::GetCodeByErrno(errnoSys);
488     EXPECT_EQ(result, BError::BackupErrorCode::E_PERM);
489     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCodeByErrno_0200";
490 }
491 
492 /**
493  * @tc.number: SUB_backup_b_error_GetCodeByErrno_0300
494  * @tc.name: b_error_GetCodeByErrno_0300
495  * @tc.desc: Test function of int interface for SUCCESS.
496  * @tc.size: MEDIUM
497  * @tc.type: FUNC
498  * @tc.level Level 0
499  * @tc.require: I6F3GV
500  */
501 HWTEST_F(BErrorTest, b_error_GetCodeByErrno_0300, testing::ext::TestSize.Level0)
502 {
503     GTEST_LOG_(INFO) << "BErrorTest-begin b_error_GetCodeByErrno_0300";
504     int32_t errnoSys = -EPERM;
505     int result = BError::GetCodeByErrno(errnoSys);
506     EXPECT_EQ(result, BError::BackupErrorCode::E_UKERR);
507     GTEST_LOG_(INFO) << "BErrorTest-end b_error_GetCodeByErrno_0300";
508 }
509 } // namespace OHOS::FileManagement::Backup