• 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 {
GetScreenSize(double & width,double & height)34 bool GetScreenSize(double& width, double& height)
35 {
36     sptr<OHOS::Rosen::Display> display =
37         OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
38     if (display == nullptr) {
39         return false;
40     }
41 
42     auto info = display->GetDisplayInfo();
43     if (info == nullptr) {
44         return false;
45     }
46 
47     width = static_cast<double>(info->GetWidth());
48     height = static_cast<double>(info->GetHeight());
49     return true;
50 }
51 };
52 
GetScType()53 uint32_t CompoRandomGenerator::GetScType()
54 {
55     // generate a number in range
56     return GetData<uint32_t>() % (SecCompType::MAX_SC_TYPE - 1) + 1;
57 }
GenerateRandomCompoStr(uint32_t type)58 std::string CompoRandomGenerator::GenerateRandomCompoStr(uint32_t type)
59 {
60     switch (type) {
61         case LOCATION_COMPONENT:
62             return ConstructLocationJson();
63         case PASTE_COMPONENT:
64             return ConstructPasteJson();
65         case SAVE_COMPONENT:
66             return ConstructSaveJson();
67         default:
68             return std::string();
69     }
70     return std::string();
71 }
72 
73 // To generate a json, some objs are in default, otherwise these may not pass value check.
ConstructLocationJson()74 std::string CompoRandomGenerator::ConstructLocationJson()
75 {
76     nlohmann::json jsonComponent;
77     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT;
78     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
79     SecCompRect window;
80     PaddingSize padding;
81     SecCompRect buttonRect;
82     ConstructWindowJson(jsonComponent, window, padding, buttonRect);
83 
84     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
85         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
86         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
87         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
88     };
89 
90     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
91         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
92     };
93     // set default val
94     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
95         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
96         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
97         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
98         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
99         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
100         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
101         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
102     };
103     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
104         { JsonTagConstants::JSON_TEXT_TAG,
105             GetData<int32_t>() % static_cast<int32_t>(LocationDesc::MAX_LABEL_TYPE) },
106         { JsonTagConstants::JSON_ICON_TAG,
107             GetData<int32_t>() % static_cast<int32_t>(LocationIcon::MAX_ICON_TYPE) },
108         { JsonTagConstants::JSON_BG_TAG,
109             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
110     };
111     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
112     jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
113     jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
114     return jsonComponent.dump();
115 }
116 
117 
ConstructSaveJson()118 std::string CompoRandomGenerator::ConstructSaveJson()
119 {
120     nlohmann::json jsonComponent;
121     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT;
122     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
123     SecCompRect window;
124     PaddingSize padding;
125     SecCompRect buttonRect;
126     ConstructWindowJson(jsonComponent, window, padding, buttonRect);
127 
128     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
129         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
130         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
131         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
132     };
133 
134     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
135         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
136     };
137     // set default val
138     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
139         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
140         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
141         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
142         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
143         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
144         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
145         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
146     };
147     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
148         { JsonTagConstants::JSON_TEXT_TAG,
149             GetData<int32_t>() % static_cast<int32_t>(SaveDesc::MAX_LABEL_TYPE) },
150         { JsonTagConstants::JSON_ICON_TAG,
151             GetData<int32_t>() % static_cast<int32_t>(SaveIcon::MAX_ICON_TYPE) },
152         { JsonTagConstants::JSON_BG_TAG,
153             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
154     };
155     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
156     jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
157     jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
158     return jsonComponent.dump();
159 }
160 
ConstructPasteJson()161 std::string CompoRandomGenerator::ConstructPasteJson()
162 {
163     nlohmann::json jsonComponent;
164     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT;
165     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
166     SecCompRect window;
167     PaddingSize padding;
168     SecCompRect buttonRect;
169     ConstructWindowJson(jsonComponent, window, padding, buttonRect);
170 
171     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
172         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
173         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
174         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
175     };
176 
177     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
178         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
179     };
180     // set default val
181     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
182         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
183         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
184         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
185         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
186         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
187         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
188         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
189     };
190     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
191         { JsonTagConstants::JSON_TEXT_TAG,
192             GetData<int32_t>() % static_cast<int32_t>(PasteDesc::MAX_LABEL_TYPE) },
193         { JsonTagConstants::JSON_ICON_TAG,
194             GetData<int32_t>() % static_cast<int32_t>(PasteIcon::MAX_ICON_TYPE) },
195         { JsonTagConstants::JSON_BG_TAG,
196             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
197     };
198     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
199     jsonComponent[JsonTagConstants::JSON_DISPLAY_ID] = 0;
200     jsonComponent[JsonTagConstants::JSON_CROSS_AXIS_STATE] = 0;
201     return jsonComponent.dump();
202 }
203 
ConstructRandomRect(SecCompRect & rect)204 void CompoRandomGenerator::ConstructRandomRect(SecCompRect &rect)
205 {
206     // a window rect should val >= 0
207     double width;
208     double height;
209     GetScreenSize(width, height);
210     rect.x_ = std::fmod(std::fabs(GetData<double>()), width);
211     rect.y_ = std::fmod(std::fabs(GetData<double>()), height);
212     rect.width_ = std::fmod(std::fabs(GetData<double>()), width);
213     rect.height_ = std::fmod(std::fabs(GetData<double>()), height);
214 }
215 
ConstructButtonRect(SecCompRect & window,PaddingSize & padding,SecCompRect & buttonRect)216 void CompoRandomGenerator::ConstructButtonRect(
217     SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect)
218 {
219     ConstructRandomRect(window);
220     double tmp1 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.height_));
221     double tmp2 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.height_));
222     double top = std::min(tmp1, tmp2);
223     double bottom = std::max(tmp1, tmp2);
224     buttonRect.y_ = window.y_ + top;
225     buttonRect.height_ = bottom - top;
226     padding.top = top;
227     padding.bottom = window.height_ - bottom;
228 
229     double width;
230     double height;
231     double sizeLimitRate = 0.09;
232     GetScreenSize(width, height);
233     double sizeLimit = width * height * sizeLimitRate;
234     double buttonMaxWidth = sizeLimit / buttonRect.height_;
235 
236     tmp1 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.width_));
237     tmp2 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.width_));
238     double left = std::min(tmp1, tmp2);
239     double right = std::max(tmp1, tmp2);
240     buttonRect.x_ = window.x_ + left;
241     buttonRect.width_ = std::min({right - left, buttonMaxWidth, window.width_ - buttonRect.x_ - 1});
242     padding.left = left;
243     padding.right = window.width_ - buttonRect.width_ - left;
244 }
245 
ConstructWindowJson(nlohmann::json & jsonComponent,SecCompRect & window,PaddingSize & padding,SecCompRect & buttonRect)246 void CompoRandomGenerator::ConstructWindowJson(
247     nlohmann::json &jsonComponent, SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect)
248 {
249     ConstructButtonRect(window, padding, buttonRect);
250     jsonComponent[JsonTagConstants::JSON_RECT] =  nlohmann::json {
251         {JsonTagConstants::JSON_RECT_X, buttonRect.x_ },
252         {JsonTagConstants::JSON_RECT_Y, buttonRect.y_ },
253         {JsonTagConstants::JSON_RECT_WIDTH, buttonRect.width_ },
254         {JsonTagConstants::JSON_RECT_HEIGHT, buttonRect.height_ }
255     };
256     jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
257         { JsonTagConstants::JSON_RECT_X, window.x_ },
258         { JsonTagConstants::JSON_RECT_Y, window.y_ },
259         { JsonTagConstants::JSON_RECT_WIDTH, window.width_ },
260         { JsonTagConstants::JSON_RECT_HEIGHT, window.height_ }
261     };
262     nlohmann::json jsonPadding = nlohmann::json {
263         { JsonTagConstants::JSON_PADDING_TOP_TAG, padding.top },
264         { JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding.right },
265         { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding.bottom },
266         { JsonTagConstants::JSON_PADDING_LEFT_TAG, padding.left },
267     };
268 
269     jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
270         { JsonTagConstants::JSON_FONT_SIZE_TAG, std::fabs(GetData<float>()) },
271         { JsonTagConstants::JSON_ICON_SIZE_TAG, std::fabs(GetData<float>()) },
272         { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, std::fabs(GetData<float>()) },
273         { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
274     };
275 }
276 }
277 }
278 }
279