• 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 
22 #include "module_ipc/service.h"
23 #include "service_reverse_mock.h"
24 #include "test_manager.h"
25 
26 namespace OHOS::FileManagement::Backup {
27 using namespace std;
28 using namespace testing;
29 
30 namespace {
31 const string BUNDLE_NAME = "com.example.app2backup";
32 const string MANAGE_JSON = "manage.json";
33 const string FILE_NAME = "1.tar";
34 constexpr int32_t SERVICE_ID = 5203;
35 } // namespace
36 
37 class ServiceTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase();
SetUp()41     void SetUp() {};
TearDown()42     void TearDown() {};
43 
44     ErrCode Init(IServiceReverse::Scenario scenario);
45 
46     static inline sptr<Service> servicePtr_ = nullptr;
47     static inline sptr<ServiceReverseMock> remote_ = nullptr;
48 };
49 
SetUpTestCase(void)50 void ServiceTest::SetUpTestCase(void)
51 {
52     GTEST_LOG_(INFO) << "SetUpTestCase enter";
53     servicePtr_ = sptr<Service>(new Service(SERVICE_ID));
54     remote_ = sptr(new ServiceReverseMock());
55 }
56 
TearDownTestCase()57 void ServiceTest::TearDownTestCase()
58 {
59     GTEST_LOG_(INFO) << "TearDownTestCase enter";
60     servicePtr_ = nullptr;
61     remote_ = nullptr;
62 }
63 
Init(IServiceReverse::Scenario scenario)64 ErrCode ServiceTest::Init(IServiceReverse::Scenario scenario)
65 {
66     vector<string> bundleNames;
67     bundleNames.emplace_back(BUNDLE_NAME);
68     ErrCode ret = 0;
69     if (scenario == IServiceReverse::Scenario::RESTORE) {
70         UniqueFd fd = servicePtr_->GetLocalCapabilities();
71         EXPECT_GT(fd, BError(BError::Codes::OK));
72         ret = servicePtr_->InitRestoreSession(remote_);
73         EXPECT_EQ(ret, BError(BError::Codes::OK));
74         ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames);
75         EXPECT_EQ(ret, BError(BError::Codes::OK));
76         ret = servicePtr_->Finish();
77         EXPECT_EQ(ret, BError(BError::Codes::OK));
78     } else if (scenario == IServiceReverse::Scenario::BACKUP) {
79         ret = servicePtr_->InitBackupSession(remote_);
80         EXPECT_EQ(ret, BError(BError::Codes::OK));
81         ret = servicePtr_->AppendBundlesBackupSession(bundleNames);
82         EXPECT_EQ(ret, BError(BError::Codes::OK));
83         ret = servicePtr_->Finish();
84         EXPECT_EQ(ret, BError(BError::Codes::OK));
85     }
86     return ret;
87 }
88 
89 /**
90  * @tc.number: SUB_Service_GetLocalCapabilities_0100
91  * @tc.name: SUB_Service_GetLocalCapabilities_0100
92  * @tc.desc: 测试 GetLocalCapabilities 获取本地能力文件
93  * @tc.size: MEDIUM
94  * @tc.type: FUNC
95  * @tc.level Level 1
96  * @tc.require: I6F3GV
97  */
98 HWTEST_F(ServiceTest, SUB_Service_GetLocalCapabilities_0100, testing::ext::TestSize.Level1)
99 {
100     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_GetLocalCapabilities_0100";
101     try {
102         UniqueFd fd = servicePtr_->GetLocalCapabilities();
103         EXPECT_GT(fd, BError(BError::Codes::OK));
104     } catch (...) {
105         EXPECT_TRUE(false);
106         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetLocalCapabilities.";
107     }
108     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetLocalCapabilities_0100";
109 }
110 
111 /**
112  * @tc.number: SUB_Service_OnStart_0100
113  * @tc.name: SUB_Service_OnStart_0100
114  * @tc.desc: 测试 OnStart 接口
115  * @tc.size: MEDIUM
116  * @tc.type: FUNC
117  * @tc.level Level 1
118  * @tc.require: I6F3GV
119  */
120 HWTEST_F(ServiceTest, SUB_Service_OnStart_0100, testing::ext::TestSize.Level1)
121 {
122     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_OnStart_0100";
123     try {
124         servicePtr_->OnStart();
125     } catch (...) {
126         EXPECT_TRUE(false);
127         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by OnStart.";
128     }
129     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_OnStart_0100";
130 }
131 
132 /**
133  * @tc.number: SUB_Service_Start_0100
134  * @tc.name: SUB_Service_Start_0100
135  * @tc.desc: 测试 Start 备份恢复启动
136  * @tc.size: MEDIUM
137  * @tc.type: FUNC
138  * @tc.level Level 1
139  * @tc.require: I6F3GV
140  */
141 HWTEST_F(ServiceTest, SUB_Service_Start_0100, testing::ext::TestSize.Level1)
142 {
143     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_Start_0100";
144     try {
145         auto ret = servicePtr_->Start();
146         EXPECT_EQ(ret, BError(BError::Codes::OK));
147     } catch (...) {
148         EXPECT_TRUE(false);
149         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by Start.";
150     }
151     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_Start_0100";
152 }
153 
154 /**
155  * @tc.number: SUB_Service_PublishFile_0100
156  * @tc.name: SUB_Service_PublishFile_0100
157  * @tc.desc: 测试 PublishFile 接口
158  * @tc.size: MEDIUM
159  * @tc.type: FUNC
160  * @tc.level Level 1
161  * @tc.require: I6F3GV
162  */
163 HWTEST_F(ServiceTest, SUB_Service_PublishFile_0100, testing::ext::TestSize.Level1)
164 {
165     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_PublishFile_0100";
166     try {
167         ErrCode ret = Init(IServiceReverse::Scenario::RESTORE);
168         EXPECT_EQ(ret, BError(BError::Codes::OK));
169         BFileInfo fileInfo {BUNDLE_NAME, FILE_NAME, 0};
170         ret = servicePtr_->PublishFile(fileInfo);
171         EXPECT_EQ(ret, BError(BError::Codes::OK));
172         GTEST_LOG_(INFO) << "ServiceTest-PublishFile Branches";
173         fileInfo.fileName = "test";
174         ret = servicePtr_->PublishFile(fileInfo);
175         EXPECT_NE(ret, BError(BError::Codes::OK));
176     } catch (...) {
177         EXPECT_TRUE(false);
178         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by PublishFile.";
179     }
180     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_PublishFile_0100";
181 }
182 
183 /**
184  * @tc.number: SUB_Service_AppFileReady_0100
185  * @tc.name: SUB_Service_AppFileReady_0100
186  * @tc.desc: 测试 AppFileReady 接口
187  * @tc.size: MEDIUM
188  * @tc.type: FUNC
189  * @tc.level Level 1
190  * @tc.require: I6F3GV
191  */
192 HWTEST_F(ServiceTest, SUB_Service_AppFileReady_0100, testing::ext::TestSize.Level1)
193 {
194     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_AppFileReady_0100";
195     try {
196         string fileName = MANAGE_JSON;
197         auto ret = servicePtr_->AppFileReady(fileName, UniqueFd(-1));
198         EXPECT_EQ(ret, BError(BError::Codes::OK));
199         GTEST_LOG_(INFO) << "ServiceTest-AppFileReady Branches";
200         fileName = "test";
201         ret = servicePtr_->AppFileReady(fileName, UniqueFd(-1));
202         EXPECT_EQ(ret, BError(BError::Codes::OK));
203     } catch (...) {
204         EXPECT_TRUE(false);
205         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by AppFileReady.";
206     }
207     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppFileReady_0100";
208 }
209 
210 /**
211  * @tc.number: SUB_Service_AppDone_0100
212  * @tc.name: SUB_Service_AppDone_0100
213  * @tc.desc: 测试 AppDone 接口
214  * @tc.size: MEDIUM
215  * @tc.type: FUNC
216  * @tc.level Level 1
217  * @tc.require: I6F3GV
218  */
219 HWTEST_F(ServiceTest, SUB_Service_AppDone_0100, testing::ext::TestSize.Level1)
220 {
221     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_AppDone_0100";
222     try {
223         GTEST_LOG_(INFO) << "SUB_Service_AppDone_0100 RESTORE";
224         auto ret = servicePtr_->AppDone(BError(BError::Codes::OK));
225         EXPECT_EQ(ret, BError(BError::Codes::OK));
226         GTEST_LOG_(INFO) << "SUB_Service_AppDone_0100 BACKUP";
227         ret = Init(IServiceReverse::Scenario::BACKUP);
228         EXPECT_EQ(ret, BError(BError::Codes::OK));
229         GTEST_LOG_(INFO) << "ServiceTest-AppDone Branches";
230         ret = servicePtr_->AppDone(1);
231         EXPECT_EQ(ret, BError(BError::Codes::OK));
232         GTEST_LOG_(INFO) << "ServiceTest-AppDone Branches End";
233         ret = servicePtr_->AppDone(BError(BError::Codes::OK));
234         EXPECT_EQ(ret, BError(BError::Codes::OK));
235     } catch (...) {
236         EXPECT_TRUE(false);
237         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by AppDone.";
238     }
239     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppDone_0100";
240 }
241 
242 /**
243  * @tc.number: SUB_Service_LaunchBackupExtension_0100
244  * @tc.name: SUB_Service_LaunchBackupExtension_0100
245  * @tc.desc: 测试 LaunchBackupExtension 接口
246  * @tc.size: MEDIUM
247  * @tc.type: FUNC
248  * @tc.level Level 1
249  * @tc.require: I6F3GV
250  */
251 HWTEST_F(ServiceTest, SUB_Service_LaunchBackupExtension_0100, testing::ext::TestSize.Level1)
252 {
253     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_LaunchBackupExtension_0100";
254     try {
255         GTEST_LOG_(INFO) << "SUB_Service_LaunchBackupExtension_0100 RESTORE";
256         ErrCode ret = Init(IServiceReverse::Scenario::RESTORE);
257         EXPECT_EQ(ret, BError(BError::Codes::OK));
258         ret = servicePtr_->LaunchBackupExtension(BUNDLE_NAME);
259         EXPECT_EQ(ret, BError(BError::Codes::OK));
260         GTEST_LOG_(INFO) << "SUB_Service_LaunchBackupExtension_0100 BACKUP";
261         ret = Init(IServiceReverse::Scenario::BACKUP);
262         EXPECT_EQ(ret, BError(BError::Codes::OK));
263         ret = servicePtr_->LaunchBackupExtension(BUNDLE_NAME);
264         EXPECT_EQ(ret, BError(BError::Codes::OK));
265     } catch (...) {
266         EXPECT_TRUE(false);
267         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by LaunchBackupExtension.";
268     }
269     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_LaunchBackupExtension_0100";
270 }
271 
272 /**
273  * @tc.number: SUB_Service_GetFileHandle_0100
274  * @tc.name: SUB_Service_GetFileHandle_0100
275  * @tc.desc: 测试 GetFileHandle 接口
276  * @tc.size: MEDIUM
277  * @tc.type: FUNC
278  * @tc.level Level 1
279  * @tc.require: I6F3GV
280  */
281 HWTEST_F(ServiceTest, SUB_Service_GetFileHandle_0100, testing::ext::TestSize.Level1)
282 {
283     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_GetFileHandle_0100";
284     try {
285         ErrCode ret = Init(IServiceReverse::Scenario::RESTORE);
286         EXPECT_EQ(ret, BError(BError::Codes::OK));
287 
288         string bundleName = BUNDLE_NAME;
289         string fileName = FILE_NAME;
290         ret = servicePtr_->GetFileHandle(bundleName, fileName);
291         EXPECT_EQ(ret, BError(BError::Codes::OK));
292     } catch (...) {
293         EXPECT_TRUE(false);
294         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetFileHandle.";
295     }
296     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetFileHandle_0100";
297 }
298 
299 /**
300  * @tc.number: SUB_Service_OnBackupExtensionDied_0100
301  * @tc.name: SUB_Service_OnBackupExtensionDied_0100
302  * @tc.desc: 测试 OnBackupExtensionDied 接口
303  * @tc.size: MEDIUM
304  * @tc.type: FUNC
305  * @tc.level Level 1
306  * @tc.require: I6F3GV
307  */
308 HWTEST_F(ServiceTest, SUB_Service_OnBackupExtensionDied_0100, testing::ext::TestSize.Level1)
309 {
310     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_OnBackupExtensionDied_0100";
311     try {
312         GTEST_LOG_(INFO) << "SUB_Service_OnBackupExtensionDied_0100 RESTORE";
313         ErrCode ret = Init(IServiceReverse::Scenario::RESTORE);
314         EXPECT_EQ(ret, BError(BError::Codes::OK));
315         string bundleName = BUNDLE_NAME;
316         servicePtr_->OnBackupExtensionDied(move(bundleName), BError(BError::Codes::OK));
317         GTEST_LOG_(INFO) << "SUB_Service_OnBackupExtensionDied_0100 BACKUP";
318         ret = Init(IServiceReverse::Scenario::BACKUP);
319         EXPECT_EQ(ret, BError(BError::Codes::OK));
320         bundleName = BUNDLE_NAME;
321         servicePtr_->OnBackupExtensionDied(move(bundleName), BError(BError::Codes::OK));
322     } catch (...) {
323         EXPECT_TRUE(false);
324         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetFileHandle.";
325     }
326     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_OnBackupExtensionDied_0100";
327 }
328 
329 /**
330  * @tc.number: SUB_Service_ExtStart_0100
331  * @tc.name: SUB_Service_ExtStart_0100
332  * @tc.desc: 测试 ExtStart 接口
333  * @tc.size: MEDIUM
334  * @tc.type: FUNC
335  * @tc.level Level 1
336  * @tc.require: I6F3GV
337  */
338 HWTEST_F(ServiceTest, SUB_Service_ExtStart_0100, testing::ext::TestSize.Level1)
339 {
340     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtStart_0100";
341     try {
342         GTEST_LOG_(INFO) << "SUB_Service_ExtStart_0100 BACKUP";
343         ErrCode ret = Init(IServiceReverse::Scenario::BACKUP);
344         EXPECT_EQ(ret, BError(BError::Codes::OK));
345         servicePtr_->ExtStart(BUNDLE_NAME);
346         GTEST_LOG_(INFO) << "ServiceTest-ExtStart BACKUP Branches";
347         servicePtr_->ExtStart(BUNDLE_NAME);
348         GTEST_LOG_(INFO) << "SUB_Service_ExtStart_0100 RESTORE";
349         ret = Init(IServiceReverse::Scenario::RESTORE);
350         servicePtr_->ExtStart(BUNDLE_NAME);
351     } catch (...) {
352         EXPECT_TRUE(false);
353         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by ExtStart.";
354     }
355     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_ExtStart_0100";
356 }
357 
358 /**
359  * @tc.number: SUB_Service_Dump_0100
360  * @tc.name: SUB_Service_Dump_0100
361  * @tc.desc: 测试 Dump 接口
362  * @tc.size: MEDIUM
363  * @tc.type: FUNC
364  * @tc.level Level 1
365  * @tc.require: I6F3GV
366  */
367 HWTEST_F(ServiceTest, SUB_Service_Dump_0100, testing::ext::TestSize.Level1)
368 {
369     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_Dump_0100";
370     try {
371         servicePtr_->Dump(-1, {});
372         TestManager tm("ServiceTest_GetFd_0100");
373         string filePath = tm.GetRootDirCurTest().append(FILE_NAME);
374         UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR));
375         servicePtr_->Dump(move(fd), {});
376     } catch (...) {
377         EXPECT_TRUE(false);
378         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by Dump.";
379     }
380     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_Dump_0100";
381 }
382 
383 /**
384  * @tc.number: SUB_Service_ExtConnectFailed_0100
385  * @tc.name: SUB_Service_ExtConnectFailed_0100
386  * @tc.desc: 测试 ExtConnectFailed 接口
387  * @tc.size: MEDIUM
388  * @tc.type: FUNC
389  * @tc.level Level 1
390  * @tc.require: I6F3GV
391  */
392 HWTEST_F(ServiceTest, SUB_Service_ExtConnectFailed_0100, testing::ext::TestSize.Level1)
393 {
394     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtConnectFailed_0100";
395     try {
396         GTEST_LOG_(INFO) << "SUB_Service_ExtConnectFailed_0100 RESTORE";
397         ErrCode ret = Init(IServiceReverse::Scenario::RESTORE);
398         EXPECT_EQ(ret, BError(BError::Codes::OK));
399         servicePtr_->ExtConnectFailed(BUNDLE_NAME, BError(BError::Codes::OK));
400         GTEST_LOG_(INFO) << "SUB_Service_ExtConnectFailed_0100 BACKUP";
401         ret = Init(IServiceReverse::Scenario::BACKUP);
402         EXPECT_EQ(ret, BError(BError::Codes::OK));
403         servicePtr_->ExtConnectFailed(BUNDLE_NAME, BError(BError::Codes::OK));
404     } catch (...) {
405         EXPECT_TRUE(false);
406         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by ExtConnectFailed.";
407     }
408     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_ExtConnectFailed_0100";
409 }
410 
411 /**
412  * @tc.number: SUB_Service_ExtConnectDone_0100
413  * @tc.name: SUB_Service_ExtConnectDone_0100
414  * @tc.desc: 测试 ExtConnectDone 接口
415  * @tc.size: MEDIUM
416  * @tc.type: FUNC
417  * @tc.level Level 1
418  * @tc.require: I6F3GV
419  */
420 HWTEST_F(ServiceTest, SUB_Service_ExtConnectDone_0100, testing::ext::TestSize.Level1)
421 {
422     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtConnectDone_0100";
423     try {
424         servicePtr_->ExtConnectDone(BUNDLE_NAME);
425     } catch (...) {
426         EXPECT_TRUE(false);
427         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by ExtConnectDone.";
428     }
429     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_ExtConnectDone_0100";
430 }
431 
432 /**
433  * @tc.number: SUB_Service_StopAll_0100
434  * @tc.name: SUB_Service_StopAll_0100
435  * @tc.desc: 测试 StopAll 接口
436  * @tc.size: MEDIUM
437  * @tc.type: FUNC
438  * @tc.level Level 1
439  * @tc.require: I6F3GV
440  */
441 HWTEST_F(ServiceTest, SUB_Service_StopAll_0100, testing::ext::TestSize.Level1)
442 {
443     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_StopAll_0100";
444     try {
445         servicePtr_->StopAll(nullptr, true);
446     } catch (...) {
447         EXPECT_TRUE(false);
448         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by StopAll.";
449     }
450     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_StopAll_0100";
451 }
452 
453 /**
454  * @tc.number: SUB_Service_OnStop_0100
455  * @tc.name: SUB_Service_OnStop_0100
456  * @tc.desc: 测试 OnStop 接口
457  * @tc.size: MEDIUM
458  * @tc.type: FUNC
459  * @tc.level Level 1
460  * @tc.require: I6F3GV
461  */
462 HWTEST_F(ServiceTest, SUB_Service_OnStop_0100, testing::ext::TestSize.Level1)
463 {
464     GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_OnStop_0100";
465     try {
466         servicePtr_->OnStop();
467     } catch (...) {
468         EXPECT_TRUE(false);
469         GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by OnStop.";
470     }
471     GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_OnStop_0100";
472 }
473 } // namespace OHOS::FileManagement::Backup