1 /*
2 * Copyright (c) 2022 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 "fuzztest_helper.h"
17
18 namespace OHOS {
19 namespace UpdateEngine {
20 constexpr uint32_t CHAR_TO_INT_INDEX0 = 0;
21 constexpr uint32_t CHAR_TO_INT_INDEX1 = 1;
22 constexpr uint32_t CHAR_TO_INT_INDEX2 = 2;
23 constexpr uint32_t CHAR_TO_INT_INDEX3 = 3;
24
25 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT0 = 0;
26 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT1 = 8;
27 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT2 = 16;
28 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT3 = 24;
29
30 constexpr uint32_t COUNT_BOOL_TYPE = 2;
31
32 constexpr uint32_t FUZZ_HEAD_DATA = 0;
33 constexpr uint32_t FUZZ_INT_LEN_DATA = 4;
34 constexpr uint32_t FUZZ_CHAR_ARRAY_LEN_DATA = 64;
35
FtCheckNewVersionDone(const BusinessError & businessError,const CheckResultEx & checkResultEx)36 static void FtCheckNewVersionDone(const BusinessError &businessError, const CheckResultEx &checkResultEx)
37 {
38 }
39
FtOnEvent(const EventInfo & eventInfo)40 static void FtOnEvent(const EventInfo &eventInfo)
41 {
42 }
43
FuzztestHelper(const uint8_t * data,size_t size)44 FuzztestHelper::FuzztestHelper(const uint8_t* data, size_t size)
45 {
46 if (size < FUZZ_DATA_LEN) {
47 return;
48 }
49 if (memcpy_s(data_, sizeof(data_), data, sizeof(data_)) != EOK) {
50 return;
51 }
52 }
53
BuildUpdateCallbackInfo()54 UpdateCallbackInfo FuzztestHelper::BuildUpdateCallbackInfo()
55 {
56 UpdateCallbackInfo cb {};
57 cb.checkNewVersionDone = FtCheckNewVersionDone;
58 cb.onEvent = FtOnEvent;
59 return cb;
60 }
61
BuildUpgradePolicy()62 UpgradePolicy FuzztestHelper::BuildUpgradePolicy()
63 {
64 UpgradePolicy upgradePolicy;
65 upgradePolicy.downloadStrategy = static_cast<bool>(GetUInt() % COUNT_BOOL_TYPE);
66 upgradePolicy.autoUpgradeStrategy = static_cast<bool>(GetUInt() % COUNT_BOOL_TYPE);
67 upgradePolicy.autoUpgradePeriods[0].start = GetUInt();
68 upgradePolicy.autoUpgradePeriods[0].end = GetUInt();
69 upgradePolicy.autoUpgradePeriods[1].start = GetUInt();
70 upgradePolicy.autoUpgradePeriods[1].end = GetUInt();
71 return upgradePolicy;
72 }
73
BuildBusinessType()74 BusinessType FuzztestHelper::BuildBusinessType()
75 {
76 BusinessType businessType;
77 businessType.vendor = BusinessVendor::PUBLIC;
78 businessType.subType = BusinessSubType::FIRMWARE;
79 return businessType;
80 }
81
BuildUpgradeInfo()82 UpgradeInfo FuzztestHelper::BuildUpgradeInfo()
83 {
84 UpgradeInfo upgradeInfo;
85 char upgradeApp[FUZZ_CHAR_ARRAY_LEN_DATA];
86 GetCharArray(upgradeApp, FUZZ_CHAR_ARRAY_LEN_DATA);
87 upgradeInfo.upgradeApp = upgradeApp;
88
89 upgradeInfo.businessType = BuildBusinessType();
90
91 char upgradeDevId[FUZZ_CHAR_ARRAY_LEN_DATA];
92 GetCharArray(upgradeDevId, FUZZ_CHAR_ARRAY_LEN_DATA);
93 upgradeInfo.upgradeDevId = upgradeDevId;
94
95 char controlDevId[FUZZ_CHAR_ARRAY_LEN_DATA];
96 GetCharArray(controlDevId, FUZZ_CHAR_ARRAY_LEN_DATA);
97 upgradeInfo.controlDevId = controlDevId;
98 return upgradeInfo;
99 }
100
BuildVersionDigestInfo()101 VersionDigestInfo FuzztestHelper::BuildVersionDigestInfo()
102 {
103 VersionDigestInfo versionDigestInfo;
104 versionDigestInfo.versionDigest = "versionDigest";
105 return versionDigestInfo;
106 }
107
GetCharArray(char * charArray,uint32_t arraySize)108 void FuzztestHelper::GetCharArray(char *charArray, uint32_t arraySize)
109 {
110 if (index_ + arraySize > FUZZ_DATA_LEN) {
111 index_ = FUZZ_HEAD_DATA;
112 }
113 for (uint32_t i = 0; i < arraySize; i++) {
114 charArray[i] = static_cast<char>(data_[i + index_]);
115 }
116 index_ += arraySize;
117 }
118
GetInt()119 int32_t FuzztestHelper::GetInt()
120 {
121 int32_t number;
122 if (index_ + FUZZ_INT_LEN_DATA > FUZZ_DATA_LEN) {
123 index_ = FUZZ_HEAD_DATA;
124 }
125 number = static_cast<int32_t>(
126 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX0]) <<
127 CHAR_TO_INT_MOVE_LEFT3) +
128 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX1]) <<
129 CHAR_TO_INT_MOVE_LEFT2) +
130 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX2]) <<
131 CHAR_TO_INT_MOVE_LEFT1) +
132 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX3]) <<
133 CHAR_TO_INT_MOVE_LEFT0));
134 index_ += FUZZ_INT_LEN_DATA;
135 return number;
136 }
137
GetUInt()138 uint32_t FuzztestHelper::GetUInt()
139 {
140 uint32_t number;
141 if (index_ + FUZZ_INT_LEN_DATA > FUZZ_DATA_LEN) {
142 index_ = FUZZ_HEAD_DATA;
143 }
144 number = (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX0]) <<
145 CHAR_TO_INT_MOVE_LEFT3) +
146 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX1]) <<
147 CHAR_TO_INT_MOVE_LEFT2) +
148 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX2]) <<
149 CHAR_TO_INT_MOVE_LEFT1) +
150 (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX3]) <<
151 CHAR_TO_INT_MOVE_LEFT0);
152 index_ += FUZZ_INT_LEN_DATA;
153 return number;
154 }
155 } // namespace UpdateEngine
156 } // namespace OHOS
157