• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "backup_kit_inner.h"
25 #include "unique_fd.h"
26 #include "utils_mock_global_variable.h"
27 
28 namespace OHOS::FileManagement::Backup {
29 using namespace std;
30 
OnFileReady(const BFileInfo & fileInfo,UniqueFd fd)31 static void OnFileReady(const BFileInfo &fileInfo, UniqueFd fd)
32 {
33     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnFileReady OK";
34 }
35 
OnBundleStarted(ErrCode err,const BundleName name)36 static void OnBundleStarted(ErrCode err, const BundleName name)
37 {
38     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBundleStarted OK";
39 }
40 
OnBundleFinished(ErrCode err,const BundleName name)41 static void OnBundleFinished(ErrCode err, const BundleName name)
42 {
43     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBundleFinished OK";
44 }
45 
OnAllBundlesFinished(ErrCode err)46 static void OnAllBundlesFinished(ErrCode err)
47 {
48     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnAllBundlesFinished OK";
49 }
50 
OnBackupServiceDied()51 static void OnBackupServiceDied()
52 {
53     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBackupServiceDied OK";
54 }
55 
56 class BSessionRestoreAsyncTest : public testing::Test {
57 public:
SetUpTestCase(void)58     static void SetUpTestCase(void) {};
TearDownTestCase()59     static void TearDownTestCase() {};
60     void SetUp() override;
61     void TearDown() override;
62 
63     void Init();
64 
65     shared_ptr<BSessionRestoreAsync> restorePtr_;
66     BSessionRestoreAsync::Callbacks callbacks_;
67 };
68 
SetUp()69 void BSessionRestoreAsyncTest::SetUp()
70 {
71     SetMockInitBackupOrRestoreSession(true);
72     SetMockGetInstance(true);
73     SetMockLoadSystemAbility(true);
74     restorePtr_ = make_shared<BSessionRestoreAsync>(callbacks_);
75 }
76 
TearDown()77 void BSessionRestoreAsyncTest::TearDown()
78 {
79     restorePtr_ = nullptr;
80 }
81 
Init()82 void BSessionRestoreAsyncTest::Init()
83 {
84     callbacks_.onFileReady = OnFileReady;
85     callbacks_.onBundleStarted = OnBundleStarted;
86     callbacks_.onBundleFinished = OnBundleFinished;
87     callbacks_.onAllBundlesFinished = OnAllBundlesFinished;
88     callbacks_.onBackupServiceDied = OnBackupServiceDied;
89 }
90 
91 /**
92  * @tc.number: SUB_backup_b_session_restore_async_0100
93  * @tc.name: SUB_backup_b_session_restore_async_0100
94  * @tc.desc: 测试Callbacks接口
95  * @tc.size: MEDIUM
96  * @tc.type: FUNC
97  * @tc.level Level 1
98  * @tc.require: I7L7A6
99  */
100 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0100, testing::ext::TestSize.Level1)
101 {
102     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0100";
103     try {
104         Init();
105         BFileInfo bFileInfo("", "", 0);
106         callbacks_.onFileReady(bFileInfo, UniqueFd(-1));
107         callbacks_.onBundleStarted(ErrCode(BError::Codes::OK), "");
108         callbacks_.onBundleFinished(ErrCode(BError::Codes::OK), "");
109         callbacks_.onAllBundlesFinished(ErrCode(BError::Codes::OK));
110         callbacks_.onBackupServiceDied();
111     } catch (...) {
112         EXPECT_TRUE(false);
113         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by Callbacks.";
114     }
115     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0100";
116 }
117 
118 /**
119  * @tc.number: SUB_backup_b_session_restore_async_0200
120  * @tc.name: SUB_backup_b_session_restore_async_0200
121  * @tc.desc: 测试Init接口
122  * @tc.size: MEDIUM
123  * @tc.type: FUNC
124  * @tc.level Level 1
125  * @tc.require: I7L7A6
126  */
127 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0200, testing::ext::TestSize.Level1)
128 {
129     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0200";
130     try {
131         auto restorePtr = BSessionRestoreAsync::Init(callbacks_);
132         EXPECT_NE(restorePtr, nullptr);
133     } catch (...) {
134         EXPECT_TRUE(false);
135         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by Init.";
136     }
137     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0200";
138 }
139 
140 /**
141  * @tc.number: SUB_backup_b_session_restore_async_0300
142  * @tc.name: SUB_backup_b_session_restore_async_0300
143  * @tc.desc: 测试PublishFile接口
144  * @tc.size: MEDIUM
145  * @tc.type: FUNC
146  * @tc.level Level 1
147  * @tc.require: I7L7A6
148  */
149 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0300, testing::ext::TestSize.Level1)
150 {
151     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0300";
152     try {
153         GTEST_LOG_(INFO) << "GetInstance is false";
154         SetMockGetInstance(false);
155         BFileInfo bFileInfo("", "", 0);
156         auto ret = restorePtr_->PublishFile(bFileInfo);
157         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
158         GTEST_LOG_(INFO) << "GetInstance is true";
159         SetMockGetInstance(true);
160         ret = restorePtr_->PublishFile(bFileInfo);
161         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
162     } catch (...) {
163         EXPECT_TRUE(false);
164         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by PublishFile.";
165     }
166     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0300";
167 }
168 
169 /**
170  * @tc.number: SUB_backup_b_session_restore_async_0400
171  * @tc.name: SUB_backup_b_session_restore_async_0400
172  * @tc.desc: 测试GetFileHandle接口
173  * @tc.size: MEDIUM
174  * @tc.type: FUNC
175  * @tc.level Level 1
176  * @tc.require: I7L7A6
177  */
178 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0400, testing::ext::TestSize.Level1)
179 {
180     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0400";
181     try {
182         GTEST_LOG_(INFO) << "GetInstance is false";
183         SetMockGetInstance(false);
184         string bundleName = "";
185         string fileName = "";
186         auto ret = restorePtr_->GetFileHandle(bundleName, fileName);
187         EXPECT_NE(ret, ErrCode(BError::Codes::OK));
188         GTEST_LOG_(INFO) << "GetInstance is true";
189         SetMockGetInstance(true);
190         ret = restorePtr_->GetFileHandle(bundleName, fileName);
191         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
192     } catch (...) {
193         EXPECT_TRUE(false);
194         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by GetFileHandle.";
195     }
196     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0400";
197 }
198 
199 /**
200  * @tc.number: SUB_backup_b_session_restore_async_0500
201  * @tc.name: SUB_backup_b_session_restore_async_0500
202  * @tc.desc: 测试AppendBundles接口
203  * @tc.size: MEDIUM
204  * @tc.type: FUNC
205  * @tc.level Level 1
206  * @tc.require: I7L7A6
207  */
208 HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0500, testing::ext::TestSize.Level1)
209 {
210     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-begin SUB_backup_b_session_restore_async_0500";
211     try {
212         SetMockGetInstance(true);
213         SetMockLoadSystemAbility(true);
214         vector<string> bundleNames;
215         ErrCode ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames);
216         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
217         ret = restorePtr_->AppendBundles(UniqueFd(-1), bundleNames);
218         EXPECT_EQ(ret, ErrCode(BError::Codes::OK));
219         restorePtr_ = nullptr;
220     } catch (...) {
221         EXPECT_TRUE(false);
222         GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by ~BSessionRestoreAsync.";
223     }
224     GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-end SUB_backup_b_session_restore_async_0500";
225 }
226 } // namespace OHOS::FileManagement::Backup