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 <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 #include "ipc_skeleton.h"
19 #include "message_parcel_warp.h"
20 #include "pasteboard_hilog.h"
21 #include "pasteboard_delay_getter_proxy.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace OHOS::MiscServices;
26 namespace OHOS {
27 namespace {
28 int g_sendRequest = 0;
29 class RemoteObjectTest : public IRemoteObject {
30 public:
RemoteObjectTest(std::u16string descriptor)31 explicit RemoteObjectTest(std::u16string descriptor) : IRemoteObject(descriptor) {}
~RemoteObjectTest()32 ~RemoteObjectTest() {}
33
GetObjectRefCount()34 int32_t GetObjectRefCount() { return 0; }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
36 MessageOption &option) { return g_sendRequest; }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)37 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)38 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
Dump(int fd,const std::vector<std::u16string> & args)39 int Dump(int fd, const std::vector<std::u16string> &args) { return 0; }
40 };
41
42 constexpr int64_t TEST_ERROR_PAW_DATA_SIZE = -1;
43 constexpr int64_t TEST_MAX_RAW_DATA_SIZE = 256 * 1024 * 1024; // 256M
44 constexpr int64_t ERR_INVALID_PARAMETER = 401;
45 } // namespace
46
47 class PasteboardDelayProxyTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53 void SetSendRequestResult(int status = 0);
54 };
55
SetUpTestCase(void)56 void PasteboardDelayProxyTest::SetUpTestCase(void) { }
57
TearDownTestCase(void)58 void PasteboardDelayProxyTest::TearDownTestCase(void) { }
59
SetUp(void)60 void PasteboardDelayProxyTest::SetUp(void) { }
61
TearDown(void)62 void PasteboardDelayProxyTest::TearDown(void) { }
63
SetSendRequestResult(int status)64 void PasteboardDelayProxyTest::SetSendRequestResult(int status)
65 {
66 g_sendRequest = status;
67 }
68
69 class PasteboardDelayProxyInterface : public MessageParcel {
70 public:
PasteboardDelayProxyInterface()71 PasteboardDelayProxyInterface() {};
~PasteboardDelayProxyInterface()72 virtual ~PasteboardDelayProxyInterface() {};
73 virtual bool WriteInterfaceToken(std::u16string name) = 0;
74 virtual bool WriteString(const std::string &value) = 0;
75 virtual int SendRequest(uint32_t code, MessageParcel &data,
76 MessageParcel &reply, MessageOption &option) = 0;
77 virtual int64_t ReadInt64() = 0;
78 virtual const void *ReadRawData(MessageParcel &parcelPata, size_t size) = 0;
79 virtual bool Decode(const std::vector<std::uint8_t> &buffer) = 0;
80 virtual const uint8_t *ReadUnpadBuffer(size_t length) = 0;
81 };
82
83 class PasteboardDelayProxyInterfaceMock : public PasteboardDelayProxyInterface {
84 public:
85 PasteboardDelayProxyInterfaceMock();
86 ~PasteboardDelayProxyInterfaceMock() override;
87 MOCK_METHOD1(WriteInterfaceToken, bool(std::u16string name));
88 MOCK_METHOD1(WriteString, bool(const std::string &value));
89 MOCK_METHOD4(SendRequest, int(uint32_t code, MessageParcel &data,
90 MessageParcel &reply, MessageOption &option));
91 MOCK_METHOD0(ReadInt64, int64_t());
92 MOCK_METHOD2(ReadRawData, const void*(MessageParcel &parcelPata, size_t size));
93 MOCK_METHOD1(Decode, bool(const std::vector<std::uint8_t> &buffer));
94 MOCK_METHOD1(ReadUnpadBuffer, const uint8_t *(size_t));
95 };
96
97 static void *g_interface = nullptr;
98
PasteboardDelayProxyInterfaceMock()99 PasteboardDelayProxyInterfaceMock::PasteboardDelayProxyInterfaceMock()
100 {
101 g_interface = reinterpret_cast<void *>(this);
102 }
103
~PasteboardDelayProxyInterfaceMock()104 PasteboardDelayProxyInterfaceMock::~PasteboardDelayProxyInterfaceMock()
105 {
106 g_interface = nullptr;
107 }
108
GetPasteboardDelayProxyInterface()109 static PasteboardDelayProxyInterface *GetPasteboardDelayProxyInterface()
110 {
111 return reinterpret_cast<PasteboardDelayProxyInterface*>(g_interface);
112 }
113
114 extern "C" {
WriteInterfaceToken(std::u16string name)115 bool MessageParcel::WriteInterfaceToken(std::u16string name)
116 {
117 PasteboardDelayProxyInterface *interface = GetPasteboardDelayProxyInterface();
118 if (interface == nullptr) {
119 return false;
120 }
121 return interface->WriteInterfaceToken(name);
122 }
123
WriteString(const std::string & value)124 bool Parcel::WriteString(const std::string &value)
125 {
126 PasteboardDelayProxyInterface *interface = GetPasteboardDelayProxyInterface();
127 if (interface == nullptr) {
128 return false;
129 }
130 return interface->WriteString(value);
131 }
132
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)133 int IRemoteObject::SendRequest(uint32_t code, MessageParcel &data,
134 MessageParcel &reply, MessageOption &option)
135 {
136 PasteboardDelayProxyInterface *interface = GetPasteboardDelayProxyInterface();
137 if (interface == nullptr) {
138 return -1;
139 }
140 return interface->SendRequest(code, data, reply, option);
141 }
142
ReadInt64()143 int64_t Parcel::ReadInt64()
144 {
145 PasteboardDelayProxyInterface *interface = GetPasteboardDelayProxyInterface();
146 if (interface == nullptr) {
147 return -1;
148 }
149 return interface->ReadInt64();
150 }
151
Decode(const std::vector<uint8_t> & buffer)152 bool TLVReadable::Decode(const std::vector<uint8_t> &buffer)
153 {
154 PasteboardDelayProxyInterface *interface = GetPasteboardDelayProxyInterface();
155 if (interface == nullptr) {
156 return false;
157 }
158 return interface->Decode(buffer);
159 }
160
ReadUnpadBuffer(size_t length)161 const uint8_t *Parcel::ReadUnpadBuffer(size_t length)
162 {
163 PasteboardDelayProxyInterface *interface = GetPasteboardDelayProxyInterface();
164 if (interface == nullptr) {
165 return nullptr;
166 }
167 return interface->ReadUnpadBuffer(length);
168 }
169 }
170
171 /**
172 * @tc.name: GetPasteDataTest001
173 * @tc.desc: Function GetPasteData when WriteInterfaceToken return error
174 * @tc.type: FUNC
175 */
176 HWTEST_F(PasteboardDelayProxyTest, GetPasteDataTest001, TestSize.Level0)
177 {
178 std::string testType = "text/plain";
179 PasteData testData;
180 NiceMock<PasteboardDelayProxyInterfaceMock> mock;
181 sptr<RemoteObjectTest> remote = sptr<RemoteObjectTest>::MakeSptr(u"test");
182 EXPECT_CALL(mock, WriteInterfaceToken(testing::_)).WillOnce(Return(false));
183
184 PasteboardDelayGetterProxy proxy(remote);
185 proxy.GetPasteData(testType, testData);
186 }
187
188 /**
189 * @tc.name: GetPasteDataTest002
190 * @tc.desc: Function GetPasteData when WriteString return error
191 * @tc.type: FUNC
192 */
193 HWTEST_F(PasteboardDelayProxyTest, GetPasteDataTest002, TestSize.Level0)
194 {
195 std::string testType = "text/plain";
196 PasteData testData;
197 NiceMock<PasteboardDelayProxyInterfaceMock> mock;
198 sptr<RemoteObjectTest> remote = sptr<RemoteObjectTest>::MakeSptr(u"test");
199 EXPECT_CALL(mock, WriteInterfaceToken(testing::_)).WillOnce(Return(true));
200 EXPECT_CALL(mock, WriteString(testing::_)).WillOnce(Return(false));
201
202 PasteboardDelayGetterProxy proxy(remote);
203 proxy.GetPasteData(testType, testData);
204 }
205
206 /**
207 * @tc.name: GetPasteDataTest003
208 * @tc.desc: Function GetPasteData when SendRequest return error
209 * @tc.type: FUNC
210 */
211 HWTEST_F(PasteboardDelayProxyTest, GetPasteDataTest003, TestSize.Level0)
212 {
213 std::string testType = "text/plain";
214 PasteData testData;
215 NiceMock<PasteboardDelayProxyInterfaceMock> mock;
216 sptr<RemoteObjectTest> remote = sptr<RemoteObjectTest>::MakeSptr(u"test");
217 EXPECT_CALL(mock, WriteInterfaceToken(testing::_)).WillOnce(Return(true));
218 EXPECT_CALL(mock, WriteString(testing::_)).WillOnce(Return(true));
219 SetSendRequestResult(ERR_INVALID_PARAMETER);
220 PasteboardDelayGetterProxy proxy(remote);
221 proxy.GetPasteData(testType, testData);
222 SetSendRequestResult(OHOS::ERR_OK);
223 }
224
225 /**
226 * @tc.name: GetPasteDataTest004
227 * @tc.desc: Function GetPasteData when ReadInt64 return error
228 * @tc.type: FUNC
229 */
230 HWTEST_F(PasteboardDelayProxyTest, GetPasteDataTest004, TestSize.Level0)
231 {
232 std::string testType = "text/plain";
233 PasteData testData;
234 NiceMock<PasteboardDelayProxyInterfaceMock> mock;
235 sptr<RemoteObjectTest> remote = sptr<RemoteObjectTest>::MakeSptr(u"test");
236 EXPECT_CALL(mock, WriteInterfaceToken(testing::_)).WillOnce(Return(true));
237 EXPECT_CALL(mock, WriteString(testing::_)).WillOnce(Return(true));
238 EXPECT_CALL(mock, SendRequest(testing::_, testing::_, testing::_, testing::_))
239 .WillRepeatedly(Return(OHOS::ERR_OK));
240 EXPECT_CALL(mock, ReadInt64()).WillRepeatedly(Return(TEST_ERROR_PAW_DATA_SIZE));
241 PasteboardDelayGetterProxy proxy(remote);
242 proxy.GetPasteData(testType, testData);
243 }
244
245 /**
246 * @tc.name: GetPasteDataTest005
247 * @tc.desc: Function GetPasteData when ReadInt64 return error
248 * @tc.type: FUNC
249 */
250 HWTEST_F(PasteboardDelayProxyTest, GetPasteDataTest005, TestSize.Level0)
251 {
252 std::string testType = "text/plain";
253 PasteData testData;
254 NiceMock<PasteboardDelayProxyInterfaceMock> mock;
255 sptr<RemoteObjectTest> remote = sptr<RemoteObjectTest>::MakeSptr(u"test");
256 EXPECT_CALL(mock, WriteInterfaceToken(testing::_)).WillOnce(Return(true));
257 EXPECT_CALL(mock, WriteString(testing::_)).WillOnce(Return(true));
258 EXPECT_CALL(mock, SendRequest(testing::_, testing::_, testing::_, testing::_))
259 .WillRepeatedly(Return(OHOS::ERR_OK));
260 EXPECT_CALL(mock, ReadInt64()).WillRepeatedly(Return(TEST_MAX_RAW_DATA_SIZE));
261 PasteboardDelayGetterProxy proxy(remote);
262 proxy.GetPasteData(testType, testData);
263 }
264
265 /**
266 * @tc.name: GetPasteDataTest006
267 * @tc.desc: Function GetUnifiedData
268 * @tc.type: FUNC
269 */
270 HWTEST_F(PasteboardDelayProxyTest, GetPasteDataTest006, TestSize.Level0)
271 {
272 std::string testType = "text/plain";
273 UDMF::UnifiedData testData;
274 sptr<RemoteObjectTest> remote = sptr<RemoteObjectTest>::MakeSptr(u"test");
275 EXPECT_NE(remote, nullptr);
276 PasteboardDelayGetterProxy proxy(remote);
277 proxy.GetUnifiedData(testType, testData);
278 }
279 } // namespace OHOS