• 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 "uriutils_fuzzer.h"
17 
18 #include <fuzzer/FuzzedDataProvider.h>
19 
20 #define private public
21 #include "uri_utils.h"
22 #undef private
23 
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::AppExecFwk;
26 
27 namespace OHOS {
28 namespace {
29 constexpr size_t STRING_MAX_LENGTH = 128;
30 }
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
32 {
33     std::string bundleName;
34     std::string identifier;
35     std::string callerName;
36     std::string targetBundleName;
37     std::string targetPkg;
38     std::string callerBundleName;
39     std::string oriUri;
40     std::string eventType;
41     Want want;
42     bool checkResult;
43     bool isSandboxApp;
44     int32_t appIndex;
45     int32_t apiVersion;
46     int32_t collaboratorType;
47     uint32_t tokenId;
48     uint32_t callerTokenId;
49     uint32_t fromTokenId;
50     uint32_t flag;
51     uint32_t initiatorTokenId;
52     std::vector<Uri> permissionedUris;
53     std::vector<std::string> uriVec;
54     // std::vector<bool> checkResults;
55     AbilityRequest abilityRequest;
56     FuzzedDataProvider fdp(data, size);
57     bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
58     identifier = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
59     callerName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
60     targetBundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
61     targetPkg = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
62     callerBundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
63     oriUri = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
64     eventType = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
65     checkResult = fdp.ConsumeBool();
66     isSandboxApp = fdp.ConsumeBool();
67     appIndex = fdp.ConsumeIntegral<int32_t>();
68     apiVersion = fdp.ConsumeIntegral<int32_t>();
69     collaboratorType = fdp.ConsumeIntegral<int32_t>();
70     tokenId = fdp.ConsumeIntegral<uint32_t>();
71     callerTokenId = fdp.ConsumeIntegral<uint32_t>();
72     fromTokenId = fdp.ConsumeIntegral<uint32_t>();
73     flag = fdp.ConsumeIntegral<uint32_t>();
74     initiatorTokenId = fdp.ConsumeIntegral<uint32_t>();
75     UriUtils::GetInstance().IsInAncoAppIdentifier(bundleName);
76     UriUtils::GetInstance().CheckIsInAncoAppIdentifier(identifier, bundleName);
77     identifier = "";
78     UriUtils::GetInstance().CheckIsInAncoAppIdentifier(identifier, bundleName);
79     UriUtils::GetInstance().GetUriListFromWantDms(want);
80     UriUtils::GetInstance().ProcessWantUri(checkResult, apiVersion, want, permissionedUris);
81     UriUtils::GetInstance().GetCallerNameAndApiVersion(tokenId, callerName, apiVersion);
82     UriUtils::GetInstance().GetUriListFromWant(want, uriVec);
83     UriUtils::GetInstance().IsGrantUriPermissionFlag(want);
84     ExtensionAbilityType extensionAbilityType = ExtensionAbilityType::SERVICE;
85     UriUtils::GetInstance().IsServiceExtensionType(extensionAbilityType);
86     extensionAbilityType = ExtensionAbilityType::UI_SERVICE;
87     UriUtils::GetInstance().IsServiceExtensionType(extensionAbilityType);
88     UriUtils::GetInstance().IsDmsCall(fromTokenId);
89     UriUtils::GetInstance().GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex);
90     UriUtils::GetInstance().GrantShellUriPermission(uriVec, flag, targetPkg, appIndex);
91     UriUtils::GetInstance().CheckUriPermission(callerTokenId, want);
92     UriUtils::GetInstance().GrantUriPermission(uriVec, flag, targetBundleName, appIndex, initiatorTokenId);
93     UriUtils::GetInstance().IsSandboxApp(tokenId);
94     UriUtils::GetInstance().GrantUriPermission(want, targetBundleName, appIndex, isSandboxApp,
95         callerTokenId, collaboratorType);
96     UriUtils::GetInstance().ProcessUDMFKey(want);
97     UriUtils::GetInstance().PublishFileOpenEvent(want);
98     UriUtils::GetInstance().GrantUriPermissionForServiceExtension(abilityRequest);
99     UriUtils::GetInstance().GrantUriPermissionForUIOrServiceExtension(abilityRequest);
100     UriUtils::GetInstance().SendGrantUriPermissionEvent(callerBundleName, targetBundleName, oriUri,
101         apiVersion, eventType);
102     return true;
103 }
104 }
105 
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109     // Run your code on data.
110     OHOS::DoSomethingInterestingWithMyAPI(data, size);
111     return 0;
112 }