1 /*
2 * Copyright (c) 2022-2024 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 "b_session_backup.h"
17
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <file_ex.h>
23 #include <gtest/gtest.h>
24
25 #include "test_manager.h"
26 #include "b_error/b_error.h"
27
28 namespace OHOS::FileManagement::Backup {
29 using namespace std;
30
31 namespace {
32 static BSessionBackup::Callbacks callbacks_ = {};
33 } // namespace
34
~BSessionBackup()35 BSessionBackup::~BSessionBackup() {}
36
Init(Callbacks callbacks)37 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks)
38 {
39 try {
40 callbacks_ = callbacks;
41 auto backup = make_unique<BSessionBackup>();
42 return backup;
43 } catch (const exception &e) {
44 return nullptr;
45 }
46 return nullptr;
47 }
48
RegisterBackupServiceDied(function<void ()> functor)49 void BSessionBackup::RegisterBackupServiceDied(function<void()> functor) {}
50
Start()51 ErrCode BSessionBackup::Start()
52 {
53 callbacks_.onBundleStarted(0, "com.example.app2backup");
54
55 BFileInfo bFileInfo("com.example.app2backup", "manage.json", 0);
56 TestManager tm("BSessionBackupMock_GetFd_0100");
57 string fileManagePath = tm.GetRootDirCurTest().append("manage.json");
58 SaveStringToFile(fileManagePath, R"([{"fileName": "1.tar"}])");
59 UniqueFd fd(open(fileManagePath.data(), O_RDWR, S_IRWXU));
60 GTEST_LOG_(INFO) << "callbacks_::onFileReady manage.json";
61 callbacks_.onFileReady(bFileInfo, move(fd), 0);
62
63 string filePath = tm.GetRootDirCurTest().append("1.tar");
64 UniqueFd fdTar(open(filePath.data(), O_RDONLY | O_CREAT, S_IRWXU));
65 bFileInfo.fileName = "1.tar";
66 GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
67 callbacks_.onFileReady(bFileInfo, move(fdTar), 0);
68
69 callbacks_.onBundleFinished(0, "com.example.app2backup");
70
71 callbacks_.onAllBundlesFinished(0);
72 callbacks_.onBundleStarted(1, "com.example.app2backup");
73 callbacks_.onBundleFinished(1, "com.example.app2backup");
74 callbacks_.onAllBundlesFinished(1);
75
76 string filePathTwo = tm.GetRootDirCurTest().append("1.tar");
77 UniqueFd fdFile(open(filePathTwo.data(), O_RDONLY | O_CREAT, S_IRWXU));
78 GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
79 callbacks_.onFileReady(bFileInfo, move(fdFile), 0);
80
81 callbacks_.onBackupServiceDied();
82 return BError(BError::Codes::OK);
83 }
84
AppendBundles(vector<BundleName> bundlesToBackup)85 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup)
86 {
87 Start();
88 return BError(BError::Codes::OK);
89 }
90
Finish()91 ErrCode BSessionBackup::Finish()
92 {
93 return BError(BError::Codes::OK);
94 }
95
Release()96 ErrCode BSessionBackup::Release()
97 {
98 return BError(BError::Codes::OK);
99 }
100 } // namespace OHOS::FileManagement::Backup