• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "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,RestoreTypeEnum restoreType,int32_t userId)61 ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap,
62                                             vector<BundleName> bundlesToRestore,
63                                             RestoreTypeEnum restoreType,
64                                             int32_t userId)
65 {
66     GTEST_LOG_(INFO) << "BSessionRestoreAsync::AppendBundles";
67     if (restoreType == RestoreTypeEnum::RESTORE_DATA_READDY) {
68         callbacks_.onBackupServiceDied();
69         return BError(BError::Codes::OK);
70     }
71     callbacks_.onBundleStarted(0, "com.example.app2backup");
72 
73     BFileInfo bFileInfo("com.example.app2backup", "1.tar", 0);
74     TestManager tm("BSessionRestoreAsyncMock_GetFd_0100");
75     string filePath = tm.GetRootDirCurTest().append("1.tar");
76     UniqueFd fd(open(filePath.data(), O_RDWR | O_CREAT, S_IRWXU));
77     GTEST_LOG_(INFO) << "callbacks_::onFileReady 1.tar";
78     callbacks_.onFileReady(bFileInfo, move(fd));
79 
80     string fileManagePath = tm.GetRootDirCurTest().append("manage.json");
81     UniqueFd fdManage(open(fileManagePath.data(), O_RDWR | O_CREAT, S_IRWXU));
82     bFileInfo.fileName = "manage.json";
83     GTEST_LOG_(INFO) << "callbacks_::onFileReady manage.json";
84     callbacks_.onFileReady(bFileInfo, move(fdManage));
85 
86     callbacks_.onBundleFinished(0, "com.example.app2backup");
87 
88     callbacks_.onAllBundlesFinished(0);
89     callbacks_.onBundleStarted(1, "com.example.app2backup");
90     callbacks_.onBundleFinished(1, "com.example.app2backup");
91     callbacks_.onAllBundlesFinished(1);
92 
93     callbacks_.onBackupServiceDied();
94     return BError(BError::Codes::OK);
95 }
96 
OnBackupServiceDied()97 void BSessionRestoreAsync::OnBackupServiceDied() {}
98 
PopBundleInfo()99 void BSessionRestoreAsync::PopBundleInfo() {}
100 
AppendBundlesImpl(AppendBundleInfo info)101 void BSessionRestoreAsync::AppendBundlesImpl(AppendBundleInfo info) {}
102 
OnBundleStarted(ErrCode errCode,const vector<BundleName> & bundlesToRestore)103 void BSessionRestoreAsync::OnBundleStarted(ErrCode errCode, const vector<BundleName> &bundlesToRestore) {}
104 
RegisterBackupServiceDied(function<void ()> functor)105 void BSessionRestoreAsync::RegisterBackupServiceDied(function<void()> functor) {}
106 } // namespace OHOS::FileManagement::Backup