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 "distributeddb_tools_unit_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "parcel.h"
21
22 using namespace testing::ext;
23 using namespace DistributedDB;
24 using namespace DistributedDBUnitTest;
25 using namespace std;
26
27 class DistributedDBParcelTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase(void)35 void DistributedDBParcelTest::SetUpTestCase(void)
36 {
37 }
38
TearDownTestCase(void)39 void DistributedDBParcelTest::TearDownTestCase(void)
40 {
41 }
42
SetUp(void)43 void DistributedDBParcelTest::SetUp(void)
44 {
45 DistributedDBToolsUnitTest::PrintTestCaseInfo();
46 }
47
TearDown(void)48 void DistributedDBParcelTest::TearDown(void)
49 {
50 }
51
52 /**
53 * @tc.name: WriteInt001
54 * @tc.desc: write and read a integer.
55 * @tc.type: FUNC
56 * @tc.require: AR000CQE0U
57 * @tc.author: weifeng
58 */
59 HWTEST_F(DistributedDBParcelTest, WriteInt001, TestSize.Level1)
60 {
61 /**
62 * @tc.steps: step1. create a vector, and write it into a buffer;
63 * @tc.expected: step1. write ok;
64 */
65 int writeData1 = INT_MAX;
66 uint32_t writeData2 = UINT32_MAX;
67 uint64_t writeData3 = 0;
68
69 uint32_t len = Parcel::GetIntLen() + Parcel::GetUInt32Len() + Parcel::GetUInt64Len();
70 len = Parcel::GetEightByteAlign(len);
71 uint8_t *buf = new (nothrow) uint8_t[len];
72 ASSERT_NE(buf, nullptr);
73 Parcel writeParcel(buf, len);
74 int ret = writeParcel.WriteInt(writeData1);
75 EXPECT_TRUE(ret == E_OK);
76 ret = writeParcel.WriteUInt32(writeData2);
77 EXPECT_TRUE(ret == E_OK);
78 ret = writeParcel.WriteUInt64(writeData3);
79 EXPECT_TRUE(ret == E_OK);
80 // write overflow
81 ret = writeParcel.WriteUInt64(writeData3);
82 EXPECT_TRUE(ret != E_OK);
83
84 /**
85 * @tc.steps: step2. read the vector, the vector should same as written vector;
86 * @tc.expected: step1. read out vector same as written vector;
87 */
88 int readData1;
89 uint32_t readData2;
90 uint64_t readData3;
91 Parcel readParcel(buf, len);
92 uint32_t readLen = readParcel.ReadInt(readData1);
93 EXPECT_TRUE(readLen == Parcel::GetIntLen());
94 readLen += readParcel.ReadUInt32(readData2);
95 EXPECT_TRUE(readLen == Parcel::GetIntLen() + Parcel::GetUInt32Len());
96 readLen += readParcel.ReadUInt64(readData3);
97 EXPECT_TRUE(readLen == Parcel::GetIntLen() + Parcel::GetUInt32Len() + Parcel::GetUInt64Len());
98 EXPECT_TRUE(!readParcel.IsError());
99 EXPECT_TRUE(readData1 == writeData1);
100 EXPECT_TRUE(readData2 == writeData2);
101 EXPECT_TRUE(readData3 == writeData3);
102 // read overflow
103 readLen = readParcel.ReadUInt64(readData3);
104 EXPECT_TRUE(readParcel.IsError());
105 EXPECT_TRUE(readLen == 0);
106 delete []buf;
107 }
108
109 /**
110 * @tc.name: WriteVector001
111 * @tc.desc: write and read a vector<uint8_t>.
112 * @tc.type: FUNC
113 * @tc.require: AR000CQE0U
114 * @tc.author: weifeng
115 */
116 HWTEST_F(DistributedDBParcelTest, WriteVector001, TestSize.Level1)
117 {
118 /**
119 * @tc.steps: step1. create a vector, and write it into a buffer;
120 * @tc.expected: step1. write ok;
121 */
122 vector<uint8_t> writeData = {1, 2, 5, 7, 20, 30, 99};
123 uint32_t len = Parcel::GetVectorLen<uint8_t>(writeData);
124 uint8_t *buf = new (nothrow) uint8_t[len];
125 ASSERT_NE(buf, nullptr);
126 Parcel writeParcel(buf, len);
127 int ret = writeParcel.WriteVector<uint8_t>(writeData);
128 EXPECT_TRUE(ret == EOK);
129
130 /**
131 * @tc.steps: step2. read the vector, the vector should same as written vector;
132 * @tc.expected: step1. read out vector same as written vector;
133 */
134 vector<uint8_t> readData;
135 Parcel readParcel(buf, len);
136 uint32_t readLen = readParcel.ReadVector<uint8_t>(readData);
137 EXPECT_TRUE(!readParcel.IsError());
138 EXPECT_TRUE(readLen == len);
139 EXPECT_TRUE(readData.size() == writeData.size());
140 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVector<uint8_t>(writeData, readData));
141 delete []buf;
142 }
143
144 /**
145 * @tc.name: WriteVector002
146 * @tc.desc: write and read an empty vector<uint8_t>.
147 * @tc.type: FUNC
148 * @tc.require: AR000CQE0U
149 * @tc.author: weifeng
150 */
151 HWTEST_F(DistributedDBParcelTest, WriteVector002, TestSize.Level1)
152 {
153 /**
154 * @tc.steps: step1. create an empty vector, and write it into a buffer;
155 * @tc.expected: step1. write ok;
156 */
157 vector<uint8_t> writeData;
158 uint32_t len = Parcel::GetVectorLen<uint8_t>(writeData);
159 uint8_t *buf = new (nothrow) uint8_t[len];
160 ASSERT_NE(buf, nullptr);
161 Parcel writeParcel(buf, len);
162 int ret = writeParcel.WriteVector<uint8_t>(writeData);
163 EXPECT_TRUE(ret == EOK);
164
165 /**
166 * @tc.steps: step2. read the vector, the vector should same as written vector;
167 * @tc.expected: step1. read out vector same as written vector;
168 */
169 vector<uint8_t> readData;
170 Parcel readParcel(buf, len);
171 uint32_t readLen = readParcel.ReadVector<uint8_t>(readData);
172 EXPECT_TRUE(!readParcel.IsError());
173 EXPECT_TRUE(readLen == len);
174 EXPECT_TRUE(readData.size() == writeData.size());
175 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVector<uint8_t>(writeData, readData));
176 delete []buf;
177 }
178
179 /**
180 * @tc.name: WriteVector003
181 * @tc.desc: write and read a vector<uint32_t>.
182 * @tc.type: FUNC
183 * @tc.require: AR000CQE0U
184 * @tc.author: weifeng
185 */
186 HWTEST_F(DistributedDBParcelTest, WriteVector003, TestSize.Level1)
187 {
188 /**
189 * @tc.steps: step1. create a vector, and write it into a buffer;
190 * @tc.expected: step1. write ok;
191 */
192 vector<uint32_t> writeData = {1, 2, 5, 7, 20, 30, 99, 0xffffffff, 0x5678, 0x98765432, 0xabcdef12};
193 uint32_t len = Parcel::GetVectorLen<uint32_t>(writeData);
194 uint8_t *buf = new (nothrow) uint8_t[len];
195 ASSERT_NE(buf, nullptr);
196 Parcel writeParcel(buf, len);
197 int ret = writeParcel.WriteVector<uint32_t>(writeData);
198 EXPECT_TRUE(ret == EOK);
199
200 /**
201 * @tc.steps: step2. read the vector, the vector should same as written vector;
202 * @tc.expected: step1. read out vector same as written vector;
203 */
204 vector<uint32_t> readData;
205 Parcel readParcel(buf, len);
206 uint32_t readLen = readParcel.ReadVector<uint32_t>(readData);
207 EXPECT_TRUE(!readParcel.IsError());
208 EXPECT_TRUE(readLen == len);
209 EXPECT_TRUE(readData.size() == writeData.size());
210 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVector<uint32_t>(writeData, readData));
211 delete []buf;
212 }
213
214 /**
215 * @tc.name: WriteVector004
216 * @tc.desc: write and read an empty vector<uint32_t>.
217 * @tc.type: FUNC
218 * @tc.require: AR000CQE0U
219 * @tc.author: weifeng
220 */
221 HWTEST_F(DistributedDBParcelTest, WriteVector004, TestSize.Level1)
222 {
223 /**
224 * @tc.steps: step1. create an empty vector, and write it into a buffer;
225 * @tc.expected: step1. write ok;
226 */
227 vector<uint32_t> writeData;
228 uint32_t len = Parcel::GetVectorLen<uint32_t>(writeData);
229 uint8_t *buf = new (nothrow) uint8_t[len];
230 ASSERT_NE(buf, nullptr);
231 Parcel writeParcel(buf, len);
232 int ret = writeParcel.WriteVector<uint32_t>(writeData);
233 EXPECT_TRUE(ret == EOK);
234
235 /**
236 * @tc.steps: step2. read the vector, the vector should same as written vector;
237 * @tc.expected: step1. read out vector same as written vector;
238 */
239 vector<uint32_t> readData;
240 Parcel readParcel(buf, len);
241 uint32_t readLen = readParcel.ReadVector<uint32_t>(readData);
242 EXPECT_TRUE(!readParcel.IsError());
243 EXPECT_TRUE(readLen == len);
244 EXPECT_TRUE(readData.size() == writeData.size());
245 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVector<uint32_t>(writeData, readData));
246 delete []buf;
247 }
248
249 /**
250 * @tc.name: WriteVector005
251 * @tc.desc: write and read a vector<uint64_t>.
252 * @tc.type: FUNC
253 * @tc.require: AR000CQE0U
254 * @tc.author: weifeng
255 */
256 HWTEST_F(DistributedDBParcelTest, WriteVector005, TestSize.Level1)
257 {
258 /**
259 * @tc.steps: step1. create a vector, and write it into a buffer;
260 * @tc.expected: step1. write ok;
261 */
262 vector<uint64_t> writeData = {1, 2, 5, 7, 20, 30, 99, 0xffffffffffffffff,
263 0x5678, 0x98765432ffffffff, 0xabcdef1212345678};
264 uint32_t len = Parcel::GetVectorLen<uint64_t>(writeData);
265 uint8_t *buf = new (nothrow) uint8_t[len];
266 ASSERT_NE(buf, nullptr);
267 Parcel writeParcel(buf, len);
268 int ret = writeParcel.WriteVector<uint64_t>(writeData);
269 EXPECT_TRUE(ret == EOK);
270
271 /**
272 * @tc.steps: step2. read the vector, the vector should same as written vector;
273 * @tc.expected: step1. read out vector same as written vector;
274 */
275 vector<uint64_t> readData;
276 Parcel readParcel(buf, len);
277 uint32_t readLen = readParcel.ReadVector<uint64_t>(readData);
278 EXPECT_TRUE(!readParcel.IsError());
279 EXPECT_TRUE(readLen == len);
280 EXPECT_TRUE(readData.size() == writeData.size());
281 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVector<uint64_t>(writeData, readData));
282 delete []buf;
283 }
284
285 /**
286 * @tc.name: WriteVector006
287 * @tc.desc: write and read an empty vector<uint64_t>.
288 * @tc.type: FUNC
289 * @tc.require: AR000CQE0U
290 * @tc.author: weifeng
291 */
292 HWTEST_F(DistributedDBParcelTest, WriteVector006, TestSize.Level1)
293 {
294 /**
295 * @tc.steps: step1. create an empty vector, and write it into a buffer;
296 * @tc.expected: step1. write ok;
297 */
298 vector<uint64_t> writeData;
299 uint32_t len = Parcel::GetVectorLen<uint64_t>(writeData);
300 uint8_t *buf = new (nothrow) uint8_t[len];
301 ASSERT_NE(buf, nullptr);
302 Parcel writeParcel(buf, len);
303 int ret = writeParcel.WriteVector<uint64_t>(writeData);
304 EXPECT_TRUE(ret == EOK);
305
306 /**
307 * @tc.steps: step2. read the vector, the vector should same as written vector;
308 * @tc.expected: step1. read out vector same as written vector;
309 */
310 vector<uint64_t> readData;
311 Parcel readParcel(buf, len);
312 uint32_t readLen = readParcel.ReadVector<uint64_t>(readData);
313 EXPECT_TRUE(!readParcel.IsError());
314 EXPECT_TRUE(readLen == len);
315 EXPECT_TRUE(readData.size() == writeData.size());
316 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVector<uint64_t>(writeData, readData));
317 delete []buf;
318 }
319
320 /**
321 * @tc.name: WriteVector007
322 * @tc.desc: write and read a vector<uint8_t>, insert a wrong len.
323 * @tc.type: FUNC
324 * @tc.require: AR000CQE0U
325 * @tc.author: weifeng
326 */
327 HWTEST_F(DistributedDBParcelTest, WriteVector007, TestSize.Level1)
328 {
329 /**
330 * @tc.steps: step1. create a vector, and write it into a buffer;
331 * @tc.expected: step1. write ok;
332 */
333 vector<uint8_t> writeData = {1, 2, 5, 7, 20, 30, 99};
334 uint32_t len = Parcel::GetVectorLen<uint8_t>(writeData);
335 uint8_t *buf = new (nothrow) uint8_t[len];
336 ASSERT_NE(buf, nullptr);
337 Parcel writeParcel(buf, len);
338 int ret = writeParcel.WriteVector<uint8_t>(writeData);
339 EXPECT_TRUE(ret == EOK);
340
341 /**
342 * @tc.steps: step2. set a wrong len, the len is INT_MAX;
343 * @tc.expected: the vector should be empty, and isError should be true.
344 */
345 *(reinterpret_cast<uint32_t *>(buf)) = HostToNet(static_cast<uint32_t>(INT32_MAX));
346
347 /**
348 * @tc.steps: step3. read the vector
349 * @tc.expected: the vector should be empty, and isError should be true.
350 */
351 vector<uint8_t> readData;
352 Parcel readParcel(buf, len);
353 uint32_t readLen = readParcel.ReadVector<uint8_t>(readData);
354 EXPECT_TRUE(readParcel.IsError());
355 EXPECT_TRUE(readLen == 0);
356 EXPECT_TRUE(readData.size() == 0);
357 delete []buf;
358 }
359
360 /**
361 * @tc.name: WriteVector008
362 * @tc.desc: write and read a vector<uint8_t>, insert a wrong len.
363 * @tc.type: FUNC
364 * @tc.require: AR000CQE0U
365 * @tc.author: weifeng
366 */
367 HWTEST_F(DistributedDBParcelTest, WriteVector008, TestSize.Level1)
368 {
369 /**
370 * @tc.steps: step1. create a vector, and write it into a buffer;
371 * @tc.expected: step1. write ok;
372 */
373 vector<uint8_t> writeData = {1, 2, 5, 7, 20, 30, 99, 0xff, 0xff, 0x1f, 0xab, 0x45};
374 uint32_t len = Parcel::GetVectorLen<uint8_t>(writeData);
375 uint8_t *buf = new (nothrow) uint8_t[len];
376 ASSERT_NE(buf, nullptr);
377 Parcel writeParcel(buf, len);
378 int ret = writeParcel.WriteVector<uint8_t>(writeData);
379 EXPECT_TRUE(ret == EOK);
380
381 /**
382 * @tc.steps: step2. set a wrong len, the len is bigger than it should be;
383 * @tc.expected: the vector should be empty, and isError should be true.
384 */
385 *(reinterpret_cast<uint32_t *>(buf)) = HostToNet(static_cast<uint32_t>(writeData.size()) + 1);
386
387 /**
388 * @tc.steps: step3. read the vector
389 * @tc.expected: the vector should be empty, and isError should be true.
390 */
391 vector<uint8_t> readData;
392 Parcel readParcel(buf, len);
393 uint32_t readLen = readParcel.ReadVector<uint8_t>(readData);
394 EXPECT_TRUE(readParcel.IsError());
395 EXPECT_TRUE(readLen == 0);
396 EXPECT_TRUE(readData.size() == 0);
397 delete []buf;
398 }
399
400 /**
401 * @tc.name: WriteVector009
402 * @tc.desc: write and read a vector<uint8_t>, insert a wrong len.
403 * @tc.type: FUNC
404 * @tc.require: AR000CQE0U
405 * @tc.author: weifeng
406 */
407 HWTEST_F(DistributedDBParcelTest, WriteVector009, TestSize.Level1)
408 {
409 /**
410 * @tc.steps: step1. create a vector, and write it into a buffer;
411 * @tc.expected: step1. write ok;
412 */
413 vector<uint8_t> writeData = {1, 2, 5, 7, 20, 30, 99};
414 uint32_t len = Parcel::GetVectorLen<uint8_t>(writeData);
415 uint8_t *buf = new (nothrow) uint8_t[len];
416 ASSERT_NE(buf, nullptr);
417 Parcel writeParcel(buf, len);
418 int ret = writeParcel.WriteVector<uint8_t>(writeData);
419 EXPECT_TRUE(ret == EOK);
420
421 /**
422 * @tc.steps: step2. set a wrong len, the len is smaller than it should be;
423 * @tc.expected: the vector should be empty, and isError should be true;
424 */
425 *(reinterpret_cast<uint32_t *>(buf)) = HostToNet(static_cast<uint32_t>(writeData.size()) - 1);
426
427 /**
428 * @tc.steps: step3. read the vector
429 * @tc.expected: the vector should be same as writeData.sub(0, len - 1);
430 */
431 vector<uint8_t> readData;
432 Parcel readParcel(buf, len);
433 uint32_t readLen = readParcel.ReadVector<uint8_t>(readData);
434 EXPECT_TRUE(!readParcel.IsError());
435 EXPECT_TRUE(readLen != 0);
436 EXPECT_TRUE(readData.size() == writeData.size() - 1);
437 EXPECT_TRUE(DistributedDBToolsUnitTest::CompareVectorN<uint8_t>(readData, writeData, writeData.size() - 1));
438 delete []buf;
439 }
440
441 #ifndef LOW_LEVEL_MEM_DEV
442 /**
443 * @tc.name: WriteVector010
444 * @tc.desc: write and read a vector<uint8_t>, vector len is INT_MAX.
445 * @tc.type: FUNC
446 * @tc.require: AR000CQE0U
447 * @tc.author: weifeng
448 */
449 HWTEST_F(DistributedDBParcelTest, WriteVector010, TestSize.Level2)
450 {
451 /**
452 * @tc.steps: step1. create a vector, and write it into a buffer;
453 * @tc.expected: step1. write ok;
454 */
455 vector<uint8_t> writeData(INT32_MAX, 0xff);
456 uint32_t len = Parcel::GetVectorLen<uint8_t>(writeData);
457 EXPECT_TRUE(len == 0);
458 len = Parcel::GetEightByteAlign(static_cast<uint32_t>(INT32_MAX) + 4);
459 uint8_t *buf = new (nothrow) uint8_t[len];
460 ASSERT_NE(buf, nullptr);
461 Parcel writeParcel(buf, len);
462 int ret = writeParcel.WriteVector<uint8_t>(writeData);
463 delete []buf;
464 EXPECT_TRUE(ret != EOK);
465 }
466 #endif
467
468 /**
469 * @tc.name: WriteString001
470 * @tc.desc: write and read a string normally.
471 * @tc.type: FUNC
472 * @tc.require: AR000CQE0U
473 * @tc.author: weifeng
474 */
475 HWTEST_F(DistributedDBParcelTest, WriteString001, TestSize.Level1)
476 {
477 /**
478 * @tc.steps: step1. create a string, and write it into a buffer;
479 * @tc.expected: step1. write ok;
480 */
481 string writeData("abcd1234ffff234");
482 uint32_t len = Parcel::GetStringLen(writeData);
483 uint8_t *buf = new (nothrow) uint8_t[len];
484 ASSERT_NE(buf, nullptr);
485 Parcel writeParcel(buf, len);
486 int ret = writeParcel.WriteString(writeData);
487 EXPECT_TRUE(ret == EOK);
488
489 /**
490 * @tc.steps: step3. read the string
491 * @tc.expected: the string should be read correctly;
492 */
493 string readData;
494 Parcel readParcel(buf, len);
495 uint32_t readLen = readParcel.ReadString(readData);
496 EXPECT_TRUE(!readParcel.IsError());
497 EXPECT_TRUE(readLen == len);
498 EXPECT_TRUE(readData == writeData);
499 delete []buf;
500 }
501
502 /**
503 * @tc.name: WriteString002
504 * @tc.desc: write and read a string, insert a wrong len.
505 * @tc.type: FUNC
506 * @tc.require: AR000CQE0U
507 * @tc.author: weifeng
508 */
509 HWTEST_F(DistributedDBParcelTest, WriteString002, TestSize.Level1)
510 {
511 /**
512 * @tc.steps: step1. create a string, and write it into a buffer;
513 * @tc.expected: step1. write ok;
514 */
515 string writeData("abcd1234ffff234");
516 uint32_t len = Parcel::GetStringLen(writeData);
517 uint8_t *buf = new (nothrow) uint8_t[len];
518 ASSERT_NE(buf, nullptr);
519 Parcel writeParcel(buf, len);
520 int ret = writeParcel.WriteString(writeData);
521 EXPECT_TRUE(ret == EOK);
522
523 /**
524 * @tc.steps: step2. set a wrong len, the len is smaller than it should be;
525 * @tc.expected: the vector should be empty, and isError should be true;
526 */
527 *(reinterpret_cast<uint32_t *>(buf)) = HostToNet(static_cast<uint32_t>(writeData.size()) - 1);
528
529 /**
530 * @tc.steps: step3. read the string
531 * @tc.expected: the string should be read correctly;
532 */
533 string readData;
534 Parcel readParcel(buf, len);
535 uint32_t readLen = readParcel.ReadString(readData);
536 EXPECT_TRUE(!readParcel.IsError());
537 EXPECT_TRUE(readLen != 0);
538 EXPECT_TRUE(readData == writeData.substr(0, writeData.size() - 1));
539 delete []buf;
540 }
541
542 /**
543 * @tc.name: WriteString003
544 * @tc.desc: write and read a string, insert a wrong len.
545 * @tc.type: FUNC
546 * @tc.require: AR000CQE0U
547 * @tc.author: weifeng
548 */
549 HWTEST_F(DistributedDBParcelTest, WriteString003, TestSize.Level1)
550 {
551 /**
552 * @tc.steps: step1. create a string, and write it into a buffer;
553 * @tc.expected: step1. write ok;
554 */
555 string writeData("abcd1234ffff2349poff");
556 uint32_t len = Parcel::GetStringLen(writeData);
557 uint8_t *buf = new (nothrow) uint8_t[len];
558 ASSERT_NE(buf, nullptr);
559 Parcel writeParcel(buf, len);
560 int ret = writeParcel.WriteString(writeData);
561 EXPECT_TRUE(ret == EOK);
562
563 /**
564 * @tc.steps: step2. set a wrong len, the len is bigger than it should be;
565 * @tc.expected: the string should be empty, and isError should be true;
566 */
567 *(reinterpret_cast<uint32_t *>(buf)) = HostToNet(static_cast<uint32_t>(writeData.size()) + 1);
568
569 /**
570 * @tc.steps: step3. read the string
571 * @tc.expected: the string should be read with error;
572 */
573 string readData;
574 Parcel readParcel(buf, len);
575 uint32_t readLen = readParcel.ReadString(readData);
576 EXPECT_TRUE(readParcel.IsError());
577 EXPECT_TRUE(readLen == 0);
578 EXPECT_TRUE(readData.size() == 0);
579 delete []buf;
580 }
581
582 /**
583 * @tc.name: WriteString004
584 * @tc.desc: write and read a string, string is empty.
585 * @tc.type: FUNC
586 * @tc.require: AR000CQE0U
587 * @tc.author: weifeng
588 */
589 HWTEST_F(DistributedDBParcelTest, WriteString004, TestSize.Level1)
590 {
591 /**
592 * @tc.steps: step1. create a string, and write it into a buffer;
593 * @tc.expected: step1. write ok;
594 */
595 string writeData;
596 uint32_t len = Parcel::GetStringLen(writeData);
597 uint8_t *buf = new (nothrow) uint8_t[len];
598 ASSERT_NE(buf, nullptr);
599 Parcel writeParcel(buf, len);
600 int ret = writeParcel.WriteString(writeData);
601 EXPECT_TRUE(ret == EOK);
602
603 /**
604 * @tc.steps: step3. read the string
605 * @tc.expected: the string should be read with error;
606 */
607 string readData;
608 Parcel readParcel(buf, len);
609 uint32_t readLen = readParcel.ReadString(readData);
610 EXPECT_TRUE(!readParcel.IsError());
611 EXPECT_TRUE(readLen != 0);
612 EXPECT_TRUE(readData.size() == 0);
613 delete []buf;
614 }
615
616 /**
617 * @tc.name: WriteString005
618 * @tc.desc: write and read a string, insert a INT_MAX len.
619 * @tc.type: FUNC
620 * @tc.require: AR000CQE0U
621 * @tc.author: weifeng
622 */
623 HWTEST_F(DistributedDBParcelTest, WriteString005, TestSize.Level1)
624 {
625 /**
626 * @tc.steps: step1. create a string, and write it into a buffer;
627 * @tc.expected: step1. write ok;
628 */
629 string writeData;
630 uint32_t len = Parcel::GetStringLen(writeData);
631 uint8_t *buf = new (nothrow) uint8_t[len];
632 ASSERT_NE(buf, nullptr);
633 Parcel writeParcel(buf, len);
634 int ret = writeParcel.WriteString(writeData);
635 EXPECT_TRUE(ret == EOK);
636
637 /**
638 * @tc.steps: step2. set a wrong len, the len is INT32_MAX;
639 * @tc.expected: the string should be empty, and isError should be true;
640 */
641 *(reinterpret_cast<uint32_t *>(buf)) = HostToNet(static_cast<uint32_t>(INT32_MAX));
642
643 /**
644 * @tc.steps: step3. read the string
645 * @tc.expected: the string should be read with error;
646 */
647 string readData;
648 Parcel readParcel(buf, len);
649 uint32_t readLen = readParcel.ReadString(readData);
650 EXPECT_TRUE(readParcel.IsError());
651 EXPECT_TRUE(readLen == 0);
652 EXPECT_TRUE(readData.size() == 0);
653 delete []buf;
654 }
655 #ifndef LOW_LEVEL_MEM_DEV
656 /**
657 * @tc.name: WriteString006
658 * @tc.desc: write and read a string, string size is INT_MAX.
659 * @tc.type: FUNC
660 * @tc.require: AR000CQE0U
661 * @tc.author: weifeng
662 */
663 HWTEST_F(DistributedDBParcelTest, WriteString006, TestSize.Level2)
664 {
665 /**
666 * @tc.steps: step1. create a string, and write it into a buffer;
667 * @tc.expected: step1. write ok;
668 */
669 string writeData(INT32_MAX, 'z');
670 uint32_t len = Parcel::GetStringLen(writeData);
671 EXPECT_TRUE(len == 0);
672 len = Parcel::GetEightByteAlign(static_cast<uint32_t>(INT32_MAX) + 4);
673 uint8_t *buf = new (nothrow) uint8_t[len];
674 ASSERT_NE(buf, nullptr);
675 Parcel writeParcel(buf, len);
676 int ret = writeParcel.WriteString(writeData);
677 delete []buf;
678 EXPECT_TRUE(ret != EOK);
679 }
680
681 /**
682 * @tc.name: WriteBoolTest
683 * @tc.desc: write and read a bool.
684 * @tc.type: FUNC
685 * @tc.require: DTS2024073106613
686 * @tc.author: suyue
687 */
688 HWTEST_F(DistributedDBParcelTest, WriteBoolTest, TestSize.Level1)
689 {
690 /**
691 * @tc.steps: step1. create a bool, and write it into a buffer;
692 * @tc.expected: step1. write ok;
693 */
694 bool writeData = true;
695 uint32_t len = Parcel::GetBoolLen();
696 len = Parcel::GetEightByteAlign(len);
697 uint8_t *buf = new(nothrow) uint8_t[len];
698 ASSERT_NE(buf, nullptr);
699
700 Parcel writeParcel(buf, len);
701 EXPECT_EQ(writeParcel.WriteBool(writeData), EOK);
702
703 /**
704 * @tc.steps: step2. read the bool, the bool should same as written bool;
705 * @tc.expected: step2. read out readData same as written writeData;
706 */
707 bool readData;
708 Parcel readParcel(buf, len);
709 uint32_t readLen = readParcel.ReadBool(readData);
710 EXPECT_TRUE(readLen == Parcel::GetBoolLen());
711 EXPECT_TRUE(!readParcel.IsError());
712 EXPECT_TRUE(readData == writeData);
713 delete []buf;
714 }
715
716 /**
717 * @tc.name: ParcelErrTest
718 * @tc.desc: write and read a bool.
719 * @tc.type: FUNC
720 * @tc.require: DTS2024073106613
721 * @tc.author: suyue
722 */
723 HWTEST_F(DistributedDBParcelTest, ParcelErrTest, TestSize.Level1)
724 {
725 /**
726 * @tc.steps: step1. WriteDouble when Parcel para is null;
727 * @tc.expected: step1. return -E_PARSE_FAIL;
728 */
729 uint8_t *buf = nullptr;
730 Parcel parcel(buf, 0);
731 EXPECT_TRUE(parcel.IsError());
732
733 int ret = parcel.WriteDouble(0);
734 EXPECT_EQ(ret, -E_PARSE_FAIL);
735 double val;
736 uint32_t expectedVal = 0;
737 EXPECT_EQ(parcel.ReadDouble(val), expectedVal);
738
739 /**
740 * @tc.steps: step2. WriteBlob when para is null;
741 * @tc.expected: step2. return -E_INVALID_ARGS;
742 */
743 ret = parcel.WriteBlob(nullptr, 0);
744 EXPECT_EQ(ret, -E_INVALID_ARGS);
745 EXPECT_EQ(parcel.ReadBlob(nullptr, 0), expectedVal);
746
747 /**
748 * @tc.steps:step3. WriteDouble when Parcel para is null;
749 * @tc.expected: step3. return -E_PARSE_FAIL;
750 */
751 ret = parcel.WriteBlob("", 1);
752 EXPECT_EQ(ret, -E_PARSE_FAIL);
753 EXPECT_EQ(parcel.ReadBlob("", 1), expectedVal);
754 }
755 #endif