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 "fuzz_common.h"
17
18 #include <algorithm>
19 #include <cmath>
20 #include <display.h>
21 #include <display_info.h>
22 #include "display_manager.h"
23 #include "location_button.h"
24 #include "paste_button.h"
25 #include "save_button.h"
26 #include "sec_comp_base.h"
27 #include "sec_comp_info.h"
28 #include "securec.h"
29
30 namespace OHOS {
31 namespace Security {
32 namespace SecurityComponent {
33 namespace {
34 static const int32_t DEFAULT_BUTTON_SIZE = 20;
35 static const int32_t DEFAULT_SCREEN_SIZE = 128;
36 static const int32_t BUTTON_FLOAT_SIZE = 10;
37 };
38
GetScType()39 uint32_t CompoRandomGenerator::GetScType()
40 {
41 // generate a number in range
42 return GetData<uint32_t>() % (SecCompType::MAX_SC_TYPE - 1) + 1;
43 }
44
GenerateRandomCompoStr(uint32_t type)45 std::string CompoRandomGenerator::GenerateRandomCompoStr(uint32_t type)
46 {
47 switch (type) {
48 case LOCATION_COMPONENT:
49 return ConstructLocationJson();
50 case PASTE_COMPONENT:
51 return ConstructPasteJson();
52 case SAVE_COMPONENT:
53 return ConstructSaveJson();
54 default:
55 return std::string();
56 }
57 return std::string();
58 }
59
60 // To generate a json, some objs are in default, otherwise these may not pass value check.
ConstructLocationJson()61 std::string CompoRandomGenerator::ConstructLocationJson()
62 {
63 nlohmann::json jsonComponent;
64 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT;
65 jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
66 jsonComponent[JsonTagConstants::JSON_IS_WEARABLE] = false;
67 SecCompRect window;
68 PaddingSize padding;
69 SecCompRect buttonRect;
70 BorderRadius borderRadius;
71 ConstructWindowJson(jsonComponent, window, padding, borderRadius, buttonRect);
72
73 jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
74 { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
75 { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
76 { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
77 };
78
79 jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
80 { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
81 };
82 // set default val
83 jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
84 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
85 { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
86 { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
87 { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
88 { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
89 { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
90 { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
91 };
92 jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
93 { JsonTagConstants::JSON_TEXT_TAG,
94 GetData<int32_t>() % static_cast<int32_t>(LocationDesc::MAX_LABEL_TYPE) },
95 { JsonTagConstants::JSON_ICON_TAG,
96 GetData<int32_t>() % static_cast<int32_t>(LocationIcon::MAX_ICON_TYPE) },
97 { JsonTagConstants::JSON_BG_TAG,
98 GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
99 };
100 jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
101 jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
102 jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
103 jsonComponent[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = false;
104 jsonComponent[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = 0.0;
105 compoJson_ = jsonComponent;
106 return compoJson_.dump();
107 }
108
ConstructSaveJson()109 std::string CompoRandomGenerator::ConstructSaveJson()
110 {
111 nlohmann::json jsonComponent;
112 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT;
113 jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
114 jsonComponent[JsonTagConstants::JSON_IS_WEARABLE] = false;
115 SecCompRect window;
116 PaddingSize padding;
117 SecCompRect buttonRect;
118 BorderRadius borderRadius;
119 ConstructWindowJson(jsonComponent, window, padding, borderRadius, buttonRect);
120
121 jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
122 { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
123 { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
124 { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
125 };
126
127 jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
128 { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
129 };
130 // set default val
131 jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
132 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
133 { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
134 { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
135 { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
136 { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
137 { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
138 { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
139 };
140 jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
141 { JsonTagConstants::JSON_TEXT_TAG,
142 GetData<int32_t>() % static_cast<int32_t>(SaveDesc::MAX_LABEL_TYPE) },
143 { JsonTagConstants::JSON_ICON_TAG,
144 GetData<int32_t>() % static_cast<int32_t>(SaveIcon::MAX_ICON_TYPE) },
145 { JsonTagConstants::JSON_BG_TAG,
146 GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
147 };
148 jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
149 jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
150 jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
151 jsonComponent[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = false;
152 jsonComponent[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = 0.0;
153 compoJson_ = jsonComponent;
154 return compoJson_.dump();
155 }
156
ConstructPasteJson()157 std::string CompoRandomGenerator::ConstructPasteJson()
158 {
159 nlohmann::json jsonComponent;
160 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT;
161 jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
162 jsonComponent[JsonTagConstants::JSON_IS_WEARABLE] = false;
163 SecCompRect window;
164 PaddingSize padding;
165 SecCompRect buttonRect;
166 BorderRadius borderRadius;
167 ConstructWindowJson(jsonComponent, window, padding, borderRadius, buttonRect);
168
169 jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
170 { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
171 { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
172 { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
173 };
174
175 jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
176 { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
177 };
178 // set default val
179 jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
180 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
181 { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
182 { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
183 { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
184 { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
185 { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
186 { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
187 };
188 jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
189 { JsonTagConstants::JSON_TEXT_TAG,
190 GetData<int32_t>() % static_cast<int32_t>(PasteDesc::MAX_LABEL_TYPE) },
191 { JsonTagConstants::JSON_ICON_TAG,
192 GetData<int32_t>() % static_cast<int32_t>(PasteIcon::MAX_ICON_TYPE) },
193 { JsonTagConstants::JSON_BG_TAG,
194 GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
195 };
196 jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
197 jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
198 jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
199 jsonComponent[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = false;
200 jsonComponent[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = 0.0;
201 compoJson_ = jsonComponent;
202 return compoJson_.dump();
203 }
204
ConstructButtonRect(SecCompRect & window,PaddingSize & padding,SecCompRect & buttonRect)205 void CompoRandomGenerator::ConstructButtonRect(
206 SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect)
207 {
208 window.x_ = (DEFAULT_SCREEN_SIZE >> 1) - BUTTON_FLOAT_SIZE;
209 window.y_ = (DEFAULT_SCREEN_SIZE >> 1) - BUTTON_FLOAT_SIZE;
210 window.width_ = (DEFAULT_SCREEN_SIZE >> 1) + BUTTON_FLOAT_SIZE;
211 window.height_ = (DEFAULT_SCREEN_SIZE >> 1) + BUTTON_FLOAT_SIZE;
212
213 buttonRect.x_ = std::fmod(std::fabs(GetData<double>()), window.width_) + window.x_;
214 buttonRect.y_ = std::fmod(std::fabs(GetData<double>()), window.height_) + window.y_;
215 buttonRect.width_ = DEFAULT_BUTTON_SIZE;
216 buttonRect.height_ = DEFAULT_BUTTON_SIZE;
217
218 padding.bottom = window.y_ + window.height_ - buttonRect.y_ - buttonRect.height_;
219 padding.top = window.y_ - buttonRect.y_;
220 padding.left = buttonRect.x_ - window.x_;
221 padding.right = window.x_ + window.width_ - buttonRect.x_ - buttonRect.width_;
222 }
223
ConstructWindowJson(nlohmann::json & jsonComponent,SecCompRect & window,PaddingSize & padding,BorderRadius & borderRadius,SecCompRect & buttonRect)224 void CompoRandomGenerator::ConstructWindowJson(nlohmann::json &jsonComponent, SecCompRect &window,
225 PaddingSize &padding, BorderRadius &borderRadius, SecCompRect &buttonRect)
226 {
227 ConstructButtonRect(window, padding, buttonRect);
228 jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json {
229 {JsonTagConstants::JSON_RECT_X, buttonRect.x_ },
230 {JsonTagConstants::JSON_RECT_Y, buttonRect.y_ },
231 {JsonTagConstants::JSON_RECT_WIDTH, buttonRect.width_ },
232 {JsonTagConstants::JSON_RECT_HEIGHT, buttonRect.height_ }
233 };
234 jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
235 { JsonTagConstants::JSON_RECT_X, window.x_ },
236 { JsonTagConstants::JSON_RECT_Y, window.y_ },
237 { JsonTagConstants::JSON_RECT_WIDTH, window.width_ },
238 { JsonTagConstants::JSON_RECT_HEIGHT, window.height_ }
239 };
240 nlohmann::json jsonPadding = nlohmann::json {
241 { JsonTagConstants::JSON_TOP_TAG, padding.top },
242 { JsonTagConstants::JSON_RIGHT_TAG, padding.right },
243 { JsonTagConstants::JSON_BOTTOM_TAG, padding.bottom },
244 { JsonTagConstants::JSON_LEFT_TAG, padding.left },
245 };
246
247 nlohmann::json jsonBorderRadius = nlohmann::json {
248 { JsonTagConstants::JSON_LEFT_TOP_TAG, borderRadius.leftTop },
249 { JsonTagConstants::JSON_RIGHT_TOP_TAG, borderRadius.rightTop },
250 { JsonTagConstants::JSON_LEFT_BOTTOM_TAG, borderRadius.leftBottom },
251 { JsonTagConstants::JSON_RIGHT_BOTTOM_TAG, borderRadius.rightBottom },
252 };
253
254 jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
255 { JsonTagConstants::JSON_FONT_SIZE_TAG, std::fabs(GetData<float>()) },
256 { JsonTagConstants::JSON_ICON_SIZE_TAG, std::fabs(GetData<float>()) },
257 { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, std::fabs(GetData<float>()) },
258 { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
259 { JsonTagConstants::JSON_BORDER_RADIUS_TAG, jsonBorderRadius},
260 };
261 }
262 }
263 }
264 }
265