• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetMessage()45 std::string CompoRandomGenerator::GetMessage()
46 {
47     bool isEmpty = GetData<bool>();
48     if (isEmpty) {
49         return "";
50     }
51     return "the text of the security component is out of range";
52 }
53 
GenerateRandomCompoStr(uint32_t type)54 std::string CompoRandomGenerator::GenerateRandomCompoStr(uint32_t type)
55 {
56     switch (type) {
57         case LOCATION_COMPONENT:
58             return ConstructLocationJson();
59         case PASTE_COMPONENT:
60             return ConstructPasteJson();
61         case SAVE_COMPONENT:
62             return ConstructSaveJson();
63         default:
64             return std::string();
65     }
66     return std::string();
67 }
68 
69 // To generate a json, some objs are in default, otherwise these may not pass value check.
ConstructLocationJson()70 std::string CompoRandomGenerator::ConstructLocationJson()
71 {
72     nlohmann::json jsonComponent;
73     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT;
74     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
75     jsonComponent[JsonTagConstants::JSON_IS_WEARABLE] = false;
76     SecCompRect window;
77     PaddingSize padding;
78     SecCompRect buttonRect;
79     BorderRadius borderRadius;
80     ConstructWindowJson(jsonComponent, window, padding, borderRadius, buttonRect);
81 
82     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
83         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
84         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
85         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
86     };
87 
88     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
89         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
90     };
91     // set default val
92     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
93         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
94         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
95         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
96         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
97         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
98         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
99         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
100     };
101     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
102         { JsonTagConstants::JSON_TEXT_TAG,
103             GetData<int32_t>() % static_cast<int32_t>(LocationDesc::MAX_LABEL_TYPE) },
104         { JsonTagConstants::JSON_ICON_TAG,
105             GetData<int32_t>() % static_cast<int32_t>(LocationIcon::MAX_ICON_TYPE) },
106         { JsonTagConstants::JSON_BG_TAG,
107             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
108     };
109     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
110     jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
111     jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
112     jsonComponent[JsonTagConstants::JSON_IS_ICON_EXCEEDED_TAG] = false;
113     jsonComponent[JsonTagConstants::JSON_IS_BORDER_COVERED_TAG] = false;
114     jsonComponent[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = false;
115     jsonComponent[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = 0.0;
116     jsonComponent[JsonTagConstants::JSON_FOREGROUND_BLUR_RADIUS_TAG] = 0.0;
117     jsonComponent[JsonTagConstants::JSON_IS_OVERLAY_TEXT_SET_TAG] = false;
118     jsonComponent[JsonTagConstants::JSON_IS_OVERLAY_NODE_SET_TAG] = false;
119     jsonComponent[JsonTagConstants::JSON_IS_CUSTOMIZABLE] = false;
120     jsonComponent[JsonTagConstants::JSON_TIP_POSITION] = TipPosition::ABOVE_BOTTOM;
121     compoJson_ = jsonComponent;
122     return compoJson_.dump();
123 }
124 
ConstructSaveJson()125 std::string CompoRandomGenerator::ConstructSaveJson()
126 {
127     nlohmann::json jsonComponent;
128     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT;
129     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
130     jsonComponent[JsonTagConstants::JSON_IS_WEARABLE] = false;
131     SecCompRect window;
132     PaddingSize padding;
133     SecCompRect buttonRect;
134     BorderRadius borderRadius;
135     ConstructWindowJson(jsonComponent, window, padding, borderRadius, buttonRect);
136 
137     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
138         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
139         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
140         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
141     };
142 
143     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
144         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
145     };
146     // set default val
147     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
148         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
149         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
150         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
151         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
152         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
153         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
154         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
155     };
156     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
157         { JsonTagConstants::JSON_TEXT_TAG,
158             GetData<int32_t>() % static_cast<int32_t>(SaveDesc::MAX_LABEL_TYPE) },
159         { JsonTagConstants::JSON_ICON_TAG,
160             GetData<int32_t>() % static_cast<int32_t>(SaveIcon::MAX_ICON_TYPE) },
161         { JsonTagConstants::JSON_BG_TAG,
162             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
163     };
164     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
165     jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
166     jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
167     jsonComponent[JsonTagConstants::JSON_IS_ICON_EXCEEDED_TAG] = false;
168     jsonComponent[JsonTagConstants::JSON_IS_BORDER_COVERED_TAG] = false;
169     jsonComponent[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = false;
170     jsonComponent[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = 0.0;
171     jsonComponent[JsonTagConstants::JSON_FOREGROUND_BLUR_RADIUS_TAG] = 0.0;
172     jsonComponent[JsonTagConstants::JSON_IS_OVERLAY_TEXT_SET_TAG] = false;
173     jsonComponent[JsonTagConstants::JSON_IS_OVERLAY_NODE_SET_TAG] = false;
174     jsonComponent[JsonTagConstants::JSON_IS_CUSTOMIZABLE] = false;
175     jsonComponent[JsonTagConstants::JSON_TIP_POSITION] = TipPosition::ABOVE_BOTTOM;
176     compoJson_ = jsonComponent;
177     return compoJson_.dump();
178 }
179 
ConstructPasteJson()180 std::string CompoRandomGenerator::ConstructPasteJson()
181 {
182     nlohmann::json jsonComponent;
183     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT;
184     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
185     jsonComponent[JsonTagConstants::JSON_IS_WEARABLE] = false;
186     SecCompRect window;
187     PaddingSize padding;
188     SecCompRect buttonRect;
189     BorderRadius borderRadius;
190     ConstructWindowJson(jsonComponent, window, padding, borderRadius, buttonRect);
191 
192     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
193         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
194         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
195         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
196     };
197 
198     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
199         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
200     };
201     // set default val
202     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
203         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
204         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
205         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
206         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
207         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
208         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
209         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
210     };
211     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
212         { JsonTagConstants::JSON_TEXT_TAG,
213             GetData<int32_t>() % static_cast<int32_t>(PasteDesc::MAX_LABEL_TYPE) },
214         { JsonTagConstants::JSON_ICON_TAG,
215             GetData<int32_t>() % static_cast<int32_t>(PasteIcon::MAX_ICON_TYPE) },
216         { JsonTagConstants::JSON_BG_TAG,
217             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
218     };
219     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
220     jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
221     jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
222     jsonComponent[JsonTagConstants::JSON_IS_ICON_EXCEEDED_TAG] = false;
223     jsonComponent[JsonTagConstants::JSON_IS_BORDER_COVERED_TAG] = false;
224     jsonComponent[JsonTagConstants::JSON_NON_COMPATIBLE_CHANGE_TAG] = false;
225     jsonComponent[JsonTagConstants::JSON_LINEAR_GRADIENT_BLUR_RADIUS_TAG] = 0.0;
226     jsonComponent[JsonTagConstants::JSON_FOREGROUND_BLUR_RADIUS_TAG] = 0.0;
227     jsonComponent[JsonTagConstants::JSON_IS_OVERLAY_TEXT_SET_TAG] = false;
228     jsonComponent[JsonTagConstants::JSON_IS_OVERLAY_NODE_SET_TAG] = false;
229     jsonComponent[JsonTagConstants::JSON_IS_CUSTOMIZABLE] = false;
230     jsonComponent[JsonTagConstants::JSON_TIP_POSITION] = TipPosition::ABOVE_BOTTOM;
231     compoJson_ = jsonComponent;
232     return compoJson_.dump();
233 }
234 
ConstructButtonRect(SecCompRect & window,PaddingSize & padding,SecCompRect & buttonRect)235 void CompoRandomGenerator::ConstructButtonRect(
236     SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect)
237 {
238     window.x_ = (DEFAULT_SCREEN_SIZE >> 1) - BUTTON_FLOAT_SIZE;
239     window.y_ = (DEFAULT_SCREEN_SIZE >> 1) - BUTTON_FLOAT_SIZE;
240     window.width_ = (DEFAULT_SCREEN_SIZE >> 1) + BUTTON_FLOAT_SIZE;
241     window.height_ = (DEFAULT_SCREEN_SIZE >> 1) + BUTTON_FLOAT_SIZE;
242 
243     buttonRect.x_ = std::fmod(std::fabs(GetData<double>()), window.width_) + window.x_;
244     buttonRect.y_ = std::fmod(std::fabs(GetData<double>()), window.height_) + window.y_;
245     buttonRect.width_ = DEFAULT_BUTTON_SIZE;
246     buttonRect.height_ = DEFAULT_BUTTON_SIZE;
247 
248     padding.bottom = window.y_ + window.height_ - buttonRect.y_ - buttonRect.height_;
249     padding.top = window.y_ - buttonRect.y_;
250     padding.left = buttonRect.x_ - window.x_;
251     padding.right = window.x_ + window.width_ - buttonRect.x_ - buttonRect.width_;
252 }
253 
ConstructWindowJson(nlohmann::json & jsonComponent,SecCompRect & window,PaddingSize & padding,BorderRadius & borderRadius,SecCompRect & buttonRect)254 void CompoRandomGenerator::ConstructWindowJson(nlohmann::json &jsonComponent, SecCompRect &window,
255     PaddingSize &padding, BorderRadius &borderRadius, SecCompRect &buttonRect)
256 {
257     ConstructButtonRect(window, padding, buttonRect);
258     jsonComponent[JsonTagConstants::JSON_RECT] =  nlohmann::json {
259         {JsonTagConstants::JSON_RECT_X, buttonRect.x_ },
260         {JsonTagConstants::JSON_RECT_Y, buttonRect.y_ },
261         {JsonTagConstants::JSON_RECT_WIDTH, buttonRect.width_ },
262         {JsonTagConstants::JSON_RECT_HEIGHT, buttonRect.height_ }
263     };
264     jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
265         { JsonTagConstants::JSON_RECT_X, window.x_ },
266         { JsonTagConstants::JSON_RECT_Y, window.y_ },
267         { JsonTagConstants::JSON_RECT_WIDTH, window.width_ },
268         { JsonTagConstants::JSON_RECT_HEIGHT, window.height_ }
269     };
270     nlohmann::json jsonPadding = nlohmann::json {
271         { JsonTagConstants::JSON_TOP_TAG, padding.top },
272         { JsonTagConstants::JSON_RIGHT_TAG, padding.right },
273         { JsonTagConstants::JSON_BOTTOM_TAG, padding.bottom },
274         { JsonTagConstants::JSON_LEFT_TAG, padding.left },
275     };
276 
277     nlohmann::json jsonBorderRadius = nlohmann::json {
278         { JsonTagConstants::JSON_LEFT_TOP_TAG, borderRadius.leftTop },
279         { JsonTagConstants::JSON_RIGHT_TOP_TAG, borderRadius.rightTop },
280         { JsonTagConstants::JSON_LEFT_BOTTOM_TAG, borderRadius.leftBottom },
281         { JsonTagConstants::JSON_RIGHT_BOTTOM_TAG, borderRadius.rightBottom },
282     };
283 
284     jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
285         { JsonTagConstants::JSON_FONT_SIZE_TAG, std::fabs(GetData<float>()) },
286         { JsonTagConstants::JSON_ICON_SIZE_TAG, std::fabs(GetData<float>()) },
287         { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, std::fabs(GetData<float>()) },
288         { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
289         { JsonTagConstants::JSON_BORDER_RADIUS_TAG, jsonBorderRadius},
290     };
291 }
292 }
293 }
294 }
295