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_restore.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 "b_error/b_error.h"
26 #include "test_manager.h"
27 
28 namespace OHOS::FileManagement::Backup {
29 using namespace std;
30 
31 namespace {
32 static BSessionRestore::Callbacks callbacks_ = {};
33 static vector<BundleName> bundlesToRestore_ = {};
34 } // namespace
35 
~BSessionRestore()36 BSessionRestore::~BSessionRestore() {}
37 
Init(Callbacks callbacks)38 unique_ptr<BSessionRestore> BSessionRestore::Init(Callbacks callbacks)
39 {
40     try {
41         callbacks_ = move(callbacks);
42         auto restore = make_unique<BSessionRestore>();
43         return restore;
44     } catch (const exception &e) {
45         return nullptr;
46     }
47     return nullptr;
48 }
49 
PublishFile(BFileInfo fileInfo)50 ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo)
51 {
52     return BError(BError::Codes::OK);
53 }
54 
Start()55 ErrCode BSessionRestore::Start()
56 {
57     callbacks_.onBundleStarted(0, "com.example.app2backup");
58 
59     BFileInfo bFileInfo("com.example.app2backup", "1.tar", 0);
60     TestManager tm("BSessionRestoreMock_GetFd_0100");
61     string filePath = tm.GetRootDirCurTest().append("1.tar");
62     UniqueFd fd(open(filePath.data(), O_RDWR | O_CREAT, S_IRWXU));
63     GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
64     callbacks_.onFileReady(bFileInfo, move(fd), 0);
65 
66     string fileManagePath = tm.GetRootDirCurTest().append("manage.json");
67     UniqueFd fdManage(open(fileManagePath.data(), O_RDWR | O_CREAT, S_IRWXU));
68     bFileInfo.fileName = "manage.json";
69     GTEST_LOG_(INFO) << "callbacks_::onFileReady manage.json";
70     callbacks_.onFileReady(bFileInfo, move(fdManage), 0);
71 
72     callbacks_.onBundleFinished(0, "com.example.app2backup");
73 
74     callbacks_.onAllBundlesFinished(0);
75     callbacks_.onBundleStarted(1, "com.example.app2backup");
76     callbacks_.onBundleFinished(1, "com.example.app2backup");
77     callbacks_.onAllBundlesFinished(1);
78 
79     callbacks_.onBackupServiceDied();
80     return BError(BError::Codes::OK);
81 }
82 
GetFileHandle(const string & bundleName,const string & fileName)83 ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &fileName)
84 {
85     return BError(BError::Codes::OK);
86 }
87 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,vector<string> detailInfos)88 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore,
89     vector<string> detailInfos)
90 {
91     Start();
92     return BError(BError::Codes::OK);
93 }
94 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)95 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
96 {
97     Start();
98     return BError(BError::Codes::OK);
99 }
100 
Finish()101 ErrCode BSessionRestore::Finish()
102 {
103     return BError(BError::Codes::OK);
104 }
105 
Release()106 ErrCode BSessionRestore::Release()
107 {
108     return BError(BError::Codes::OK);
109 }
110 
RegisterBackupServiceDied(function<void ()> functor)111 void BSessionRestore::RegisterBackupServiceDied(function<void()> functor) {}
112 } // namespace OHOS::FileManagement::Backup