1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "UdmfClientSaInvokeTest"
16 #include <gtest/gtest.h>
17
18 #include "token_setproc.h"
19 #include "accesstoken_kit.h"
20 #include "executor_pool.h"
21 #include "nativetoken_kit.h"
22
23 #include "logger.h"
24 #include "image.h"
25 #include "plain_text.h"
26 #include "system_defined_pixelmap.h"
27 #include "udmf_client.h"
28
29 using namespace testing::ext;
30 using namespace OHOS::Security::AccessToken;
31 using namespace OHOS::UDMF;
32 using namespace OHOS;
33 namespace OHOS::Test {
34 constexpr const int32_t FIVE = 5;
35 constexpr const int32_t TEN = 10;
36
37 class UdmfClientSaInvokeTest : public testing::Test {
38 public:
39
40 void SetDataTest();
41
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp() override;
45 void TearDown() override;
46
47 static void SetHapToken();
48 };
49
SetUpTestCase()50 void UdmfClientSaInvokeTest::SetUpTestCase()
51 {
52 SetHapToken();
53 }
54
TearDownTestCase()55 void UdmfClientSaInvokeTest::TearDownTestCase()
56 {
57 auto tokenId = AccessTokenKit::GetNativeTokenId("msdp_sa");
58 AccessTokenKit::DeleteToken(tokenId);
59 }
60
SetUp()61 void UdmfClientSaInvokeTest::SetUp() {}
62
TearDown()63 void UdmfClientSaInvokeTest::TearDown() {}
64
65
SetHapToken()66 void UdmfClientSaInvokeTest::SetHapToken()
67 {
68 auto tokenId = AccessTokenKit::GetNativeTokenId("msdp_sa");
69 SetSelfTokenID(tokenId);
70 }
71
72 /**
73 * @tc.name: SetDataTest
74 * @tc.desc: Test set data success. Record size more than 4M and less than 15.5M.
75 * @tc.type: FUNC
76 */
SetDataTest()77 void UdmfClientSaInvokeTest::SetDataTest()
78 {
79 LOG_INFO(UDMF_TEST, "SetDataTest begin.");
80 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
81 std::vector<uint8_t> rawData;
82 for (int i = 0; i < FIVE * 1024 * 1024; i++) {
83 rawData.emplace_back(1);
84 }
85 auto pixelMap = std::make_shared<SystemDefinedPixelMap>(rawData);
86 UnifiedData data;
87 data.AddRecord(pixelMap);
88
89 std::vector<uint8_t> rawData2;
90 for (int i = 0; i < TEN * 1024 * 1024; i++) {
91 rawData2.emplace_back(1);
92 }
93 auto pixelMap2 = std::make_shared<SystemDefinedPixelMap>(rawData2);
94 data.AddRecord(pixelMap2);
95 auto recordSize = data.GetRecords().size();
96
97 std::string key;
98 auto status = UdmfClient::GetInstance().SetData(option, data, key);
99 ASSERT_EQ(E_OK, status);
100
101 UnifiedData readData;
102 QueryOption queryOption = {.key = key};
103 status = UdmfClient::GetInstance().GetData(queryOption, readData);
104 ASSERT_EQ(E_OK, status);
105 ASSERT_EQ(recordSize, readData.GetRecords().size());
106 auto readPixelMap = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(0));
107 EXPECT_EQ(FIVE * 1024 * 1024, readPixelMap->GetRawData().size());
108
109 auto readPixelMap2 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(1));
110 EXPECT_TRUE(readPixelMap2->GetRawData().size() == TEN * 1024 * 1024);
111 EXPECT_EQ(TEN * 1024 * 1024, readPixelMap2->GetRawData().size());
112 LOG_INFO(UDMF_TEST, "SetDataTest end.");
113 }
114
115 /**
116 * @tc.name: SetData001
117 * @tc.desc: Test set data success. Record size Less than 2M.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(UdmfClientSaInvokeTest, SetData001, TestSize.Level1)
121 {
122 LOG_INFO(UDMF_TEST, "SetData001 begin.");
123
124 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
125 auto plainText = std::make_shared<PlainText>("something", "anything");
126 UnifiedData data;
127 data.AddRecord(plainText);
128 std::string key;
129 auto status = UdmfClient::GetInstance().SetData(option, data, key);
130 ASSERT_EQ(E_OK, status);
131
132 UnifiedData readData;
133 QueryOption queryOption = {.key = key};
134 status = UdmfClient::GetInstance().GetData(queryOption, readData);
135 ASSERT_EQ(E_OK, status);
136 ASSERT_EQ(1, readData.GetRecords().size());
137 auto readPlainText = std::static_pointer_cast<PlainText>(readData.GetRecordAt(0));
138 EXPECT_EQ("something", readPlainText->GetContent());
139 EXPECT_EQ("anything", readPlainText->GetAbstract());
140 LOG_INFO(UDMF_TEST, "SetData001 end.");
141 }
142
143 /**
144 * @tc.name: SetData002
145 * @tc.desc: Test set data success. Record size greater than 2M.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(UdmfClientSaInvokeTest, SetData002, TestSize.Level1)
149 {
150 LOG_INFO(UDMF_TEST, "SetData002 begin.");
151
152 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
153 std::vector<uint8_t> rawData;
154 for (int i = 0; i < 2 * 1024 * 1024 + 1; i++) {
155 rawData.emplace_back(1);
156 }
157 auto pixelMap = std::make_shared<SystemDefinedPixelMap>(rawData);
158 UnifiedData data;
159 data.AddRecord(pixelMap);
160 std::string key;
161 auto status = UdmfClient::GetInstance().SetData(option, data, key);
162 ASSERT_EQ(E_OK, status);
163
164 UnifiedData readData;
165 QueryOption queryOption = {.key = key};
166 status = UdmfClient::GetInstance().GetData(queryOption, readData);
167 ASSERT_EQ(E_OK, status);
168 ASSERT_EQ(1, readData.GetRecords().size());
169 auto readPixelMap = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(0));
170 EXPECT_EQ(2 * 1024 * 1024 + 1, readPixelMap->GetRawData().size());
171 LOG_INFO(UDMF_TEST, "SetData002 end.");
172 }
173
174 /**
175 * @tc.name: SetData003
176 * @tc.desc: Test set data success. Every record size greater than 2M, and data greater than 4M.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(UdmfClientSaInvokeTest, SetData003, TestSize.Level1)
180 {
181 LOG_INFO(UDMF_TEST, "SetData003 begin.");
182
183 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
184 std::vector<uint8_t> rawData1;
185 for (int i = 0; i < 3 * 1024 * 1024; i++) {
186 rawData1.emplace_back(1);
187 }
188 auto pixelMap1 = std::make_shared<SystemDefinedPixelMap>(rawData1);
189 std::vector<uint8_t> rawData2;
190 for (int i = 0; i < 3 * 1024 * 1024; i++) {
191 rawData2.emplace_back(2);
192 }
193 auto pixelMap2 = std::make_shared<SystemDefinedPixelMap>(rawData2);
194 std::vector<uint8_t> rawData3;
195 for (int i = 0; i < 3 * 1024 * 1024; i++) {
196 rawData3.emplace_back(3);
197 }
198 auto pixelMap3 = std::make_shared<SystemDefinedPixelMap>(rawData3);
199 std::vector<uint8_t> rawData4;
200 for (int i = 0; i < 3 * 1024 * 1024; i++) {
201 rawData4.emplace_back(4);
202 }
203 auto pixelMap4 = std::make_shared<SystemDefinedPixelMap>(rawData4);
204
205 UnifiedData data;
206 data.AddRecord(pixelMap1);
207 data.AddRecord(pixelMap2);
208 data.AddRecord(pixelMap3);
209 data.AddRecord(pixelMap4);
210 std::string key;
211 auto status = UdmfClient::GetInstance().SetData(option, data, key);
212 ASSERT_EQ(E_OK, status);
213
214 UnifiedData readData;
215 QueryOption queryOption = {.key = key};
216 status = UdmfClient::GetInstance().GetData(queryOption, readData);
217 ASSERT_EQ(E_OK, status);
218 ASSERT_EQ(4, readData.GetRecords().size());
219 auto readPixelMap1 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(0));
220 EXPECT_EQ(3 * 1024 * 1024, readPixelMap1->GetRawData().size());
221 auto readPixelMap2 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(1));
222 EXPECT_EQ(3 * 1024 * 1024, readPixelMap2->GetRawData().size());
223 auto readPixelMap3 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(2));
224 EXPECT_EQ(3 * 1024 * 1024, readPixelMap3->GetRawData().size());
225 auto readPixelMap4 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(3));
226 EXPECT_EQ(3 * 1024 * 1024, readPixelMap4->GetRawData().size());
227 LOG_INFO(UDMF_TEST, "SetData003 end.");
228 }
229
230 /**
231 * @tc.name: SetData004
232 * @tc.desc: Test set data error. Record size equals 4M.
233 * @tc.type: FUNC
234 */
235 HWTEST_F(UdmfClientSaInvokeTest, SetData004, TestSize.Level1)
236 {
237 LOG_INFO(UDMF_TEST, "SetData004 begin.");
238
239 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
240 std::vector<uint8_t> rawData;
241 for (int i = 0; i < 4 * 1024 * 1024; i++) {
242 rawData.emplace_back(1);
243 }
244 auto pixelMap = std::make_shared<SystemDefinedPixelMap>(rawData);
245 UnifiedData data;
246 data.AddRecord(pixelMap);
247 std::string key;
248 auto status = UdmfClient::GetInstance().SetData(option, data, key);
249 ASSERT_EQ(E_OK, status);
250
251 UnifiedData readData;
252 QueryOption queryOption = {.key = key};
253 status = UdmfClient::GetInstance().GetData(queryOption, readData);
254 ASSERT_EQ(E_OK, status);
255 ASSERT_EQ(1, readData.GetRecords().size());
256 auto readPixelMap1 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(0));
257 EXPECT_EQ(4 * 1024 * 1024, readPixelMap1->GetRawData().size());
258 LOG_INFO(UDMF_TEST, "SetData004 end.");
259 }
260
261 /**
262 * @tc.name: SetData005
263 * @tc.desc: Test set data error. Record type is file.
264 * @tc.type: FUNC
265 */
266 HWTEST_F(UdmfClientSaInvokeTest, SetData005, TestSize.Level1)
267 {
268 LOG_INFO(UDMF_TEST, "SetData005 begin.");
269
270 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
271 std::string uri = "https://image.jpg";
272 auto image = std::make_shared<Image>(uri);
273 UnifiedData data;
274 data.AddRecord(image);
275 std::string key;
276 auto status = UdmfClient::GetInstance().SetData(option, data, key);
277 ASSERT_EQ(E_INVALID_PARAMETERS, status);
278 LOG_INFO(UDMF_TEST, "SetData005 end.");
279 }
280
281 /**
282 * @tc.name: SetData006
283 * @tc.desc: Test set data success. Record size more than 4M and less than 15.5M.
284 * @tc.type: FUNC
285 */
286 HWTEST_F(UdmfClientSaInvokeTest, SetData006, TestSize.Level1)
287 {
288 LOG_INFO(UDMF_TEST, "SetData006 begin.");
289 SetDataTest();
290 LOG_INFO(UDMF_TEST, "SetData006 end.");
291 }
292
293 /**
294 * @tc.name: SetData007
295 * @tc.desc: Test set data error. Record size Greater than 15.5M.
296 * @tc.type: FUNC
297 */
298 HWTEST_F(UdmfClientSaInvokeTest, SetData007, TestSize.Level1)
299 {
300 LOG_INFO(UDMF_TEST, "SetData007 begin.");
301
302 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
303 std::vector<uint8_t> rawData;
304 for (int i = 0; i < 15.5 * 1024 * 1024 + 1; i++) {
305 rawData.emplace_back(1);
306 }
307 auto pixelMap = std::make_shared<SystemDefinedPixelMap>(rawData);
308 UnifiedData data;
309 data.AddRecord(pixelMap);
310 std::string key;
311 auto status = UdmfClient::GetInstance().SetData(option, data, key);
312 ASSERT_EQ(E_INVALID_PARAMETERS, status);
313 LOG_INFO(UDMF_TEST, "SetData007 end.");
314 }
315
316 /**
317 * @tc.name: SetData008
318 * @tc.desc: Test set data success. Record size equals 15.5M.
319 * @tc.type: FUNC
320 */
321 HWTEST_F(UdmfClientSaInvokeTest, SetData008, TestSize.Level1)
322 {
323 LOG_INFO(UDMF_TEST, "SetData008 begin.");
324
325 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
326 std::vector<uint8_t> rawData;
327 for (int i = 0; i < 15.5 * 1024 * 1024; i++) {
328 rawData.emplace_back(1);
329 }
330 auto pixelMap = std::make_shared<SystemDefinedPixelMap>(rawData);
331 UnifiedData data;
332 data.AddRecord(pixelMap);
333
334 std::vector<uint8_t> rawData2;
335 for (int i = 0; i < 15 * 1024 * 1024; i++) {
336 rawData2.emplace_back(1);
337 }
338 auto pixelMap2 = std::make_shared<SystemDefinedPixelMap>(rawData2);
339 data.AddRecord(pixelMap2);
340
341 std::string key;
342 auto status = UdmfClient::GetInstance().SetData(option, data, key);
343 ASSERT_EQ(E_OK, status);
344
345 UnifiedData readData;
346 QueryOption queryOption = {.key = key};
347 status = UdmfClient::GetInstance().GetData(queryOption, readData);
348 ASSERT_EQ(E_OK, status);
349 ASSERT_EQ(2, readData.GetRecords().size());
350 auto readPixelMap1 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(0));
351 EXPECT_EQ(15.5 * 1024 * 1024, readPixelMap1->GetRawData().size());
352
353 auto readPixelMap2 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(1));
354 EXPECT_EQ(15 * 1024 * 1024, readPixelMap2->GetRawData().size());
355 LOG_INFO(UDMF_TEST, "SetData008 end.");
356 }
357
358 /**
359 * @tc.name: SetData009
360 * @tc.desc: Test set data with diffient types.
361 * @tc.type: FUNC
362 */
363 HWTEST_F(UdmfClientSaInvokeTest, SetData009, TestSize.Level1)
364 {
365 LOG_INFO(UDMF_TEST, "SetData009 begin.");
366
367 CustomOption option = { .intention = Intention::UD_INTENTION_DRAG };
368 std::vector<uint8_t> rawData;
369 for (int i = 0; i < FIVE * 1024 * 1024; i++) {
370 rawData.emplace_back(1);
371 }
372 auto pixelMap = std::make_shared<SystemDefinedPixelMap>(rawData);
373 UnifiedData data;
374 data.AddRecord(pixelMap);
375
376 std::string content;
377 for (int i = 0; i < FIVE * 1024 * 1024; i++) {
378 content.push_back('a');
379 }
380
381 auto plain_text = std::make_shared<PlainText>(content, "");
382 data.AddRecord(plain_text);
383
384 std::string key;
385 auto status = UdmfClient::GetInstance().SetData(option, data, key);
386 ASSERT_EQ(E_OK, status);
387
388 UnifiedData readData;
389 QueryOption queryOption = {.key = key};
390 status = UdmfClient::GetInstance().GetData(queryOption, readData);
391 ASSERT_EQ(E_OK, status);
392 ASSERT_EQ(2, readData.GetRecords().size());
393 auto readPixelMap1 = std::static_pointer_cast<SystemDefinedPixelMap>(readData.GetRecordAt(0));
394 EXPECT_EQ(FIVE * 1024 * 1024, readPixelMap1->GetRawData().size());
395
396 auto readPlainText = std::static_pointer_cast<PlainText>(readData.GetRecordAt(1));
397 EXPECT_EQ(FIVE * 1024 * 1024, readPlainText->GetContent().size());
398 LOG_INFO(UDMF_TEST, "SetData008 end.");
399 }
400
401 /**
402 * @tc.name: SetData010
403 * @tc.desc: Test set data concurrently.
404 * @tc.type: FUNC
405 */
406 HWTEST_F(UdmfClientSaInvokeTest, SetData010, TestSize.Level0)
407 {
408 LOG_INFO(UDMF_TEST, "SetData010 begin.");
__anon2e63bda90102() 409 std::thread t1([&]() {
410 SetDataTest();
411 });
412 EXPECT_NO_FATAL_FAILURE(t1.join());
__anon2e63bda90202() 413 std::thread t2([&]() {
414 SetDataTest();
415 });
416 EXPECT_NO_FATAL_FAILURE(t2.join());
__anon2e63bda90302() 417 std::thread t3([&]() {
418 SetDataTest();
419 });
420 EXPECT_NO_FATAL_FAILURE(t3.join());
421
__anon2e63bda90402() 422 std::thread t4([&]() {
423 SetDataTest();
424 });
425 EXPECT_NO_FATAL_FAILURE(t4.join());
426 LOG_INFO(UDMF_TEST, "SetData010 end.");
427 }
428
429 } // OHOS::Test