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
Init(Callbacks callbacks,std::string & errMsg,ErrCode & errCode)49 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks, std::string &errMsg, ErrCode &errCode)
50 {
51 try {
52 callbacks_ = callbacks;
53 auto backup = make_unique<BSessionBackup>();
54 return backup;
55 } catch (const exception &e) {
56 return nullptr;
57 }
58 return nullptr;
59 }
60
RegisterBackupServiceDied(function<void ()> functor)61 void BSessionBackup::RegisterBackupServiceDied(function<void()> functor) {}
62
Start()63 ErrCode BSessionBackup::Start()
64 {
65 callbacks_.onBundleStarted(0, "com.example.app2backup");
66
67 BFileInfo bFileInfo("com.example.app2backup", "manage.json", 0);
68 TestManager tm("BSessionBackupMock_GetFd_0100");
69 string fileManagePath = tm.GetRootDirCurTest().append("manage.json");
70 SaveStringToFile(fileManagePath, R"([{"fileName": "1.tar"}])");
71 UniqueFd fd(open(fileManagePath.data(), O_RDWR, S_IRWXU));
72 GTEST_LOG_(INFO) << "callbacks_::onFileReady manage.json";
73 callbacks_.onFileReady(bFileInfo, move(fd), 0);
74
75 string filePath = tm.GetRootDirCurTest().append("1.tar");
76 UniqueFd fdTar(open(filePath.data(), O_RDONLY | O_CREAT, S_IRWXU));
77 bFileInfo.fileName = "1.tar";
78 GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
79 callbacks_.onFileReady(bFileInfo, move(fdTar), 0);
80
81 callbacks_.onBundleFinished(0, "com.example.app2backup");
82
83 callbacks_.onAllBundlesFinished(0);
84 callbacks_.onBundleStarted(1, "com.example.app2backup");
85 callbacks_.onBundleFinished(1, "com.example.app2backup");
86 callbacks_.onAllBundlesFinished(1);
87
88 string filePathTwo = tm.GetRootDirCurTest().append("1.tar");
89 UniqueFd fdFile(open(filePathTwo.data(), O_RDONLY | O_CREAT, S_IRWXU));
90 GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
91 callbacks_.onFileReady(bFileInfo, move(fdFile), 0);
92
93 callbacks_.onBackupServiceDied();
94 return BError(BError::Codes::OK);
95 }
96
AppendBundles(vector<BundleName> bundlesToBackup)97 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup)
98 {
99 Start();
100 return BError(BError::Codes::OK);
101 }
102
Finish()103 ErrCode BSessionBackup::Finish()
104 {
105 return BError(BError::Codes::OK);
106 }
107
Release()108 ErrCode BSessionBackup::Release()
109 {
110 return BError(BError::Codes::OK);
111 }
112
Cancel(std::string bundleName)113 ErrCode BSessionBackup::Cancel(std::string bundleName)
114 {
115 return BError(BError::Codes::OK);
116 }
117 } // namespace OHOS::FileManagement::Backup