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
16
17 #define LOG_TAG "TlvUtilTest"
18
19 #include "tlv_util.h"
20 #include <gtest/gtest.h>
21 #include "unified_data.h"
22 #include "logger.h"
23 #include "plain_text.h"
24 #include "html.h"
25 #include "link.h"
26 #include "system_defined_appitem.h"
27 #include "application_defined_record.h"
28 #include "file.h"
29 #include "system_defined_form.h"
30 #include "system_defined_pixelmap.h"
31 #include "system_defined_record.h"
32 #include "udmf_conversion.h"
33 #include "unified_meta.h"
34 #include "unified_record.h"
35 #include "unified_types.h"
36
37 using namespace testing::ext;
38 using namespace OHOS::UDMF;
39 using namespace OHOS;
40
41 namespace OHOS::Test {
42 class TlvUtilTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45
46 static void TearDownTestCase(void);
47
48 void SetUp();
49
50 void TearDown();
51 };
52
SetUpTestCase(void)53 void TlvUtilTest::SetUpTestCase(void) {}
TearDownTestCase(void)54 void TlvUtilTest::TearDownTestCase(void) {}
SetUp(void)55 void TlvUtilTest::SetUp(void) {}
TearDown(void)56 void TlvUtilTest::TearDown(void) {}
57
58 /* *
59 * @tc.name: CountBufferSize_001
60 * @tc.desc: test fundamental for countBufferSize
61 * @tc.type: FUNC
62 */
HWTEST_F(TlvUtilTest, CountBufferSize_001, TestSize.Level1)63 HWTEST_F(TlvUtilTest, CountBufferSize_001, TestSize.Level1)
64 {
65 LOG_INFO(UDMF_TEST, "CountBufferSize_001 begin.");
66 std::vector<uint8_t> dataBytes;
67 auto tlvObject = TLVObject(dataBytes);
68 uint8_t num1 = 1;
69 EXPECT_EQ(sizeof(TLVHead) + sizeof(num1), TLVUtil::CountBufferSize(num1, tlvObject)); // 7
70 uint16_t num2 = 1;
71 EXPECT_EQ(sizeof(TLVHead) + sizeof(num2), TLVUtil::CountBufferSize(num2, tlvObject)); // 8
72 uint32_t num3 = 1;
73 EXPECT_EQ(sizeof(TLVHead) + sizeof(num3), TLVUtil::CountBufferSize(num3, tlvObject)); // 10
74 int16_t num4 = 1;
75 EXPECT_EQ(sizeof(TLVHead) + sizeof(num4), TLVUtil::CountBufferSize(num4, tlvObject)); // 8
76 int32_t num5 = 1;
77 EXPECT_EQ(sizeof(TLVHead) + sizeof(num5), TLVUtil::CountBufferSize(num5, tlvObject)); // 10
78 bool boolean = true;
79 EXPECT_EQ(sizeof(TLVHead) + sizeof(boolean), TLVUtil::CountBufferSize(boolean, tlvObject)); // 7
80 double doubleNum = 1;
81 EXPECT_EQ(sizeof(TLVHead) + sizeof(doubleNum), TLVUtil::CountBufferSize(doubleNum, tlvObject)); // 14
82 EXPECT_EQ(sizeof(TLVHead), TLVUtil::CountBufferSize(std::nullptr_t(), tlvObject)); // 6
83 std::monostate monostate;
84 EXPECT_EQ(sizeof(TLVHead), TLVUtil::CountBufferSize(monostate, tlvObject)); // 6
85 std::string str = "abc";
86 EXPECT_EQ(sizeof(TLVHead) + str.size(), TLVUtil::CountBufferSize(str, tlvObject)); // 9
87 UDType type = XML;
88 EXPECT_EQ(sizeof(TLVHead) + sizeof(type), TLVUtil::CountBufferSize(type, tlvObject)); // 10
89 DataStatus status = HISTORY;
90 EXPECT_EQ(sizeof(TLVHead) + sizeof(status), TLVUtil::CountBufferSize(status, tlvObject)); // 10
91 std::vector<uint8_t> u8Vector = { 1, 1, 1 };
92 EXPECT_EQ(sizeof(TLVHead) + u8Vector.size(), TLVUtil::CountBufferSize(u8Vector, tlvObject)); // 9
93 std::shared_ptr<uint8_t> ptr;
94 EXPECT_EQ(sizeof(TLVHead), TLVUtil::CountBufferSize(ptr, tlvObject)); // 6
95 ptr = std::make_shared<uint8_t>(1);
96 EXPECT_EQ(sizeof(TLVHead) + 1, TLVUtil::CountBufferSize(ptr, tlvObject)); // 7
97 LOG_INFO(UDMF_TEST, "CountBufferSize_001 end.");
98 }
99
100 /* *
101 * @tc.name: CountBufferSize_002
102 * @tc.desc: test STL for countBufferSize
103 * @tc.type: FUNC
104 */
HWTEST_F(TlvUtilTest, CountBufferSize_002, TestSize.Level1)105 HWTEST_F(TlvUtilTest, CountBufferSize_002, TestSize.Level1)
106 {
107 LOG_INFO(UDMF_TEST, "CountBufferSize_002 begin.");
108 std::vector<uint8_t> dataBytes;
109 auto tlvObject = TLVObject(dataBytes);
110 Privilege privilege1;
111 privilege1.readPermission = "111";
112 Privilege privilege2;
113 privilege2.readPermission = "111";
114 privilege2.writePermission = "xx";
115 std::vector<Privilege> privilegeVector{ privilege1, privilege2 };
116 EXPECT_EQ(10 * sizeof(TLVHead) + 2 * sizeof(uint32_t) + sizeof(size_t) + 8,
117 TLVUtil::CountBufferSize(privilegeVector, tlvObject)); // 80
118
119 Object object;
120 std::map<std::string, ValueType> map;
121 map["keyString"] = "value";
122 double key = 12;
123 map["keyNum"] = key;
124 object.value_ = map;
125 EXPECT_EQ(12 * sizeof(TLVHead) + 36, TLVUtil::CountBufferSize(object, tlvObject)); // 108
126 auto total = tlvObject.GetTotal();
127 EXPECT_EQ(188, total);
128 LOG_INFO(UDMF_TEST, "CountBufferSize_002 end.");
129 }
130
131 /* *
132 * @tc.name: CountBufferSize_003
133 * @tc.desc: test udmf for countBufferSize
134 * @tc.type: FUNC
135 */
HWTEST_F(TlvUtilTest, CountBufferSize_003, TestSize.Level1)136 HWTEST_F(TlvUtilTest, CountBufferSize_003, TestSize.Level1)
137 {
138 LOG_INFO(UDMF_TEST, "CountBufferSize_003 begin.");
139 std::vector<uint8_t> dataBytes;
140 auto tlvObject = TLVObject(dataBytes);
141
142 std::shared_ptr<Object> object = std::make_shared<Object>();
143 std::map<std::string, ValueType> map;
144 map["uniformDataType"] = UtdUtils::GetUtdIdFromUtdEnum(UDType::PLAIN_TEXT);
145 map["textContent"] = "content";
146 map["abstract"] = "abstract";
147 object->value_ = map;
148 std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(PLAIN_TEXT, object);
149
150 std::vector<std::shared_ptr<UnifiedRecord>> vector = { record };
151 UnifiedData data;
152 data.SetRecords(vector);
153 std::vector<UnifiedData> unifiedData = { data };
154 auto size = TLVUtil::CountBufferSize(unifiedData, tlvObject);
155 EXPECT_EQ(tlvObject.GetTotal(), size); // 269
156 LOG_INFO(UDMF_TEST, "CountBufferSize_003 end.");
157 }
158
159 /* *
160 * @tc.name: CountBufferSize_004
161 * @tc.desc: test other for countBufferSize
162 * @tc.type: FUNC
163 */
HWTEST_F(TlvUtilTest, CountBufferSize_004, TestSize.Level1)164 HWTEST_F(TlvUtilTest, CountBufferSize_004, TestSize.Level1)
165 {
166 LOG_INFO(UDMF_TEST, "CountBufferSize_004 begin.");
167 std::vector<uint8_t> dataBytes;
168 auto tlvObject = TLVObject(dataBytes);
169
170 UnifiedKey key;
171 key.key = "123456";
172 key.intention = "DRAG";
173 key.bundleName = "com.xxx";
174 EXPECT_EQ(5 * sizeof(TLVHead) + 17, TLVUtil::CountBufferSize(key, tlvObject));
175
176 Privilege privilege;
177 EXPECT_EQ(4 * sizeof(TLVHead) + sizeof(int32_t), TLVUtil::CountBufferSize(privilege, tlvObject));
178
179 Runtime runtime;
180 EXPECT_EQ(19 * sizeof(TLVHead) + sizeof(bool) + sizeof(size_t) + 2 * sizeof(int64_t) + 2 * sizeof(int32_t) +
181 2 * sizeof(uint32_t),
182 TLVUtil::CountBufferSize(runtime, tlvObject));
183 LOG_INFO(UDMF_TEST, "CountBufferSize_004 end.");
184 }
185
186 /* *
187 * @tc.name: WritingAndReading_001
188 * @tc.desc: test fundamental for Writing And Reading
189 * @tc.type: FUNC
190 */
HWTEST_F(TlvUtilTest, WritingAndReading_001, TestSize.Level1)191 HWTEST_F(TlvUtilTest, WritingAndReading_001, TestSize.Level1)
192 {
193 LOG_INFO(UDMF_TEST, "WritingAndReading_001 begin.");
194 std::vector<uint8_t> dataBytes;
195 auto tlvObject = TLVObject(dataBytes);
196 uint16_t num1 = 1;
197 auto result = TLVUtil::Writing(num1, tlvObject, TAG::TAG_UINT16);
198 int8_t num2 = 2;
199 result = TLVUtil::Writing(num2, tlvObject, TAG::TAG_INT8) && result;
200 uint32_t num3 = 3;
201 result = TLVUtil::Writing(num3, tlvObject, TAG::TAG_UINT32) && result;
202 int16_t num4 = 4;
203 result = TLVUtil::Writing(num4, tlvObject, TAG::TAG_INT16) && result;
204 int32_t num5 = 5;
205 result = result = TLVUtil::Writing(num5, tlvObject, TAG::TAG_INT32) && result;
206 bool boolean = true;
207 result = TLVUtil::Writing(boolean, tlvObject, TAG::TAG_BOOL) && result;
208 result = TLVUtil::Writing(std::nullptr_t(), tlvObject, TAG::TAG_NULL);
209 std::monostate monostate;
210 result = TLVUtil::Writing(monostate, tlvObject, TAG::TAG_MONOSTATE) && result;
211 std::string str = "abc";
212 result = TLVUtil::Writing(str, tlvObject, TAG::TAG_STRING) && result;
213 UDType type = XML;
214 result = TLVUtil::Writing(type, tlvObject, TAG::TAG_UD_TYPE) && result;
215 DataStatus status = HISTORY;
216 result = TLVUtil::Writing(status, tlvObject, TAG::TAG_DATA_STATUS) && result;
217 std::vector<uint8_t> u8Vector = { 1, 2, 3 };
218 result = TLVUtil::Writing(u8Vector, tlvObject, TAG::TAG_UINT8) && result;
219 EXPECT_TRUE(result);
220
221 uint16_t num1Result;
222 int8_t num2Result;
223 uint32_t num3Result;
224 int16_t num4Result;
225 int32_t num5Result;
226 bool booleanResult;
227 std::string strResult;
228 UDType typeResult;
229 DataStatus statusResult;
230 std::vector<uint8_t> u8VectorResult;
231
232 tlvObject.ResetCursor();
233 TLVUtil::ReadTlv(statusResult, tlvObject, TAG::TAG_DATA_STATUS);
234 tlvObject.ResetCursor();
235 EXPECT_EQ(status, statusResult);
236
237 tlvObject.ResetCursor();
238 TLVUtil::ReadTlv(typeResult, tlvObject, TAG::TAG_UD_TYPE);
239 EXPECT_EQ(type, typeResult);
240
241 tlvObject.ResetCursor();
242 TLVUtil::ReadTlv(strResult, tlvObject, TAG::TAG_STRING);
243 EXPECT_EQ(str, strResult);
244
245 tlvObject.ResetCursor();
246 TLVUtil::ReadTlv(booleanResult, tlvObject, TAG::TAG_BOOL);
247 EXPECT_EQ(boolean, booleanResult);
248
249 tlvObject.ResetCursor();
250 TLVUtil::ReadTlv(num5Result, tlvObject, TAG::TAG_INT32);
251 EXPECT_EQ(num5, num5Result);
252
253 tlvObject.ResetCursor();
254 TLVUtil::ReadTlv(num4Result, tlvObject, TAG::TAG_INT16);
255 EXPECT_EQ(num4, num4Result);
256
257 tlvObject.ResetCursor();
258 TLVUtil::ReadTlv(num3Result, tlvObject, TAG::TAG_UINT32);
259 EXPECT_EQ(num3, num3Result);
260
261 tlvObject.ResetCursor();
262 TLVUtil::ReadTlv(num2Result, tlvObject, TAG::TAG_INT8);
263 EXPECT_EQ(num2, num2Result);
264
265 tlvObject.ResetCursor();
266 TLVUtil::ReadTlv(num1Result, tlvObject, TAG::TAG_UINT16);
267 EXPECT_EQ(num1, num1Result);
268
269 tlvObject.ResetCursor();
270 TLVUtil::ReadTlv(u8VectorResult, tlvObject, TAG::TAG_UINT8);
271 for (int i = 0; i < 3; i++) {
272 EXPECT_EQ(u8Vector[i], u8VectorResult[i]);
273 }
274 LOG_INFO(UDMF_TEST, "WritingAndReading_001 end.");
275 }
276
277 /* *
278 * @tc.name: WritingAndReading_002
279 * @tc.desc: test Runtime for Writing And Reading
280 * @tc.type: FUNC
281 */
HWTEST_F(TlvUtilTest, WritingAndReading_002, TestSize.Level1)282 HWTEST_F(TlvUtilTest, WritingAndReading_002, TestSize.Level1)
283 {
284 LOG_INFO(UDMF_TEST, "WritingAndReading_002 begin.");
285 UnifiedKey key;
286 key.key = "123456";
287 Privilege privilege;
288 privilege.readPermission = "read";
289 privilege.tokenId = 333;
290 Privilege privilege2;
291 privilege2.writePermission = "read";
292 privilege2.tokenId = 444;
293 Runtime runtime;
294 runtime.dataStatus = DELETED;
295 runtime.key = key;
296 runtime.privileges.push_back(privilege);
297 runtime.privileges.push_back(privilege2);
298 runtime.createTime = 1;
299 runtime.dataVersion = 3;
300 runtime.createPackage = "package";
301 runtime.isPrivate = true;
302
303 std::vector<uint8_t> dataBytes;
304 auto tlvObject = TLVObject(dataBytes);
305 EXPECT_TRUE(TLVUtil::Writing(runtime, tlvObject, TAG::TAG_RUNTIME));
306
307 tlvObject.ResetCursor();
308 Runtime runtimeResult;
309 EXPECT_TRUE(TLVUtil::ReadTlv(runtimeResult, tlvObject, TAG::TAG_RUNTIME));
310 EXPECT_EQ(runtime.key.key, runtimeResult.key.key);
311 EXPECT_EQ(runtime.key.key, runtimeResult.key.key);
312 EXPECT_EQ(runtime.dataStatus, runtimeResult.dataStatus);
313 EXPECT_EQ(runtime.createTime, runtimeResult.createTime);
314 EXPECT_EQ(runtime.dataVersion, runtimeResult.dataVersion);
315 EXPECT_EQ(runtime.createPackage, runtimeResult.createPackage);
316 EXPECT_EQ(runtime.isPrivate, runtimeResult.isPrivate);
317 EXPECT_EQ(runtime.privileges[0].readPermission, runtimeResult.privileges[0].readPermission);
318 EXPECT_EQ(runtime.privileges[0].tokenId, runtimeResult.privileges[0].tokenId);
319 EXPECT_EQ(runtime.privileges[1].writePermission, runtimeResult.privileges[1].writePermission);
320 EXPECT_EQ(runtime.privileges[1].tokenId, runtimeResult.privileges[1].tokenId);
321
322 LOG_INFO(UDMF_TEST, "WritingAndReading_002 end.");
323 }
324
325 /* *
326 * @tc.name: WritingAndReading_003
327 * @tc.desc: test UnifiedData for Writing And Reading
328 * @tc.type: FUNC
329 */
HWTEST_F(TlvUtilTest, WritingAndReading_003, TestSize.Level1)330 HWTEST_F(TlvUtilTest, WritingAndReading_003, TestSize.Level1)
331 {
332 LOG_INFO(UDMF_TEST, "WritingAndReading_003 begin.");
333
334 std::map<std::string, ValueType> value;
335 value["fileType"] = "File Type";
336 value["fileUri"] = "File Uri";
337 std::shared_ptr<Object> obj = std::make_shared<Object>();
338 obj->value_ = value;
339 std::shared_ptr<UnifiedRecord> fileUri = std::make_shared<UnifiedRecord>(UDType::FILE_URI, obj);
340
341 std::shared_ptr<UnifiedRecord> plainText = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "this is a content");
342 std::shared_ptr<UnifiedRecord> html = std::make_shared<Html>(UDType::HTML, "this is a HTML content");
343
344 std::vector<std::shared_ptr<UnifiedRecord>> records = { fileUri, plainText, html };
345
346 UnifiedData data1;
347 data1.SetRecords(records);
348
349 std::shared_ptr<SystemDefinedAppItem> appItem =
350 std::make_shared<SystemDefinedAppItem>(UDType::SYSTEM_DEFINED_APP_ITEM, "OTHER param");
351 appItem->SetAppId("com.demo");
352 std::shared_ptr<ApplicationDefinedRecord> defineRecord =
353 std::make_shared<ApplicationDefinedRecord>(UDType::APPLICATION_DEFINED_RECORD, "OTHER param");
354 std::vector<uint8_t> u8Vector = { 1, 2, 3, 4 };
355 defineRecord->SetRawData(u8Vector);
356 std::shared_ptr<UnifiedRecord> file = std::make_shared<File>(UDType::FILE, "this is a oriUri");
357
358 std::vector<std::shared_ptr<UnifiedRecord>> records2 = { appItem, defineRecord, file };
359
360 UnifiedData data2;
361 data2.SetRecords(records2);
362
363 std::vector<UnifiedData> datas = { data1, data2 };
364
365 std::vector<uint8_t> dataBytes;
366 auto tlvObject = TLVObject(dataBytes);
367
368 UdmfConversion::InitValueObject(datas);
369 EXPECT_TRUE(TLVUtil::Writing(datas, tlvObject, TAG::TAG_UNIFIED_DATA));
370
371 tlvObject.ResetCursor();
372 std::vector<UnifiedData> datasResult;
373
374 EXPECT_TRUE(TLVUtil::ReadTlv(datasResult, tlvObject, TAG::TAG_UNIFIED_DATA));
375 EXPECT_EQ(2, datasResult.size());
376 UdmfConversion::ConvertRecordToSubclass(datasResult);
377
378 auto recordsResult = datasResult[0].GetRecords();
379 EXPECT_EQ(3, recordsResult.size());
380
381 auto fileUriResult = recordsResult[0];
382 EXPECT_EQ(UDType::FILE_URI, fileUriResult->GetType());
383 auto fileUriValue = fileUriResult->GetValue();
384 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(fileUriValue));
385 auto fileUriObj = std::get<std::shared_ptr<Object>>(fileUriValue);
386 EXPECT_EQ("File Uri", std::get<std::string>(fileUriObj->value_["fileUri"]));
387 EXPECT_EQ("File Type", std::get<std::string>(fileUriObj->value_["fileType"]));
388
389 auto plainTextResult = recordsResult[1];
390 EXPECT_EQ(UDType::PLAIN_TEXT, plainTextResult->GetType());
391 auto plainTextSubclass = std::static_pointer_cast<PlainText>(plainTextResult);
392 EXPECT_EQ("this is a content", plainTextSubclass->GetContent());
393 auto plainTextValue = plainTextSubclass->GetValue();
394 EXPECT_TRUE(std::holds_alternative<std::string>(plainTextValue));
395
396 auto htmlResult = recordsResult[2];
397 EXPECT_EQ(UDType::HTML, htmlResult->GetType());
398 auto htmlSubclass = std::static_pointer_cast<Html>(htmlResult);
399 EXPECT_EQ("this is a HTML content", htmlSubclass->GetHtmlContent());
400 auto htmlValue = htmlSubclass->GetValue();
401 EXPECT_TRUE(std::holds_alternative<std::string>(htmlValue));
402
403 auto recordsResult2 = datasResult[1].GetRecords();
404 EXPECT_EQ(3, recordsResult2.size());
405 auto appItemResult = recordsResult2[0];
406 EXPECT_EQ(UDType::SYSTEM_DEFINED_APP_ITEM, appItemResult->GetType());
407 auto appItemSubclass = std::static_pointer_cast<SystemDefinedAppItem>(appItemResult);
408 EXPECT_EQ("com.demo", appItemSubclass->GetAppId());
409 auto appItemValue = appItemSubclass->GetValue();
410 EXPECT_TRUE(std::holds_alternative<std::string>(appItemValue));
411
412 auto defineRecordResult = recordsResult2[1];
413 EXPECT_EQ(UDType::APPLICATION_DEFINED_RECORD, defineRecordResult->GetType());
414 auto adefineRecordSubclass = std::static_pointer_cast<ApplicationDefinedRecord>(defineRecordResult);
415 auto u8VectorResult = adefineRecordSubclass->GetRawData();
416 EXPECT_EQ(4, u8VectorResult.size());
417 auto adefineRecordValue = adefineRecordSubclass->GetValue();
418 EXPECT_TRUE(std::holds_alternative<std::string>(adefineRecordValue));
419
420 auto fileResult = recordsResult2[2];
421 EXPECT_EQ(UDType::FILE, fileResult->GetType());
422 auto fileSubclass = std::static_pointer_cast<File>(fileResult);
423 EXPECT_EQ(16, fileSubclass->GetSize());
424 auto fileValue = fileSubclass->GetValue();
425 EXPECT_TRUE(std::holds_alternative<std::string>(fileValue));
426
427 LOG_INFO(UDMF_TEST, "WritingAndReading_003 end.");
428 }
429
430 /* *
431 * @tc.name: WritingAndReading_004
432 * @tc.desc: test UnifiedData for Writing And Reading
433 * @tc.type: FUNC
434 */
HWTEST_F(TlvUtilTest, WritingAndReading_004, TestSize.Level1)435 HWTEST_F(TlvUtilTest, WritingAndReading_004, TestSize.Level1)
436 {
437 LOG_INFO(UDMF_TEST, "WritingAndReading_004 begin.");
438
439 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
440 OHOS::Media::InitializationOptions opts = { { 5, 7 },
441 Media::PixelFormat::ARGB_8888,
442 Media::PixelFormat::ARGB_8888 };
443 std::unique_ptr<OHOS::Media::PixelMap> pixelMap =
444 OHOS::Media::PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
445 std::shared_ptr<OHOS::Media::PixelMap> pixelMapIn = move(pixelMap);
446 std::map<std::string, ValueType> value;
447 value["pixelMap"] = pixelMapIn;
448 std::shared_ptr<Object> obj = std::make_shared<Object>();
449 obj->value_ = value;
450 std::shared_ptr<UnifiedRecord> pixelMapRecord =
451 std::make_shared<SystemDefinedPixelMap>(UDType::SYSTEM_DEFINED_PIXEL_MAP, obj);
452
453 std::shared_ptr<SystemDefinedForm> form =
454 std::make_shared<SystemDefinedForm>(UDType::SYSTEM_DEFINED_FORM, "Other parm");
455 form->SetFormName("HAPPY DAY");
456
457 UDDetails details;
458 details.emplace("name", "ZhangSan");
459 details.emplace("age", 30);
460 details.emplace("isFemal", true);
461 std::shared_ptr<SystemDefinedRecord> definedRecord =
462 std::make_shared<SystemDefinedRecord>(UDType::SYSTEM_DEFINED_RECORD, "Other parm");
463 definedRecord->SetDetails(details);
464 std::vector<std::shared_ptr<UnifiedRecord>> records = { pixelMapRecord, form, definedRecord };
465
466 UnifiedData data;
467 data.SetRecords(records);
468
469 std::vector<UnifiedData> datas = { data };
470
471 std::vector<uint8_t> dataBytes;
472 auto tlvObject = TLVObject(dataBytes);
473
474 UdmfConversion::InitValueObject(datas);
475 EXPECT_TRUE(TLVUtil::Writing(datas, tlvObject, TAG::TAG_UNIFIED_DATA));
476
477 tlvObject.ResetCursor();
478 std::vector<UnifiedData> datasResult;
479
480 EXPECT_TRUE(TLVUtil::ReadTlv(datasResult, tlvObject, TAG::TAG_UNIFIED_DATA));
481 EXPECT_EQ(1, datasResult.size());
482 UdmfConversion::ConvertRecordToSubclass(datasResult);
483
484 auto recordsResult = datasResult[0].GetRecords();
485 EXPECT_EQ(3, recordsResult.size());
486
487 auto pixelMapRecordResult = recordsResult[0];
488 EXPECT_EQ(UDType::SYSTEM_DEFINED_PIXEL_MAP, pixelMapRecordResult->GetType());
489 auto pixelMapValue = pixelMapRecordResult->GetValue();
490 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(pixelMapValue));
491 auto pixelMapObj = std::get<std::shared_ptr<Object>>(pixelMapValue);
492 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(pixelMapObj->value_["pixelMap"]));
493 auto piexelMapResult = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(pixelMapObj->value_["pixelMap"]);
494 EXPECT_EQ(7, piexelMapResult->GetHeight());
495
496 auto formResult = recordsResult[1];
497 EXPECT_EQ(UDType::SYSTEM_DEFINED_FORM, formResult->GetType());
498 auto formSubclass = std::static_pointer_cast<SystemDefinedForm>(formResult);
499 EXPECT_EQ("HAPPY DAY", formSubclass->GetFormName());
500 auto formValue = formSubclass->GetValue();
501 EXPECT_TRUE(std::holds_alternative<std::string>(formValue));
502
503 auto definedRecordResult = recordsResult[2];
504 EXPECT_EQ(UDType::SYSTEM_DEFINED_RECORD, definedRecordResult->GetType());
505 auto definedRecordSubclass = std::static_pointer_cast<SystemDefinedRecord>(definedRecordResult);
506 auto detailsRecord = definedRecordSubclass->GetDetails();
507 EXPECT_EQ("ZhangSan", std::get<std::string>(detailsRecord["name"]));
508 EXPECT_EQ(30, std::get<int32_t>(detailsRecord["age"]));
509 EXPECT_TRUE(std::get<bool>(detailsRecord["isFemal"]));
510 auto definedRecordValue = definedRecordSubclass->GetValue();
511 EXPECT_TRUE(std::holds_alternative<std::string>(definedRecordValue));
512
513 LOG_INFO(UDMF_TEST, "WritingAndReading_004 end.");
514 }
515
516 /* *
517 * @tc.name: WritingAndReading_005
518 * @tc.desc: test Want for Writing And Reading
519 * @tc.type: FUNC
520 */
HWTEST_F(TlvUtilTest, WritingAndReading_005, TestSize.Level1)521 HWTEST_F(TlvUtilTest, WritingAndReading_005, TestSize.Level1)
522 {
523 LOG_INFO(UDMF_TEST, "WritingAndReading_005 begin.");
524 std::shared_ptr<OHOS::AAFwk::Want> want = std::make_shared<OHOS::AAFwk::Want>();
525 std::string idKey = "id";
526 int32_t idValue = 123;
527 want->SetParam(idKey, idValue);
528 std::map<std::string, ValueType> value;
529 value["want"] = want;
530 std::shared_ptr<Object> obj = std::make_shared<Object>();
531 obj->value_ = value;
532
533 std::vector<uint8_t> dataBytes;
534 auto tlvObject = TLVObject(dataBytes);
535
536 EXPECT_TRUE(TLVUtil::Writing(obj, tlvObject, TAG::TAG_OBJECT_VALUE));
537 tlvObject.ResetCursor();
538 std::shared_ptr<Object> objResult = std::make_shared<Object>();
539 EXPECT_TRUE(TLVUtil::ReadTlv(objResult, tlvObject, TAG::TAG_OBJECT_VALUE));
540 auto valueResult = objResult->value_;
541 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<OHOS::AAFwk::Want>>(valueResult["want"]));
542 auto wantResult = std::get<std::shared_ptr<OHOS::AAFwk::Want>>(valueResult["want"]);
543 EXPECT_EQ(idValue, wantResult->GetIntParam(idKey, 0));
544 LOG_INFO(UDMF_TEST, "WritingAndReading_005 end.");
545 }
546
547 /* *
548 * @tc.name: WritingAndReadingFile_001
549 * @tc.desc: test Unified Data for Writing And Reading
550 * @tc.type: FUNC
551 */
HWTEST_F(TlvUtilTest, WritingAndReadingFile_001, TestSize.Level1)552 HWTEST_F(TlvUtilTest, WritingAndReadingFile_001, TestSize.Level1)
553 {
554 LOG_INFO(UDMF_TEST, "WritingAndReadingFile_001 begin.");
555 std::map<std::string, ValueType> value;
556 value["fileType"] = "File Type";
557 value["fileUri"] = "File Uri";
558 std::shared_ptr<Object> obj = std::make_shared<Object>();
559 obj->value_ = value;
560 std::shared_ptr<UnifiedRecord> fileUri = std::make_shared<UnifiedRecord>(UDType::FILE_URI, obj);
561
562 std::shared_ptr<UnifiedRecord> plainText = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "this is a content");
563 std::shared_ptr<UnifiedRecord> html = std::make_shared<Html>(UDType::HTML, "this is a HTML content");
564
565 std::vector<std::shared_ptr<UnifiedRecord>> records = { fileUri, plainText, html };
566
567 UnifiedData data1;
568 data1.SetRecords(records);
569
570 std::shared_ptr<SystemDefinedAppItem> appItem =
571 std::make_shared<SystemDefinedAppItem>(UDType::SYSTEM_DEFINED_APP_ITEM, "OTHER param");
572 appItem->SetAppId("com.demo");
573 std::shared_ptr<ApplicationDefinedRecord> defineRecord =
574 std::make_shared<ApplicationDefinedRecord>(UDType::APPLICATION_DEFINED_RECORD, "OTHER param");
575 std::vector<uint8_t> u8Vector = { 1, 2, 3, 4 };
576 defineRecord->SetRawData(u8Vector);
577 std::shared_ptr<UnifiedRecord> fileRecord = std::make_shared<File>(UDType::FILE, "this is a oriUri");
578
579 std::vector<std::shared_ptr<UnifiedRecord>> records2 = { appItem, defineRecord, fileRecord };
580
581 UnifiedData data2;
582 data2.SetRecords(records2);
583
584 std::vector<UnifiedData> datas = { data1, data2 };
585
586 std::string dataFile = "demo1";
587 std::vector<uint8_t> dataBytes;
588 auto tlvObject = TLVObject(dataBytes);
589
590 std::FILE *file = fopen(dataFile.c_str(), "w+");
591 tlvObject.SetFile(file);
592 UdmfConversion::InitValueObject(datas);
593 EXPECT_TRUE(TLVUtil::Writing(datas, tlvObject, TAG::TAG_UNIFIED_DATA));
594
595 tlvObject.ResetCursor();
596 std::vector<UnifiedData> datasResult;
597
598 EXPECT_TRUE(TLVUtil::ReadTlv(datasResult, tlvObject, TAG::TAG_UNIFIED_DATA));
599 EXPECT_EQ(2, datasResult.size());
600 UdmfConversion::ConvertRecordToSubclass(datasResult);
601
602 auto recordsResult = datasResult[0].GetRecords();
603 EXPECT_EQ(3, recordsResult.size());
604
605 auto fileUriResult = recordsResult[0];
606 EXPECT_EQ(UDType::FILE_URI, fileUriResult->GetType());
607 auto fileUriValue = fileUriResult->GetValue();
608 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(fileUriValue));
609 auto fileUriObj = std::get<std::shared_ptr<Object>>(fileUriValue);
610 EXPECT_EQ("File Uri", std::get<std::string>(fileUriObj->value_["fileUri"]));
611 EXPECT_EQ("File Type", std::get<std::string>(fileUriObj->value_["fileType"]));
612
613 auto plainTextResult = recordsResult[1];
614 EXPECT_EQ(UDType::PLAIN_TEXT, plainTextResult->GetType());
615 auto plainTextSubclass = std::static_pointer_cast<PlainText>(plainTextResult);
616 EXPECT_EQ("this is a content", plainTextSubclass->GetContent());
617 auto plainTextValue = plainTextSubclass->GetValue();
618 EXPECT_TRUE(std::holds_alternative<std::string>(plainTextValue));
619
620 auto htmlResult = recordsResult[2];
621 EXPECT_EQ(UDType::HTML, htmlResult->GetType());
622 auto htmlSubclass = std::static_pointer_cast<Html>(htmlResult);
623 EXPECT_EQ("this is a HTML content", htmlSubclass->GetHtmlContent());
624 auto htmlValue = htmlSubclass->GetValue();
625 EXPECT_TRUE(std::holds_alternative<std::string>(htmlValue));
626
627 auto recordsResult2 = datasResult[1].GetRecords();
628 EXPECT_EQ(3, recordsResult2.size());
629 auto appItemResult = recordsResult2[0];
630 EXPECT_EQ(UDType::SYSTEM_DEFINED_APP_ITEM, appItemResult->GetType());
631 auto appItemSubclass = std::static_pointer_cast<SystemDefinedAppItem>(appItemResult);
632 EXPECT_EQ("com.demo", appItemSubclass->GetAppId());
633 auto appItemValue = appItemSubclass->GetValue();
634 EXPECT_TRUE(std::holds_alternative<std::string>(appItemValue));
635
636 auto defineRecordResult = recordsResult2[1];
637 EXPECT_EQ(UDType::APPLICATION_DEFINED_RECORD, defineRecordResult->GetType());
638 auto adefineRecordSubclass = std::static_pointer_cast<ApplicationDefinedRecord>(defineRecordResult);
639 auto u8VectorResult = adefineRecordSubclass->GetRawData();
640 EXPECT_EQ(4, u8VectorResult.size());
641 auto adefineRecordValue = adefineRecordSubclass->GetValue();
642 EXPECT_TRUE(std::holds_alternative<std::string>(adefineRecordValue));
643
644 auto fileResult = recordsResult2[2];
645 EXPECT_EQ(UDType::FILE, fileResult->GetType());
646 auto fileSubclass = std::static_pointer_cast<File>(fileResult);
647 EXPECT_EQ(16, fileSubclass->GetSize());
648 auto fileValue = fileSubclass->GetValue();
649 EXPECT_TRUE(std::holds_alternative<std::string>(fileValue));
650
651 fclose(file);
652 LOG_INFO(UDMF_TEST, "WritingAndReadingFile_001 end.");
653 }
654 }
655