1 /*
2  * Copyright (c) 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_restore_async.h"
17 
18 #include <fcntl.h>
19 #include <memory>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <file_ex.h>
24 #include <gtest/gtest.h>
25 
26 #include "b_error/b_error.h"
27 #include "test_manager.h"
28 
29 namespace OHOS::FileManagement::Backup {
30 using namespace std;
31 
32 namespace {
33 static BSessionRestoreAsync::Callbacks callbacks_ = {};
34 static vector<BundleName> bundlesToRestore_ = {};
35 } // namespace
36 
~BSessionRestoreAsync()37 BSessionRestoreAsync::~BSessionRestoreAsync() {}
38 
Init(Callbacks callbacks)39 shared_ptr<BSessionRestoreAsync> BSessionRestoreAsync::Init(Callbacks callbacks)
40 {
41     GTEST_LOG_(INFO) << "BSessionRestoreAsync::Init";
42     try {
43         auto restore = make_shared<BSessionRestoreAsync>(callbacks);
44         return restore;
45     } catch (const exception &e) {
46         return nullptr;
47     }
48     return nullptr;
49 }
50 
PublishFile(BFileInfo fileInfo)51 ErrCode BSessionRestoreAsync::PublishFile(BFileInfo fileInfo)
52 {
53     return BError(BError::Codes::OK);
54 }
55 
GetFileHandle(const string & bundleName,const string & fileName)56 ErrCode BSessionRestoreAsync::GetFileHandle(const string &bundleName, const string &fileName)
57 {
58     return BError(BError::Codes::OK);
59 }
60 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,vector<string> detailInfos,RestoreTypeEnum restoreType,int32_t userId)61 ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore,
62     vector<string> detailInfos, RestoreTypeEnum restoreType, int32_t userId)
63 {
64     GTEST_LOG_(INFO) << "BSessionRestoreAsync::AppendBundles";
65     if (restoreType == RestoreTypeEnum::RESTORE_DATA_READDY) {
66         callbacks_.onBackupServiceDied();
67         return BError(BError::Codes::OK);
68     }
69     callbacks_.onBundleStarted(0, "com.example.app2backup");
70 
71     BFileInfo bFileInfo("com.example.app2backup", "1.tar", 0);
72     TestManager tm("BSessionRestoreAsyncMock_GetFd_0100");
73     string filePath = tm.GetRootDirCurTest().append("1.tar");
74     UniqueFd fd(open(filePath.data(), O_RDWR | O_CREAT, S_IRWXU));
75     GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
76     callbacks_.onFileReady(bFileInfo, move(fd), 0);
77 
78     string fileManagePath = tm.GetRootDirCurTest().append("manage.json");
79     UniqueFd fdManage(open(fileManagePath.data(), O_RDWR | O_CREAT, S_IRWXU));
80     bFileInfo.fileName = "manage.json";
81     GTEST_LOG_(INFO) << "File ready callback for manage.json";
82     callbacks_.onFileReady(bFileInfo, move(fdManage), 0);
83     callbacks_.onBundleFinished(0, "com.example.app2backup");
84 
85     callbacks_.onAllBundlesFinished(0);
86     callbacks_.onBundleStarted(1, "com.example.app2backup");
87     callbacks_.onBundleFinished(1, "com.example.app2backup");
88     callbacks_.onAllBundlesFinished(1);
89     callbacks_.onResultReport("com.example.app2backup", "ok");
90     callbacks_.onBackupServiceDied();
91     callbacks_.onProcess("com.example.app2backup", "100");
92     return BError(BError::Codes::OK);
93 }
94 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,RestoreTypeEnum restoreType,int32_t userId)95 ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap,
96                                             vector<BundleName> bundlesToRestore,
97                                             RestoreTypeEnum restoreType,
98                                             int32_t userId)
99 {
100     GTEST_LOG_(INFO) << "BSessionRestoreAsync::AppendBundles";
101     if (restoreType == RestoreTypeEnum::RESTORE_DATA_READDY) {
102         callbacks_.onBackupServiceDied();
103         return BError(BError::Codes::OK);
104     }
105     callbacks_.onBundleStarted(0, "com.example.app2backup");
106 
107     BFileInfo bFileInfo("com.example.app2backup", "1.tar", 0);
108     TestManager tm("BSessionRestoreAsyncMock_GetFd_0100");
109     string filePath = tm.GetRootDirCurTest().append("1.tar");
110     UniqueFd fd(open(filePath.data(), O_RDWR | O_CREAT, S_IRWXU));
111     GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
112     callbacks_.onFileReady(bFileInfo, move(fd), 0);
113 
114     string fileManagePath = tm.GetRootDirCurTest().append("manage.json");
115     UniqueFd fdManage(open(fileManagePath.data(), O_RDWR | O_CREAT, S_IRWXU));
116     bFileInfo.fileName = "manage.json";
117     GTEST_LOG_(INFO) << "File ready callback for manage.json";
118     callbacks_.onFileReady(bFileInfo, move(fdManage), 0);
119 
120     callbacks_.onBundleFinished(0, "com.example.app2backup");
121 
122     callbacks_.onAllBundlesFinished(0);
123     callbacks_.onBundleStarted(1, "com.example.app2backup");
124     callbacks_.onBundleFinished(1, "com.example.app2backup");
125     callbacks_.onAllBundlesFinished(1);
126     callbacks_.onBackupServiceDied();
127     callbacks_.onProcess("com.example.app2backup", "100");
128     return BError(BError::Codes::OK);
129 }
130 
OnBackupServiceDied()131 void BSessionRestoreAsync::OnBackupServiceDied() {}
132 
Release()133 ErrCode BSessionRestoreAsync::Release()
134 {
135     return BError(BError::Codes::OK);
136 }
137 
RegisterBackupServiceDied(function<void ()> functor)138 void BSessionRestoreAsync::RegisterBackupServiceDied(function<void()> functor) {}
139 } // namespace OHOS::FileManagement::Backup