1 /*
2 * Copyright (c) 2025 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <thread>
19
20 #include "entry_getter.h"
21 #include "errors.h"
22 #include "pasteboard_client.h"
23 #include "pasteboard_error.h"
24 #include "pasteboard_hilog.h"
25 #include "pasteboard_service.h"
26 #include "pasteboard_service_loader.h"
27 #include "unistd.h"
28 #include "tlv_writeable.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace OHOS;
33 using namespace OHOS::MiscServices;
34
35 namespace OHOS {
36 constexpr uint8_t TEST_DATA = 0x02;
37 constexpr int64_t MIN_ASHMEM_DATA_SIZE = 32 * 1024; // 32K
38 constexpr int32_t DEFAULT_MAX_RAW_DATA_SIZE = 128 * 1024 * 1024; // 128M
39 class PasteboardClientInterface {
40 public:
PasteboardClientInterface()41 PasteboardClientInterface() {};
~PasteboardClientInterface()42 virtual ~PasteboardClientInterface() {};
43
44 virtual bool Encode(std::vector<uint8_t> &buffer) = 0;
45 virtual bool Decode(const std::vector<std::uint8_t> &buffer) = 0;
46 virtual bool WriteInt64(int64_t value) = 0;
47 virtual bool WriteUnpadBuffer(const void *data, size_t size) = 0;
48 };
49 class PasteboardClientMock : public PasteboardClientInterface {
50 public:
51 PasteboardClientMock();
52 ~PasteboardClientMock() override;
53
54 MOCK_METHOD1(Encode, bool(std::vector<uint8_t> &buffer));
55 MOCK_METHOD1(Decode, bool(const std::vector<std::uint8_t> &buffer));
56 MOCK_METHOD1(WriteInt64, bool(int64_t value));
57 MOCK_METHOD2(WriteUnpadBuffer, bool(const void *data, size_t size));
58 };
59 static void *g_interface = nullptr;
60
PasteboardClientMock()61 PasteboardClientMock::PasteboardClientMock()
62 {
63 g_interface = reinterpret_cast<void *>(this);
64 }
65
~PasteboardClientMock()66 PasteboardClientMock::~PasteboardClientMock()
67 {
68 g_interface = nullptr;
69 }
70
GetPasteboardClientInterface()71 static PasteboardClientInterface *GetPasteboardClientInterface()
72 {
73 return reinterpret_cast<PasteboardClientInterface *>(g_interface);
74 }
75
Encode(std::vector<uint8_t> & buffer,bool isRemote) const76 bool TLVWriteable::Encode(std::vector<uint8_t> &buffer, bool isRemote) const
77 {
78 (void)isRemote;
79 if (GetPasteboardClientInterface() == nullptr) {
80 return false;
81 }
82 if (g_interface) {
83 buffer.push_back(TEST_DATA);
84 }
85 return GetPasteboardClientInterface()->Encode(buffer);
86 }
Decode(const std::vector<std::uint8_t> & buffer)87 bool TLVReadable::Decode(const std::vector<std::uint8_t> &buffer)
88 {
89 if (GetPasteboardClientInterface() == nullptr) {
90 return false;
91 }
92 return GetPasteboardClientInterface()->Decode(buffer);
93 }
WriteInt64(int64_t value)94 bool Parcel::WriteInt64(int64_t value)
95 {
96 if (GetPasteboardClientInterface() == nullptr) {
97 return false;
98 }
99 return GetPasteboardClientInterface()->WriteInt64(value);
100 }
WriteUnpadBuffer(const void * data,size_t size)101 bool Parcel::WriteUnpadBuffer(const void *data, size_t size)
102 {
103 if (GetPasteboardClientInterface() == nullptr) {
104 return false;
105 }
106 return GetPasteboardClientInterface()->WriteUnpadBuffer(data, size);
107 }
108
109 class UDMFEntryGetterImpl : public UDMF::EntryGetter {
110 public:
111 UDMF::ValueType GetValueByType(const std::string &utdId) override;
112 };
113
GetValueByType(const std::string & utdId)114 UDMF::ValueType UDMFEntryGetterImpl::GetValueByType(const std::string &utdId)
115 {
116 return nullptr;
117 }
118
119 namespace MiscServices {
120 class PasteboardClientMockTest : public testing::Test {
121 public:
122 static void SetUpTestCase(void);
123 static void TearDownTestCase(void);
124 void SetUp() override;
125 void TearDown() override;
126 };
127
SetUpTestCase(void)128 void PasteboardClientMockTest::SetUpTestCase(void)
129 {
130 }
131
TearDownTestCase(void)132 void PasteboardClientMockTest::TearDownTestCase(void)
133 {
134 }
135
SetUp(void)136 void PasteboardClientMockTest::SetUp(void) { }
137
TearDown(void)138 void PasteboardClientMockTest::TearDown(void) { }
139
140 /**
141 * @tc.name: WritePasteDataTest001
142 * @tc.desc: WritePasteDataTest001
143 * @tc.type: FUNC
144 * @tc.require:
145 * @tc.author:
146 */
147 HWTEST_F(PasteboardClientMockTest, WritePasteDataTest001, TestSize.Level0)
148 {
149 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- WritePasteDataTest001 enter-----");
150 PasteData pasteData;
151 int64_t tlvSize = 1;
152 MessageParcelWarp messageData;
153 MessageParcel parcelPata;
154 std::vector<uint8_t> buffer = {0x01, 0x02, 0x03};
155 int fd = 3;
156 NiceMock<PasteboardClientMock> mock;
157
158 EXPECT_CALL(mock, Encode).WillRepeatedly(testing::Return(false));
159 auto result = PasteboardClient::GetInstance()->WritePasteData(
160 pasteData, buffer, fd, tlvSize, messageData, parcelPata);
161 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR));
162 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- WritePasteDataTest001 end-----");
163 }
164
165 /**
166 * @tc.name: WritePasteDataTest002
167 * @tc.desc: WritePasteDataTest002
168 * @tc.type: FUNC
169 * @tc.require:
170 * @tc.author:
171 */
172 HWTEST_F(PasteboardClientMockTest, WritePasteDataTest002, TestSize.Level0)
173 {
174 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- WritePasteDataTest002 enter-----");
175 PasteData pasteData;
176 int64_t tlvSize = MIN_ASHMEM_DATA_SIZE + 1;
177 MessageParcelWarp messageData;
178 MessageParcel parcelPata;
179 std::vector<uint8_t> buffer = {0x01, 0x02, 0x03};
180 int fd = 3;
181 NiceMock<PasteboardClientMock> mock;
182
183 EXPECT_CALL(mock, Encode).WillRepeatedly(testing::Return(true));
184 EXPECT_CALL(mock, WriteInt64).WillRepeatedly(testing::Return(true));
185 EXPECT_CALL(mock, WriteUnpadBuffer).WillRepeatedly(testing::Return(true));
186 auto result = PasteboardClient::GetInstance()->WritePasteData(
187 pasteData, buffer, fd, tlvSize, messageData, parcelPata);
188 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::E_OK));
189 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- WritePasteDataTest002 end-----");
190 }
191
192 /**
193 * @tc.name: GetProgressByProgressInfoTest001
194 * @tc.desc: GetProgressByProgressInfoTest001
195 * @tc.type: FUNC
196 * @tc.require:
197 * @tc.author:
198 */
199 HWTEST_F(PasteboardClientMockTest, GetProgressByProgressInfoTest001, TestSize.Level0)
200 {
201 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetProgressByProgressInfoTest001 enter-----");
202 auto params = std::make_shared<GetDataParams>();
203 EXPECT_NE(params, nullptr);
204 params->info = nullptr;
205
206 PasteboardClient::GetInstance()->GetProgressByProgressInfo(params);
207 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetProgressByProgressInfoTest001 end-----");
208 }
209
210 /**
211 * @tc.name: ConvertErrCode001
212 * @tc.desc: ConvertErrCode001 enter ERR_INVALID_VALUE and return SERIALIZATION_ERROR
213 * @tc.type: FUNC
214 * @tc.require:
215 * @tc.author:
216 */
217 HWTEST_F(PasteboardClientMockTest, ConvertErrCode001, TestSize.Level0)
218 {
219 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode001 enter-----");
220 int32_t code = PasteboardClient::GetInstance()->ConvertErrCode(ERR_INVALID_VALUE);
221 EXPECT_EQ(static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), code);
222 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode001 end-----");
223 }
224
225 /**
226 * @tc.name: ConvertErrCode002
227 * @tc.desc: ConvertErrCode002 enter ERR_INVALID_DATA and return SERIALIZATION_ERROR
228 * @tc.type: FUNC
229 * @tc.require:
230 * @tc.author:
231 */
232 HWTEST_F(PasteboardClientMockTest, ConvertErrCode002, TestSize.Level0)
233 {
234 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode002 enter-----");
235 int32_t code = PasteboardClient::GetInstance()->ConvertErrCode(ERR_INVALID_DATA);
236 EXPECT_EQ(static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR), code);
237 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode002 end-----");
238 }
239
240 /**
241 * @tc.name: ConvertErrCode003
242 * @tc.desc: ConvertErrCode003 enter ERR_OK and return E_OK
243 * @tc.type: FUNC
244 * @tc.require:
245 * @tc.author:
246 */
247 HWTEST_F(PasteboardClientMockTest, ConvertErrCode003, TestSize.Level0)
248 {
249 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode003 enter-----");
250 int32_t code = PasteboardClient::GetInstance()->ConvertErrCode(ERR_OK);
251 EXPECT_EQ(static_cast<int32_t>(PasteboardError::E_OK), code);
252 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode003 end-----");
253 }
254
255 /**
256 * @tc.name: ConvertErrCode004
257 * @tc.desc: ConvertErrCode004 enter errCode and return errCode
258 * @tc.type: FUNC
259 * @tc.require:
260 * @tc.author:
261 */
262 HWTEST_F(PasteboardClientMockTest, ConvertErrCode004, TestSize.Level0)
263 {
264 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode004 enter-----");
265 int32_t code = PasteboardClient::GetInstance()->ConvertErrCode(ERR_PERMISSION_DENIED);
266 EXPECT_EQ(static_cast<int32_t>(ERR_PERMISSION_DENIED), code);
267 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ConvertErrCode004 end-----");
268 }
269
270 /**
271 * @tc.name: RemoveAppShareOptions001
272 * @tc.desc: RemoveAppShareOptions001
273 * @tc.type: FUNC
274 * @tc.require:
275 * @tc.author:
276 */
277 HWTEST_F(PasteboardClientMockTest, RemoveAppShareOptions001, TestSize.Level0)
278 {
279 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- RemoveAppShareOptions001 enter-----");
280 int32_t ret = PasteboardClient::GetInstance()->RemoveAppShareOptions();
281 EXPECT_EQ(static_cast<int32_t>(PasteboardError::PERMISSION_VERIFICATION_ERROR), ret);
282 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- RemoveAppShareOptions001 end-----");
283 }
284
285 /**
286 * @tc.name: CreateGetterAgent001
287 * @tc.desc: CreateGetterAgent001
288 * @tc.type: FUNC
289 * @tc.require:
290 * @tc.author:
291 */
292
293 HWTEST_F(PasteboardClientMockTest, CreateGetterAgent001, TestSize.Level0)
294 {
295 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- CreateGetterAgent001 enter-----");
296 sptr<PasteboardDelayGetterClient> delayGetterAgent;
297 std::shared_ptr<PasteboardDelayGetter> delayGetter = nullptr;
298 sptr<PasteboardEntryGetterClient> entryGetterAgent = nullptr;
299 std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters;
300 PasteData pasteData;
301
302 auto udmfEntryGetterImpl = std::make_shared<UDMFEntryGetterImpl>();
303 EXPECT_NE(udmfEntryGetterImpl, nullptr);
304 entryGetters.emplace(1, udmfEntryGetterImpl);
305 pasteData.SetDelayData(true);
306 pasteData.SetDelayRecord(true);
307 PasteboardClient::GetInstance()->CreateGetterAgent(delayGetterAgent,
308 delayGetter, entryGetterAgent, entryGetters, pasteData);
309 entryGetters.clear();
310 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- CreateGetterAgent001 end-----");
311 }
312
313 /**
314 * @tc.name: ProcessRadarReport001
315 * @tc.desc: ProcessRadarReport001
316 * @tc.type: FUNC
317 * @tc.require:
318 * @tc.author:
319 */
320 HWTEST_F(PasteboardClientMockTest, ProcessRadarReport001, TestSize.Level0)
321 {
322 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ProcessRadarReport001 enter-----");
323 int32_t ret;
324 PasteData pasteData;
325 PasteDataFromServiceInfo pasteDataFromServiceInfo;
326 int32_t syncTime;
327 std::string pasteDataInfoSummary = "";
328
329 syncTime = 0;
330 ret = static_cast<int32_t>(PasteboardError::E_OK);
331 pasteData.deviceId_ = "";
332 PasteboardClient::GetInstance()->ProcessRadarReport(
333 ret, pasteData, pasteDataFromServiceInfo, syncTime);
334 EXPECT_EQ(pasteData.deviceId_.length(), 0);
335 pasteData.deviceId_ = "deviceId_";
336 PasteboardClient::GetInstance()->ProcessRadarReport(
337 ret, pasteData, pasteDataFromServiceInfo, syncTime);
338
339 ret = static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR);
340 PasteboardClient::GetInstance()->ProcessRadarReport(
341 ret, pasteData, pasteDataFromServiceInfo, syncTime);
342
343 ret = static_cast<int32_t>(PasteboardError::TASK_PROCESSING);
344 PasteboardClient::GetInstance()->ProcessRadarReport(
345 ret, pasteData, pasteDataFromServiceInfo, syncTime);
346 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ProcessRadarReport001 end-----");
347 }
348
349 /**
350 * @tc.name: ProcessPasteData001
351 * @tc.desc: ProcessPasteData001
352 * @tc.type: FUNC
353 * @tc.require:
354 * @tc.author:
355 */
356 HWTEST_F(PasteboardClientMockTest, ProcessPasteData001, TestSize.Level0)
357 {
358 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ProcessPasteData001 enter-----");
359 PasteData pasteData;
360 int64_t rawDataSize = 0;
361 int fd = -1;
362 const std::vector<uint8_t> recvTLV = {};
363 int ret = -1;
364
365 // fd < 0
366 ret = PasteboardClient::GetInstance()->ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
367 EXPECT_EQ(static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), ret);
368
369 // rawDataSize =< 0
370 fd = 4;
371 ret = PasteboardClient::GetInstance()->ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
372 EXPECT_EQ(static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), ret);
373
374 // rawDataSize > DEFAULT_MAX_RAW_DATA_SIZE
375 rawDataSize = DEFAULT_MAX_RAW_DATA_SIZE * 2;
376 ret = PasteboardClient::GetInstance()->ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
377 EXPECT_EQ(static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), ret);
378
379 // rawDataSize > MIN_ASHMEM_DATA_SIZE
380 rawDataSize = MIN_ASHMEM_DATA_SIZE * 2;
381 ret = PasteboardClient::GetInstance()->ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
382 EXPECT_EQ(static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), ret);
383
384 // rawDataSize else
385 rawDataSize = MIN_ASHMEM_DATA_SIZE / 2;
386 ret = PasteboardClient::GetInstance()->ProcessPasteData<PasteData>(pasteData, rawDataSize, fd, recvTLV);
387 EXPECT_EQ(static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR), ret);
388 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- ProcessPasteData001 end-----");
389 }
390
391 HWTEST_F(PasteboardClientMockTest, GetDataReportTest001, TestSize.Level0)
392 {
393 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest001 enter-----");
394 PasteData pasteData;
395 pasteData.deviceId_ = "";
396 const int32_t syncTime = 0;
397 const std::string currentId = "test_111";
398 const std::string currentPid = "test.app";
399 PasteboardClient::GetInstance()->GetDataReport(pasteData, syncTime, currentId, currentPid,
400 static_cast<int32_t>(PasteboardError::E_OK));
401 EXPECT_EQ(pasteData.deviceId_.length(), 0);
402 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest001 end-----");
403 }
404
405 HWTEST_F(PasteboardClientMockTest, GetDataReportTest002, TestSize.Level0)
406 {
407 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest002 enter-----");
408 PasteData pasteData;
409 const int32_t syncTime = 100;
410 pasteData.deviceId_ = "test_222";
411 const std::string currentPid = "test.app";
412 PasteboardClient::GetInstance()->GetDataReport(
413 pasteData, syncTime, "error_222", currentPid, static_cast<int32_t>(PasteboardError::E_OK));
414 EXPECT_NE(pasteData.deviceId_.length(), 0);
415 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest002 end-----");
416 }
417
418 HWTEST_F(PasteboardClientMockTest, GetDataReportTest003, TestSize.Level0)
419 {
420 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest003 enter-----");
421 PasteData pasteData;
422 const int32_t syncTime = 0;
423 const std::string currentId = "test_333";
424 const std::string currentPid = "test.app";
425 PasteboardClient::GetInstance()->GetDataReport(pasteData, syncTime, currentId, currentPid,
426 static_cast<int32_t>(PasteboardError::TASK_PROCESSING));
427 EXPECT_EQ(pasteData.deviceId_.length(), 0);
428 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest003 end-----");
429 }
430
431 HWTEST_F(PasteboardClientMockTest, GetDataReportTest004, TestSize.Level0)
432 {
433 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest004 enter-----");
434 PasteData pasteData;
435 const int32_t syncTime = 0;
436 const std::string currentId = "test_444";
437 const std::string currentPid = "test.app";
438 PasteboardClient::GetInstance()->GetDataReport(pasteData, syncTime, currentId, currentPid,
439 static_cast<int32_t>(PasteboardError::INVALID_RETURN_VALUE_ERROR));
440 EXPECT_EQ(pasteData.deviceId_.length(), 0);
441 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "----- GetDataReportTest004 end-----");
442 }
443 }
444 } // namespace OHOS::MiscServices
445