1 /*
2  * Copyright (c) 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 #include <memory>
19 #include <shared_mutex>
20 #include <vector>
21 
22 #include "b_error/b_error.h"
23 #include "b_file_info.h"
24 #include "b_session_restore_async.h"
25 #include "backup_kit_inner.h"
26 #include "unique_fd.h"
27 #include "utils_mock_global_variable.h"
28 
29 namespace OHOS::FileManagement::Backup {
30 using namespace std;
31 
OnFileReady(const BFileInfo & fileInfo,UniqueFd fd,int32_t errCode)32 static void OnFileReady(const BFileInfo &fileInfo, UniqueFd fd, int32_t errCode)
33 {
34     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnFileReady OK";
35 }
36 
OnBundleStarted(ErrCode err,const BundleName name)37 static void OnBundleStarted(ErrCode err, const BundleName name)
38 {
39     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBundleStarted OK";
40 }
41 
OnBundleFinished(ErrCode err,const BundleName name)42 static void OnBundleFinished(ErrCode err, const BundleName name)
43 {
44     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBundleFinished OK";
45 }
46 
OnAllBundlesFinished(ErrCode err)47 static void OnAllBundlesFinished(ErrCode err)
48 {
49     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnAllBundlesFinished OK";
50 }
51 
OnBackupServiceDied()52 static void OnBackupServiceDied()
53 {
54     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBackupServiceDied OK";
55 }
56 
57 class BSessionRestoreAsyncTest : public testing::Test {
58 public:
SetUpTestCase(void)59     static void SetUpTestCase(void) {};
TearDownTestCase()60     static void TearDownTestCase() {};
61     void SetUp() override;
62     void TearDown() override;
63 
64     void Init();
65 
66     shared_ptr<BSessionRestoreAsync> restorePtr_ = nullptr;
67     BSessionRestoreAsync::Callbacks callbacks_;
68 };
69 
SetUp()70 void BSessionRestoreAsyncTest::SetUp()
71 {
72     SetMockInitBackupOrRestoreSession(true);
73     SetMockGetInstance(true);
74     SetMockLoadSystemAbility(true);
75     restorePtr_ = make_shared<BSessionRestoreAsync>(callbacks_);
76 }
77 
TearDown()78 void BSessionRestoreAsyncTest::TearDown()
79 {
80     restorePtr_ = nullptr;
81 }
82 
Init()83 void BSessionRestoreAsyncTest::Init()
84 {
85     callbacks_.onFileReady = OnFileReady;
86     callbacks_.onBundleStarted = OnBundleStarted;
87     callbacks_.onBundleFinished = OnBundleFinished;
88     callbacks_.onAllBundlesFinished = OnAllBundlesFinished;
89     callbacks_.onBackupServiceDied = OnBackupServiceDied;
90 }
91 
92 /**
93  * @tc.number: SUB_backup_b_session_restore_async_0100
94  * @tc.name: SUB_backup_b_session_restore_async_0100
95  * @tc.desc: 测试Callbacks接口
96  * @tc.size: MEDIUM
97  * @tc.type: FUNC
98  * @tc.level Level 1
99  * @tc.require: I7L7A6
100  */
101 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0100, testing::ext::TestSize.Level1)
102 {
103     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0100";
104     try {
105         Init();
106         BFileInfo backupFile("", "", 0);
107         callbacks_.onFileReady(backupFile, UniqueFd(-1), 0);
108         callbacks_.onBundleStarted(ErrCode(BError::Codes::OK), "");
109         callbacks_.onBundleFinished(ErrCode(BError::Codes::OK), "");
110         callbacks_.onAllBundlesFinished(ErrCode(BError::Codes::OK));
111         callbacks_.onBackupServiceDied();
112     } catch (...) {
113         EXPECT_TRUE(false);
114         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by Callbacks.";
115     }
116     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0100";
117 }
118 
119 /**
120  * @tc.number: SUB_backup_b_session_restore_async_0200
121  * @tc.name: SUB_backup_b_session_restore_async_0200
122  * @tc.desc: 测试Init接口
123  * @tc.size: MEDIUM
124  * @tc.type: FUNC
125  * @tc.level Level 1
126  * @tc.require: I7L7A6
127  */
128 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0200, testing::ext::TestSize.Level1)
129 {
130     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0200";
131     try {
132         auto restorePtr = BSessionRestoreAsync::Init(callbacks_);
133         EXPECT_NE(restorePtr, nullptr);
134     } catch (...) {
135         EXPECT_TRUE(false);
136         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by Init.";
137     }
138     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0200";
139 }
140 
141 /**
142  * @tc.number: SUB_backup_b_session_restore_async_0300
143  * @tc.name: SUB_backup_b_session_restore_async_0300
144  * @tc.desc: 测试PublishFile接口
145  * @tc.size: MEDIUM
146  * @tc.type: FUNC
147  * @tc.level Level 1
148  * @tc.require: I7L7A6
149  */
150 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0300, testing::ext::TestSize.Level1)
151 {
152     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0300";
153     try {
154         if (restorePtr_ == nullptr) {
155             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_async_0300 restorePtr_ == nullptr";
156             return;
157         }
158         GTEST_LOG_(INFO) << "Mock instance set to false";
159         SetMockGetInstance(false);
160         BFileInfo bFileInfo("", "", 0);
161         auto ret = restorePtr_->PublishFile(bFileInfo);
162         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
163         GTEST_LOG_(INFO) << "Mock instance set to true";
164         SetMockGetInstance(true);
165         ret = restorePtr_->PublishFile(bFileInfo);
166         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
167     } catch (...) {
168         EXPECT_TRUE(false);
169         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by PublishFile.";
170     }
171     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0300";
172 }
173 
174 /**
175  * @tc.number: SUB_backup_b_session_restore_async_0400
176  * @tc.name: SUB_backup_b_session_restore_async_0400
177  * @tc.desc: 测试GetFileHandle接口
178  * @tc.size: MEDIUM
179  * @tc.type: FUNC
180  * @tc.level Level 1
181  * @tc.require: I7L7A6
182  */
183 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0400, testing::ext::TestSize.Level1)
184 {
185     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0400";
186     try {
187         if (restorePtr_ == nullptr) {
188             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_async_0400 restorePtr_ == nullptr";
189             return;
190         }
191         GTEST_LOG_(INFO) << "Mock instance set to false";
192         SetMockGetInstance(false);
193         string bundleName = "";
194         string fileName = "";
195         auto ret = restorePtr_->GetFileHandle(bundleName, fileName);
196         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
197         GTEST_LOG_(INFO) << "Mock instance set to true";
198         SetMockGetInstance(true);
199         ret = restorePtr_->GetFileHandle(bundleName, fileName);
200         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
201     } catch (...) {
202         EXPECT_TRUE(false);
203         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by GetFileHandle.";
204     }
205     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0400";
206 }
207 
208 /**
209  * @tc.number: SUB_backup_b_session_restore_async_0500
210  * @tc.name: SUB_backup_b_session_restore_async_0500
211  * @tc.desc: 测试AppendBundles接口
212  * @tc.size: MEDIUM
213  * @tc.type: FUNC
214  * @tc.level Level 1
215  * @tc.require: I7L7A6
216  */
217 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0500, testing::ext::TestSize.Level1)
218 {
219     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0500";
220     try {
221         if (restorePtr_ == nullptr) {
222             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_async_0500 restorePtr_ == nullptr";
223             return;
224         }
225         SetMockGetInstance(true);
226         SetMockLoadSystemAbility(true);
227         vector<string> bundleNames;
228         ErrCode ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames);
229         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
230         ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames);
231         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
232         SetMockGetInstance(false);
233         ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames);
234         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
235         restorePtr_ = nullptr;
236     } catch (...) {
237         EXPECT_TRUE(false);
238         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by ~BSessionRestoreAsync.";
239     }
240     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0500";
241 }
242 
243 /**
244  * @tc.number: SUB_backup_b_session_restore_async_0501
245  * @tc.name: SUB_backup_b_session_restore_async_0501
246  * @tc.desc: 测试AppendBundles接口
247  * @tc.size: MEDIUM
248  * @tc.type: FUNC
249  * @tc.level Level 1
250  * @tc.require: I7L7A6
251  */
252 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0501, testing::ext::TestSize.Level1)
253 {
254     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0501";
255     try {
256         if (restorePtr_ == nullptr) {
257             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_async_0501 restorePtr_ == nullptr";
258             return;
259         }
260         SetMockGetInstance(true);
261         SetMockLoadSystemAbility(true);
262         vector<string> bundleNames;
263         vector<string> detailInfos;
264         ErrCode ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames, detailInfos);
265         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
266         ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames, detailInfos);
267         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
268         SetMockGetInstance(false);
269         ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames, detailInfos);
270         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
271         restorePtr_ = nullptr;
272     } catch (...) {
273         EXPECT_TRUE(false);
274         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by ~BSessionRestoreAsync.";
275     }
276     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0501";
277 }
278 
279 /**
280  * @tc.number: SUB_backup_b_session_restore_async_0600
281  * @tc.name: SUB_backup_b_session_restore_async_0600
282  * @tc.desc: 测试RegisterBackupServiceDied接口
283  * @tc.size: MEDIUM
284  * @tc.type: FUNC
285  * @tc.level Level 1
286  * @tc.require: I7L7A6
287  */
288 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0600, testing::ext::TestSize.Level1)
289 {
290     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0600";
291     try {
292         if (restorePtr_ == nullptr) {
293             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_async_0600 restorePtr_ == nullptr";
294             return;
295         }
296         GTEST_LOG_(INFO) << "GetInstance is false";
297         SetMockGetInstance(false);
298         restorePtr_->RegisterBackupServiceDied(nullptr);
299         GTEST_LOG_(INFO) << "GetInstance is true but not equal to parameter";
300         SetMockGetInstance(true);
301         restorePtr_->RegisterBackupServiceDied(nullptr);
302         Init();
303         SetMockGetInstance(true);
304         restorePtr_->RegisterBackupServiceDied(callbacks_.onBackupServiceDied);
305     } catch (...) {
306         EXPECT_TRUE(false);
307         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by RegisterBackupServiceDied.";
308     }
309     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0600";
310 }
311 
312 /**
313  * @tc.number: SUB_backup_b_session_restore_async_0700
314  * @tc.name: SUB_backup_b_session_restore_async_0700
315  * @tc.desc: 测试Release接口
316  * @tc.size: MEDIUM
317  * @tc.type: FUNC
318  * @tc.level Level 1
319  * @tc.require: I7L7A6
320  */
321 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0700, testing::ext::TestSize.Level1)
322 {
323     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0700";
324     try {
325         if (restorePtr_ == nullptr) {
326             GTEST_LOG_(INFO) << "SUB_backup_b_session_restore_async_0700 restorePtr_ == nullptr";
327             return;
328         }
329         SetMockGetInstance(false);
330         ErrCode ret = restorePtr_->Release();
331         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
332         SetMockGetInstance(true);
333         ret = restorePtr_->Release();
334         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
335     } catch (...) {
336         EXPECT_TRUE(false);
337         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by ~BSessionRestoreAsync.";
338     }
339     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0700";
340 }
341 } // namespace OHOS::FileManagement::Backup