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 "updatecallerinfoutil_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "ability_fuzz_util.h"
23 #define private public
24 #include "update_caller_info_util.h"
25 #undef private
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AppExecFwk;
28
29 namespace OHOS {
30 namespace {
31 constexpr size_t STRING_MAX_LENGTH = 128;
32 }
33
GetFuzzAbilityToken()34 sptr<Token> GetFuzzAbilityToken()
35 {
36 sptr<Token> token = nullptr;
37 AbilityRequest abilityRequest;
38 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
39 abilityRequest.abilityInfo.name = "MainAbility";
40 abilityRequest.abilityInfo.type = AbilityType::DATA;
41 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
42 if (abilityRecord) {
43 token = abilityRecord->GetToken();
44 }
45 return token;
46 }
47
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)48 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
49 {
50 FuzzedDataProvider fdp(data, size);
51 ElementName elementName;
52 AbilityFuzzUtil::GenerateElementName(fdp, elementName);
53 Want want;
54 want.SetElement(elementName);
55 sptr<IRemoteObject> callerToken = GetFuzzAbilityToken();
56 sptr<IRemoteObject> asCallerSourceToken = GetFuzzAbilityToken();
57 int32_t requestCode = fdp.ConsumeIntegral<int32_t>();
58 int32_t appIndex = fdp.ConsumeIntegral<int32_t>();
59 bool backFlag = fdp.ConsumeBool();
60 bool isRemote = fdp.ConsumeBool();
61 std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
62 std::string abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
63
64 UpdateCallerInfoUtil::GetInstance().UpdateAsCallerSourceInfo(want, asCallerSourceToken, callerToken);
65 UpdateCallerInfoUtil::GetInstance().UpdateCallerInfo(want, callerToken);
66 UpdateCallerInfoUtil::GetInstance().UpdateBackToCallerFlag(callerToken, want, requestCode, backFlag);
67 UpdateCallerInfoUtil::GetInstance().UpdateCallerInfoFromToken(want, callerToken);
68 UpdateCallerInfoUtil::GetInstance().UpdateDmsCallerInfo(want, callerToken);
69 UpdateCallerInfoUtil::GetInstance().UpdateSignatureInfo(bundleName, want, isRemote);
70 UpdateCallerInfoUtil::GetInstance().UpdateAsCallerInfoFromToken(want, asCallerSourceToken);
71 UpdateCallerInfoUtil::GetInstance().UpdateAsCallerInfoFromCallerRecord(want, callerToken);
72 UpdateCallerInfoUtil::GetInstance().UpdateAsCallerInfoFromDialog(want);
73 UpdateCallerInfoUtil::GetInstance().UpdateCallerBundleName(want, bundleName);
74 UpdateCallerInfoUtil::GetInstance().UpdateCallerAbilityName(want, abilityName);
75 UpdateCallerInfoUtil::GetInstance().UpdateCallerAppCloneIndex(want, appIndex);
76 UpdateCallerInfoUtil::GetInstance().ClearProtectedWantParam(want);
77
78 return true;
79 }
80 }
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 // Run your code on data.
86 OHOS::DoSomethingInterestingWithMyAPI(data, size);
87 return 0;
88 }