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