• 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 #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 GetMessage();
34     std::string GenerateRandomCompoStr(uint32_t type);
35     std::string ConstructLocationJson();
36     std::string ConstructSaveJson();
37     std::string ConstructPasteJson();
GetData()38     template <class T> T GetData()
39     {
40         T object{0};
41         size_t objectSize = sizeof(object);
42         if (data_ == nullptr) {
43             return object; // return empty obj
44         }
45         size_t readedSize = 0;
46         char* objectPtr = reinterpret_cast<char*>(&object);
47         while (readedSize < objectSize) {
48             size_t needRead = std::min(objectSize - readedSize, dataLenth_ - basePos);
49             errno_t ret = memcpy_s(objectPtr + readedSize, needRead, data_ + basePos, needRead);
50             if (ret != EOK) {
51                 T empty{0};
52                 return empty;
53             }
54             readedSize += needRead;
55             basePos += needRead;
56             if (basePos >= dataLenth_) {
57                 basePos = 0;
58             }
59         }
60         return object;
61     }
62     nlohmann::json compoJson_;
63 private:
64     void ConstructButtonRect(
65         SecCompRect &window, PaddingSize &padding, SecCompRect &buttonRect);
66     void ConstructWindowJson(nlohmann::json &jsonComponent,
67         SecCompRect &window, PaddingSize &padding, BorderRadius &borderRadius, SecCompRect &buttonRect);
68 
69     const uint8_t *data_;
70     const size_t dataLenth_;
71     size_t basePos = 0;
72 };
73 }
74 }
75 }
76 #endif // SECURITY_COMPONENT_MANAGER_FUZZ_COMMON_H