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 <thread>
19 #include <if_system_ability_manager.h>
20 #include <iservice_registry.h>
21
22 #include "message_parcel_warp.h"
23 #include "pasteboard_client_death_observer_stub.h"
24 #include "pasteboard_error.h"
25 #include "pasteboard_hilog.h"
26 #include "pasteboard_load_callback.h"
27 #include "pasteboard_service_loader.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS::MiscServices {
31 using namespace testing::ext;
32 using namespace testing;
33 using namespace OHOS::Media;
34 namespace {
35 const uint32_t DATAID_TEST = 1;
36 const uint32_t RECORD_TEST = 1;
37 const std::u16string DESCRIPTOR_TEST = u"test_descriptor";
38 }
39
40 class PasteboardServiceLoaderInterface {
41 public:
PasteboardServiceLoaderInterface()42 PasteboardServiceLoaderInterface(){};
~PasteboardServiceLoaderInterface()43 virtual ~PasteboardServiceLoaderInterface(){};
44 virtual bool Encode(std::vector<uint8_t> &buffer) const = 0;
45 };
46
47 class PasteboardServiceLoaderInterfaceMock : public PasteboardServiceLoaderInterface {
48 public:
49 PasteboardServiceLoaderInterfaceMock();
50 ~PasteboardServiceLoaderInterfaceMock() override;
51 MOCK_CONST_METHOD1(Encode, bool(std::vector<uint8_t> &buffer));
52 };
53
54 static void *g_interface = nullptr;
55 static bool g_accountIds = false;
56
PasteboardServiceLoaderInterfaceMock()57 PasteboardServiceLoaderInterfaceMock::PasteboardServiceLoaderInterfaceMock()
58 {
59 g_interface = reinterpret_cast<void *>(this);
60 }
61
~PasteboardServiceLoaderInterfaceMock()62 PasteboardServiceLoaderInterfaceMock::~PasteboardServiceLoaderInterfaceMock()
63 {
64 g_interface = nullptr;
65 }
66
GetPasteboardServiceLoaderInterface()67 static PasteboardServiceLoaderInterface *GetPasteboardServiceLoaderInterface()
68 {
69 return reinterpret_cast<PasteboardServiceLoaderInterface*>(g_interface);
70 }
71
72 extern "C" {
Encode(std::vector<uint8_t> & buffer,bool isRemote) const73 bool TLVWriteable::Encode(std::vector<uint8_t> &buffer, bool isRemote) const
74 {
75 (void)isRemote;
76 PasteboardServiceLoaderInterface *interface = GetPasteboardServiceLoaderInterface();
77 if (interface == nullptr) {
78 return false;
79 }
80 return interface->Encode(buffer);
81 }
82 }
83 static bool g_addDeathRecipient = false;
84 class TestIRemoteObject : public IRemoteObject {
TestIRemoteObject()85 TestIRemoteObject(): IRemoteObject(DESCRIPTOR_TEST) {}
86
GetObjectRefCount()87 int32_t GetObjectRefCount()
88 {
89 return 0;
90 }
91
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)92 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
93 {
94 return 0;
95 }
96
AddDeathRecipient(const sptr<DeathRecipient> & recipient)97 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
98 {
99 return g_addDeathRecipient ;
100 }
101
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)102 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
103 {
104 return true;
105 }
106
Dump(int fd,const std::vector<std::u16string> & args)107 int Dump(int fd, const std::vector<std::u16string> &args)
108 {
109 return 0;
110 }
111 };
112 class PasteboardServiceLoaderTest : public testing::Test {
113 public:
114 static void SetUpTestCase(void);
115 static void TearDownTestCase(void);
116 void SetUp() override;
117 void TearDown() override;
118 };
119
SetUpTestCase(void)120 void PasteboardServiceLoaderTest::SetUpTestCase(void){ }
121
TearDownTestCase(void)122 void PasteboardServiceLoaderTest::TearDownTestCase(void){ }
123
SetUp(void)124 void PasteboardServiceLoaderTest::SetUp(void) { }
125
TearDown(void)126 void PasteboardServiceLoaderTest::TearDown(void) { }
127
128 /**
129 * @tc.name: GetPasteboardServiceProxyTest001
130 * @tc.desc: GetPasteboardServiceProxy is normal
131 * @tc.type: FUNC
132 * @tc.require:
133 * @tc.author:
134 */
135 HWTEST_F(PasteboardServiceLoaderTest, GetPasteboardServiceProxyTest001, TestSize.Level0)
136 {
137 auto ret = PasteboardServiceLoader::GetInstance().GetPasteboardServiceProxy();
138 EXPECT_EQ(ret, nullptr);
139 }
140
141 /**
142 * @tc.name: GetPasteboardServiceTest001
143 * @tc.desc: GetPasteboardService is normal
144 * @tc.type: FUNC
145 * @tc.require:
146 * @tc.author:
147 */
148 HWTEST_F(PasteboardServiceLoaderTest, GetPasteboardServiceTest001, TestSize.Level0)
149 {
150 sptr<ISystemAbilityManager> saMgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151 EXPECT_NE(saMgrProxy, nullptr);
152 sptr<IRemoteObject> remoteObject = saMgrProxy->CheckSystemAbility(PASTEBOARD_SERVICE_ID);
153 EXPECT_NE(remoteObject, nullptr);
154 PasteboardServiceLoader::GetInstance().SetPasteboardServiceProxy(remoteObject);
155 auto ret = PasteboardServiceLoader::GetInstance().GetPasteboardService();
156 EXPECT_NE(ret, nullptr);
157 }
158
159 /**
160 * @tc.name: GetPasteboardServiceTest002
161 * @tc.desc: constructing_ is true
162 * @tc.type: FUNC
163 * @tc.require:
164 * @tc.author:
165 */
166 HWTEST_F(PasteboardServiceLoaderTest, GetPasteboardServiceTest002, TestSize.Level0)
167 {
168 PasteboardServiceLoader::GetInstance().constructing_ = true;
169 auto ret = PasteboardServiceLoader::GetInstance().GetPasteboardService();
170 EXPECT_NE(ret, nullptr);
171 }
172
173 /**
174 * @tc.name: GetPasteboardServiceTest003
175 * @tc.desc: GetPasteboardService is normal
176 * @tc.type: FUNC
177 * @tc.require:
178 * @tc.author:
179 */
180 HWTEST_F(PasteboardServiceLoaderTest, GetPasteboardServiceTest003, TestSize.Level0)
181 {
182 auto ret = PasteboardServiceLoader::GetInstance().GetPasteboardService();
183 EXPECT_NE(ret, nullptr);
184 }
185
186 /**
187 * @tc.name: SetPasteboardServiceProxyTest001
188 * @tc.desc: SetPasteboardServiceProxy is normal
189 * @tc.type: FUNC
190 * @tc.require:
191 * @tc.author:
192 */
193 HWTEST_F(PasteboardServiceLoaderTest, SetPasteboardServiceProxyTest001, TestSize.Level0)
194 {
195 sptr<ISystemAbilityManager> saMgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
196 EXPECT_NE(saMgrProxy, nullptr);
197 sptr<IRemoteObject> remoteObject = saMgrProxy->CheckSystemAbility(PASTEBOARD_SERVICE_ID);
198 EXPECT_NE(remoteObject, nullptr);
199 PasteboardServiceLoader::GetInstance().SetPasteboardServiceProxy(remoteObject);
200 }
201
202 /**
203 * @tc.name: SetPasteboardServiceProxyTest003
204 * @tc.desc: AddDeathRecipient is fail
205 * @tc.type: FUNC
206 * @tc.require:
207 * @tc.author:
208 */
209 HWTEST_F(PasteboardServiceLoaderTest, SetPasteboardServiceProxyTest003, TestSize.Level0)
210 {
211 sptr<IRemoteObject> rObject = sptr<TestIRemoteObject>::MakeSptr();
212 EXPECT_NE(rObject, nullptr);
213 g_addDeathRecipient = false;
214 PasteboardServiceLoader::GetInstance().SetPasteboardServiceProxy(rObject);
215 }
216
217 /**
218 * @tc.name: ReleaseDeathRecipientTest001
219 * @tc.desc: Release DeathRecipient
220 * @tc.type: FUNC
221 * @tc.require:
222 * @tc.author:
223 */
224 HWTEST_F(PasteboardServiceLoaderTest, ReleaseDeathRecipientTest001, TestSize.Level0)
225 {
226 sptr<ISystemAbilityManager> saMgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
227 EXPECT_NE(saMgrProxy, nullptr);
228 sptr<IRemoteObject> remoteObject = saMgrProxy->CheckSystemAbility(PASTEBOARD_SERVICE_ID);
229 EXPECT_NE(remoteObject, nullptr);
230 PasteboardServiceLoader::GetInstance().ReleaseDeathRecipient();
231 EXPECT_TRUE(PasteboardServiceLoader::GetInstance().deathRecipient_ == nullptr);
232 }
233
234 /**
235 * @tc.name: GetRecordValueByTypeTest001
236 * @tc.desc: GetRecordValueByType is return SERIALIZATION_ERROR
237 * @tc.type: FUNC
238 * @tc.require:
239 * @tc.author:
240 */
241 HWTEST_F(PasteboardServiceLoaderTest, GetRecordValueByTypeTest001, TestSize.Level0)
242 {
243 auto value = std::make_shared<PasteDataEntry>();
244 NiceMock<PasteboardServiceLoaderInterfaceMock> mock;
245 EXPECT_CALL(mock, Encode(testing::_)).WillOnce(Return(false));
246 int32_t result = PasteboardServiceLoader::GetInstance().GetRecordValueByType(DATAID_TEST, RECORD_TEST, *value);
247 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::SERIALIZATION_ERROR));
248 }
249
250 /**
251 * @tc.name: GetRecordValueByTypeTest002
252 * @tc.desc: GetRecordValueByType is return INVALID_PARAM_ERROR
253 * @tc.type: FUNC
254 * @tc.require:
255 * @tc.author:
256 */
257 HWTEST_F(PasteboardServiceLoaderTest, GetRecordValueByTypeTest002, TestSize.Level0)
258 {
259 auto value = std::make_shared<PasteDataEntry>();
260 NiceMock<PasteboardServiceLoaderInterfaceMock> mock;
261 EXPECT_CALL(mock, Encode(testing::_)).WillOnce(Return(true));
262 int32_t result = PasteboardServiceLoader::GetInstance().GetRecordValueByType(DATAID_TEST, RECORD_TEST, *value);
263 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::INVALID_PARAM_ERROR));
264 }
265
266 /**
267 * @tc.name: ProcessPasteDataTest001
268 * @tc.desc: ProcessPasteData is ok
269 * @tc.type: FUNC
270 * @tc.require:
271 * @tc.author:
272 */
273 HWTEST_F(PasteboardServiceLoaderTest, ProcessPasteDataTest001, TestSize.Level0)
274 {
275 std::vector<uint8_t> sendTLV(0);
276 int64_t tlvSize = 1;
277 auto mpw = std::make_shared<MessageParcelWarp>();
278 auto value = std::make_shared<PasteDataEntry>();
279 int fd = mpw->CreateTmpFd();
280 int32_t result = PasteboardServiceLoader::GetInstance().ProcessPasteData(*value, tlvSize, fd, sendTLV);
281 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::E_OK));
282 mpw->writeRawDataFd_ = -1;
283 }
284
285 /**
286 * @tc.name: ProcessPasteDataTest002
287 * @tc.desc: fd is -1
288 * @tc.type: FUNC
289 * @tc.require:
290 * @tc.author:
291 */
292 HWTEST_F(PasteboardServiceLoaderTest, ProcessPasteDataTest002, TestSize.Level0)
293 {
294 std::vector<uint8_t> sendTLV(0);
295 auto value = std::make_shared<PasteDataEntry>();
296 int64_t tlvSize = 1;
297 int fd = -1;
298 int32_t result = PasteboardServiceLoader::GetInstance().ProcessPasteData(*value, tlvSize, fd, sendTLV);
299 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR));
300 }
301
302 /**
303 * @tc.name: ProcessPasteDataTest003
304 * @tc.desc: rawDataSize = 0
305 * @tc.type: FUNC
306 * @tc.require:
307 * @tc.author:
308 */
309 HWTEST_F(PasteboardServiceLoaderTest, ProcessPasteDataTest003, TestSize.Level0)
310 {
311 auto value = std::make_shared<PasteDataEntry>();
312 auto mpw = std::make_shared<MessageParcelWarp>();
313 int64_t rawDataSize = 0;
314 int fd = mpw->CreateTmpFd();
315 std::vector<uint8_t> sendTLV(0);
316 int32_t result = PasteboardServiceLoader::GetInstance().ProcessPasteData(*value, rawDataSize, fd, sendTLV);
317 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR));
318 mpw->writeRawDataFd_ = -1;
319 }
320
321 /**
322 * @tc.name: ProcessPasteDataTest004
323 * @tc.desc: rawDataSize > DEFAULT_MAX_RAW_DATA_SIZE
324 * @tc.type: FUNC
325 * @tc.require:
326 * @tc.author:
327 */
328 HWTEST_F(PasteboardServiceLoaderTest, ProcessPasteDataTest004, TestSize.Level0)
329 {
330 auto mpw = std::make_shared<MessageParcelWarp>();
331 auto value = std::make_shared<PasteDataEntry>();
332 int64_t rawDataSize = DEFAULT_MAX_RAW_DATA_SIZE + 1;
333 int fd = mpw->CreateTmpFd();
334 std::vector<uint8_t> sendTLV(0);
335 int32_t result = PasteboardServiceLoader::GetInstance().ProcessPasteData(*value, rawDataSize, fd, sendTLV);
336 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR));
337 mpw->writeRawDataFd_ = -1;
338 }
339
340 /**
341 * @tc.name: ProcessPasteDataTest005
342 * @tc.desc: rawDataSize < DEFAULT_MAX_RAW_DATA_SIZE
343 * @tc.type: FUNC
344 * @tc.require:
345 * @tc.author:
346 */
347 HWTEST_F(PasteboardServiceLoaderTest, ProcessPasteDataTest005, TestSize.Level0)
348 {
349 auto mpw = std::make_shared<MessageParcelWarp>();
350 auto value = std::make_shared<PasteDataEntry>();
351 int64_t rawDataSize = DEFAULT_MAX_RAW_DATA_SIZE - 1;
352 int fd = mpw->CreateTmpFd();
353 std::vector<uint8_t> sendTLV(0);
354 int32_t result = PasteboardServiceLoader::GetInstance().ProcessPasteData(*value, rawDataSize, fd, sendTLV);
355 EXPECT_EQ(result, static_cast<int32_t>(PasteboardError::DESERIALIZATION_ERROR));
356 mpw->writeRawDataFd_ = -1;
357 }
358 }