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 ACCESSCONTROL_SANDBOX_MANAGER_FUZZ_COMMON
17 #define ACCESSCONTROL_SANDBOX_MANAGER_FUZZ_COMMON
18 #include <vector>
19 #include "policy_info.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace AccessControl {
24 namespace SandboxManager {
25 class PolicyInfoRandomGenerator {
26 public:
PolicyInfoRandomGenerator(const uint8_t * data,const size_t size)27     PolicyInfoRandomGenerator(const uint8_t *data, const size_t size)
28         : data_(data), dataLenth_(size)
29     {}
GetData()30     template <class T> T GetData()
31     {
32         T object{};
33         size_t objectSize = sizeof(object);
34         if (data_ == nullptr || objectSize > dataLenth_ - basePos) {
35             basePos = 0; // reset read point
36             return object; // return empty obj
37         }
38         errno_t ret = memcpy_s(&object, objectSize, data_ + basePos, objectSize);
39         if (ret != EOK) {
40             return {};
41         }
42         basePos += objectSize;
43         return object;
44     }
45 
46     void GeneratePolicyInfo(PolicyInfo &policyInfo);
47     void GeneratePolicyInfoVec(std::vector<PolicyInfo> &policyInfoVec);
48     void GenerateString(std::string &str);
49     void GenerateStringVec(std::vector<std::string> &strVec);
50 private:
51     const uint8_t *data_;
52     const size_t dataLenth_;
53     size_t basePos = 0;
54 };
55 }
56 }
57 }
58 #endif