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 "insightintentdbcache_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "ability_fuzz_util.h"
23 #include "insight_intent_db_cache.h"
24
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AbilityRuntime;
27 using namespace OHOS::AppExecFwk;
28
29 namespace OHOS {
30 namespace {
31 constexpr size_t STRING_MAX_LENGTH = 128;
32 }
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)33 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
34 {
35 int32_t userId;
36 std::string bundleName;
37 std::string moduleName;
38 std::string intentName;
39 ExtractInsightIntentProfileInfo info;
40 ExtractInsightIntentProfileInfoVec profileInfos;
41 ExtractInsightIntentGenericInfo genericInfo;
42 std::vector<ExtractInsightIntentGenericInfo> genericInfos;
43 ExtractInsightIntentInfo intentInfo;
44 std::vector<ExtractInsightIntentInfo> intentInfos;
45 FuzzedDataProvider fdp(data, size);
46 userId = fdp.ConsumeIntegral<int32_t>();
47 bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
48 moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
49 intentName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
50 AbilityFuzzUtil::GetRandomExtractInsightIntentProfileInfo(fdp, info);
51 profileInfos.insightIntents.push_back(info);
52 AbilityFuzzUtil::GetRandomExtractInsightIntentGenericInfo(fdp, genericInfo);
53 AbilityFuzzUtil::GetRandomExtractInsightIntentInfo(fdp, intentInfo);
54 DelayedSingleton<InsightIntentDbCache>::GetInstance()->InitInsightIntentCache(userId);
55 DelayedSingleton<InsightIntentDbCache>::GetInstance()->SaveInsightIntentTotalInfo(bundleName, moduleName,
56 userId, profileInfos);
57 DelayedSingleton<InsightIntentDbCache>::GetInstance()->DeleteInsightIntentTotalInfo(bundleName, moduleName,
58 userId);
59 DelayedSingleton<InsightIntentDbCache>::GetInstance()->DeleteInsightIntentByUserId(userId);
60 DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetAllInsightIntentGenericInfo(genericInfos);
61 DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetInsightIntentGenericInfoByName(bundleName, genericInfos);
62 DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetInsightIntentGenericInfo(bundleName, moduleName,
63 intentName, genericInfo);
64 DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetAllInsightIntentInfo(userId, intentInfos);
65 DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetInsightIntentInfoByName(bundleName, userId, intentInfos);
66 DelayedSingleton<InsightIntentDbCache>::GetInstance()->GetInsightIntentInfo(bundleName, moduleName,
67 intentName, userId, intentInfo);
68 return true;
69 }
70 }
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75 // Run your code on data.
76 OHOS::DoSomethingInterestingWithMyAPI(data, size);
77 return 0;
78 }