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     return jsonComponent.dump();
113 }
114 
115 
ConstructSaveJson()116 std::string CompoRandomGenerator::ConstructSaveJson()
117 {
118     nlohmann::json jsonComponent;
119     jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT;
120     jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0;
121     SecCompRect window;
122     PaddingSize padding;
123     SecCompRect buttonRect;
124     ConstructWindowJson(jsonComponent, window, padding, buttonRect);
125 
126     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
127         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
128         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
129         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
130     };
131 
132     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
133         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
134     };
135     // set default val
136     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
137         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
138         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
139         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
140         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
141         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
142         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
143         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
144     };
145     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
146         { JsonTagConstants::JSON_TEXT_TAG,
147             GetData<int32_t>() % static_cast<int32_t>(SaveDesc::MAX_LABEL_TYPE) },
148         { JsonTagConstants::JSON_ICON_TAG,
149             GetData<int32_t>() % static_cast<int32_t>(SaveIcon::MAX_ICON_TYPE) },
150         { JsonTagConstants::JSON_BG_TAG,
151             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
152     };
153     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
154     return jsonComponent.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     SecCompRect window;
163     PaddingSize padding;
164     SecCompRect buttonRect;
165     ConstructWindowJson(jsonComponent, window, padding, buttonRect);
166 
167     jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
168         { JsonTagConstants::JSON_FONT_COLOR_TAG, GetData<uint32_t>() },
169         { JsonTagConstants::JSON_ICON_COLOR_TAG, GetData<uint32_t>() },
170         { JsonTagConstants::JSON_BG_COLOR_TAG, GetData<uint32_t>() }
171     };
172 
173     jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
174         { JsonTagConstants::JSON_BORDER_WIDTH_TAG, GetData<float>() },
175     };
176     // set default val
177     jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
178         { JsonTagConstants::JSON_PARENT_EFFECT_TAG, GetData<bool>() },
179         { JsonTagConstants::JSON_IS_CLIPPED_TAG, GetData<bool>() },
180         { JsonTagConstants::JSON_TOP_CLIP_TAG, GetData<double>() },
181         { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, GetData<double>() },
182         { JsonTagConstants::JSON_LEFT_CLIP_TAG, GetData<double>() },
183         { JsonTagConstants::JSON_RIGHT_CLIP_TAG, GetData<double>() },
184         { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
185     };
186     jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
187         { JsonTagConstants::JSON_TEXT_TAG,
188             GetData<int32_t>() % static_cast<int32_t>(PasteDesc::MAX_LABEL_TYPE) },
189         { JsonTagConstants::JSON_ICON_TAG,
190             GetData<int32_t>() % static_cast<int32_t>(PasteIcon::MAX_ICON_TYPE) },
191         { JsonTagConstants::JSON_BG_TAG,
192             GetData<int32_t>() % static_cast<int32_t>(SecCompBackground::MAX_BG_TYPE) },
193     };
194     jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0;
195     return jsonComponent.dump();
196 }
197 
ConstructRandomRect(SecCompRect & rect)198 void CompoRandomGenerator::ConstructRandomRect(SecCompRect &rect)
199 {
200     // a window rect should val >= 0
201     double width;
202     double height;
203     GetScreenSize(width, height);
204     rect.x_ = std::fmod(std::fabs(GetData<double>()), width);
205     rect.y_ = std::fmod(std::fabs(GetData<double>()), height);
206     rect.width_ = std::fmod(std::fabs(GetData<double>()), width);
207     rect.height_ = std::fmod(std::fabs(GetData<double>()), height);
208 }
209 
ConstructButtonRect(SecCompRect & window,PaddingSize & padding,SecCompRect & buttonRect)210 void CompoRandomGenerator::ConstructButtonRect(
211     SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect)
212 {
213     ConstructRandomRect(window);
214     double tmp1 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.height_));
215     double tmp2 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.height_));
216     double top = std::min(tmp1, tmp2);
217     double bottom = std::max(tmp1, tmp2);
218     buttonRect.y_ = window.y_ + top;
219     buttonRect.height_ = bottom - top;
220     padding.top = top;
221     padding.bottom = window.height_ - bottom;
222 
223     double width;
224     double height;
225     double sizeLimitRate = 0.09;
226     GetScreenSize(width, height);
227     double sizeLimit = width * height * sizeLimitRate;
228     double buttonMaxWidth = sizeLimit / buttonRect.height_;
229 
230     tmp1 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.width_));
231     tmp2 = std::fmod(std::fabs(GetData<double>()), static_cast<double>(window.width_));
232     double left = std::min(tmp1, tmp2);
233     double right = std::max(tmp1, tmp2);
234     buttonRect.x_ = window.x_ + left;
235     buttonRect.width_ = std::min({right - left, buttonMaxWidth, window.width_ - buttonRect.x_ - 1});
236     padding.left = left;
237     padding.right = window.width_ - buttonRect.width_ - left;
238 }
239 
ConstructWindowJson(nlohmann::json & jsonComponent,SecCompRect & window,PaddingSize & padding,SecCompRect & buttonRect)240 void CompoRandomGenerator::ConstructWindowJson(
241     nlohmann::json &jsonComponent, SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect)
242 {
243     ConstructButtonRect(window, padding, buttonRect);
244     jsonComponent[JsonTagConstants::JSON_RECT] =  nlohmann::json {
245         {JsonTagConstants::JSON_RECT_X, buttonRect.x_ },
246         {JsonTagConstants::JSON_RECT_Y, buttonRect.y_ },
247         {JsonTagConstants::JSON_RECT_WIDTH, buttonRect.width_ },
248         {JsonTagConstants::JSON_RECT_HEIGHT, buttonRect.height_ }
249     };
250     jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json {
251         { JsonTagConstants::JSON_RECT_X, window.x_ },
252         { JsonTagConstants::JSON_RECT_Y, window.y_ },
253         { JsonTagConstants::JSON_RECT_WIDTH, window.width_ },
254         { JsonTagConstants::JSON_RECT_HEIGHT, window.height_ }
255     };
256     nlohmann::json jsonPadding = nlohmann::json {
257         { JsonTagConstants::JSON_PADDING_TOP_TAG, padding.top },
258         { JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding.right },
259         { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding.bottom },
260         { JsonTagConstants::JSON_PADDING_LEFT_TAG, padding.left },
261     };
262 
263     jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json {
264         { JsonTagConstants::JSON_FONT_SIZE_TAG, std::fabs(GetData<float>()) },
265         { JsonTagConstants::JSON_ICON_SIZE_TAG, std::fabs(GetData<float>()) },
266         { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, std::fabs(GetData<float>()) },
267         { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding },
268     };
269 }
270 }
271 }
272 }
273