1 /*
2 * Copyright (c) 2021 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 <algorithm>
18 #include <fstream>
19 #include <iostream>
20 #include "directory_ex.h"
21 #include "parcel.h"
22 #include "refbase.h"
23 #include "securec.h"
24 using namespace testing::ext;
25 using namespace std;
26
27 namespace OHOS {
28 namespace {
29 const int MAX_PARCEL_SIZE = 1000;
30 char g_data[MAX_PARCEL_SIZE];
31 class UtilsParcelTest : public testing::Test {
32 public:
33 static constexpr size_t DEFAULT_CPACITY = 204800; // 200K
34 static constexpr size_t CAPACITY_THRESHOLD = 4096; // 4k
35 static void TearDownTestCase(void);
36 };
37
TearDownTestCase(void)38 void UtilsParcelTest::TearDownTestCase(void)
39 {
40 for (int i = 0; i < MAX_PARCEL_SIZE; i++) {
41 g_data[i] = 0;
42 }
43 }
44
45 /*-------------------------------base data------------------------------------*/
46
47 struct TestData {
48 bool booltest;
49 int8_t int8test;
50 int16_t int16test;
51 int32_t int32test;
52 uint8_t uint8test;
53 uint16_t uint16test;
54 uint32_t uint32test;
55 };
56
WriteTestData(Parcel & parcel,const struct TestData & data)57 void WriteTestData(Parcel &parcel, const struct TestData &data)
58 {
59 bool result = false;
60
61 result = parcel.WriteBool(data.booltest);
62 EXPECT_EQ(result, true);
63
64 result = parcel.WriteInt8(data.int8test);
65 EXPECT_EQ(result, true);
66
67 result = parcel.WriteInt16(data.int16test);
68 EXPECT_EQ(result, true);
69
70 result = parcel.WriteInt32(data.int32test);
71 EXPECT_EQ(result, true);
72
73 result = parcel.WriteUint8(data.uint8test);
74 EXPECT_EQ(result, true);
75
76 result = parcel.WriteUint16(data.uint16test);
77 EXPECT_EQ(result, true);
78
79 result = parcel.WriteUint32(data.uint32test);
80 EXPECT_EQ(result, true);
81 }
82
WriteUnalignedTestData(Parcel & parcel,const struct TestData & data)83 void WriteUnalignedTestData(Parcel &parcel, const struct TestData &data)
84 {
85 bool result = false;
86
87 result = parcel.WriteBoolUnaligned(data.booltest);
88 EXPECT_EQ(result, true);
89
90 result = parcel.WriteInt8Unaligned(data.int8test);
91 EXPECT_EQ(result, true);
92
93 result = parcel.WriteInt16Unaligned(data.int16test);
94 EXPECT_EQ(result, true);
95
96 result = parcel.WriteUint8Unaligned(data.uint8test);
97 EXPECT_EQ(result, true);
98
99 result = parcel.WriteUint16Unaligned(data.uint16test);
100 EXPECT_EQ(result, true);
101 }
102
ReadTestData(Parcel & parcel,const struct TestData & data)103 void ReadTestData(Parcel &parcel, const struct TestData &data)
104 {
105 bool readbool = parcel.ReadBool();
106 EXPECT_EQ(readbool, data.booltest);
107
108 int8_t readint8 = parcel.ReadInt8();
109 EXPECT_EQ(readint8, data.int8test);
110
111 int16_t readint16 = parcel.ReadInt16();
112 EXPECT_EQ(readint16, data.int16test);
113
114 int32_t readint32 = parcel.ReadInt32();
115 EXPECT_EQ(readint32, data.int32test);
116
117 uint8_t readuint8 = parcel.ReadUint8();
118 EXPECT_EQ(readuint8, data.uint8test);
119
120 uint16_t readuint16 = parcel.ReadUint16();
121 EXPECT_EQ(readuint16, data.uint16test);
122
123 uint32_t readuint32 = parcel.ReadUint32();
124 EXPECT_EQ(readuint32, data.uint32test);
125 }
126
ReadUnalignedTestData(Parcel & parcel,const struct TestData & data)127 void ReadUnalignedTestData(Parcel &parcel, const struct TestData &data)
128 {
129 bool readbool = parcel.ReadBoolUnaligned();
130 EXPECT_EQ(readbool, data.booltest);
131
132 int8_t readint8;
133 EXPECT_TRUE(parcel.ReadInt8Unaligned(readint8));
134 EXPECT_EQ(readint8, data.int8test);
135
136 int16_t readint16;
137 EXPECT_TRUE(parcel.ReadInt16Unaligned(readint16));
138 EXPECT_EQ(readint16, data.int16test);
139
140 uint8_t readuint8;
141 EXPECT_TRUE(parcel.ReadUint8Unaligned(readuint8));
142 EXPECT_EQ(readuint8, data.uint8test);
143
144 uint16_t readuint16;
145 EXPECT_TRUE(parcel.ReadUint16Unaligned(readuint16));
146 EXPECT_EQ(readuint16, data.uint16test);
147 }
148
ReadTestDataWithTarget(Parcel & parcel,const struct TestData & data)149 void ReadTestDataWithTarget(Parcel &parcel, const struct TestData &data)
150 {
151 bool result = false;
152 bool boolVal = true;
153 result = parcel.ReadBool(boolVal);
154 EXPECT_EQ(result, true);
155 EXPECT_EQ(boolVal, data.booltest);
156
157 int8_t int8Val;
158 result = parcel.ReadInt8(int8Val);
159 EXPECT_EQ(result, true);
160 EXPECT_EQ(int8Val, data.int8test);
161
162 int16_t int16Val;
163 result = parcel.ReadInt16(int16Val);
164 EXPECT_EQ(result, true);
165 EXPECT_EQ(int16Val, data.int16test);
166
167 int32_t int32Val;
168 result = parcel.ReadInt32(int32Val);
169 EXPECT_EQ(result, true);
170 EXPECT_EQ(int32Val, data.int32test);
171
172 uint8_t uint8Val;
173 result = parcel.ReadUint8(uint8Val);
174 EXPECT_EQ(result, true);
175 EXPECT_EQ(uint8Val, data.uint8test);
176
177 uint16_t uint16Val;
178 result = parcel.ReadUint16(uint16Val);
179 EXPECT_EQ(result, true);
180 EXPECT_EQ(uint16Val, data.uint16test);
181
182 uint32_t uint32Val;
183 result = parcel.ReadUint32(uint32Val);
184 EXPECT_EQ(result, true);
185 EXPECT_EQ(uint32Val, data.uint32test);
186 }
187
188 /**
189 * Here to simulate the scenario of ipc sending data, the buffer will be released when the Parcel object is destructed.
190 */
SendData(void * & buffer,size_t size,const uint8_t * data)191 bool SendData(void *&buffer, size_t size, const uint8_t *data)
192 {
193 if (size <= 0) {
194 return false;
195 }
196 buffer = malloc(size);
197 if (buffer == nullptr) {
198 return false;
199 }
200 if (memcpy_s(buffer, size, data, size) != EOK) {
201 return false;
202 }
203 return true;
204 }
205
206 /**
207 * @tc.name: test_parcel_SetMaxCapacity_001
208 * @tc.desc: test parcel primary type read write.
209 * @tc.type: FUNC
210 */
211 HWTEST_F(UtilsParcelTest, test_parcel_SetMaxCapacity_001, TestSize.Level0)
212 {
213 size_t cap = DEFAULT_CPACITY;
214 Parcel parcel(nullptr);
215 EXPECT_TRUE(parcel.SetMaxCapacity(cap + 1));
216 EXPECT_FALSE(parcel.SetMaxCapacity(cap - 1));
217 }
218
219 /**
220 * @tc.name: test_parcel_SetAllocator_001
221 * @tc.desc: test setting allocator to parcels with and without existed allocator.
222 * @tc.type: FUNC
223 */
224 HWTEST_F(UtilsParcelTest, test_parcel_SetAllocator_001, TestSize.Level0)
225 {
226 Allocator* alloc = new DefaultAllocator();
227 Parcel parcel(alloc);
228 EXPECT_FALSE(parcel.SetAllocator(alloc));
229 EXPECT_FALSE(parcel.SetAllocator(nullptr));
230
231 struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
232
233 WriteTestData(parcel, data);
234 parcel.SetAllocator(new DefaultAllocator());
235 ReadTestData(parcel, data);
236 }
237
238 /**
239 * @tc.name: test_parcel_WriteAndRead_001
240 * @tc.desc: test parcel primary type read write.
241 * @tc.type: FUNC
242 */
243 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_001, TestSize.Level0)
244 {
245 Parcel parcel(nullptr);
246 struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
247 WriteTestData(parcel, data);
248 ReadTestData(parcel, data);
249
250 WriteUnalignedTestData(parcel, data);
251 ReadUnalignedTestData(parcel, data);
252 }
253
254 /**
255 * @tc.name: test_parcel_WriteAndRead_002
256 * @tc.desc: test parcel primary type read write.
257 * @tc.type: FUNC
258 */
259 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_002, TestSize.Level0)
260 {
261 Parcel parcel1(nullptr);
262 Parcel parcel2(nullptr);
263 struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
264 WriteTestData(parcel1, data);
265 WriteUnalignedTestData(parcel1, data);
266
267 void *buffer = nullptr;
268 size_t size = parcel1.GetDataSize();
269 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
270 ASSERT_FALSE(false);
271 }
272
273 bool result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
274 EXPECT_EQ(result, true);
275
276 ReadTestData(parcel2, data);
277 ReadUnalignedTestData(parcel2, data);
278 }
279
280 /**
281 * @tc.name: test_parcel_WriteAndRead_003
282 * @tc.desc: test parcel primary type read write.
283 * @tc.type: FUNC
284 */
285 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_003, TestSize.Level0)
286 {
287 Parcel parcel1(nullptr);
288 Parcel parcel2(nullptr);
289 struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
290 WriteTestData(parcel1, data);
291
292 void *buffer = nullptr;
293 size_t size = parcel1.GetDataSize();
294 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
295 ASSERT_FALSE(false);
296 }
297
298 bool result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
299 EXPECT_EQ(result, true);
300
301 ReadTestDataWithTarget(parcel2, data);
302 }
303
304 /**
305 * @tc.name: test_parcel_WriteAndRead_004
306 * @tc.desc: test parcel primary type read write.
307 * @tc.type: FUNC
308 */
309 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_004, TestSize.Level0)
310 {
311 Parcel parcel1(nullptr);
312 bool result;
313
314 int64_t int64test = -0x1234567887654321;
315 result = parcel1.WriteInt64(int64test);
316 EXPECT_EQ(result, true);
317
318 uint64_t uint64test = 0x1234567887654321;
319 result = parcel1.WriteUint64(uint64test);
320 EXPECT_EQ(result, true);
321
322 int64_t readint64 = parcel1.ReadInt64();
323 EXPECT_EQ(readint64, int64test);
324
325 uint64_t readuint64 = parcel1.ReadUint64();
326 EXPECT_EQ(readuint64, uint64test);
327
328 Parcel parcel2(nullptr);
329
330 void *buffer = nullptr;
331 size_t size = parcel1.GetDataSize();
332 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
333 ASSERT_FALSE(false);
334 }
335
336 result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
337
338 readint64 = parcel2.ReadInt64();
339 EXPECT_EQ(readint64, int64test);
340
341 readuint64 = parcel2.ReadUint64();
342 EXPECT_EQ(readuint64, uint64test);
343 }
344
345 /**
346 * @tc.name: test_parcel_WriteAndRead_String_001
347 * @tc.desc: test parcel string read write.
348 * @tc.type: FUNC
349 */
350 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_String_001, TestSize.Level0)
351 {
352 Parcel parcel1(nullptr);
353 bool result;
354
355 string strWrite = "test";
356 result = parcel1.WriteString(strWrite);
357 EXPECT_EQ(result, true);
358
359 string strWrite1 =
360 "test for write string padded**********************************************************##################";
361 result = parcel1.WriteString(strWrite1);
362 EXPECT_EQ(result, true);
363
364 string strWrite2 =
365 "test for write string padded**********************************************************##################";
366 result = parcel1.WriteString(strWrite2);
367 EXPECT_EQ(result, true);
368
369 string strRead = parcel1.ReadString();
370 string strRead1 = parcel1.ReadString();
371 string strRead2 = parcel1.ReadString();
372 EXPECT_EQ(0, strcmp(strRead.c_str(), strWrite.c_str()));
373 EXPECT_EQ(0, strcmp(strRead1.c_str(), strWrite1.c_str()));
374 EXPECT_EQ(0, strcmp(strRead2.c_str(), strWrite2.c_str()));
375
376 Parcel parcel2(nullptr);
377
378 void *buffer = nullptr;
379 size_t size = parcel1.GetDataSize();
380 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
381 ASSERT_FALSE(false);
382 }
383
384 result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
385
386 strRead = parcel2.ReadString();
387 strRead1 = parcel2.ReadString();
388 strRead2 = parcel2.ReadString();
389 EXPECT_EQ(0, strcmp(strRead.c_str(), strWrite.c_str()));
390 EXPECT_EQ(0, strcmp(strRead1.c_str(), strWrite1.c_str()));
391 EXPECT_EQ(0, strcmp(strRead2.c_str(), strWrite2.c_str()));
392 }
393
394 /**
395 * @tc.name: test_parcel_WriteAndRead_String_002
396 * @tc.desc: test parcel string read write.
397 * @tc.type: FUNC
398 */
399 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_String_002, TestSize.Level0)
400 {
401 Parcel parcel1(nullptr);
402
403 u16string str16Write = u"12345";
404 bool result = parcel1.WriteString16(str16Write);
405 EXPECT_EQ(result, true);
406
407 u16string str16Write2 = u"12345 test for write16string padded*********";
408 result = parcel1.WriteString16(str16Write2);
409 EXPECT_EQ(result, true);
410
411 u16string str16Read = parcel1.ReadString16();
412 u16string str16Read2 = parcel1.ReadString16();
413 EXPECT_EQ(0, str16Read.compare(str16Write));
414 EXPECT_EQ(0, str16Read2.compare(str16Write2));
415
416 Parcel parcel2(nullptr);
417
418 void *buffer = nullptr;
419 size_t size = parcel1.GetDataSize();
420 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
421 ASSERT_FALSE(false);
422 }
423
424 result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
425
426 str16Read = parcel2.ReadString16();
427 str16Read2 = parcel2.ReadString16();
428 EXPECT_EQ(0, str16Read.compare(str16Write));
429 EXPECT_EQ(0, str16Read2.compare(str16Write2));
430 }
431
432 /**
433 * @tc.name: test_parcel_WriteAndRead_String_003
434 * @tc.desc: test parcel CString read write.
435 * @tc.type: FUNC
436 */
437 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_String_003, TestSize.Level0)
438 {
439 bool result;
440 Parcel parcel(nullptr);
441 string test1 = "12345";
442 string test2 = "23456";
443 string test3 = "34567";
444 string test4 = "45678";
445 result = parcel.WriteCString(nullptr);
446 EXPECT_FALSE(result);
447 result = parcel.WriteCString(test1.c_str());
448 EXPECT_TRUE(result);
449 result = parcel.WriteCString(test2.c_str());
450 EXPECT_TRUE(result);
451 result = parcel.WriteCString(test3.c_str());
452 EXPECT_TRUE(result);
453 result = parcel.WriteCString(test4.c_str());
454 EXPECT_TRUE(result);
455
456 EXPECT_EQ(0, strcmp(test1.c_str(), parcel.ReadCString()));
457 EXPECT_EQ(0, strcmp(test2.c_str(), parcel.ReadCString()));
458 EXPECT_EQ(0, strcmp(test3.c_str(), parcel.ReadCString()));
459 EXPECT_EQ(0, strcmp(test4.c_str(), parcel.ReadCString()));
460 }
461
462 /**
463 * @tc.name: test_parcel_WriteAndRead_String004
464 * @tc.desc: test parcel CString read write.
465 * @tc.type: FUNC
466 */
467 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_String004, TestSize.Level0)
468 {
469 Parcel parcel1(nullptr);
470 bool result = false;
471
472 // write from Java, read from C++
473 result = parcel1.WriteString16WithLength(nullptr, 0);
474 EXPECT_EQ(result, true);
475
476 u16string str16write = u"12345";
477 char16_t *value1 = str16write.data();
478 result = parcel1.WriteString16WithLength(value1, str16write.length());
479 EXPECT_EQ(result, true);
480
481 u16string str16write2 = u"12345 test for write16string padded*********";
482 char16_t *value2 = str16write2.data();
483 result = parcel1.WriteString16WithLength(value2, str16write2.length());
484 EXPECT_EQ(result, true);
485
486 u16string str16readNull = parcel1.ReadString16();
487 u16string str16read1 = parcel1.ReadString16();
488 u16string str16read2 = parcel1.ReadString16();
489 EXPECT_EQ(0, str16readNull.compare(std::u16string()));
490 EXPECT_EQ(0, str16read1.compare(str16write));
491 EXPECT_EQ(0, str16read2.compare(str16write2));
492
493 // write from C++, read from Java
494 result = parcel1.WriteString16(str16write);
495 EXPECT_EQ(result, true);
496
497 result = parcel1.WriteString16(str16write2);
498 EXPECT_EQ(result, true);
499
500 int32_t readLength1 = 0;
501 u16string str16read3 = parcel1.ReadString16WithLength(readLength1);
502 EXPECT_EQ(readLength1, static_cast<int32_t>(str16write.length()));
503
504 int32_t readLength2 = 0;
505 u16string str16read4 = parcel1.ReadString16WithLength(readLength2);
506 EXPECT_EQ(readLength2, static_cast<int32_t>(str16write2.length()));
507
508 EXPECT_EQ(0, str16read3.compare(str16write));
509 EXPECT_EQ(0, str16read4.compare(str16write2));
510 }
511
512 /**
513 * @tc.name: test_parcel_WriteAndRead_String005
514 * @tc.desc: test parcel CString read write.
515 * @tc.type: FUNC
516 */
517 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_String005, TestSize.Level0)
518 {
519 Parcel parcel1(nullptr);
520 bool result = false;
521
522 // write from Java, read from C++
523 result = parcel1.WriteString8WithLength(nullptr, 0);
524 EXPECT_EQ(result, true);
525
526 string str8write = "12345";
527 char *value1 = str8write.data();
528 result = parcel1.WriteString8WithLength(value1, str8write.length());
529 EXPECT_EQ(result, true);
530
531 string str8write2 = "12345 test for write16string padded*********";
532 char *value2 = str8write2.data();
533 result = parcel1.WriteString8WithLength(value2, str8write2.length());
534 EXPECT_EQ(result, true);
535
536 string str8readNull = parcel1.ReadString();
537 string str8read1 = parcel1.ReadString();
538 string str8read2 = parcel1.ReadString();
539 EXPECT_EQ(0, str8readNull.compare(std::string()));
540 EXPECT_EQ(0, str8read1.compare(str8write));
541 EXPECT_EQ(0, str8read2.compare(str8write2));
542
543 // write from C++, read from Java
544 result = parcel1.WriteString(str8write);
545 EXPECT_EQ(result, true);
546
547 result = parcel1.WriteString(str8write2);
548 EXPECT_EQ(result, true);
549
550 int32_t readLength1 = 0;
551 string str8read3 = parcel1.ReadString8WithLength(readLength1);
552 EXPECT_EQ(readLength1, static_cast<int32_t>(str8write.length()));
553
554 int32_t readLength2 = 0;
555 string str8read4 = parcel1.ReadString8WithLength(readLength2);
556 EXPECT_EQ(readLength2, static_cast<int32_t>(str8write2.length()));
557
558 EXPECT_EQ(0, str8read3.compare(str8write));
559 EXPECT_EQ(0, str8read4.compare(str8write2));
560 }
561
562 /**
563 * @tc.name: test_parcel_WriteAndRead_Float_001
564 * @tc.desc: test parcel float types read write.
565 * @tc.type: FUNC
566 */
567 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_Float_001, TestSize.Level0)
568 {
569 Parcel parcel1(nullptr);
570 bool result;
571
572 float floatwrite = 12.345678f;
573 result = parcel1.WriteFloat(floatwrite);
574 EXPECT_EQ(result, true);
575
576 double doublewrite = 1345.7653;
577 result = parcel1.WriteDouble(doublewrite);
578 EXPECT_EQ(result, true);
579
580 float floatread;
581 result = parcel1.ReadFloat(floatread);
582 EXPECT_EQ(result, true);
583 EXPECT_EQ(floatwrite, floatread);
584
585 double doubleread;
586 doubleread = parcel1.ReadDouble();
587 EXPECT_EQ(doublewrite, doubleread);
588
589 Parcel parcel2(nullptr);
590
591 void *buffer = nullptr;
592 size_t size = parcel1.GetDataSize();
593 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
594 ASSERT_FALSE(false);
595 }
596
597 result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
598 result = parcel2.ReadFloat(floatread);
599 EXPECT_EQ(result, true);
600 EXPECT_EQ(floatwrite, floatread);
601
602 doubleread = parcel2.ReadDouble();
603 EXPECT_EQ(doublewrite, doubleread);
604 }
605
606 /**
607 * @tc.name: test_parcel_WriteAndRead_String_005
608 * @tc.desc: test parcel String type read write.
609 * @tc.type: FUNC
610 */
611 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndRead_String_005, TestSize.Level0)
612 {
613 Parcel parcel1(nullptr);
614 bool result;
615
616 string strwrite = "test";
617 result = parcel1.WriteString(strwrite);
618 EXPECT_EQ(result, true);
619
620 string strwrite1 =
621 "test for write string padded**********************************************************##################";
622 result = parcel1.WriteString(strwrite1);
623 EXPECT_EQ(result, true);
624
625 string strwrite2 =
626 "test for write string padded**********************************************************##################";
627 result = parcel1.WriteString(strwrite2);
628 EXPECT_EQ(result, true);
629
630 string strread;
631 string strread1;
632 string strread2;
633 result = parcel1.ReadString(strread);
634 EXPECT_EQ(result, true);
635 result = parcel1.ReadString(strread1);
636 EXPECT_EQ(result, true);
637 result = parcel1.ReadString(strread2);
638 EXPECT_EQ(result, true);
639 EXPECT_EQ(0, strcmp(strread.c_str(), strwrite.c_str()));
640 EXPECT_EQ(0, strcmp(strread1.c_str(), strwrite1.c_str()));
641 EXPECT_EQ(0, strcmp(strread2.c_str(), strwrite2.c_str()));
642
643 Parcel parcel2(nullptr);
644
645 void *buffer = nullptr;
646 size_t size = parcel1.GetDataSize();
647 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
648 ASSERT_FALSE(false);
649 }
650
651 result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
652 EXPECT_EQ(result, true);
653
654 result = parcel2.ReadString(strread);
655 EXPECT_EQ(result, true);
656 result = parcel2.ReadString(strread1);
657 EXPECT_EQ(result, true);
658 result = parcel2.ReadString(strread2);
659 EXPECT_EQ(result, true);
660 EXPECT_EQ(0, strcmp(strread.c_str(), strwrite.c_str()));
661 EXPECT_EQ(0, strcmp(strread1.c_str(), strwrite1.c_str()));
662 EXPECT_EQ(0, strcmp(strread2.c_str(), strwrite2.c_str()));
663 }
664
665 struct Padded {
666 char title;
667 int32_t handle;
668 uint64_t cookie;
669 };
670
671 struct Unpadded {
672 char tip;
673 };
674
ValidatePadded(const struct Padded & left,const struct Padded & right)675 void ValidatePadded(const struct Padded &left, const struct Padded &right)
676 {
677 EXPECT_EQ(left.title, right.title);
678 EXPECT_EQ(left.handle, right.handle);
679 EXPECT_EQ(left.cookie, right.cookie);
680 }
681
ValidateUnpadded(const struct Unpadded & left,const struct Unpadded & right)682 void ValidateUnpadded(const struct Unpadded &left, const struct Unpadded &right)
683 {
684 EXPECT_EQ(left.tip, right.tip);
685 }
686
687 /**
688 * @tc.name: test_CalcNewCapacity_001
689 * @tc.desc: test kinds of input to CalcNewCapacity.
690 * @tc.type: FUNC
691 */
692 HWTEST_F(UtilsParcelTest, test_CalcNewCapacity_001, TestSize.Level0)
693 {
694 Parcel parcel;
695 bool ret;
696
697 size_t newMaxCapacity;
698 size_t minNewCapacity = CAPACITY_THRESHOLD;
699 const string strLenThreshd = string(minNewCapacity, 't');
700 ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strLenThreshd.data()), minNewCapacity);
701 EXPECT_EQ(true, ret); // calculated capacity = CAPACITY_THRESHOLD
702
703 newMaxCapacity = CAPACITY_THRESHOLD - 1;
704 minNewCapacity = newMaxCapacity;
705 const string strLessThreshd = string(minNewCapacity, 'l');
706 parcel.SetMaxCapacity(newMaxCapacity);
707 ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strLessThreshd.data()), minNewCapacity);
708 EXPECT_EQ(true, ret); // calculated capacity = newMaxCapacity
709
710 newMaxCapacity = -1; // minNewCapacity = CAPACITY_THRESHOLD - 1
711 const string strNoMaxCap = string(minNewCapacity, 'n');
712 parcel.SetMaxCapacity(newMaxCapacity);
713 ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strNoMaxCap.data()), minNewCapacity);
714 EXPECT_EQ(ret, true); // calculated capacity = CAPACITY_THRESHOLD
715
716 minNewCapacity = CAPACITY_THRESHOLD + 1; // newMaxCapacity = -1
717 const string strExtThreshd = string(minNewCapacity, 'e');
718 parcel.SetMaxCapacity(newMaxCapacity);
719 ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strExtThreshd.data()), minNewCapacity);
720 EXPECT_EQ(ret, true); // calculated capacity = 2 * CAPACITY_THRESHOLD
721
722 newMaxCapacity = CAPACITY_THRESHOLD; // minNewCapacity = CAPACITY_THRESHOLD + 1
723 const string strCapThreshd = string(minNewCapacity, 'e');
724 parcel.SetMaxCapacity(newMaxCapacity);
725 ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strCapThreshd.data()), minNewCapacity);
726 EXPECT_EQ(ret, true); // calculated capacity = CAPACITY_THRESHOLD
727 }
728
729 /**
730 * @tc.name: test_SetDataCapacity_001
731 * @tc.desc: test kinds of input to SetDataCapacity.
732 * @tc.type: FUNC
733 */
734 HWTEST_F(UtilsParcelTest, test_SetDataCapacity_001, TestSize.Level0)
735 {
736 Parcel parcel;
737 bool result;
738 struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
739
740 WriteTestData(parcel, data);
741 result = parcel.SetDataCapacity(0);
742 EXPECT_FALSE(result);
743 }
744
745 /**
746 * @tc.name: test_SetDataSize_001
747 * @tc.desc: test kinds of input to SetDataSize.
748 * @tc.type: FUNC
749 */
750 HWTEST_F(UtilsParcelTest, test_SetDataSize_001, TestSize.Level0)
751 {
752 Parcel parcel;
753 bool result;
754 result = parcel.SetDataCapacity(sizeof(bool));
755 EXPECT_TRUE(result);
756 result = parcel.WriteBool(true);
757 EXPECT_TRUE(result);
758 result = parcel.SetDataSize(DEFAULT_CPACITY + 1);
759 EXPECT_FALSE(result);
760 }
761
762 /**
763 * @tc.name: test_parcel_Data_Structure_001
764 * @tc.desc: test parcel struct data related function.
765 * @tc.type: FUNC
766 */
767 HWTEST_F(UtilsParcelTest, test_parcel_Data_Structure_001, TestSize.Level0)
768 {
769 Parcel parcel(nullptr);
770 bool result;
771
772 const struct Padded pad = { 'p', 0x34567890, -0x2345678998765432 };
773 const struct Unpadded unpad = { 'u' };
774
775 result = parcel.WriteBuffer(static_cast<const void *>(&pad), sizeof(struct Padded));
776 EXPECT_EQ(true, result);
777 const struct Padded *padRead = reinterpret_cast<const struct Padded *>(parcel.ReadBuffer(sizeof(struct Padded)));
778 ValidatePadded(*padRead, pad);
779 EXPECT_EQ(parcel.GetWritePosition(), parcel.GetReadPosition());
780
781 result = parcel.WriteBuffer(static_cast<const void *>(&unpad), sizeof(struct Unpadded));
782 const struct Unpadded *unpadRead =
783 reinterpret_cast<const struct Unpadded *>(parcel.ReadBuffer(sizeof(struct Unpadded)));
784 ValidateUnpadded(*unpadRead, unpad);
785 EXPECT_NE(parcel.GetWritePosition(), parcel.GetReadPosition());
786
787 parcel.RewindRead(0);
788 parcel.RewindWrite(0);
789 EXPECT_EQ(parcel.GetWritePosition(), parcel.GetReadPosition());
790
791 result = parcel.WriteUnpadBuffer(static_cast<const void *>(&pad), sizeof(struct Padded));
792 EXPECT_EQ(true, result);
793 const struct Padded *padReadNew =
794 reinterpret_cast<const struct Padded *>(parcel.ReadUnpadBuffer(sizeof(struct Padded)));
795 ValidatePadded(*padReadNew, pad);
796 EXPECT_EQ(parcel.GetWritePosition(), parcel.GetReadPosition());
797
798 result = parcel.WriteUnpadBuffer(static_cast<const void *>(&unpad), sizeof(struct Unpadded));
799 EXPECT_EQ(true, result);
800 const struct Unpadded *unpadReadNew =
801 reinterpret_cast<const struct Unpadded *>(parcel.ReadUnpadBuffer(sizeof(struct Unpadded)));
802 ValidateUnpadded(*unpadReadNew, unpad);
803 EXPECT_EQ(parcel.GetWritePosition(), parcel.GetReadPosition());
804 }
805
806 /**
807 * @tc.name: test_parcel_Data_Structure_002
808 * @tc.desc: test invalid input to WriteBuffer and WriteBufferAddTerminator.
809 * @tc.type: FUNC
810 */
811 HWTEST_F(UtilsParcelTest, test_parcel_Data_Structure_002, TestSize.Level0)
812 {
813 Parcel parcel(nullptr);
814 bool result;
815
816 const string str = "test invalid input";
817 const string strOverflow = "test write with SIZE_MAX bytes";
818 const string strWriteFail = string((DEFAULT_CPACITY+1)/sizeof(char), 'f');
819 const string strWriteTermFail = string((DEFAULT_CPACITY-2)/sizeof(char), 't');
820
821 result = parcel.WriteBuffer(nullptr, sizeof(string));
822 EXPECT_EQ(false, result);
823 result = parcel.WriteBufferAddTerminator(nullptr, sizeof(string), sizeof(char));
824 EXPECT_EQ(false, result);
825
826 result = parcel.WriteBuffer(static_cast<const void *>(str.data()), 0);
827 EXPECT_EQ(false, result);
828 result = parcel.WriteBufferAddTerminator(static_cast<const void *>(str.data()), 0, sizeof(char));
829 EXPECT_EQ(false, result);
830
831 result = parcel.WriteBuffer(static_cast<const void *>(strOverflow.data()), SIZE_MAX);
832 EXPECT_EQ(false, result);
833 result = parcel.WriteBufferAddTerminator(static_cast<const void *>(strOverflow.data()),
834 SIZE_MAX, sizeof(char));
835 EXPECT_EQ(false, result);
836
837 result = parcel.WriteBuffer(static_cast<const void *>(strWriteFail.data()), strWriteFail.length());
838 EXPECT_EQ(false, result);
839 result = parcel.WriteBufferAddTerminator(static_cast<const void *>(strWriteFail.data()),
840 strWriteFail.length(), sizeof(char));
841 EXPECT_EQ(false, result);
842
843 result = parcel.WriteBufferAddTerminator(static_cast<const void *>(str.data()), str.length(), sizeof(char));
844 EXPECT_EQ(true, result);
845
846 Parcel recvParcel(nullptr);
847 void *buffer = nullptr;
848 size_t size = parcel.GetDataSize();
849 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel.GetData()))) {
850 ASSERT_FALSE(false);
851 }
852 result = recvParcel.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel.GetDataSize());
853 EXPECT_EQ(result, true);
854 result = recvParcel.WriteBufferAddTerminator(static_cast<const void *>(&str), str.length() + 1, sizeof(char));
855 EXPECT_EQ(result, false);
856 }
857
858 struct VectorTestData {
859 vector<bool> booltest = { false, false, true, false, true };
860 vector<int8_t> int8test = { 0x01, 0x10, -0x20, 0x30, 0x40 };
861 vector<int16_t> int16test = { 0x1234, -0x2345, 0x3456, -0x4567, 0x5678 };
862 vector<int32_t> int32test = { 0x12345678, -0x23456789, 0x34567890, -0x45678901 };
863 vector<int64_t> int64test = { 0x1234567887654321, -0x2345678998765432 };
864 vector<uint8_t> uint8test = { 0x01, 0x10, 0x20, 0x30, 0x40 };
865 vector<uint16_t> uint16test = { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
866 vector<uint32_t> uint32test = { 0x12345678, 0x23456789, 0x34567890, 0x45678901 };
867 vector<uint64_t> uint64test = { 0x1234567887654321, 0x2345678998765432 };
868 };
869
WriteVectorTestData(Parcel & parcel,const VectorTestData & data)870 void WriteVectorTestData(Parcel &parcel, const VectorTestData &data)
871 {
872 bool result = parcel.WriteBoolVector(data.booltest);
873 EXPECT_EQ(result, true);
874 result = parcel.WriteInt8Vector(data.int8test);
875 EXPECT_EQ(result, true);
876 result = parcel.WriteInt16Vector(data.int16test);
877 EXPECT_EQ(result, true);
878 result = parcel.WriteInt32Vector(data.int32test);
879 EXPECT_EQ(result, true);
880 result = parcel.WriteInt64Vector(data.int64test);
881 EXPECT_EQ(result, true);
882 result = parcel.WriteUInt8Vector(data.uint8test);
883 EXPECT_EQ(result, true);
884 result = parcel.WriteUInt16Vector(data.uint16test);
885 EXPECT_EQ(result, true);
886 result = parcel.WriteUInt32Vector(data.uint32test);
887 EXPECT_EQ(result, true);
888 result = parcel.WriteUInt64Vector(data.uint64test);
889 EXPECT_EQ(result, true);
890 }
891
ReadVectorTestData(Parcel & parcel,const VectorTestData & data)892 void ReadVectorTestData(Parcel &parcel, const VectorTestData &data)
893 {
894 vector<bool> boolread;
895 vector<int8_t> int8read;
896 vector<int16_t> int16read;
897 vector<int32_t> int32read;
898 vector<int64_t> int64read;
899
900 vector<uint8_t> uint8read;
901 vector<uint16_t> uint16read;
902 vector<uint32_t> uint32read;
903 vector<uint64_t> uint64read;
904
905 bool result = parcel.ReadBoolVector(&boolread);
906 EXPECT_EQ(result, true);
907 for (size_t i = 0; i < data.booltest.size(); i++) {
908 EXPECT_EQ(data.booltest[i], boolread[i]);
909 }
910
911 result = parcel.ReadInt8Vector(&int8read);
912 EXPECT_EQ(result, true);
913 for (size_t i = 0; i < data.int8test.size(); i++) {
914 EXPECT_EQ(data.int8test[i], int8read[i]);
915 }
916
917 result = parcel.ReadInt16Vector(&int16read);
918 EXPECT_EQ(result, true);
919 for (size_t i = 0; i < data.int16test.size(); i++) {
920 EXPECT_EQ(data.int16test[i], int16read[i]);
921 }
922
923 result = parcel.ReadInt32Vector(&int32read);
924 EXPECT_EQ(result, true);
925 for (size_t i = 0; i < data.int32test.size(); i++) {
926 EXPECT_EQ(data.int32test[i], int32read[i]);
927 }
928
929 result = parcel.ReadInt64Vector(&int64read);
930 EXPECT_EQ(result, true);
931 for (size_t i = 0; i < data.int64test.size(); i++) {
932 EXPECT_EQ(data.int64test[i], int64read[i]);
933 }
934
935 result = parcel.ReadUInt8Vector(&uint8read);
936 EXPECT_EQ(result, true);
937 for (size_t i = 0; i < data.uint8test.size(); i++) {
938 EXPECT_EQ(data.uint8test[i], uint8read[i]);
939 }
940
941 result = parcel.ReadUInt16Vector(&uint16read);
942 EXPECT_EQ(result, true);
943 for (size_t i = 0; i < data.uint16test.size(); i++) {
944 EXPECT_EQ(data.uint16test[i], uint16read[i]);
945 }
946
947 result = parcel.ReadUInt32Vector(&uint32read);
948 EXPECT_EQ(result, true);
949 for (size_t i = 0; i < data.uint32test.size(); i++) {
950 EXPECT_EQ(data.uint32test[i], uint32read[i]);
951 }
952
953 result = parcel.ReadUInt64Vector(&uint64read);
954 EXPECT_EQ(result, true);
955 for (size_t i = 0; i < data.uint64test.size(); i++) {
956 EXPECT_EQ(data.uint64test[i], uint64read[i]);
957 }
958 }
959
960 /**
961 * @tc.name: test_parcel_WriteAndReadVector_001
962 * @tc.desc: test vector parcel read and write.
963 * @tc.type: FUNC
964 */
965 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndReadVector_001, TestSize.Level0)
966 {
967 Parcel parcel(nullptr);
968 struct VectorTestData data;
969
970 WriteVectorTestData(parcel, data);
971 ReadVectorTestData(parcel, data);
972 }
973
974 /**
975 * @tc.name: test_parcel_WriteAndReadVector_002
976 * @tc.desc: test vector parcel read and write.
977 * @tc.type: FUNC
978 */
979 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndReadVector_002, TestSize.Level0)
980 {
981 Parcel parcel1(nullptr);
982 struct VectorTestData data;
983 WriteVectorTestData(parcel1, data);
984
985 Parcel parcel2(nullptr);
986
987 void *buffer = nullptr;
988 size_t size = parcel1.GetDataSize();
989 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
990 ASSERT_FALSE(false);
991 }
992
993 bool result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
994 EXPECT_EQ(result, true);
995 ReadVectorTestData(parcel2, data);
996 }
997
998 /**
999 * @tc.name: test_parcel_WriteAndReadVector_003
1000 * @tc.desc: test vector parcel read and write.
1001 * @tc.type: FUNC
1002 */
1003 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndReadVector_003, TestSize.Level0)
1004 {
1005 Parcel parcel1(nullptr);
1006 Parcel parcel2(nullptr);
1007 bool result;
1008 vector<string> stringtest{ "test", "test for", "test for write", "test for write vector" };
1009 vector<u16string> string16test{ u"test", u"test for", u"test for write", u"test for write vector" };
1010
1011 result = parcel1.WriteStringVector(stringtest);
1012 EXPECT_EQ(result, true);
1013 result = parcel1.WriteString16Vector(string16test);
1014 EXPECT_EQ(result, true);
1015
1016 vector<string> stringread;
1017 result = parcel1.ReadStringVector(&stringread);
1018 EXPECT_EQ(result, true);
1019 for (size_t i = 0; i < stringtest.size(); i++) {
1020 EXPECT_EQ(stringtest[i], stringread[i]);
1021 }
1022
1023 vector<u16string> u16stringread;
1024 result = parcel1.ReadString16Vector(&u16stringread);
1025 EXPECT_EQ(result, true);
1026 for (size_t i = 0; i < string16test.size(); i++) {
1027 EXPECT_EQ(0, string16test[i].compare(u16stringread[i]));
1028 }
1029
1030 void *buffer = nullptr;
1031 size_t size = parcel1.GetDataSize();
1032 if (!SendData(buffer, size, reinterpret_cast<const uint8_t *>(parcel1.GetData()))) {
1033 ASSERT_FALSE(false);
1034 }
1035
1036 result = parcel2.ParseFrom(reinterpret_cast<uintptr_t>(buffer), parcel1.GetDataSize());
1037 result = parcel2.ReadStringVector(&stringread);
1038 EXPECT_EQ(result, true);
1039 for (size_t i = 0; i < stringtest.size(); i++) {
1040 EXPECT_EQ(stringtest[i], stringread[i]);
1041 }
1042
1043 result = parcel2.ReadString16Vector(&u16stringread);
1044 EXPECT_EQ(result, true);
1045 for (size_t i = 0; i < string16test.size(); i++) {
1046 EXPECT_EQ(0, string16test[i].compare(u16stringread[i]));
1047 }
1048 }
1049
1050 /**
1051 * @tc.name: test_parcel_WriteAndReadVector_004
1052 * @tc.desc: test vector parcel read and write.
1053 * @tc.type: FUNC
1054 */
1055 HWTEST_F(UtilsParcelTest, test_parcel_WriteAndReadVector_004, TestSize.Level0)
1056 {
1057 Parcel parcel1(nullptr);
1058 Parcel parcel2(nullptr);
1059 bool result;
1060 vector<float> floattest{ 11221.132313, 11221.45678 };
1061 vector<double> doubletest{ 1122.132313, 1122.45678 };
1062
1063 result = parcel1.WriteFloatVector(floattest);
1064 EXPECT_EQ(result, true);
1065 result = parcel1.WriteDoubleVector(doubletest);
1066 EXPECT_EQ(result, true);
1067
1068 vector<float> floatread;
1069 vector<double> doubleread;
1070
1071 result = parcel1.ReadFloatVector(&floatread);
1072 EXPECT_EQ(result, true);
1073 for (size_t i = 0; i < floattest.size(); i++) {
1074 EXPECT_EQ(floattest[i], floatread[i]);
1075 }
1076
1077 result = parcel1.ReadDoubleVector(&doubleread);
1078 EXPECT_EQ(result, true);
1079 for (size_t i = 0; i < doubletest.size(); i++) {
1080 EXPECT_EQ(doubletest[i], doubleread[i]);
1081 }
1082 }
1083
1084 class TestParcelable : public virtual Parcelable {
1085 public:
1086 TestParcelable() = default;
1087 ~TestParcelable() = default;
1088
1089 bool Marshalling(Parcel &parcel) const override;
1090 static TestParcelable *Unmarshalling(Parcel &parcel);
1091
1092 public:
1093 int32_t int32Write_ = -0x12345678;
1094 int32_t int32Read_;
1095 };
1096
Marshalling(Parcel & parcel) const1097 bool TestParcelable::Marshalling(Parcel &parcel) const
1098 {
1099 bool result = parcel.WriteInt32(this->int32Write_);
1100 return result;
1101 }
1102
Unmarshalling(Parcel & parcel)1103 TestParcelable *TestParcelable::Unmarshalling(Parcel &parcel)
1104 {
1105 auto *read = new TestParcelable();
1106 read->int32Read_ = parcel.ReadInt32();
1107 return read;
1108 }
1109
1110 /**
1111 * @tc.name: test_parcel_parcelable_001
1112 * @tc.desc: test parcel read and write parcelable obj.
1113 * @tc.type: FUNC
1114 */
1115 HWTEST_F(UtilsParcelTest, test_parcel_parcelable_001, TestSize.Level0)
1116 {
1117 Parcel parcel(nullptr);
1118 sptr<TestParcelable> parcelableWrite = new TestParcelable();
1119 bool result = false;
1120
1121 result = parcel.WriteParcelable(parcelableWrite);
1122 EXPECT_EQ(true, result);
1123 EXPECT_EQ(parcel.GetWritePosition(), parcel.GetDataSize());
1124
1125 sptr<TestParcelable> parcelableRead = parcel.ReadParcelable<TestParcelable>();
1126 EXPECT_EQ(parcelableWrite->int32Write_, parcelableRead->int32Read_);
1127 EXPECT_EQ(parcel.GetReadPosition(), parcel.GetDataSize());
1128 }
1129
1130 /**
1131 * @tc.name: test_parcel_parcelable_002
1132 * @tc.desc: test parcel read and write parcelable obj.
1133 * @tc.type: FUNC
1134 */
1135 HWTEST_F(UtilsParcelTest, test_parcel_parcelable_002, TestSize.Level0)
1136 {
1137 Parcel parcel(nullptr);
1138
1139 bool result = parcel.WriteParcelable(nullptr);
1140 EXPECT_EQ(true, result);
1141
1142 sptr<TestParcelable> parcelableRead = parcel.ReadParcelable<TestParcelable>();
1143 EXPECT_EQ(nullptr, parcelableRead);
1144 }
1145
1146 /**
1147 * @tc.name: test_parcel_parcelable_003
1148 * @tc.desc: test parcel read and write parcelable obj.
1149 * @tc.type: FUNC
1150 */
1151 HWTEST_F(UtilsParcelTest, test_parcel_parcelable_003, TestSize.Level0)
1152 {
1153 Parcel parcel(nullptr);
1154 sptr<TestParcelable> parcelableWriteNull;
1155 bool result = parcel.WriteStrongParcelable(parcelableWriteNull);
1156 EXPECT_EQ(true, result);
1157
1158 sptr<TestParcelable> parcelableWrite = new TestParcelable();
1159
1160 bool test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1161 EXPECT_EQ(false, test);
1162 test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::IPC);
1163 EXPECT_EQ(false, test);
1164 test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::RPC);
1165 EXPECT_EQ(false, test);
1166
1167 result = parcel.WriteStrongParcelable(parcelableWrite);
1168 EXPECT_EQ(true, result);
1169
1170 sptr<TestParcelable> parcelableReadNull = parcel.ReadParcelable<TestParcelable>();
1171 EXPECT_EQ(nullptr, parcelableReadNull);
1172
1173 sptr<TestParcelable> parcelableRead = parcel.ReadParcelable<TestParcelable>();
1174 EXPECT_EQ(parcelableWrite->int32Write_, parcelableRead->int32Read_);
1175
1176 test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1177 EXPECT_EQ(true, test);
1178 test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::IPC);
1179 EXPECT_EQ(false, test);
1180 test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::RPC);
1181 EXPECT_EQ(false, test);
1182
1183 parcelableWrite->ClearBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1184 test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1185 EXPECT_EQ(false, test);
1186 }
1187
1188 /**
1189 * @tc.name: test_SetMaxCapacity_001
1190 * @tc.desc: test parcel capacity function.
1191 * @tc.type: FUNC
1192 */
1193 HWTEST_F(UtilsParcelTest, test_SetMaxCapacity_001, TestSize.Level0)
1194 {
1195 Parcel parcel(nullptr);
1196 char test[48] = {0};
1197 bool ret;
1198 ret = parcel.WriteBuffer(test, 48);
1199 EXPECT_EQ(true, ret);
1200 // because default maxCap is 200 * 1024, so reset it more
1201 parcel.SetMaxCapacity(201 * 1024);
1202 // test write data over max capacity: 205780 + 48 > 201 * 1024
1203 char test2[205780] = {0};
1204 ret = parcel.WriteBuffer(test2, 205780);
1205 EXPECT_EQ(false, ret);
1206 }
1207
1208 /**
1209 * @tc.name: test_SetMaxCapacity_002
1210 * @tc.desc: test parcel capacity function.
1211 * @tc.type: FUNC
1212 */
1213 HWTEST_F(UtilsParcelTest, test_SetMaxCapacity_002, TestSize.Level0)
1214 {
1215 Parcel parcel(nullptr);
1216 char test[48] = {0};
1217 bool ret;
1218 ret = parcel.WriteInt32(5767168);
1219 EXPECT_EQ(true, ret);
1220 ret = parcel.WriteBuffer(test, 48);
1221 EXPECT_EQ(true, ret);
1222 vector<std::u16string> val;
1223 ret = parcel.ReadString16Vector(&val);
1224 EXPECT_EQ(false, ret);
1225 }
1226 } // namespace
1227 } // namespace OHOS