• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_control_proxy.h"
21 
22 #include "bmsaddappjumpcontrolrule_fuzzer.h"
23 #include "securec.h"
24 #include "bms_fuzztest_util.h"
25 
26 using namespace OHOS::AppExecFwk;
27 namespace {
GetRandomAppJumpControlRule(FuzzedDataProvider & fdp,AppJumpControlRule & appJumpControlRule)28 void GetRandomAppJumpControlRule(FuzzedDataProvider& fdp, AppJumpControlRule& appJumpControlRule)
29 {
30     appJumpControlRule.jumpMode =
31         static_cast<AbilityJumpMode>(fdp.ConsumeIntegralInRange<int8_t>(0, 2));
32     appJumpControlRule.callerPkg = fdp.ConsumeRandomLengthString(BMSFuzzTestUtil::STRING_MAX_LENGTH);
33     appJumpControlRule.targetPkg = fdp.ConsumeRandomLengthString(BMSFuzzTestUtil::STRING_MAX_LENGTH);
34     appJumpControlRule.controlMessage = fdp.ConsumeRandomLengthString(BMSFuzzTestUtil::STRING_MAX_LENGTH);
35 }
36 }
37 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)38     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
39     {
40         sptr<IRemoteObject> object;
41         AppControlProxy appControl(object);
42 
43         FuzzedDataProvider fdp(data, size);
44         size_t arraySize = fdp.ConsumeIntegralInRange<size_t>(0, BMSFuzzTestUtil::ARRAY_MAX_LENGTH);
45 
46         std::vector<AppJumpControlRule> controlRules;
47         for (size_t i = 0; i < arraySize; i++) {
48             AppJumpControlRule appJumpControlRule;
49             GetRandomAppJumpControlRule(fdp, appJumpControlRule);
50             controlRules.emplace_back(appJumpControlRule);
51         }
52 
53         int32_t userId = BMSFuzzTestUtil::GenerateRandomUser(fdp);
54         appControl.AddAppJumpControlRule(controlRules, userId);
55         return true;
56     }
57 }
58 
59 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61 {
62     /* Run your code on data */
63     if (data == nullptr) {
64         return 0;
65     }
66     OHOS::DoSomethingInterestingWithMyAPI(data, size);
67     return 0;
68 }