• 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 "abilityresidentprocessrdb_fuzzer.h"
17 
18 #include <charconv>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include "hilog_tag_wrapper.h"
21 #include "parser_util.h"
22 
23 #define private public
24 #include "ability_resident_process_rdb.h"
25 #include "rdb_data_manager.h"
26 #undef private
27 
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::AbilityRuntime;
30 
31 namespace OHOS {
32 namespace {
33 constexpr size_t STRING_MAX_LENGTH = 128;
34 }
35 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)36 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
37 {
38     struct AmsRdbConfig amsRdbConfig;
39     amsRdbConfig.tableName = "resident_process_list";
40     AmsResidentProcessRdbCallBack amsCallback(amsRdbConfig);
41     std::unique_ptr<RdbDataManager> rdbMgr =
42         std::make_unique<RdbDataManager>(amsRdbConfig);
43     rdbMgr->Init(amsCallback);
44     int currentVersion;
45     int targetVersion;
46     std::string databaseFile;
47     std::string bundleName;
48     bool enable;
49     FuzzedDataProvider fdp(data, size);
50     currentVersion = fdp.ConsumeIntegral<int32_t>();
51     targetVersion = fdp.ConsumeIntegral<int32_t>();
52     databaseFile = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
53     bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
54     enable = fdp.ConsumeBool();
55     amsCallback.OnCreate(*(rdbMgr->rdbStore_.get()));
56     amsCallback.OnUpgrade(*(rdbMgr->rdbStore_.get()), currentVersion, targetVersion);
57     amsCallback.OnDowngrade(*(rdbMgr->rdbStore_.get()), currentVersion, targetVersion);
58     amsCallback.OnOpen(*(rdbMgr->rdbStore_.get()));
59     amsCallback.onCorruption(databaseFile);
60     AmsResidentProcessRdb::GetInstance().Init();
61     AmsResidentProcessRdb::GetInstance().UpdateResidentProcessEnable(bundleName, enable);
62     AmsResidentProcessRdb::GetInstance().RemoveData(bundleName);
63     return true;
64 }
65 }
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     // Run your code on data.
71     OHOS::DoSomethingInterestingWithMyAPI(data, size);
72     return 0;
73 }
74 
75