1 /*
2 * Copyright (c) 2022 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 "kvstoredisksize_fuzzer.h"
17 #include "distributeddb_tools_test.h"
18
19 using namespace DistributedDB;
20 using namespace DistributedDBTest;
21 namespace OHOS {
22 static auto g_kvManager = KvStoreDelegateManager("APP_ID", "USER_ID");
NbDbTest(const uint8_t * data,size_t size)23 void NbDbTest(const uint8_t *data, size_t size)
24 {
25 std::string rawString(reinterpret_cast<const char *>(data), size);
26 CipherPassword passwd;
27 passwd.SetValue(data, size);
28 KvStoreNbDelegate::Option nbOption {true, true, false, CipherType::DEFAULT, passwd};
29 KvStoreNbDelegate *kvNbDelegatePtr = nullptr;
30 g_kvManager.GetKvStore(rawString, nbOption,
31 [&kvNbDelegatePtr](DBStatus status, KvStoreNbDelegate* kvNbDelegate) {
32 if (status == DBStatus::OK) {
33 kvNbDelegatePtr = kvNbDelegate;
34 }
35 });
36
37 if (kvNbDelegatePtr != nullptr) {
38 Key key;
39 Value value;
40 DistributedDBToolsTest::GetRandomKeyValue(key, DBConstant::MAX_KEY_SIZE);
41 DistributedDBToolsTest::GetRandomKeyValue(value, DBConstant::MAX_VALUE_SIZE);
42 kvNbDelegatePtr->Put(key, value);
43 g_kvManager.CloseKvStore(kvNbDelegatePtr);
44 }
45 uint64_t dbSize = 0;
46 g_kvManager.GetKvStoreDiskSize(rawString, dbSize);
47 g_kvManager.DeleteKvStore(rawString);
48 }
49 }
50
51 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
53 {
54 /* Run your code on data */
55 std::string rawString(reinterpret_cast<const char *>(data), size);
56 KvStoreDelegateManager::SetProcessLabel(rawString, rawString);
57 KvStoreConfig config;
58 DistributedDBToolsTest::TestDirInit(config.dataDir);
59 OHOS::g_kvManager.SetKvStoreConfig(config);
60 OHOS::NbDbTest(data, size);
61 DistributedDBToolsTest::RemoveTestDbFiles(config.dataDir);
62 return 0;
63 }
64
65