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 #ifndef SECURITY_COMPONENT_MANAGER_FUZZ_COMMON_H
17 #define SECURITY_COMPONENT_MANAGER_FUZZ_COMMON_H
18 #include <string>
19 #include "nlohmann/json.hpp"
20 #include "securec.h"
21 #include "sec_comp_info.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace SecurityComponent {
26 class CompoRandomGenerator {
27 public:
CompoRandomGenerator(const uint8_t * data,const size_t size)28     CompoRandomGenerator(const uint8_t *data, const size_t size)
29         : data_(data), dataLenth_(size)
30     {}
31 
32     uint32_t GetScType();
33     std::string GenerateRandomCompoStr(uint32_t type);
34     std::string ConstructLocationJson();
35     std::string ConstructSaveJson();
36     std::string ConstructPasteJson();
GetData()37     template <class T> T GetData()
38     {
39         T object{};
40         size_t objectSize = sizeof(object);
41         if (data_ == nullptr || objectSize > dataLenth_ - basePos) {
42             basePos = 0; // reset read point
43             return object; // return empty obj
44         }
45         errno_t ret = memcpy_s(&object, objectSize, data_ + basePos, objectSize);
46         if (ret != EOK) {
47             return {};
48         }
49         basePos += objectSize;
50         return object;
51     }
52 private:
53     void ConstructRandomRect(SecCompRect &rect);
54     void ConstructButtonRect(
55         SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect);
56     void ConstructWindowJson(nlohmann::json &jsonComponent,
57         SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect);
58 
59     const uint8_t *data_;
60     const size_t dataLenth_;
61     size_t basePos = 0;
62 };
63 }
64 }
65 }
66 #endif // SECURITY_COMPONENT_MANAGER_FUZZ_COMMON_H