• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "unique_fd.h"
23 #include "utils_mock_global_variable.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 
OnFileReady(const BFileInfo & fileInfo,UniqueFd fd)28 static void OnFileReady(const BFileInfo &fileInfo, UniqueFd fd)
29 {
30     GTEST_LOG_(INFO) << "BSessionRestoreTest OnFileReady OK";
31 }
32 
OnBundleStarted(ErrCode err,const BundleName name)33 static void OnBundleStarted(ErrCode err, const BundleName name)
34 {
35     GTEST_LOG_(INFO) << "BSessionRestoreTest OnBundleStarted OK";
36 }
37 
OnBundleFinished(ErrCode err,const BundleName name)38 static void OnBundleFinished(ErrCode err, const BundleName name)
39 {
40     GTEST_LOG_(INFO) << "BSessionRestoreTest OnBundleFinished OK";
41 }
42 
OnAllBundlesFinished(ErrCode err)43 static void OnAllBundlesFinished(ErrCode err)
44 {
45     GTEST_LOG_(INFO) << "BSessionRestoreTest OnAllBundlesFinished OK";
46 }
47 
OnBackupServiceDied()48 static void OnBackupServiceDied()
49 {
50     GTEST_LOG_(INFO) << "BSessionRestoreTest OnBackupServiceDied OK";
51 }
52 
53 class BSessionRestoreTest : public testing::Test {
54 public:
SetUpTestCase(void)55     static void SetUpTestCase(void) {};
TearDownTestCase()56     static void TearDownTestCase() {};
57     void SetUp() override;
58     void TearDown() override;
59 
60     void Init();
61 
62     unique_ptr<BSessionRestore> restorePtr_;
63     BSessionRestore::Callbacks callbacks_;
64 };
65 
SetUp()66 void BSessionRestoreTest::SetUp()
67 {
68     SetMockInitBackupOrRestoreSession(true);
69     SetMockGetInstance(true);
70     SetMockLoadSystemAbility(true);
71     restorePtr_ = unique_ptr<BSessionRestore>();
72 }
73 
TearDown()74 void BSessionRestoreTest::TearDown()
75 {
76     restorePtr_ = nullptr;
77 }
78 
Init()79 void BSessionRestoreTest::Init()
80 {
81     callbacks_.onFileReady = OnFileReady;
82     callbacks_.onBundleStarted = OnBundleStarted;
83     callbacks_.onBundleFinished = OnBundleFinished;
84     callbacks_.onAllBundlesFinished = OnAllBundlesFinished;
85     callbacks_.onBackupServiceDied = OnBackupServiceDied;
86 }
87 
88 /**
89  * @tc.number: SUB_backup_b_session_restore_0100
90  * @tc.name: SUB_backup_b_session_restore_0100
91  * @tc.desc: 测试Start接口
92  * @tc.size: MEDIUM
93  * @tc.type: FUNC
94  * @tc.level Level 1
95  * @tc.require: I6F3GV
96  */
97 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0100, testing::ext::TestSize.Level1)
98 {
99     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0100";
100     try {
101         GTEST_LOG_(INFO) << "GetInstance is true";
102         auto ret = restorePtr_->Start();
103         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
104         GTEST_LOG_(INFO) << "GetInstance is false";
105         SetMockGetInstance(false);
106         ret = restorePtr_->Start();
107         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
108     } catch (...) {
109         EXPECT_TRUE(false);
110         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Start.";
111     }
112     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0100";
113 }
114 
115 /**
116  * @tc.number: SUB_backup_b_session_restore_0200
117  * @tc.name: SUB_backup_b_session_restore_0200
118  * @tc.desc: 测试Callbacks接口
119  * @tc.size: MEDIUM
120  * @tc.type: FUNC
121  * @tc.level Level 1
122  * @tc.require: I6F3GV
123  */
124 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0200, testing::ext::TestSize.Level1)
125 {
126     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0200";
127     try {
128         Init();
129         BFileInfo bFileInfo("", "", 0);
130         callbacks_.onFileReady(bFileInfo, UniqueFd(-1));
131         callbacks_.onBundleStarted(ErrCode(BError::Codes::OK), "");
132         callbacks_.onBundleFinished(ErrCode(BError::Codes::OK), "");
133         callbacks_.onAllBundlesFinished(ErrCode(BError::Codes::OK));
134         callbacks_.onBackupServiceDied();
135     } catch (...) {
136         EXPECT_TRUE(false);
137         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Callbacks.";
138     }
139     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0200";
140 }
141 
142 /**
143  * @tc.number: SUB_backup_b_session_restore_0300
144  * @tc.name: SUB_backup_b_session_restore_0300
145  * @tc.desc: 测试Init接口
146  * @tc.size: MEDIUM
147  * @tc.type: FUNC
148  * @tc.level Level 1
149  * @tc.require: I6F3GV
150  */
151 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0300, testing::ext::TestSize.Level1)
152 {
153     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0300";
154     try {
155         GTEST_LOG_(INFO) << "GetInstance is false";
156         SetMockGetInstance(false);
157         vector<string> bundlesToRestore;
158         auto restorePtr = BSessionRestore::Init({});
159         EXPECT_EQ(restorePtr, nullptr);
160         GTEST_LOG_(INFO) << "GetInstance is true";
161         GTEST_LOG_(INFO) << "InitBackupSession is false";
162         SetMockGetInstance(true);
163         SetMockInitBackupOrRestoreSession(false);
164         restorePtr = BSessionRestore::Init({});
165         EXPECT_EQ(restorePtr, nullptr);
166         GTEST_LOG_(INFO) << "InitBackupSession is true";
167         SetMockInitBackupOrRestoreSession(true);
168         Init();
169         restorePtr = BSessionRestore::Init(callbacks_);
170         EXPECT_NE(restorePtr, nullptr);
171     } catch (...) {
172         EXPECT_TRUE(false);
173         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by Init.";
174     }
175     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0300";
176 }
177 
178 /**
179  * @tc.number: SUB_backup_b_session_restore_0500
180  * @tc.name: SUB_backup_b_session_restore_0500
181  * @tc.desc: 测试PublishFile接口
182  * @tc.size: MEDIUM
183  * @tc.type: FUNC
184  * @tc.level Level 1
185  * @tc.require: I6F3GV
186  */
187 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0500, testing::ext::TestSize.Level1)
188 {
189     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0500";
190     try {
191         GTEST_LOG_(INFO) << "GetInstance is false";
192         SetMockGetInstance(false);
193         BFileInfo bFileInfo("", "", 0);
194         auto ret = restorePtr_->PublishFile(bFileInfo);
195         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
196         GTEST_LOG_(INFO) << "GetInstance is true";
197         SetMockGetInstance(true);
198         ret = restorePtr_->PublishFile(bFileInfo);
199         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
200     } catch (...) {
201         EXPECT_TRUE(false);
202         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by PublishFile.";
203     }
204     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0500";
205 }
206 
207 /**
208  * @tc.number: SUB_backup_b_session_restore_0600
209  * @tc.name: SUB_backup_b_session_restore_0600
210  * @tc.desc: 测试GetFileHandle接口
211  * @tc.size: MEDIUM
212  * @tc.type: FUNC
213  * @tc.level Level 1
214  * @tc.require: I6F3GV
215  */
216 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0600, testing::ext::TestSize.Level1)
217 {
218     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0600";
219     try {
220         GTEST_LOG_(INFO) << "GetInstance is false";
221         SetMockGetInstance(false);
222         string bundleName = "";
223         string fileName = "";
224         auto ret = restorePtr_->GetFileHandle(bundleName, fileName);
225         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
226         GTEST_LOG_(INFO) << "GetInstance is true";
227         SetMockGetInstance(true);
228         ret = restorePtr_->GetFileHandle(bundleName, fileName);
229         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
230     } catch (...) {
231         EXPECT_TRUE(false);
232         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by GetFileHandle.";
233     }
234     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0600";
235 }
236 
237 /**
238  * @tc.number: SUB_backup_b_session_restore_0700
239  * @tc.name: SUB_backup_b_session_restore_0700
240  * @tc.desc: 测试RegisterBackupServiceDied接口
241  * @tc.size: MEDIUM
242  * @tc.type: FUNC
243  * @tc.level Level 1
244  * @tc.require: I6F3GV
245  */
246 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0700, testing::ext::TestSize.Level1)
247 {
248     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0700";
249     try {
250         GTEST_LOG_(INFO) << "GetInstance is false";
251         SetMockGetInstance(false);
252         restorePtr_->RegisterBackupServiceDied(nullptr);
253         GTEST_LOG_(INFO) << "GetInstance is true";
254         SetMockGetInstance(true);
255         restorePtr_->RegisterBackupServiceDied(nullptr);
256     } catch (...) {
257         EXPECT_TRUE(false);
258         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by RegisterBackupServiceDied.";
259     }
260     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0700";
261 }
262 
263 /**
264  * @tc.number: SUB_backup_b_session_restore_0800
265  * @tc.name: SUB_backup_b_session_restore_0800
266  * @tc.desc: 测试析构流程接口
267  * @tc.size: MEDIUM
268  * @tc.type: FUNC
269  * @tc.level Level 1
270  * @tc.require: I6F3GV
271  */
272 HWTEST_F(BSessionRestoreTest, SUB_backup_b_session_restore_0800, testing::ext::TestSize.Level1)
273 {
274     GTEST_LOG_(INFO) << "BSessionRestoreTest-begin SUB_backup_b_session_restore_0800";
275     try {
276         SetMockGetInstance(true);
277         SetMockLoadSystemAbility(true);
278         Init();
279         auto restorePtr = BSessionRestore::Init(callbacks_);
280         EXPECT_NE(restorePtr, nullptr);
281 
282         GTEST_LOG_(INFO) << "GetInstance is false";
283         SetMockGetInstance(false);
284         restorePtr = nullptr;
285 
286         SetMockGetInstance(true);
287         SetMockLoadSystemAbility(true);
288         restorePtr = BSessionRestore::Init(callbacks_);
289         EXPECT_NE(restorePtr, nullptr);
290 
291         GTEST_LOG_(INFO) << "LoadSystemAbility is false";
292         SetMockLoadSystemAbility(false);
293         restorePtr = nullptr;
294     } catch (...) {
295         EXPECT_TRUE(false);
296         GTEST_LOG_(INFO) << "BSessionRestoreTest-an exception occurred by ~BSessionRestore.";
297     }
298     GTEST_LOG_(INFO) << "BSessionRestoreTest-end SUB_backup_b_session_restore_0800";
299 }
300 } // namespace OHOS::FileManagement::Backup