• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "message_parcel_warp.h"
20 #include "pasteboard_entry_getter_stub.h"
21 
22 using namespace OHOS;
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace OHOS::MiscServices;
26 
27 namespace OHOS {
28 namespace {
29     const int64_t INT64_NEGATIVE_NUMBER = -1;
30     const uint32_t UINT32_NEGATIVE_NUMBER = -1;
31     const int64_t INT64_POSITIVE_NUMBER = 1;
32     const int64_t DEFAULT_MAX_RAW_DATA_SIZE_ADD = 128 * 1024 * 1024 + 1;
33     const int64_t DEFAULT_MAX_RAW_DATA_SIZE_SUB = 128 * 1024 * 1024 - 1;
34     const int INT_NEGATIVE_NUMBER = -1;
35     const int INT_POSITIVE_NUMBER_THREE = 333;
36     const int INT_POSITIVE_NUMBER = 1;
37     const int64_t INT64_POSITIVE_TWO_NUMBER = 2;
38     const int32_t INT32_POSITIVE_TWO_NUMBER = 22;
39     const uint32_t UINT32_POSITIVE_NUMBER = 1;
40     const uint8_t UINT8_DATA_ARRAY[] = {0x01, 0x02, 0x03, 0x04};
41     const size_t UINT8_DATA_ARRAY_LENGTH = 4;
42     const uint8_t UINT8_RANDOM = 0x01;
43     const std::u16string U16STRING_RAMDOM_VALUE = u"aaa";
44 }
45 
46 namespace MiscServices {
47 class PasteboardEntryGetterStubI : public PasteboardEntryGetterStub {
48 public:
GetRecordValueByType(uint32_t recordId,PasteDataEntry & value)49     int32_t GetRecordValueByType(uint32_t recordId, PasteDataEntry &value)
50     {
51         return 0;
52     }
53 };
54 } // namespace MiscServices
55 
56 class PasteboardEntryGetterStubInterface {
57 public:
PasteboardEntryGetterStubInterface()58     PasteboardEntryGetterStubInterface() {};
~PasteboardEntryGetterStubInterface()59     virtual ~PasteboardEntryGetterStubInterface() {};
60 
61     virtual uint32_t ReadUint32() = 0;
62     virtual int64_t ReadInt64() = 0;
63     virtual bool Decode(const std::vector<std::uint8_t> &buffer) = 0;
64     virtual bool WriteInt32(int32_t value) = 0;
65     virtual bool Encode(std::vector<uint8_t> &buffer) = 0;
66     virtual bool WriteInt64(int64_t data) = 0;
67     virtual std::u16string ReadInterfaceToken() = 0;
68     virtual int OnRemoteRequest(uint32_t code, MessageParcel &data,
69         MessageParcel &reply, MessageOption &option) = 0;
70     virtual uint8_t *ReadUnpadBuffer(size_t length) = 0;
71 };
72 
73 class PasteboardEntryGetterStubMock : public PasteboardEntryGetterStubInterface {
74 public:
75     PasteboardEntryGetterStubMock();
76     ~PasteboardEntryGetterStubMock() override;
77 
78     MOCK_METHOD0(ReadUint32, uint32_t());
79     MOCK_METHOD0(ReadInt64, int64_t());
80     MOCK_METHOD1(Decode, bool(const std::vector<std::uint8_t> &buffer));
81     MOCK_METHOD1(WriteInt32, bool(int32_t value));
82     MOCK_METHOD1(Encode, bool(std::vector<uint8_t> &buffer));
83     MOCK_METHOD1(WriteInt64, bool(int64_t data));
84     MOCK_METHOD0(ReadInterfaceToken, std::u16string());
85     MOCK_METHOD1(ReadUnpadBuffer, uint8_t *(size_t length));
86     MOCK_METHOD4(OnRemoteRequest, int(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option));
87 };
88 
89 static void *g_interface = nullptr;
90 static bool g_encodeSwitch = false;
91 
PasteboardEntryGetterStubMock()92 PasteboardEntryGetterStubMock::PasteboardEntryGetterStubMock()
93 {
94     g_interface = reinterpret_cast<void *>(this);
95 }
96 
~PasteboardEntryGetterStubMock()97 PasteboardEntryGetterStubMock::~PasteboardEntryGetterStubMock()
98 {
99     g_interface = nullptr;
100 }
101 
GetPasteboardEntryGetterStubInterface()102 static PasteboardEntryGetterStubInterface *GetPasteboardEntryGetterStubInterface()
103 {
104     return reinterpret_cast<PasteboardEntryGetterStubInterface *>(g_interface);
105 }
106 
107 extern "C" {
ReadUnpadBuffer(size_t length)108     const uint8_t *Parcel::ReadUnpadBuffer(size_t length)
109     {
110         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
111             return nullptr;
112         }
113         return GetPasteboardEntryGetterStubInterface()->ReadUnpadBuffer(length);
114     }
115 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)116     int IPCObjectStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
117     {
118         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
119             return INT_NEGATIVE_NUMBER;
120         }
121         return GetPasteboardEntryGetterStubInterface()->OnRemoteRequest(code, data, reply, option);
122     }
123 
ReadInterfaceToken()124     std::u16string MessageParcel::ReadInterfaceToken()
125     {
126         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
127             return std::u16string();
128         }
129         return GetPasteboardEntryGetterStubInterface()->ReadInterfaceToken();
130     }
131 
WriteInt64(int64_t data)132     bool Parcel::WriteInt64(int64_t data)
133     {
134         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
135             return false;
136         }
137         return GetPasteboardEntryGetterStubInterface()->WriteInt64(data);
138     }
139 
Encode(std::vector<uint8_t> & buffer,bool isRemote) const140     bool TLVWriteable::Encode(std::vector<uint8_t> &buffer, bool isRemote) const
141     {
142         (void)isRemote;
143         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
144             return false;
145         }
146         buffer.clear();
147         if (g_encodeSwitch) {
148             buffer.push_back(UINT8_RANDOM);
149         }
150 
151         return GetPasteboardEntryGetterStubInterface()->Encode(buffer);
152     }
153 
ReadUint32()154     uint32_t Parcel::ReadUint32()
155     {
156         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
157             return 0;
158         }
159         return GetPasteboardEntryGetterStubInterface()->ReadUint32();
160     }
161 
ReadInt64()162     int64_t Parcel::ReadInt64()
163     {
164         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
165             return 0;
166         }
167         return GetPasteboardEntryGetterStubInterface()->ReadInt64();
168     }
169 
170 
Decode(const std::vector<std::uint8_t> & buffer)171     bool TLVReadable::Decode(const std::vector<std::uint8_t> &buffer)
172     {
173         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
174             return false;
175         }
176         return GetPasteboardEntryGetterStubInterface()->Decode(buffer);
177     }
178 
WriteInt32(int32_t value)179     bool Parcel::WriteInt32(int32_t value)
180     {
181         if (GetPasteboardEntryGetterStubInterface() == nullptr) {
182             return false;
183         }
184         return GetPasteboardEntryGetterStubInterface()->WriteInt32(value);
185     }
186 }
187 
188 namespace MiscServices {
189 class PasteboardEntryGetterStubTest : public testing::Test {
190 public:
191     static void SetUpTestCase(void);
192     static void TearDownTestCase(void);
193     void SetUp();
194     void TearDown();
195 };
196 
SetUpTestCase(void)197 void PasteboardEntryGetterStubTest::SetUpTestCase(void) { }
198 
TearDownTestCase(void)199 void PasteboardEntryGetterStubTest::TearDownTestCase(void) { }
200 
SetUp(void)201 void PasteboardEntryGetterStubTest::SetUp(void) { }
202 
TearDown(void)203 void PasteboardEntryGetterStubTest::TearDown(void) { }
204 
205 /**
206  * @tc.name: OnGetRecordValueByTypeTest001
207  * @tc.desc: Test function OnGetRecordValueByType when rawDataSize == -1
208  * @tc.type: FUNC
209  */
210 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest001, TestSize.Level0)
211 {
212     PasteboardEntryGetterStubI stub;
213     MessageParcel data;
214     MessageParcel reply;
215     NiceMock<PasteboardEntryGetterStubMock> mock;
216     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
217     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(INT64_NEGATIVE_NUMBER));
218     int32_t result = stub.OnGetRecordValueByType(data, reply);
219     std::cout << "OnGetRecordValueByType aaaaa" << std::endl;
220     EXPECT_EQ(result, ERR_INVALID_VALUE);
221 }
222 
223 /**
224  * @tc.name: OnGetRecordValueByTypeTest002
225  * @tc.desc: Test function OnGetRecordValueByType when rawDataSize == 1 and rawDataSize > messageData.GetRawDataSize()
226  * @tc.type: FUNC
227  */
228 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest002, TestSize.Level0)
229 {
230     PasteboardEntryGetterStubI stub;
231     MessageParcel data;
232     MessageParcel reply;
233     NiceMock<PasteboardEntryGetterStubMock> mock;
234     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
235     EXPECT_CALL(mock, ReadInt64()).WillRepeatedly(Return(DEFAULT_MAX_RAW_DATA_SIZE_ADD));
236     int32_t result = stub.OnGetRecordValueByType(data, reply);
237     EXPECT_EQ(result, ERR_INVALID_VALUE);
238 }
239 
240 /**
241  * @tc.name: OnGetRecordValueByTypeTest003
242  * @tc.desc: Test function OnGetRecordValueByType when rawData == nullptr
243  * @tc.type: FUNC
244  */
245 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest003, TestSize.Level0)
246 {
247     PasteboardEntryGetterStubI stub;
248     MessageParcel data;
249     MessageParcel reply;
250     NiceMock<PasteboardEntryGetterStubMock> mock;
251     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
252     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH))
253         .WillOnce(Return(UINT8_DATA_ARRAY_LENGTH + UINT8_DATA_ARRAY_LENGTH));
254     int32_t result = stub.OnGetRecordValueByType(data, reply);
255     EXPECT_EQ(result, ERR_INVALID_VALUE);
256 }
257 
258 /**
259  * @tc.name: OnGetRecordValueByTypeTest004
260  * @tc.desc: Test function OnGetRecordValueByType when Decode return false
261  * @tc.type: FUNC
262  */
263 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest004, TestSize.Level0)
264 {
265     PasteboardEntryGetterStubI stub;
266     MessageParcel data;
267     MessageParcel reply;
268     NiceMock<PasteboardEntryGetterStubMock> mock;
269     data.WriteRawData(UINT8_DATA_ARRAY, UINT8_DATA_ARRAY_LENGTH);
270 
271     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
272     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH)).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH));
273     EXPECT_CALL(mock, ReadUnpadBuffer(testing::_)).WillOnce(Return(const_cast<uint8_t*>(UINT8_DATA_ARRAY)));
274     EXPECT_CALL(mock, Decode(testing::_)).WillOnce(Return(false));
275     int32_t result = stub.OnGetRecordValueByType(data, reply);
276     EXPECT_EQ(result, ERR_INVALID_VALUE);
277 }
278 
279 /**
280  * @tc.name: OnGetRecordValueByTypeTest005
281  * @tc.desc: Test function OnGetRecordValueByType when WriteInt32 return false
282  * @tc.type: FUNC
283  */
284 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest005, TestSize.Level0)
285 {
286     PasteboardEntryGetterStubI stub;
287     MessageParcel data;
288     MessageParcel reply;
289     NiceMock<PasteboardEntryGetterStubMock> mock;
290     data.WriteRawData(UINT8_DATA_ARRAY, UINT8_DATA_ARRAY_LENGTH);
291     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
292     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH)).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH));
293     EXPECT_CALL(mock, ReadUnpadBuffer(testing::_)).WillOnce(Return(const_cast<uint8_t*>(UINT8_DATA_ARRAY)));
294     EXPECT_CALL(mock, Decode(testing::_)).WillOnce(Return(true));
295     EXPECT_CALL(mock, WriteInt32(testing::_)).WillOnce(Return(false));
296 
297     int32_t result = stub.OnGetRecordValueByType(data, reply);
298     EXPECT_EQ(result, ERR_INVALID_VALUE);
299 }
300 
301 /**
302  * @tc.name: OnGetRecordValueByTypeTest006
303  * @tc.desc: Test function OnGetRecordValueByType when Encode return false
304  * @tc.type: FUNC
305  */
306 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest006, TestSize.Level0)
307 {
308     PasteboardEntryGetterStubI stub;
309     MessageParcel data;
310     MessageParcel reply;
311     NiceMock<PasteboardEntryGetterStubMock> mock;
312     data.WriteRawData(UINT8_DATA_ARRAY, UINT8_DATA_ARRAY_LENGTH);
313     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
314     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH)).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH));
315     EXPECT_CALL(mock, ReadUnpadBuffer(testing::_)).WillOnce(Return(const_cast<uint8_t*>(UINT8_DATA_ARRAY)));
316     EXPECT_CALL(mock, Decode(testing::_)).WillOnce(Return(true));
317     EXPECT_CALL(mock, WriteInt32(testing::_)).WillOnce(Return(true));
318     EXPECT_CALL(mock, Encode(testing::_)).WillOnce(Return(false));
319 
320     int32_t result = stub.OnGetRecordValueByType(data, reply);
321     EXPECT_EQ(result, ERR_INVALID_VALUE);
322 }
323 
324 /**
325  * @tc.name: OnGetRecordValueByTypeTest007
326  * @tc.desc: Test function OnGetRecordValueByType when WriteInt64 return false
327  * @tc.type: FUNC
328  */
329 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest007, TestSize.Level0)
330 {
331     PasteboardEntryGetterStubI stub;
332     MessageParcel data;
333     MessageParcel reply;
334     NiceMock<PasteboardEntryGetterStubMock> mock;
335     g_encodeSwitch = false;
336     data.WriteRawData(UINT8_DATA_ARRAY, UINT8_DATA_ARRAY_LENGTH);
337     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
338     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH)).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH));
339     EXPECT_CALL(mock, ReadUnpadBuffer(testing::_)).WillOnce(Return(const_cast<uint8_t*>(UINT8_DATA_ARRAY)));
340     EXPECT_CALL(mock, Decode(testing::_)).WillOnce(Return(true));
341     EXPECT_CALL(mock, WriteInt32(testing::_)).WillOnce(Return(true));
342     EXPECT_CALL(mock, Encode(testing::_)).WillOnce(Return(true));
343     EXPECT_CALL(mock, WriteInt64(testing::_)).WillOnce(Return(false));
344 
345     int32_t result = stub.OnGetRecordValueByType(data, reply);
346     EXPECT_EQ(result, ERR_INVALID_VALUE);
347 }
348 
349 /**
350  * @tc.name: OnGetRecordValueByTypeTest008
351  * @tc.desc: Test function OnGetRecordValueByType when done
352  * @tc.type: FUNC
353  */
354 HWTEST_F(PasteboardEntryGetterStubTest, OnGetRecordValueByTypeTest008, TestSize.Level0)
355 {
356     PasteboardEntryGetterStubI stub;
357     MessageParcel data;
358     MessageParcel reply;
359     NiceMock<PasteboardEntryGetterStubMock> mock;
360     g_encodeSwitch = true;
361     data.WriteRawData(UINT8_DATA_ARRAY, UINT8_DATA_ARRAY_LENGTH);
362     EXPECT_CALL(mock, ReadUint32()).WillOnce(Return(UINT32_NEGATIVE_NUMBER));
363     EXPECT_CALL(mock, ReadInt64()).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH)).WillOnce(Return(UINT8_DATA_ARRAY_LENGTH));
364     EXPECT_CALL(mock, ReadUnpadBuffer(testing::_)).WillOnce(Return(const_cast<uint8_t*>(UINT8_DATA_ARRAY)));
365     EXPECT_CALL(mock, Decode(testing::_)).WillOnce(Return(true));
366     EXPECT_CALL(mock, WriteInt32(testing::_)).WillOnce(Return(true));
367     EXPECT_CALL(mock, Encode(testing::_)).WillOnce(Return(true));
368     EXPECT_CALL(mock, WriteInt64(testing::_)).WillOnce(Return(true)).WillOnce(Return(true));
369 
370     int32_t result = stub.OnGetRecordValueByType(data, reply);
371     EXPECT_EQ(result, ERR_OK);
372 }
373 
374 /**
375  * @tc.name: OnRemoteRequestTest001
376  * @tc.desc: Test function OnRemoteRequest when remoteDescriptor != localDescriptor
377  * @tc.type: FUNC
378  */
379 HWTEST_F(PasteboardEntryGetterStubTest, OnRemoteRequestTest001, TestSize.Level0)
380 {
381     PasteboardEntryGetterStubI stub;
382     MessageParcel data;
383     MessageParcel reply;
384     MessageOption option;
385     std::u16string localDescriptor = PasteboardEntryGetterStub::GetDescriptor();
386     NiceMock<PasteboardEntryGetterStubMock> mock;
387     EXPECT_CALL(mock, ReadInterfaceToken()).WillOnce(Return(localDescriptor + U16STRING_RAMDOM_VALUE));
388 
389     int32_t result = stub.OnRemoteRequest(UINT32_POSITIVE_NUMBER, data, reply, option);
390     EXPECT_EQ(result, ERR_INVALID_VALUE);
391 }
392 
393 /**
394  * @tc.name: OnRemoteRequestTest002
395  * @tc.desc: Test function OnRemoteRequest when itFunc != memberFuncMap_.end()
396  * @tc.type: FUNC
397  */
398 HWTEST_F(PasteboardEntryGetterStubTest, OnRemoteRequestTest002, TestSize.Level0)
399 {
400     PasteboardEntryGetterStubI stub;
401     MessageParcel data;
402     MessageParcel reply;
403     MessageOption option;
404     std::u16string localDescriptor = PasteboardEntryGetterStub::GetDescriptor();
405     stub.memberFuncMap_[UINT32_POSITIVE_NUMBER] = &PasteboardEntryGetterStub::OnGetRecordValueByType;
406     g_encodeSwitch = true;
407     data.WriteRawData(UINT8_DATA_ARRAY, UINT8_DATA_ARRAY_LENGTH);
408     NiceMock<PasteboardEntryGetterStubMock> mock;
409     int32_t result = stub.OnRemoteRequest(UINT32_POSITIVE_NUMBER, data, reply, option);
410     EXPECT_EQ(result, INT32_POSITIVE_TWO_NUMBER);
411 }
412 
413 /**
414  * @tc.name: OnRemoteRequestTest003
415  * @tc.desc: Test function OnRemoteRequest when memberFuncMap_ not find code
416  * @tc.type: FUNC
417  */
418 HWTEST_F(PasteboardEntryGetterStubTest, OnRemoteRequestTest003, TestSize.Level0)
419 {
420     PasteboardEntryGetterStubI stub;
421     MessageParcel data;
422     MessageParcel reply;
423     MessageOption option;
424     std::u16string localDescriptor = PasteboardEntryGetterStub::GetDescriptor();
425     stub.memberFuncMap_.erase(UINT32_POSITIVE_NUMBER);
426     NiceMock<PasteboardEntryGetterStubMock> mock;
427     EXPECT_CALL(mock, ReadInterfaceToken()).WillOnce(Return(localDescriptor));
428     EXPECT_CALL(mock, OnRemoteRequest(testing::_, testing::_, testing::_, testing::_))
429         .WillOnce(Return(INT_POSITIVE_NUMBER_THREE));
430 
431     int32_t result = stub.OnRemoteRequest(UINT32_POSITIVE_NUMBER, data, reply, option);
432     EXPECT_EQ(result, INT_POSITIVE_NUMBER_THREE);
433 }
434 }
435 } // namespace OHOS::MiscServices