• 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 "transaction_fuzzer.h"
16 
17 #include <fuzzer/FuzzedDataProvider.h>
18 #include <rdb_helper.h>
19 #include <rdb_store.h>
20 #include <rdb_store_config.h>
21 #include <securec.h>
22 #include <values_bucket.h>
23 
24 #include <memory>
25 
26 #include "connection_pool.h"
27 #include "rdb_errno.h"
28 #include "rdb_helper.h"
29 #include "trans_db.h"
30 
31 using namespace OHOS;
32 using namespace OHOS::NativeRdb;
33 namespace OHOS {
34 
35 static const std::string RDB_PATH = "/data/test/transactionFuzzTest.db";
36 static const std::string CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test "
37                                              "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
38                                              "name TEXT NOT NULL, age INTEGER, salary REAL, "
39                                              "blobType BLOB)";
40 
41 class TransactionTestOpenCallback : public RdbOpenCallback {
42 public:
43     int OnCreate(RdbStore &store) override;
44     int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override;
45 };
46 
OnCreate(RdbStore & store)47 int TransactionTestOpenCallback::OnCreate(RdbStore &store)
48 {
49     return store.ExecuteSql(CREATE_TABLE_TEST);
50 }
51 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)52 int TransactionTestOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
53 {
54     return E_OK;
55 }
56 
TransactionCommitFailFuzzTest(FuzzedDataProvider & provider)57 void TransactionCommitFailFuzzTest(FuzzedDataProvider &provider)
58 {
59     RdbHelper::DeleteRdbStore(RDB_PATH);
60     RdbStoreConfig config(RDB_PATH);
61     config.SetHaMode(HAMode::MAIN_REPLICA);
62     config.SetReadOnly(false);
63     TransactionTestOpenCallback helper;
64     int errCode = E_OK;
65     std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
66     if (store == nullptr || errCode != E_OK) {
67         return;
68     }
69 
70     store->Execute("DROP TABLE IF EXISTS test1");
71     auto res = store->Execute("CREATE TABLE test1 (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)");
72     if (res.first != E_OK) {
73         return;
74     }
75 
76     auto [ret, transaction] = store->CreateTransaction(Transaction::DEFERRED);
77     if (transaction == nullptr || ret != E_OK) {
78         return;
79     }
80 
81     Transaction::Row row;
82     row.Put("id", provider.ConsumeIntegral<int>());
83     row.Put("name", provider.ConsumeRandomLengthString());
84     auto result = transaction->Insert("test1", row);
85     const int count = 1;
86     if (result.first != E_OK || result.second != count) {
87         return;
88     }
89 
90     // Constructing a Commit Failure Scenario
91     std::string walFile = RDB_PATH + "-wal";
92 
93     // Disabling wal File Operations
94     std::string chattrAddiCmd = "chattr +i " + walFile;
95     system(chattrAddiCmd.c_str());
96 
97     ret = transaction->Commit();
98     if (ret == E_OK) {
99         return;
100     }
101 
102     // Enable the wal file operation.
103     std::string chattrSubiCmd = "chattr -i " + walFile;
104     system(chattrSubiCmd.c_str());
105 
106     RdbHelper::DeleteRdbStore(RDB_PATH);
107 }
108 } // namespace OHOS
109 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
111 {
112     FuzzedDataProvider provider(data, size);
113     OHOS::TransactionCommitFailFuzzTest(provider);
114     return 0;
115 }
116