• 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 "dialogsessionmanagerfirst_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #define private public
21 #include "dialog_session_manager.h"
22 #undef private
23 #include "ability_fuzz_util.h"
24 #include "ability_record.h"
25 #include "dialog_session_manager.h"
26 
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29 
30 namespace OHOS {
31 namespace {
32     constexpr size_t CODE_TWO = 2;
33 } // namespace
34 
GetFuzzAbilityToken()35 sptr<Token> GetFuzzAbilityToken()
36 {
37     sptr<Token> token = nullptr;
38     AbilityRequest abilityRequest;
39     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
40     abilityRequest.abilityInfo.name = "MainAbility";
41     abilityRequest.abilityInfo.type = AbilityType::DATA;
42     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
43     if (abilityRecord) {
44         token = abilityRecord->GetToken();
45     }
46     return token;
47 }
48 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)49 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
50 {
51     AbilityRequest info;
52     SelectorType type;
53     sptr<IRemoteObject> callerToken = GetFuzzAbilityToken();
54     std::string dialogSessionId;
55     FuzzedDataProvider fdp(data, size);
56     bool isSCBCall = fdp.ConsumeBool();
57     int32_t userId = fdp.ConsumeIntegral<int32_t>();
58     dialogSessionId = fdp.ConsumeRandomLengthString();
59     bool needGrantUriPermission = fdp.ConsumeBool();
60     type = static_cast<SelectorType>(fdp.ConsumeIntegralInRange<int32_t>(0, CODE_TWO));
61     AbilityFuzzUtil::GetRandomAbilityRequestInfo(fdp, info);
62     std::shared_ptr<DialogSessionManager> dialogSessionManager = std::make_shared<DialogSessionManager>();
63     if (dialogSessionManager == nullptr) {
64         return false;
65     }
66     dialogSessionManager->SetQueryERMSInfo(dialogSessionId, info);
67     dialogSessionManager->UpdateExtensionWantWithDialogCallerInfo(info, callerToken, isSCBCall);
68     dialogSessionManager->OnlySetDialogCallerInfo(info, userId, type, dialogSessionId, needGrantUriPermission);
69     return true;
70 }
71 } // namespace OHOS
72 
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
75 {
76     /* Run your code on data */
77 
78     OHOS::DoSomethingInterestingWithMyAPI(data, size);
79     return 0;
80 }