• 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 #include "rdbimpl_fuzzer.h"
16 
17 #include <fuzzer/FuzzedDataProvider.h>
18 
19 #include "rdb_errno.h"
20 #include "rdb_store_config.h"
21 #include "rdb_store_impl.h"
22 #include "rdb_helper.h"
23 #include "rdb_open_callback.h"
24 #include "rdb_store.h"
25 
26 using namespace OHOS;
27 using namespace OHOS::NativeRdb;
28 namespace OHOS {
29 
30 static const std::string RDB_PATH = "/data/test/RdbImplFuzzer.db";
31 
32 class RdbTestOpenCallback : public RdbOpenCallback {
33 public:
34     int OnCreate(RdbStore &store) override;
35     int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override;
36     static const std::string CREATE_TABLE_TEST;
37 };
38 const std::string RdbTestOpenCallback::CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test "
39                                                            "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
40                                                            "name TEXT NOT NULL, age INTEGER, salary REAL, "
41                                                            "blobType BLOB)";
42 
OnCreate(RdbStore & store)43 int RdbTestOpenCallback::OnCreate(RdbStore &store)
44 {
45     return store.ExecuteSql(CREATE_TABLE_TEST);
46 }
47 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)48 int RdbTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
49 {
50     return E_OK;
51 }
52 
RdbStoreImplFuzz(const uint8_t * data,size_t size)53 void RdbStoreImplFuzz(const uint8_t *data, size_t size)
54 {
55     int errCode = E_OK;
56     RdbStoreConfig config(RDB_PATH);
57     RdbTestOpenCallback helper;
58     std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
59     if (store == nullptr || errCode != E_OK) {
60         return;
61     }
62 
63     FuzzedDataProvider provider(data, size);
64     std::vector<std::string> tables;
65     std::string rawString = provider.ConsumeRandomLengthString();
66     tables.push_back(rawString);
67 
68     OHOS::DistributedRdb::DistributedConfig distributedConfig;
69     distributedConfig.autoSync = provider.ConsumeBool();
70 
71     OHOS::DistributedRdb::Reference reference;
72     reference.sourceTable = provider.ConsumeRandomLengthString();
73     reference.targetTable = provider.ConsumeRandomLengthString();
74     const int mapSize = 10;
75     for (int i = 0; i < mapSize; i++) {
76         reference.refFields.insert(
77             std::make_pair(provider.ConsumeRandomLengthString(), provider.ConsumeRandomLengthString()));
78     }
79 
80     distributedConfig.references.push_back(reference);
81     distributedConfig.isRebuild = provider.ConsumeBool();
82     distributedConfig.asyncDownloadAsset = provider.ConsumeBool();
83     distributedConfig.enableCloud = provider.ConsumeBool();
84 
85     DistributedRdb::DistributedTableType type = static_cast<DistributedRdb::DistributedTableType>(
86         provider.ConsumeIntegralInRange<int>(DistributedRdb::DISTRIBUTED_DEVICE, DistributedRdb::DISTRIBUTED_SEARCH));
87     store->SetDistributedTables(tables, type, distributedConfig);
88 }
89 } // namespace OHOS
90 
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
93 {
94     /* Run your code on data */
95     OHOS::RdbStoreImplFuzz(data, size);
96     return 0;
97 }
98