1 /*
2 * Copyright (c) 2022-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 <cstdio>
17 #include <fcntl.h>
18 #include <gtest/gtest.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include "b_error/b_error.h"
23 #include "b_file_info.h"
24 #include "iservice_registry.h"
25 #include "module_ipc/service_reverse_proxy.h"
26 #include "service_reverse_mock.h"
27 #include "test_manager.h"
28 #include "unique_fd.h"
29
30 namespace OHOS::FileManagement::Backup {
31 using namespace std;
32 using namespace testing;
33
34 namespace {
35 const string BUNDLE_NAME = "com.example.app2backup";
36 const string FILE_NAME = "1.tar";
37 } // namespace
38
39 class ServiceReverseProxyTest : public testing::Test {
40 public:
SetUpTestCase(void)41 static void SetUpTestCase(void) {};
TearDownTestCase()42 static void TearDownTestCase() {};
43 void SetUp() override;
44 void TearDown() override;
45 shared_ptr<ServiceReverseProxy> proxy_ = nullptr;
46 sptr<ServiceReverseMock> mock_ = nullptr;
47 };
48
SetUp()49 void ServiceReverseProxyTest::SetUp()
50 {
51 mock_ = sptr(new ServiceReverseMock());
52 proxy_ = make_shared<ServiceReverseProxy>(mock_);
53 }
TearDown()54 void ServiceReverseProxyTest::TearDown()
55 {
56 mock_ = nullptr;
57 proxy_ = nullptr;
58 }
59
60 /**
61 * @tc.number: SUB_ServiceReverse_proxy_BackupOnFileReady_0100
62 * @tc.name: SUB_ServiceReverse_proxy_BackupOnFileReady_0100
63 * @tc.desc: Test function of BackupOnFileReady interface for SUCCESS.
64 * @tc.size: MEDIUM
65 * @tc.type: FUNC
66 * @tc.level Level 1
67 * @tc.require: I6F3GV
68 */
69 HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_BackupOnFileReady_0100, testing::ext::TestSize.Level1)
70 {
71 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_BackupOnFileReady_0100";
72 try {
73 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
74 .Times(1)
75 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
76
77 TestManager tm("ServiceReverseProxyTest_GetFd_0100");
78 std::string filePath = tm.GetRootDirCurTest().append(FILE_NAME);
79 UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR));
80 proxy_->BackupOnFileReady(BUNDLE_NAME, FILE_NAME, fd);
81 } catch (...) {
82 EXPECT_TRUE(false);
83 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by BackupOnFileReady.";
84 }
85 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_BackupOnFileReady_0100";
86 }
87
88 /**
89 * @tc.number: SUB_ServiceReverse_proxy_BackupOnBundleStarted_0100
90 * @tc.name: SUB_ServiceReverse_proxy_BackupOnBundleStarted_0100
91 * @tc.desc: Test function of BackupOnBundleStarted interface for SUCCESS.
92 * @tc.size: MEDIUM
93 * @tc.type: FUNC
94 * @tc.level Level 1
95 * @tc.require: I6F3GV
96 */
97 HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_BackupOnBundleStarted_0100, testing::ext::TestSize.Level1)
98 {
99 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_BackupOnBundleStarted_0100";
100 try {
101 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
102 .Times(1)
103 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
104 proxy_->BackupOnBundleStarted(BError(BError::Codes::OK), BUNDLE_NAME);
105 } catch (...) {
106 EXPECT_TRUE(false);
107 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by BackupOnBundleStarted.";
108 }
109 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_BackupOnBundleStarted_0100";
110 }
111
112 /**
113 * @tc.number: SUB_ServiceReverse_proxy_BackupOnBundleFinished_0100
114 * @tc.name: SUB_ServiceReverse_proxy_BackupOnBundleFinished_0100
115 * @tc.desc: Test function of BackupOnBundleFinished interface for SUCCESS.
116 * @tc.size: MEDIUM
117 * @tc.type: FUNC
118 * @tc.level Level 1
119 * @tc.require: I6F3GV
120 */
121 HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_BackupOnBundleFinished_0100, testing::ext::TestSize.Level1)
122 {
123 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_BackupOnBundleFinished_0100";
124 try {
125 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
126 .Times(1)
127 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
128 proxy_->BackupOnBundleFinished(BError(BError::Codes::OK), BUNDLE_NAME);
129 } catch (...) {
130 EXPECT_TRUE(false);
131 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by BackupOnBundleFinished.";
132 }
133 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_BackupOnBundleFinished_0100";
134 }
135
136 /**
137 * @tc.number: SUB_ServiceReverse_proxy_BackupOnAllBundlesFinished_0100
138 * @tc.name: SUB_ServiceReverse_proxy_BackupOnAllBundlesFinished_0100
139 * @tc.desc: Test function of BackupOnAllBundlesFinished interface for SUCCESS.
140 * @tc.size: MEDIUM
141 * @tc.type: FUNC
142 * @tc.level Level 1
143 * @tc.require: I6F3GV
144 */
145 HWTEST_F(ServiceReverseProxyTest,
146 SUB_ServiceReverse_proxy_BackupOnAllBundlesFinished_0100,
147 testing::ext::TestSize.Level1)
148 {
149 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_BackupOnAllBundlesFinished_0100";
150 try {
151 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
152 .Times(1)
153 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
154 proxy_->BackupOnAllBundlesFinished(BError(BError::Codes::OK));
155 } catch (...) {
156 EXPECT_TRUE(false);
157 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by BackupOnAllBundlesFinished.";
158 }
159 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_BackupOnAllBundlesFinished_0100";
160 }
161
162 /**
163 * @tc.number: SUB_ServiceReverse_proxy_RestoreOnBundleStarted_0100
164 * @tc.name: SUB_ServiceReverse_proxy_RestoreOnBundleStarted_0100
165 * @tc.desc: Test function of RestoreOnBundleStarted interface for SUCCESS.
166 * @tc.size: MEDIUM
167 * @tc.type: FUNC
168 * @tc.level Level 1
169 * @tc.require: I6F3GV
170 */
171 HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_RestoreOnBundleStarted_0100, testing::ext::TestSize.Level1)
172 {
173 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_RestoreOnBundleStarted_0100";
174 try {
175 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
176 .Times(1)
177 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
178 proxy_->RestoreOnBundleStarted(BError(BError::Codes::OK), BUNDLE_NAME);
179 } catch (...) {
180 EXPECT_TRUE(false);
181 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by RestoreOnBundleStarted.";
182 }
183 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_RestoreOnBundleStarted_0100";
184 }
185
186 /**
187 * @tc.number: SUB_ServiceReverse_proxy_RestoreOnBundleFinished_0100
188 * @tc.name: SUB_ServiceReverse_proxy_RestoreOnBundleFinished_0100
189 * @tc.desc: Test function of RestoreOnBundleFinished interface for SUCCESS.
190 * @tc.size: MEDIUM
191 * @tc.type: FUNC
192 * @tc.level Level 1
193 * @tc.require: I6F3GV
194 */
195 HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_RestoreOnBundleFinished_0100, testing::ext::TestSize.Level1)
196 {
197 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_RestoreOnBundleFinished_0100";
198 try {
199 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
200 .Times(1)
201 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
202 proxy_->RestoreOnBundleFinished(BError(BError::Codes::OK), BUNDLE_NAME);
203 } catch (...) {
204 EXPECT_TRUE(false);
205 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by RestoreOnBundleFinished.";
206 }
207 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_RestoreOnBundleFinished_0100";
208 }
209
210 /**
211 * @tc.number: SUB_ServiceReverse_proxy_RestoreOnAllBundlesFinished_0100
212 * @tc.name: SUB_ServiceReverse_proxy_RestoreOnAllBundlesFinished_0100
213 * @tc.desc: Test function of RestoreOnAllBundlesFinished interface for SUCCESS.
214 * @tc.size: MEDIUM
215 * @tc.type: FUNC
216 * @tc.level Level 1
217 * @tc.require: I6F3GV
218 */
219 HWTEST_F(ServiceReverseProxyTest,
220 SUB_ServiceReverse_proxy_RestoreOnAllBundlesFinished_0100,
221 testing::ext::TestSize.Level1)
222 {
223 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_RestoreOnAllBundlesFinished_0100";
224 try {
225 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
226 .Times(1)
227 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
228 proxy_->RestoreOnAllBundlesFinished(BError(BError::Codes::OK));
229 } catch (...) {
230 EXPECT_TRUE(false);
231 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by RestoreOnAllBundlesFinished.";
232 }
233 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_RestoreOnAllBundlesFinished_0100";
234 }
235
236 /**
237 * @tc.number: SUB_ServiceReverse_proxy_RestoreOnFileReady_0100
238 * @tc.name: SUB_ServiceReverse_proxy_RestoreOnFileReady_0100
239 * @tc.desc: Test function of RestoreOnFileReady interface for SUCCESS.
240 * @tc.size: MEDIUM
241 * @tc.type: FUNC
242 * @tc.level Level 1
243 * @tc.require: I6F3GV
244 */
245 HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_RestoreOnFileReady_0100, testing::ext::TestSize.Level1)
246 {
247 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_RestoreOnFileReady_0100";
248 try {
249 EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
250 .Times(1)
251 .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest));
252
253 TestManager tm("ServiceReverseProxyTest_GetFd_0200");
254 std::string filePath = tm.GetRootDirCurTest().append(FILE_NAME);
255 UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR));
256 proxy_->RestoreOnFileReady(BUNDLE_NAME, FILE_NAME, fd);
257 } catch (...) {
258 EXPECT_TRUE(false);
259 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by RestoreOnFileReady.";
260 }
261 GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_RestoreOnFileReady_0100";
262 }
263 } // namespace OHOS::FileManagement::Backup