1 /*
2 * Copyright (c) 2025 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 <cstddef>
17 #include <cstdint>
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #include "app_running_control_rule_result.h"
21 #include "parcel.h"
22
23 #include "bmsapprunningcontrolruleresult_fuzzer.h"
24 #include "bms_fuzztest_util.h"
25
26 using namespace OHOS::AppExecFwk;
27 namespace {
GenerateAppRunningControlRuleResult(FuzzedDataProvider & fdp)28 AppRunningControlRuleResult GenerateAppRunningControlRuleResult(FuzzedDataProvider& fdp)
29 {
30 AppRunningControlRuleResult appRunningControlRuleResult;
31 appRunningControlRuleResult.isEdm = fdp.ConsumeBool();
32 appRunningControlRuleResult.controlMessage = fdp.ConsumeRandomLengthString(BMSFuzzTestUtil::STRING_MAX_LENGTH);
33 appRunningControlRuleResult.controlWant = nullptr;
34
35 return appRunningControlRuleResult;
36 }
37 }
38 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
40 {
41 Parcel dataMessageParcel;
42 FuzzedDataProvider fdp(data, size);
43 AppRunningControlRuleResult oldRule = GenerateAppRunningControlRuleResult(fdp);
44 if (!oldRule.Marshalling(dataMessageParcel)) {
45 return false;
46 }
47 auto rulePtr = AppRunningControlRuleResult::Unmarshalling(dataMessageParcel);
48 if (rulePtr == nullptr) {
49 return false;
50 }
51 delete rulePtr;
52 rulePtr = nullptr;
53
54 AppRunningControlRuleResult readRule = GenerateAppRunningControlRuleResult(fdp);
55 bool ret = readRule.ReadFromParcel(dataMessageParcel);
56 return ret;
57 }
58 }
59
60 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63 // Run your code on data.
64 OHOS::DoSomethingInterestingWithMyAPI(data, size);
65 return 0;
66 }
67