1 /*
2  * Copyright (c) 2022-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 <cstdint>
17 #include <gtest/gtest.h>
18 
19 #include "b_error/b_error.h"
20 #include "b_file_info.h"
21 #include "backup_kit_inner.h"
22 #include "test_manager.h"
23 #include "unique_fd.h"
24 #include "utils_mock_global_variable.h"
25 
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 
30 namespace OHOS::FileManagement::Backup {
31 using namespace std;
32 
OnFileReady(const BFileInfo & fileInfo,UniqueFd fd,int32_t errCode)33 static void OnFileReady(const BFileInfo &fileInfo, UniqueFd fd, int32_t errCode)
34 {
35     GTEST_LOG_(INFO) << "BSessionRestoreTest OnFileReady OK";
36 }
37 
OnBundleStarted(ErrCode err,const BundleName name)38 static void OnBundleStarted(ErrCode err, const BundleName name)
39 {
40     GTEST_LOG_(INFO) << "BSessionRestoreTest OnBundleStarted OK";
41 }
42 
OnBundleFinished(ErrCode err,const BundleName name)43 static void OnBundleFinished(ErrCode err, const BundleName name)
44 {
45     GTEST_LOG_(INFO) << "BSessionRestoreTest OnBundleFinished OK";
46 }
47 
OnAllBundlesFinished(ErrCode err)48 static void OnAllBundlesFinished(ErrCode err)
49 {
50     GTEST_LOG_(INFO) << "BSessionRestoreTest OnAllBundlesFinished OK";
51 }
52 
OnBackupServiceDied()53 static void OnBackupServiceDied()
54 {
55     GTEST_LOG_(INFO) << "BSessionRestoreTest OnBackupServiceDied OK";
56 }
57 
58 class BSessionRestoreTest : public testing::Test {
59 public:
SetUpTestCase(void)60     static void SetUpTestCase(void) {};
TearDownTestCase()61     static void TearDownTestCase() {};
62     void SetUp() override;
63     void TearDown() override;
64 
65     void Init();
66 
67     unique_ptr<BSessionRestore> restorePtr_ = nullptr;
68     BSessionRestore::Callbacks callbacks_;
69 };
70 
SetUp()71 void BSessionRestoreTest::SetUp()
72 {
73     SetMockInitBackupOrRestoreSession(true);
74     SetMockGetInstance(true);
75     SetMockLoadSystemAbility(true);
76     restorePtr_ = unique_ptr<BSessionRestore>();
77 }
78 
TearDown()79 void BSessionRestoreTest::TearDown()
80 {
81     restorePtr_ = nullptr;
82 }
83 
Init()84 void BSessionRestoreTest::Init()
85 {
86     callbacks_.onFileReady = OnFileReady;
87     callbacks_.onBundleStarted = OnBundleStarted;
88     callbacks_.onBundleFinished = OnBundleFinished;
89     callbacks_.onAllBundlesFinished = OnAllBundlesFinished;
90     callbacks_.onBackupServiceDied = OnBackupServiceDied;
91 }
92 
93 /**
94  * @tc.number: SUB_backup_b_session_restore_0100
95  * @tc.name: SUB_backup_b_session_restore_0100
96  * @tc.desc: 测试Start接口
97  * @tc.size: MEDIUM
98  * @tc.type: FUNC
99  * @tc.level Level 1
100  * @tc.require: I6F3GV
101  */
102 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0100, testing::ext::TestSize.Level1)
103 {
104     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0100";
105     try {
106         GTEST_LOG_(INFO) << "GetInstance is true";
107         if (restorePtr_ == nullptr) {
108             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0100 restorePtr_ == nullptr";
109             return;
110         }
111         auto ret = restorePtr_->Start();
112         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
113         GTEST_LOG_(INFO) << "GetInstance is false";
114         SetMockGetInstance(false);
115         ret = restorePtr_->Start();
116         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
117     } catch (...) {
118         EXPECT_TRUE(false);
119         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Start.";
120     }
121     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0100";
122 }
123 
124 /**
125  * @tc.number: SUB_backup_b_session_restore_0200
126  * @tc.name: SUB_backup_b_session_restore_0200
127  * @tc.desc: 测试Callbacks接口
128  * @tc.size: MEDIUM
129  * @tc.type: FUNC
130  * @tc.level Level 1
131  * @tc.require: I6F3GV
132  */
133 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0200, testing::ext::TestSize.Level1)
134 {
135     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0200";
136     try {
137         GTEST_LOG_(INFO) << "GetInstance is true";
138         if (restorePtr_ == nullptr) {
139             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0200 restorePtr_ == nullptr";
140             return;
141         }
142         Init();
143         BFileInfo bFileInfo("", "", 0);
144         callbacks_.onFileReady(bFileInfo, UniqueFd(-1), 0);
145         callbacks_.onBundleStarted(ErrCode(BError::Codes::OK), "");
146         callbacks_.onBundleFinished(ErrCode(BError::Codes::OK), "");
147         callbacks_.onAllBundlesFinished(ErrCode(BError::Codes::OK));
148         callbacks_.onBackupServiceDied();
149     } catch (...) {
150         EXPECT_TRUE(false);
151         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Callbacks.";
152     }
153     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0200";
154 }
155 
156 /**
157  * @tc.number: SUB_backup_b_session_restore_0300
158  * @tc.name: SUB_backup_b_session_restore_0300
159  * @tc.desc: 测试Init接口
160  * @tc.size: MEDIUM
161  * @tc.type: FUNC
162  * @tc.level Level 1
163  * @tc.require: I6F3GV
164  */
165 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0300, testing::ext::TestSize.Level1)
166 {
167     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0300";
168     try {
169         GTEST_LOG_(INFO) << "GetInstance is true";
170         if (restorePtr_ == nullptr) {
171             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0300 restorePtr_ == nullptr";
172             return;
173         }
174         GTEST_LOG_(INFO) << "GetInstance is false";
175         SetMockGetInstance(false);
176         vector<string> bundlesToRestore;
177         auto restorePtr = BSessionRestore::Init({});
178         EXPECT_EQ(restorePtr, nullptr);
179         GTEST_LOG_(INFO) << "GetInstance is true";
180         GTEST_LOG_(INFO) << "InitBackupSession is false";
181         SetMockGetInstance(true);
182         SetMockInitBackupOrRestoreSession(false);
183         restorePtr = BSessionRestore::Init({});
184         EXPECT_EQ(restorePtr, nullptr);
185         GTEST_LOG_(INFO) << "InitBackupSession is true";
186         SetMockInitBackupOrRestoreSession(true);
187         Init();
188         restorePtr = BSessionRestore::Init(callbacks_);
189         EXPECT_NE(restorePtr, nullptr);
190     } catch (...) {
191         EXPECT_TRUE(false);
192         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Init.";
193     }
194     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0300";
195 }
196 
197 /**
198  * @tc.number: SUB_backup_b_session_restore_0400
199  * @tc.name: SUB_backup_b_session_restore_0400
200  * @tc.desc: 测试PublishFile接口
201  * @tc.size: MEDIUM
202  * @tc.type: FUNC
203  * @tc.level Level 1
204  * @tc.require: I6F3GV
205  */
206 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0400, testing::ext::TestSize.Level1)
207 {
208     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0400";
209     try {
210         if (restorePtr_ == nullptr) {
211             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0400 restorePtr_ == nullptr";
212             return;
213         }
214         GTEST_LOG_(INFO) << "GetInstance is false";
215         SetMockGetInstance(false);
216         BFileInfo bFileInfo("", "", 0);
217         auto ret = restorePtr_->PublishFile(bFileInfo);
218         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
219         GTEST_LOG_(INFO) << "GetInstance is true";
220         SetMockGetInstance(true);
221         ret = restorePtr_->PublishFile(bFileInfo);
222         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
223     } catch (...) {
224         EXPECT_TRUE(false);
225         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by PublishFile.";
226     }
227     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0400";
228 }
229 
230 /**
231  * @tc.number: SUB_backup_b_session_restore_0500
232  * @tc.name: SUB_backup_b_session_restore_0500
233  * @tc.desc: 测试GetFileHandle接口
234  * @tc.size: MEDIUM
235  * @tc.type: FUNC
236  * @tc.level Level 1
237  * @tc.require: I6F3GV
238  */
239 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0500, testing::ext::TestSize.Level1)
240 {
241     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0500";
242     try {
243         if (restorePtr_ == nullptr) {
244             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0500 restorePtr_ == nullptr";
245             return;
246         }
247         GTEST_LOG_(INFO) << "GetInstance is false";
248         SetMockGetInstance(false);
249         string bundleName = "";
250         string fileName = "";
251         auto ret = restorePtr_->GetFileHandle(bundleName, fileName);
252         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
253         GTEST_LOG_(INFO) << "GetInstance is true";
254         SetMockGetInstance(true);
255         ret = restorePtr_->GetFileHandle(bundleName, fileName);
256         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
257     } catch (...) {
258         EXPECT_TRUE(false);
259         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by GetFileHandle.";
260     }
261     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0500";
262 }
263 
264 /**
265  * @tc.number: SUB_backup_b_session_restore_0600
266  * @tc.name: SUB_backup_b_session_restore_0600
267  * @tc.desc: 测试AppendBundles接口
268  * @tc.size: MEDIUM
269  * @tc.type: FUNC
270  * @tc.level Level 1
271  * @tc.require: I6F3GV
272  */
273 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0600, testing::ext::TestSize.Level1)
274 {
275     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0600";
276     try {
277         if (restorePtr_ == nullptr) {
278             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0600 restorePtr_ == nullptr";
279             return;
280         }
281         const string fileName = "1.tar";
282         TestManager tm("SUB_backup_b_session_restore_0600");
283         string filePath = tm.GetRootDirCurTest().append(fileName);
284         UniqueFd remoteCap(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR));
285         string bundleName = "";
286         vector<string> bundlesToRestore;
287         vector<string> detailInfos;
288         bundlesToRestore.emplace_back(bundleName);
289 
290         GTEST_LOG_(INFO) << "GetInstance is false";
291         SetMockGetInstance(false);
292         auto ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos);
293         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
294 
295         GTEST_LOG_(INFO) << "GetInstance is true";
296         SetMockGetInstance(true);
297         ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos);
298         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
299     } catch (...) {
300         EXPECT_TRUE(false);
301         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by AppendBundles.";
302     }
303     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0600";
304 }
305 
306 /**
307  * @tc.number: SUB_backup_b_session_restore_0601
308  * @tc.name: SUB_backup_b_session_restore_0601
309  * @tc.desc: 测试AppendBundles接口
310  * @tc.size: MEDIUM
311  * @tc.type: FUNC
312  * @tc.level Level 1
313  * @tc.require: I6F3GV
314  */
315 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0601, testing::ext::TestSize.Level1)
316 {
317     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0601";
318     try {
319         if (restorePtr_ == nullptr) {
320             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0601 restorePtr_ == nullptr";
321             return;
322         }
323         const string fileName = "1.tar";
324         TestManager tm("SUB_backup_b_session_restore_0601");
325         string filePath = tm.GetRootDirCurTest().append(fileName);
326         UniqueFd remoteCap(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR));
327         string bundleName = "";
328         vector<string> bundlesToRestore;
329         bundlesToRestore.emplace_back(bundleName);
330         GTEST_LOG_(INFO) << "GetInstance is false";
331         SetMockGetInstance(false);
332         auto ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore);
333         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
334         GTEST_LOG_(INFO) << "GetInstance is true";
335         SetMockGetInstance(true);
336         ret = restorePtr_->AppendBundles(move(remoteCap), bundlesToRestore);
337         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
338     } catch (...) {
339         EXPECT_TRUE(false);
340         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by AppendBundles.";
341     }
342     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0601";
343 }
344 
345 /**
346  * @tc.number: SUB_backup_b_session_restore_0700
347  * @tc.name: SUB_backup_b_session_restore_0700
348  * @tc.desc: 测试Finish接口
349  * @tc.size: MEDIUM
350  * @tc.type: FUNC
351  * @tc.level Level 1
352  * @tc.require: I6F3GV
353  */
354 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0700, testing::ext::TestSize.Level1)
355 {
356     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0700";
357     try {
358         if (restorePtr_ == nullptr) {
359             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0700 restorePtr_ == nullptr";
360             return;
361         }
362         GTEST_LOG_(INFO) << "GetInstance is false";
363         SetMockGetInstance(false);
364         auto ret = restorePtr_->Finish();
365         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
366 
367         GTEST_LOG_(INFO) << "GetInstance is true";
368         SetMockGetInstance(true);
369         ret = restorePtr_->Finish();
370         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
371     } catch (...) {
372         EXPECT_TRUE(false);
373         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Finish.";
374     }
375     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0700";
376 }
377 
378 /**
379  * @tc.number: SUB_backup_b_session_restore_0800
380  * @tc.name: SUB_backup_b_session_restore_0800
381  * @tc.desc: 测试RegisterBackupServiceDied接口
382  * @tc.size: MEDIUM
383  * @tc.type: FUNC
384  * @tc.level Level 1
385  * @tc.require: I6F3GV
386  */
387 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0800, testing::ext::TestSize.Level1)
388 {
389     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0800";
390     try {
391         if (restorePtr_ == nullptr) {
392             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0800 restorePtr_ == nullptr";
393             return;
394         }
395         GTEST_LOG_(INFO) << "GetInstance is false";
396         SetMockGetInstance(false);
397         restorePtr_->RegisterBackupServiceDied(nullptr);
398         GTEST_LOG_(INFO) << "GetInstance is true but not equal to parameter";
399         SetMockGetInstance(true);
400         restorePtr_->RegisterBackupServiceDied(nullptr);
401     } catch (...) {
402         EXPECT_TRUE(false);
403         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by RegisterBackupServiceDied.";
404     }
405     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0800";
406 }
407 
408 /**
409  * @tc.number: SUB_backup_b_session_restore_0900
410  * @tc.name: SUB_backup_b_session_restore_0900
411  * @tc.desc: 测试析构流程接口
412  * @tc.size: MEDIUM
413  * @tc.type: FUNC
414  * @tc.level Level 1
415  * @tc.require: I6F3GV
416  */
417 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0900, testing::ext::TestSize.Level1)
418 {
419     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0900";
420     try {
421         if (restorePtr_ == nullptr) {
422             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_0900 restorePtr_ == nullptr";
423             return;
424         }
425         SetMockGetInstance(true);
426         SetMockLoadSystemAbility(true);
427         Init();
428         auto restorePtr = BSessionRestore::Init(callbacks_);
429         EXPECT_NE(restorePtr, nullptr);
430 
431         GTEST_LOG_(INFO) << "GetInstance is false";
432         SetMockGetInstance(false);
433         restorePtr = nullptr;
434 
435         SetMockGetInstance(true);
436         SetMockLoadSystemAbility(true);
437         restorePtr = BSessionRestore::Init(callbacks_);
438         EXPECT_NE(restorePtr, nullptr);
439 
440         GTEST_LOG_(INFO) << "LoadSystemAbility is false";
441         SetMockLoadSystemAbility(false);
442         restorePtr = nullptr;
443     } catch (...) {
444         EXPECT_TRUE(false);
445         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by ~BSessionRestore.";
446     }
447     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0900";
448 }
449 } // namespace OHOS::FileManagement::Backup