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 "abssharedresultset_fuzzer.h"
16
17 #include <fuzzer/FuzzedDataProvider.h>
18
19 #include <string>
20 #include <vector>
21
22 #include "abs_shared_result_set.h"
23 #include "rdb_types.h"
24 #include "ashmem.h"
25 #include "rd_statement.h"
26 #include "rdb_errno.h"
27 #include "rdb_store_config.h"
28 #include "rdb_store_impl.h"
29 #include "refbase.h"
30 #include "shared_block.h"
31
32
33 using namespace OHOS;
34 using namespace OHOS::NativeRdb;
35
36 static const size_t DEFAULT_BLOCK_SIZE = 1 * 1024 * 1024;
37
38 namespace OHOS {
FuzzGetString(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)39 void FuzzGetString(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
40 {
41 int columnIndex = provider.ConsumeIntegral<int>();
42 std::string value;
43 resultSet.GetString(columnIndex, value);
44 }
45
FuzzGet(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)46 void FuzzGet(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
47 {
48 int32_t columnIndex = provider.ConsumeIntegral<int32_t>();
49 ValueObject value;
50 resultSet.Get(columnIndex, value);
51 }
52
FuzzGetSize(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)53 void FuzzGetSize(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
54 {
55 int columnIndex = provider.ConsumeIntegral<int>();
56 size_t size;
57 resultSet.GetSize(columnIndex, size);
58 }
59
FuzzGetColumnType(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)60 void FuzzGetColumnType(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
61 {
62 int columnIndex = provider.ConsumeIntegral<int>();
63 ColumnType columnType;
64 resultSet.GetColumnType(columnIndex, columnType);
65 }
66
FuzzGoToRow(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)67 void FuzzGoToRow(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
68 {
69 int position = provider.ConsumeIntegral<int>();
70 resultSet.GoToRow(position);
71 }
72
FuzzGetRowCount(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)73 void FuzzGetRowCount(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
74 {
75 int count;
76 resultSet.GetRowCount(count);
77 }
78
FuzzGetBlock(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)79 void FuzzGetBlock(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
80 {
81 resultSet.GetBlock();
82 }
83
FuzzOnGo(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)84 void FuzzOnGo(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
85 {
86 int oldRowIndex = provider.ConsumeIntegral<int>();
87 int newRowIndex = provider.ConsumeIntegral<int>();
88 resultSet.OnGo(oldRowIndex, newRowIndex);
89 }
90
FuzzSetBlock(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)91 void FuzzSetBlock(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
92 {
93 AppDataFwk::SharedBlock *block = nullptr;
94 std::string sharedBlockName = "SharedBlockFuzzTestBlock";
95 auto errcode = AppDataFwk::SharedBlock::Create(sharedBlockName, DEFAULT_BLOCK_SIZE, block);
96 if (errcode != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
97 return;
98 }
99 resultSet.SetBlock(block);
100 }
101
FuzzClose(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)102 void FuzzClose(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
103 {
104 resultSet.Close();
105 }
106
FuzzHasBlock(FuzzedDataProvider & provider,AbsSharedResultSet & resultSet)107 void FuzzHasBlock(FuzzedDataProvider &provider, AbsSharedResultSet &resultSet)
108 {
109 resultSet.HasBlock();
110 }
111 } // namespace OHOS
112
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)113 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
114 {
115 FuzzedDataProvider provider(data, size);
116
117 // Create an instance of AbsSharedResultSet
118 const int randomStringLength = 30;
119 std::string tableName = provider.ConsumeRandomLengthString(randomStringLength);
120 AbsSharedResultSet resultSet(tableName);
121
122 // Fuzzing for GetString
123 OHOS::FuzzGetString(provider, resultSet);
124
125 // Fuzzing for Get
126 OHOS::FuzzGet(provider, resultSet);
127
128 // Fuzzing for GetSize
129 OHOS::FuzzGetSize(provider, resultSet);
130
131 // Fuzzing for GetColumnType
132 OHOS::FuzzGetColumnType(provider, resultSet);
133
134 // Fuzzing for GoToRow
135 OHOS::FuzzGoToRow(provider, resultSet);
136
137 // Fuzzing for GetRowCount
138 OHOS::FuzzGetRowCount(provider, resultSet);
139
140 // Fuzzing for GetBlock
141 OHOS::FuzzGetBlock(provider, resultSet);
142
143 // Fuzzing for OnGo
144 OHOS::FuzzOnGo(provider, resultSet);
145
146 // Fuzzing for SetBlock
147 OHOS::FuzzSetBlock(provider, resultSet);
148
149 // Fuzzing for Close
150 OHOS::FuzzClose(provider, resultSet);
151
152 // Fuzzing for HasBlock
153 OHOS::FuzzHasBlock(provider, resultSet);
154
155 return 0;
156 }
157