• 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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include "bundle_resource_event_subscriber.h"
22 #include "common_event_support.h"
23 #include "securec.h"
24 
25 #include "bmsbundleresourceeventsubscriber_fuzzer.h"
26 #include "bms_fuzztest_util.h"
27 
28 using namespace OHOS::AppExecFwk;
29 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
30 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
32 {
33     FuzzedDataProvider fdp(data, size);
34     OHOS::EventFwk::MatchingSkills matchingSkills;
35     matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
36     OHOS::EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
37     auto subscriberPtr = std::make_shared<BundleResourceEventSubscriber>(subscribeInfo);
38 
39     OHOS::EventFwk::CommonEventData commonEventData;
40     subscriberPtr->OnReceiveEvent(commonEventData);
41 
42     OHOS::AAFwk::Want want;
43 
44     want.SetParam("oldId", Constants::DEFAULT_USERID);
45     commonEventData.SetWant(want);
46     subscriberPtr->OnReceiveEvent(commonEventData);
47 
48     want.SetParam("oldId", Constants::INVALID_USERID);
49     commonEventData.SetWant(want);
50     subscriberPtr->OnReceiveEvent(commonEventData);
51 
52     want.SetParam("oldId", Constants::U1);
53     commonEventData.SetWant(want);
54     subscriberPtr->OnReceiveEvent(commonEventData);
55 
56     int32_t userId = fdp.ConsumeIntegral<int32_t>();
57     want.SetParam("oldId", userId);
58     commonEventData.SetWant(want);
59     subscriberPtr->OnReceiveEvent(commonEventData);
60 
61     int32_t oldUserId = fdp.ConsumeIntegral<int32_t>();
62     int32_t newUserId = fdp.ConsumeIntegral<int32_t>();
63     subscriberPtr->OnUserIdChanged(oldUserId, newUserId);
64 
65     return true;
66 }
67 }
68 
69 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72     /* Run your code on data */
73     OHOS::DoSomethingInterestingWithMyAPI(data, size);
74     return 0;
75 }