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 #include <cstdint>
17 #include <cstring>
18
19 #define private public
20 #define protected public
21 #include <gtest/gtest.h>
22
23 #include "gmock/gmock.h"
24 #include "log.h"
25 #include "parcel_helper.h"
26 #include "request_common.h"
27 #include "request_common_utils.h"
28
29 using namespace testing::ext;
30 using namespace OHOS::Request;
31
32 #undef private
33 #undef protected
34
35 class ParcelHelperTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase(void)43 void ParcelHelperTest::SetUpTestCase(void)
44 {
45 // input testSuit setup step,setup invoked before all testCases
46 }
47
TearDownTestCase(void)48 void ParcelHelperTest::TearDownTestCase(void)
49 {
50 // input testSuit teardown step,teardown invoked after all testCases
51 }
52
SetUp(void)53 void ParcelHelperTest::SetUp(void)
54 {
55 // input testCase setup step,setup invoked before each testCase
56 testing::UnitTest *test = testing::UnitTest::GetInstance();
57 ASSERT_NE(test, nullptr);
58 const testing::TestInfo *testInfo = test->current_test_info();
59 ASSERT_NE(testInfo, nullptr);
60 string testCaseName = string(testInfo->name());
61 REQUEST_HILOGI("[SetUp] %{public}s start", testCaseName.c_str());
62 GTEST_LOG_(INFO) << testCaseName.append(" start");
63 }
64
TearDown(void)65 void ParcelHelperTest::TearDown(void)
66 {
67 // input testCase teardown step,teardown invoked after each testCase
68 }
69
70 /**
71 * @tc.name: UnMarshalFormItem001
72 * @tc.desc: Test UnMarshalFormItem interface base function
73 * @tc.precon: NA
74 * @tc.step: 1. Create MessageParcel and TaskInfo instances
75 * 2. Write zero size to parcel
76 * 3. Call UnMarshalFormItem and verify returns true
77 * 4. Write size 1 without data to test failure case
78 * 5. Write valid form data (name and value) to parcel
79 * 6. Call UnMarshalFormItem and verify successful unmarshalling
80 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals form items
81 * @tc.type: FUNC
82 * @tc.require: issueNumber
83 * @tc.level: Level 1
84 */
85 HWTEST_F(ParcelHelperTest, UnMarshalFormItem001, TestSize.Level1)
86 {
87 OHOS::MessageParcel data;
88 TaskInfo info;
89 uint32_t size = 0;
90 data.WriteUint32(size);
91 EXPECT_TRUE(ParcelHelper::UnMarshalFormItem(data, info));
92 size = 1;
93 data.WriteUint32(size);
94 EXPECT_FALSE(ParcelHelper::UnMarshalFormItem(data, info));
95 data.WriteUint32(size);
96 data.WriteString("name");
97 data.WriteString("value");
98 EXPECT_TRUE(ParcelHelper::UnMarshalFormItem(data, info));
99 EXPECT_EQ(info.forms[0].name, "name");
100 EXPECT_EQ(info.forms[0].value, "value");
101 }
102
103 /**
104 * @tc.name: UnMarshalFileSpec001
105 * @tc.desc: Test UnMarshalFileSpec interface base function
106 * @tc.precon: NA
107 * @tc.step: 1. Create MessageParcel and TaskInfo instances
108 * 2. Write zero size to parcel
109 * 3. Call UnMarshalFileSpec and verify returns true
110 * 4. Write size 1 without data to test failure case
111 * 5. Write valid file specification data to parcel
112 * 6. Call UnMarshalFileSpec and verify successful unmarshalling
113 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals file specifications
114 * @tc.type: FUNC
115 * @tc.require: issueNumber
116 * @tc.level: Level 1
117 */
118 HWTEST_F(ParcelHelperTest, UnMarshalFileSpec001, TestSize.Level1)
119 {
120 OHOS::MessageParcel data;
121 TaskInfo info;
122 uint32_t size = 0;
123 data.WriteUint32(size);
124 EXPECT_TRUE(ParcelHelper::UnMarshalFileSpec(data, info));
125 size = 1;
126 data.WriteUint32(size);
127 EXPECT_FALSE(ParcelHelper::UnMarshalFileSpec(data, info));
128 data.WriteUint32(size);
129 data.WriteString("name");
130 data.WriteString("uri");
131 data.WriteString("filename");
132 data.WriteString("type");
133 EXPECT_TRUE(ParcelHelper::UnMarshalFileSpec(data, info));
134 EXPECT_EQ(info.files[0].name, "name");
135 EXPECT_EQ(info.files[0].uri, "uri");
136 EXPECT_EQ(info.files[0].filename, "filename");
137 EXPECT_EQ(info.files[0].type, "type");
138 }
139
140 /**
141 * @tc.name: UnMarshalMapProgressExtras001
142 * @tc.desc: Test UnMarshalMapProgressExtras interface base function
143 * @tc.precon: NA
144 * @tc.step: 1. Create MessageParcel and TaskInfo instances
145 * 2. Write zero size to parcel
146 * 3. Call UnMarshalMapProgressExtras and verify returns true
147 * 4. Write size 1 without data to test failure case
148 * 5. Write valid key-value pair to parcel
149 * 6. Call UnMarshalMapProgressExtras and verify successful unmarshalling
150 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals progress extras
151 * @tc.type: FUNC
152 * @tc.require: issueNumber
153 * @tc.level: Level 1
154 */
155 HWTEST_F(ParcelHelperTest, UnMarshalMapProgressExtras001, TestSize.Level1)
156 {
157 OHOS::MessageParcel data;
158 TaskInfo info;
159 uint32_t size = 0;
160 data.WriteUint32(size);
161 EXPECT_TRUE(ParcelHelper::UnMarshalMapProgressExtras(data, info));
162 size = 1;
163 data.WriteUint32(size);
164 EXPECT_FALSE(ParcelHelper::UnMarshalMapProgressExtras(data, info));
165 data.WriteUint32(size);
166 data.WriteString("key");
167 data.WriteString("value");
168 EXPECT_TRUE(ParcelHelper::UnMarshalMapProgressExtras(data, info));
169 EXPECT_EQ(info.progress.extras["key"], "value");
170 }
171
172 /**
173 * @tc.name: UnMarshalMapExtras001
174 * @tc.desc: Test UnMarshalMapExtras interface base function
175 * @tc.precon: NA
176 * @tc.step: 1. Create MessageParcel and TaskInfo instances
177 * 2. Write zero size to parcel
178 * 3. Call UnMarshalMapExtras and verify returns true
179 * 4. Write size 1 without data to test failure case
180 * 5. Write valid key-value pair to parcel
181 * 6. Call UnMarshalMapExtras and verify successful unmarshalling
182 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals extras mapping
183 * @tc.type: FUNC
184 * @tc.require: issueNumber
185 * @tc.level: Level 1
186 */
187 HWTEST_F(ParcelHelperTest, UnMarshalMapExtras001, TestSize.Level1)
188 {
189 OHOS::MessageParcel data;
190 TaskInfo info;
191 uint32_t size = 0;
192 data.WriteUint32(size);
193 EXPECT_TRUE(ParcelHelper::UnMarshalMapExtras(data, info));
194 size = 1;
195 data.WriteUint32(size);
196 EXPECT_FALSE(ParcelHelper::UnMarshalMapExtras(data, info));
197 data.WriteUint32(size);
198 data.WriteString("key");
199 data.WriteString("value");
200 EXPECT_TRUE(ParcelHelper::UnMarshalMapExtras(data, info));
201 EXPECT_EQ(info.extras["key"], "value");
202 }
203
204 /**
205 * @tc.name: UnMarshalTaskState001
206 * @tc.desc: Test UnMarshalTaskState interface base function
207 * @tc.precon: NA
208 * @tc.step: 1. Create MessageParcel and TaskInfo instances
209 * 2. Write zero size to parcel
210 * 3. Call UnMarshalTaskState and verify returns true
211 * 4. Write size 1 with incomplete data to test failure case
212 * 5. Write complete task state data (path, responseCode, message) to parcel
213 * 6. Call UnMarshalTaskState and verify successful unmarshalling
214 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals task states
215 * @tc.type: FUNC
216 * @tc.require: issueNumber
217 * @tc.level: Level 1
218 */
219 HWTEST_F(ParcelHelperTest, UnMarshalTaskState001, TestSize.Level1)
220 {
221 OHOS::MessageParcel data;
222 TaskInfo info;
223 uint32_t size = 0;
224 data.WriteUint32(size);
225 EXPECT_TRUE(ParcelHelper::UnMarshalTaskState(data, info));
226 size = 1;
227 uint32_t responseCode = 0;
228 data.WriteUint32(size);
229 EXPECT_FALSE(ParcelHelper::UnMarshalTaskState(data, info));
230 data.WriteUint32(size);
231 data.WriteString("path");
232 data.WriteUint32(responseCode);
233 data.WriteString("message");
234 EXPECT_TRUE(ParcelHelper::UnMarshalTaskState(data, info));
235 EXPECT_EQ(info.taskStates[0].path, "path");
236 EXPECT_EQ(info.taskStates[0].responseCode, responseCode);
237 EXPECT_EQ(info.taskStates[0].message, "message");
238 }
239
240 /**
241 * @tc.name: UnMarshalConfigHeaders001
242 * @tc.desc: Test UnMarshalConfigHeaders interface base function
243 * @tc.precon: NA
244 * @tc.step: 1. Create MessageParcel and Config instances
245 * 2. Write zero size to parcel
246 * 3. Call UnMarshalConfigHeaders and verify returns true
247 * 4. Write size 1 without data to test failure case
248 * 5. Write valid header key-value pair to parcel
249 * 6. Call UnMarshalConfigHeaders and verify successful unmarshalling
250 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals config headers
251 * @tc.type: FUNC
252 * @tc.require: issueNumber
253 * @tc.level: Level 1
254 */
255 HWTEST_F(ParcelHelperTest, UnMarshalConfigHeaders001, TestSize.Level1)
256 {
257 OHOS::MessageParcel data;
258 Config config;
259 uint32_t size = 0;
260 data.WriteUint32(size);
261 EXPECT_TRUE(ParcelHelper::UnMarshalConfigHeaders(data, config));
262 size = 1;
263 data.WriteUint32(size);
264 EXPECT_FALSE(ParcelHelper::UnMarshalConfigHeaders(data, config));
265 data.WriteUint32(size);
266 data.WriteString("key");
267 data.WriteString("value");
268 EXPECT_TRUE(ParcelHelper::UnMarshalConfigHeaders(data, config));
269 EXPECT_EQ(config.headers["key"], "value");
270 }
271
272 /**
273 * @tc.name: UnMarshalConfigExtras001
274 * @tc.desc: Test UnMarshalConfigExtras interface base function
275 * @tc.precon: NA
276 * @tc.step: 1. Create MessageParcel and Config instances
277 * 2. Write zero size to parcel
278 * 3. Call UnMarshalConfigExtras and verify returns true
279 * 4. Write size 1 without data to test failure case
280 * 5. Write valid key-value pair to parcel
281 * 6. Call UnMarshalConfigExtras and verify successful unmarshalling
282 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals config extras
283 * @tc.type: FUNC
284 * @tc.require: issueNumber
285 * @tc.level: Level 1
286 */
287 HWTEST_F(ParcelHelperTest, UnMarshalConfigExtras001, TestSize.Level1)
288 {
289 OHOS::MessageParcel data;
290 Config config;
291 uint32_t size = 0;
292 data.WriteUint32(size);
293 EXPECT_TRUE(ParcelHelper::UnMarshalConfigExtras(data, config));
294 size = 1;
295 data.WriteUint32(size);
296 EXPECT_FALSE(ParcelHelper::UnMarshalConfigExtras(data, config));
297 data.WriteUint32(size);
298 data.WriteString("key");
299 data.WriteString("value");
300 EXPECT_TRUE(ParcelHelper::UnMarshalConfigExtras(data, config));
301 EXPECT_EQ(config.extras["key"], "value");
302 }
303
304 /**
305 * @tc.name: UnMarshalConfigFormItem001
306 * @tc.desc: Test UnMarshalConfigFormItem interface base function
307 * @tc.precon: NA
308 * @tc.step: 1. Create MessageParcel and Config instances
309 * 2. Write zero size to parcel
310 * 3. Call UnMarshalConfigFormItem and verify returns true
311 * 4. Write size 1 without data to test failure case
312 * 5. Write valid form item data (name and value) to parcel
313 * 6. Call UnMarshalConfigFormItem and verify successful unmarshalling
314 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals config form items
315 * @tc.type: FUNC
316 * @tc.require: issueNumber
317 * @tc.level: Level 1
318 */
319 HWTEST_F(ParcelHelperTest, UnMarshalConfigFormItem001, TestSize.Level1)
320 {
321 OHOS::MessageParcel data;
322 Config config;
323 uint32_t size = 0;
324 data.WriteUint32(size);
325 EXPECT_TRUE(ParcelHelper::UnMarshalConfigFormItem(data, config));
326 size = 1;
327 data.WriteUint32(size);
328 EXPECT_FALSE(ParcelHelper::UnMarshalConfigFormItem(data, config));
329 data.WriteUint32(size);
330 data.WriteString("name");
331 data.WriteString("value");
332 EXPECT_TRUE(ParcelHelper::UnMarshalConfigFormItem(data, config));
333 EXPECT_EQ(config.forms[0].name, "name");
334 EXPECT_EQ(config.forms[0].value, "value");
335 }
336
337 /**
338 * @tc.name: UnMarshalConfigFileSpec001
339 * @tc.desc: Test UnMarshalConfigFileSpec interface base function
340 * @tc.precon: NA
341 * @tc.step: 1. Create MessageParcel and Config instances
342 * 2. Write zero size to parcel
343 * 3. Call UnMarshalConfigFileSpec and verify returns true
344 * 4. Write size 1 without data to test failure case
345 * 5. Write valid file specification data to parcel
346 * 6. Call UnMarshalConfigFileSpec and verify successful unmarshalling
347 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals config file specifications
348 * @tc.type: FUNC
349 * @tc.require: issueNumber
350 * @tc.level: Level 1
351 */
352 HWTEST_F(ParcelHelperTest, UnMarshalConfigFileSpec001, TestSize.Level1)
353 {
354 OHOS::MessageParcel data;
355 Config config;
356 uint32_t size = 0;
357 data.WriteUint32(size);
358 EXPECT_TRUE(ParcelHelper::UnMarshalConfigFileSpec(data, config));
359 size = 1;
360 data.WriteUint32(size);
361 EXPECT_FALSE(ParcelHelper::UnMarshalConfigFileSpec(data, config));
362 data.WriteUint32(size);
363 data.WriteString("name");
364 data.WriteString("uri");
365 data.WriteString("filename");
366 data.WriteString("type");
367 EXPECT_TRUE(ParcelHelper::UnMarshalConfigFileSpec(data, config));
368 EXPECT_EQ(config.files[0].name, "name");
369 EXPECT_EQ(config.files[0].uri, "uri");
370 EXPECT_EQ(config.files[0].filename, "filename");
371 EXPECT_EQ(config.files[0].type, "type");
372 }
373
374 /**
375 * @tc.name: UnMarshalConfigBodyFileName001
376 * @tc.desc: Test UnMarshalConfigBodyFileName interface base function
377 * @tc.precon: NA
378 * @tc.step: 1. Create MessageParcel and Config instances
379 * 2. Write zero size to parcel
380 * 3. Call UnMarshalConfigBodyFileName and verify returns true
381 * 4. Write size 1 without data to test failure case
382 * 5. Write valid filename string to parcel
383 * 6. Call UnMarshalConfigBodyFileName and verify successful unmarshalling
384 * @tc.expect: Returns true for valid data, false for invalid data, and correctly unmarshals config body filenames
385 * @tc.type: FUNC
386 * @tc.require: issueNumber
387 * @tc.level: Level 1
388 */
389 HWTEST_F(ParcelHelperTest, UnMarshalConfigBodyFileName001, TestSize.Level1)
390 {
391 OHOS::MessageParcel data;
392 Config config;
393 uint32_t size = 0;
394 data.WriteUint32(size);
395 EXPECT_TRUE(ParcelHelper::UnMarshalConfigBodyFileName(data, config));
396 size = 1;
397 data.WriteUint32(size);
398 EXPECT_FALSE(ParcelHelper::UnMarshalConfigBodyFileName(data, config));
399 data.WriteUint32(size);
400 data.WriteString("name");
401 EXPECT_TRUE(ParcelHelper::UnMarshalConfigBodyFileName(data, config));
402 EXPECT_EQ(config.bodyFileNames[0], "name");
403 }
404
MarshalBase(OHOS::MessageParcel & data)405 void MarshalBase(OHOS::MessageParcel &data)
406 {
407 TaskInfo info;
408 data.WriteBool(info.gauge);
409 data.WriteBool(info.retry);
410 data.WriteUint32(static_cast<uint32_t>(info.action));
411 data.WriteUint32(static_cast<uint32_t>(info.mode));
412 data.WriteUint32(info.code);
413 data.WriteUint32(info.tries);
414 data.WriteString("uid");
415 data.WriteString("bundle");
416 data.WriteString(info.url);
417 data.WriteString("tid");
418 data.WriteString(info.title);
419 data.WriteString("mimeType");
420 data.WriteUint64(info.ctime);
421 data.WriteUint64(info.mtime);
422 data.WriteString(info.data);
423 data.WriteString(info.description);
424 data.WriteUint32(info.priority);
425 }
426
MarshalProgress(OHOS::MessageParcel & data)427 void MarshalProgress(OHOS::MessageParcel &data)
428 {
429 State state = State::DEFAULT;
430 uint32_t index = 0;
431 uint64_t progress = 0;
432 uint64_t totalProgress = 0;
433 std::vector<int64_t> val;
434 val.push_back(1);
435 data.WriteUint32(static_cast<uint32_t>(state));
436 data.WriteUint32(index);
437 data.WriteUint64(progress);
438 data.WriteUint64(totalProgress);
439 data.WriteInt64Vector(val);
440 }
441
442 /**
443 * @tc.name: UnMarshal001
444 * @tc.desc: Test UnMarshal interface base function
445 * @tc.precon: NA
446 * @tc.step: 1. Create MessageParcel and TaskInfo instances
447 * 2. Marshal base task info data to parcel
448 * 3. Test with form items, file specs, progress extras, map extras, and task states
449 * 4. Test different combinations of empty and non-empty collections
450 * 5. Verify all data is correctly unmarshalled
451 * @tc.expect: All task info fields are correctly unmarshalled including version, uid, bundle, tid, and progress data
452 * @tc.type: FUNC
453 * @tc.require: issueNumber
454 * @tc.level: Level 1
455 */
456 HWTEST_F(ParcelHelperTest, UnMarshal001, TestSize.Level1)
457 {
458 OHOS::MessageParcel data;
459 TaskInfo info;
460 Version version = Version::API10;
461
462 MarshalBase(data);
463 uint32_t formItemSize = 1;
464 data.WriteUint32(formItemSize);
465 ParcelHelper::UnMarshal(data, info);
466
467 MarshalBase(data);
468 formItemSize = 0;
469 uint32_t fileSpecSize = 1;
470 data.WriteUint32(formItemSize);
471 data.WriteUint32(fileSpecSize);
472 ParcelHelper::UnMarshal(data, info);
473
474 MarshalBase(data);
475 fileSpecSize = 0;
476 data.WriteUint32(formItemSize);
477 data.WriteUint32(fileSpecSize);
478 MarshalProgress(data);
479 uint32_t progressExtrasSize = 1;
480 data.WriteUint32(progressExtrasSize);
481 ParcelHelper::UnMarshal(data, info);
482
483 MarshalBase(data);
484 data.WriteUint32(formItemSize);
485 data.WriteUint32(fileSpecSize);
486 MarshalProgress(data);
487 progressExtrasSize = 0;
488 uint32_t mapExtrasSize = 1;
489 data.WriteUint32(progressExtrasSize);
490 data.WriteUint32(mapExtrasSize);
491 ParcelHelper::UnMarshal(data, info);
492
493 MarshalBase(data);
494 data.WriteUint32(formItemSize);
495 data.WriteUint32(fileSpecSize);
496 MarshalProgress(data);
497 mapExtrasSize = 0;
498 uint32_t taskStateSize = 1;
499 data.WriteUint32(progressExtrasSize);
500 data.WriteUint32(mapExtrasSize);
501 data.WriteUint32(static_cast<uint32_t>(version));
502 data.WriteUint32(taskStateSize);
503 ParcelHelper::UnMarshal(data, info);
504
505 MarshalBase(data);
506 data.WriteUint32(formItemSize);
507 data.WriteUint32(fileSpecSize);
508 MarshalProgress(data);
509 taskStateSize = 0;
510 data.WriteUint32(progressExtrasSize);
511 data.WriteUint32(mapExtrasSize);
512 data.WriteUint32(static_cast<uint32_t>(version));
513 data.WriteUint32(taskStateSize);
514 ParcelHelper::UnMarshal(data, info);
515
516 EXPECT_EQ(info.version, Version::API10);
517 EXPECT_EQ(info.uid, "uid");
518 EXPECT_EQ(info.bundle, "bundle");
519 EXPECT_EQ(info.tid, "tid");
520 EXPECT_EQ(info.mimeType, "mimeType");
521 EXPECT_EQ(info.progress.sizes.size(), 1);
522 EXPECT_EQ(info.progress.sizes[0], 1);
523 }
524
MarshalConfigBase(OHOS::MessageParcel & data)525 void MarshalConfigBase(OHOS::MessageParcel &data)
526 {
527 Config config;
528 data.WriteUint32(static_cast<uint32_t>(config.action));
529 data.WriteUint32(static_cast<uint32_t>(config.mode));
530 data.WriteUint32(config.bundleType);
531 data.WriteBool(config.overwrite);
532 data.WriteUint32(static_cast<uint32_t>(config.network));
533 config.metered = data.WriteBool(config.metered);
534 data.WriteBool(config.roaming);
535 data.WriteBool(config.retry);
536 data.WriteBool(config.redirect);
537 data.WriteUint32(config.index);
538 data.WriteInt64(config.begins);
539 data.WriteInt64(config.ends);
540 data.WriteBool(config.gauge);
541 data.WriteBool(config.precise);
542 data.WriteUint32(config.priority);
543 data.WriteBool(config.background);
544 data.WriteBool(config.multipart);
545 data.WriteString("bundleName");
546 data.WriteString("url");
547 data.WriteString("title");
548 data.WriteString("description");
549 data.WriteString("method");
550 }
551
552 /**
553 * @tc.name: UnMarshalConfig001
554 * @tc.desc: Test UnMarshalConfig interface base function
555 * @tc.precon: NA
556 * @tc.step: 1. Create MessageParcel and Config instances
557 * 2. Marshal base config data to parcel
558 * 3. Test with headers, extras, form items, file specs, and body filenames
559 * 4. Test different combinations of empty and non-empty collections
560 * 5. Verify all config data is correctly unmarshalled
561 * @tc.expect: All config fields are correctly unmarshalled including version, bundleName, url, title,
562 description, method, data, and token
563 * @tc.type: FUNC
564 * @tc.require: issueNumber
565 * @tc.level: Level 1
566 */
567 HWTEST_F(ParcelHelperTest, UnMarshalConfig001, TestSize.Level1)
568 {
569 Config config;
570 OHOS::MessageParcel data;
571 Version version = Version::API10;
572
573 MarshalConfigBase(data);
574 uint32_t configHeadersSize = 1;
575 data.WriteUint32(configHeadersSize);
576 ParcelHelper::UnMarshalConfig(data, config);
577
578 MarshalConfigBase(data);
579 configHeadersSize = 0;
580 data.WriteUint32(configHeadersSize);
581 data.WriteString("data");
582 data.WriteString("token");
583 uint32_t configExtrasSize = 1;
584 data.WriteUint32(configExtrasSize);
585 ParcelHelper::UnMarshalConfig(data, config);
586
587 MarshalConfigBase(data);
588 configExtrasSize = 0;
589 data.WriteUint32(configHeadersSize);
590 data.WriteString("data");
591 data.WriteString("token");
592 data.WriteUint32(configExtrasSize);
593 data.WriteUint32(static_cast<uint32_t>(version));
594 uint32_t configFormItemSize = 1;
595 data.WriteUint32(configFormItemSize);
596 ParcelHelper::UnMarshalConfig(data, config);
597
598 MarshalConfigBase(data);
599 data.WriteUint32(configHeadersSize);
600 data.WriteString("data");
601 data.WriteString("token");
602 data.WriteUint32(configExtrasSize);
603 data.WriteUint32(static_cast<uint32_t>(version));
604 configFormItemSize = 0;
605 data.WriteUint32(configFormItemSize);
606 uint32_t configFileSpecSize = 1;
607 data.WriteUint32(configFileSpecSize);
608 ParcelHelper::UnMarshalConfig(data, config);
609
610 MarshalConfigBase(data);
611 data.WriteUint32(configHeadersSize);
612 data.WriteString("data");
613 data.WriteString("token");
614 data.WriteUint32(configExtrasSize);
615 data.WriteUint32(static_cast<uint32_t>(version));
616 data.WriteUint32(configFormItemSize);
617 configFileSpecSize = 0;
618 data.WriteUint32(configFileSpecSize);
619 uint32_t configBodyFileNameSize = 1;
620 data.WriteUint32(configBodyFileNameSize);
621 ParcelHelper::UnMarshalConfig(data, config);
622
623 MarshalConfigBase(data);
624 data.WriteUint32(configHeadersSize);
625 data.WriteString("data");
626 data.WriteString("token");
627 data.WriteUint32(configExtrasSize);
628 data.WriteUint32(static_cast<uint32_t>(version));
629 data.WriteUint32(configFormItemSize);
630 data.WriteUint32(configFileSpecSize);
631 configBodyFileNameSize = 0;
632 data.WriteUint32(configBodyFileNameSize);
633 ParcelHelper::UnMarshalConfig(data, config);
634
635 EXPECT_EQ(config.version, Version::API10);
636 EXPECT_EQ(config.bundleName, "bundleName");
637 EXPECT_EQ(config.url, "url");
638 EXPECT_EQ(config.title, "title");
639 EXPECT_EQ(config.description, "description");
640 EXPECT_EQ(config.method, "method");
641 EXPECT_EQ(config.data, "data");
642 EXPECT_EQ(config.token, "token");
643 }
644
645 /**
646 * @tc.name: CommonUtilsGetFaultByReason001
647 * @tc.desc: Test CommonUtils GetFaultByReason interface
648 * @tc.precon: NA
649 * @tc.step: 1. Call GetFaultByReason with various reason codes
650 * 2. Verify correct fault mapping for each reason
651 * 3. Test edge cases and boundary values
652 * @tc.expect: Each reason code maps to the expected fault type according to the mapping rules
653 * @tc.type: FUNC
654 * @tc.require: issueNumber
655 * @tc.level: Level 1
656 */
657 HWTEST_F(ParcelHelperTest, CommonUtilsGetFaultByReason001, TestSize.Level1)
658 {
659 EXPECT_EQ(CommonUtils::GetFaultByReason(REASON_OK), Faults::OTHERS);
660 EXPECT_EQ(CommonUtils::GetFaultByReason(TASK_SURVIVAL_ONE_MONTH), Faults::OTHERS);
661 EXPECT_EQ(CommonUtils::GetFaultByReason(WAITTING_NETWORK_ONE_DAY), Faults::OTHERS);
662 EXPECT_EQ(CommonUtils::GetFaultByReason(STOPPED_NEW_FRONT_TASK), Faults::OTHERS);
663 EXPECT_EQ(CommonUtils::GetFaultByReason(RUNNING_TASK_MEET_LIMITS), Faults::OTHERS);
664 EXPECT_EQ(CommonUtils::GetFaultByReason(USER_OPERATION), Faults::OTHERS);
665 EXPECT_EQ(CommonUtils::GetFaultByReason(APP_BACKGROUND_OR_TERMINATE), Faults::OTHERS);
666 EXPECT_EQ(CommonUtils::GetFaultByReason(NETWORK_OFFLINE), Faults::DISCONNECTED);
667 EXPECT_EQ(CommonUtils::GetFaultByReason(UNSUPPORTED_NETWORK_TYPE), Faults::OTHERS);
668 EXPECT_EQ(CommonUtils::GetFaultByReason(BUILD_CLIENT_FAILED), Faults::PARAM);
669 EXPECT_EQ(CommonUtils::GetFaultByReason(BUILD_REQUEST_FAILED), Faults::PARAM);
670 EXPECT_EQ(CommonUtils::GetFaultByReason(GET_FILESIZE_FAILED), Faults::FSIO);
671 EXPECT_EQ(CommonUtils::GetFaultByReason(CONTINUOUS_TASK_TIMEOUT), Faults::TIMEOUT);
672 EXPECT_EQ(CommonUtils::GetFaultByReason(CONNECT_ERROR), Faults::TCP);
673 EXPECT_EQ(CommonUtils::GetFaultByReason(REQUEST_ERROR), Faults::PROTOCOL);
674 EXPECT_EQ(CommonUtils::GetFaultByReason(UPLOAD_FILE_ERROR), Faults::OTHERS);
675 EXPECT_EQ(CommonUtils::GetFaultByReason(REDIRECT_ERROR), Faults::REDIRECT);
676 EXPECT_EQ(CommonUtils::GetFaultByReason(PROTOCOL_ERROR), Faults::PROTOCOL);
677 EXPECT_EQ(CommonUtils::GetFaultByReason(IO_ERROR), Faults::FSIO);
678 EXPECT_EQ(CommonUtils::GetFaultByReason(UNSUPPORT_RANGE_REQUEST), Faults::PROTOCOL);
679 EXPECT_EQ(CommonUtils::GetFaultByReason(OTHERS_ERROR), Faults::OTHERS);
680 EXPECT_EQ(CommonUtils::GetFaultByReason(ACCOUNT_STOPPED), Faults::OTHERS);
681 EXPECT_EQ(CommonUtils::GetFaultByReason(NETWORK_CHANGED), Faults::OTHERS);
682 EXPECT_EQ(CommonUtils::GetFaultByReason(DNS), Faults::DNS);
683 EXPECT_EQ(CommonUtils::GetFaultByReason(TCP), Faults::TCP);
684 EXPECT_EQ(CommonUtils::GetFaultByReason(SSL), Faults::SSL);
685 EXPECT_EQ(CommonUtils::GetFaultByReason(INSUFFICIENT_SPACE), Faults::OTHERS);
686 EXPECT_EQ(CommonUtils::GetFaultByReason(NETWORK_APP), Faults::DISCONNECTED);
687 EXPECT_EQ(CommonUtils::GetFaultByReason(NETWORK_ACCOUNT), Faults::DISCONNECTED);
688 EXPECT_EQ(CommonUtils::GetFaultByReason(APP_ACCOUNT), Faults::OTHERS);
689 EXPECT_EQ(CommonUtils::GetFaultByReason(NETWORK_APP_ACCOUNT), Faults::DISCONNECTED);
690 Reason code = static_cast<Reason>(1000);
691 EXPECT_EQ(CommonUtils::GetFaultByReason(code), Faults::OTHERS);
692 }
693
694 /**
695 * @tc.name: GetMsgByReason001
696 * @tc.desc: Test GetMsgByReason with all valid reason codes and verify correct message mapping
697 * @tc.precon: CommonUtils class is properly initialized and available
698 * @tc.step: 1. Call GetMsgByReason with enum Reason
699 * @tc.expect: Each valid reason code returns corresponding predefined message, invalid code returns "unknown"
700 * @tc.type: FUNC
701 * @tc.require: issueNumber
702 * @tc.level: Level 1
703 */
704 HWTEST_F(ParcelHelperTest, CommonUtilsGetMsgByReason001, TestSize.Level1)
705 {
706 EXPECT_EQ(CommonUtils::GetMsgByReason(REASON_OK), CommonUtils::REASON_OK_INFO);
707 EXPECT_EQ(CommonUtils::GetMsgByReason(REASON_OK), CommonUtils::REASON_OK_INFO);
708 EXPECT_EQ(CommonUtils::GetMsgByReason(TASK_SURVIVAL_ONE_MONTH), CommonUtils::TASK_SURVIVAL_ONE_MONTH_INFO);
709 EXPECT_EQ(CommonUtils::GetMsgByReason(WAITTING_NETWORK_ONE_DAY), CommonUtils::WAITTING_NETWORK_ONE_DAY_INFO);
710 EXPECT_EQ(CommonUtils::GetMsgByReason(STOPPED_NEW_FRONT_TASK), CommonUtils::STOPPED_NEW_FRONT_TASK_INFO);
711 EXPECT_EQ(CommonUtils::GetMsgByReason(RUNNING_TASK_MEET_LIMITS), CommonUtils::RUNNING_TASK_MEET_LIMITS_INFO);
712 EXPECT_EQ(CommonUtils::GetMsgByReason(USER_OPERATION), CommonUtils::USER_OPERATION_INFO);
713 EXPECT_EQ(CommonUtils::GetMsgByReason(APP_BACKGROUND_OR_TERMINATE), CommonUtils::APP_BACKGROUND_OR_TERMINATE_INFO);
714 EXPECT_EQ(CommonUtils::GetMsgByReason(NETWORK_OFFLINE), CommonUtils::NETWORK_OFFLINE_INFO);
715 EXPECT_EQ(CommonUtils::GetMsgByReason(UNSUPPORTED_NETWORK_TYPE), CommonUtils::UNSUPPORTED_NETWORK_TYPE_INFO);
716 EXPECT_EQ(CommonUtils::GetMsgByReason(BUILD_CLIENT_FAILED), CommonUtils::BUILD_CLIENT_FAILED_INFO);
717 EXPECT_EQ(CommonUtils::GetMsgByReason(BUILD_REQUEST_FAILED), CommonUtils::BUILD_REQUEST_FAILED_INFO);
718 EXPECT_EQ(CommonUtils::GetMsgByReason(GET_FILESIZE_FAILED), CommonUtils::GET_FILESIZE_FAILED_INFO);
719 EXPECT_EQ(CommonUtils::GetMsgByReason(CONTINUOUS_TASK_TIMEOUT), CommonUtils::CONTINUOUS_TASK_TIMEOUT_INFO);
720 EXPECT_EQ(CommonUtils::GetMsgByReason(CONNECT_ERROR), CommonUtils::CONNECT_ERROR_INFO);
721 EXPECT_EQ(CommonUtils::GetMsgByReason(REQUEST_ERROR), CommonUtils::REQUEST_ERROR_INFO);
722 EXPECT_EQ(CommonUtils::GetMsgByReason(UPLOAD_FILE_ERROR), CommonUtils::UPLOAD_FILE_ERROR_INFO);
723 EXPECT_EQ(CommonUtils::GetMsgByReason(REDIRECT_ERROR), CommonUtils::REDIRECT_ERROR_INFO);
724 EXPECT_EQ(CommonUtils::GetMsgByReason(PROTOCOL_ERROR), CommonUtils::PROTOCOL_ERROR_INFO);
725 EXPECT_EQ(CommonUtils::GetMsgByReason(IO_ERROR), CommonUtils::IO_ERROR_INFO);
726 EXPECT_EQ(CommonUtils::GetMsgByReason(UNSUPPORT_RANGE_REQUEST), CommonUtils::UNSUPPORT_RANGE_REQUEST_INFO);
727 EXPECT_EQ(CommonUtils::GetMsgByReason(OTHERS_ERROR), CommonUtils::OTHERS_ERROR_INFO);
728 EXPECT_EQ(CommonUtils::GetMsgByReason(ACCOUNT_STOPPED), CommonUtils::ACCOUNT_STOPPED_INFO);
729 EXPECT_EQ(CommonUtils::GetMsgByReason(NETWORK_CHANGED), CommonUtils::NETWORK_CHANGED_INFO);
730 EXPECT_EQ(CommonUtils::GetMsgByReason(DNS), CommonUtils::DNS_INFO);
731 EXPECT_EQ(CommonUtils::GetMsgByReason(TCP), CommonUtils::TCP_INFO);
732 EXPECT_EQ(CommonUtils::GetMsgByReason(SSL), CommonUtils::SSL_INFO);
733 EXPECT_EQ(CommonUtils::GetMsgByReason(INSUFFICIENT_SPACE), CommonUtils::INSUFFICIENT_SPACE_INFO);
734 EXPECT_EQ(CommonUtils::GetMsgByReason(NETWORK_APP), CommonUtils::NETWORK_APP_INFO);
735 EXPECT_EQ(CommonUtils::GetMsgByReason(NETWORK_ACCOUNT), CommonUtils::NETWORK_ACCOUNT_INFO);
736 EXPECT_EQ(CommonUtils::GetMsgByReason(APP_ACCOUNT), CommonUtils::APP_ACCOUNT_INFO);
737 EXPECT_EQ(CommonUtils::GetMsgByReason(NETWORK_APP_ACCOUNT), CommonUtils::NETWORK_ACCOUNT_APP_INFO);
738 Reason code = static_cast<Reason>(1000);
739 EXPECT_EQ(CommonUtils::GetMsgByReason(code), "unknown");
740 }