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 "kvdelegatemanager_fuzzer.h"
17 #include <codecvt>
18 #include <cstdio>
19 #include <list>
20 #include <securec.h>
21 #include "distributeddb_data_generate_unit_test.h"
22 #include "distributeddb_tools_test.h"
23 #include "kv_store_delegate_manager.h"
24 #include "log_print.h"
25 #include "runtime_config.h"
26
27 class KvDelegateManagerFuzzer {
28 /* Keep C++ file names the same as the class name. */
29 };
30
31 namespace OHOS {
32 using namespace DistributedDB;
33 using namespace DistributedDBTest;
34
35 KvStoreDelegateManager g_mgr("APP_ID", "USER_ID");
36 const std::string DUMP_DISTRIBUTED_DB = "--database";
37
GetRandomString(const uint8_t * data,size_t size,size_t len,uint32_t & start)38 std::string GetRandomString(const uint8_t *data, size_t size, size_t len, uint32_t &start)
39 {
40 std::string res;
41 if (size == 0) {
42 return "";
43 }
44 if (start >= size || start + len >= size) {
45 return std::string(data, data + size - 1);
46 }
47 res = std::string(data + start, data + start + len - 1);
48 start += len;
49 return res;
50 }
51
GetRandomAutoLaunchOption(const uint8_t * data,size_t size,AutoLaunchOption & option)52 void GetRandomAutoLaunchOption(const uint8_t* data, size_t size, AutoLaunchOption &option)
53 {
54 std::string randomStr = size == 0 ? "" : std::string(data, data + size - 1);
55 option.schema = randomStr;
56 option.observer = nullptr;
57 option.notifier = nullptr;
58 option.storeObserver = nullptr;
59 }
60
RuntimeConfigFuzz()61 void RuntimeConfigFuzz()
62 {
63 int handle = -1;
64 std::vector<std::u16string> params;
65 const std::u16string u16DumpParam = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.
66 from_bytes(DUMP_DISTRIBUTED_DB);
67 params.push_back(u16DumpParam);
68 RuntimeConfig::Dump(handle, params);
69 }
70
CallbackFuzz(const uint8_t * data,std::string storeId)71 void CallbackFuzz(const uint8_t *data, std::string storeId)
72 {
73 uint64_t diskSize = static_cast<uint64_t>(*data);
74 g_mgr.GetKvStoreDiskSize(storeId, diskSize);
75 bool isPermissionCheck = static_cast<bool>(*data);
76 auto permissionCheckCallback = [isPermissionCheck](const std::string &userId, const std::string &appId,
77 const std::string &storeId, uint8_t flag) -> bool { return isPermissionCheck; };
78 (void) KvStoreDelegateManager::SetPermissionCheckCallback(permissionCheckCallback);
79 auto permissionCheckCallbackV2 = [isPermissionCheck](const std::string &userId, const std::string &appId,
80 const std::string &storeId, const std::string &deviceId, uint8_t flag) -> bool {
81 return isPermissionCheck;
82 };
83 (void) KvStoreDelegateManager::SetPermissionCheckCallback(permissionCheckCallbackV2);
84 bool isSyncActivationCheck = static_cast<bool>(*data);
85 auto syncActivationCheck = [isSyncActivationCheck](const std::string &userId, const std::string &appId,
86 const std::string &storeId) -> bool { return isSyncActivationCheck; };
87 KvStoreDelegateManager::SetSyncActivationCheckCallback(syncActivationCheck);
88 KvStoreDelegateManager::NotifyUserChanged();
89 }
90
CombineTest(const uint8_t * data,size_t size)91 void CombineTest(const uint8_t *data, size_t size)
92 {
93 LOGD("Begin KvDelegateManagerFuzzer");
94 std::string path;
95 DistributedDBToolsTest::TestDirInit(path);
96 const int paramCount = 3;
97 for (size_t len = 1; len < (size / paramCount); len++) {
98 uint32_t start = 0;
99 std::string appId = GetRandomString(data, size, len, start);
100 std::string userId = GetRandomString(data, size, len, start);
101 std::string storeId = GetRandomString(data, size, len, start);
102 std::string dir;
103 (void) KvStoreDelegateManager::GetDatabaseDir(storeId, appId, userId, dir);
104 (void) KvStoreDelegateManager::GetDatabaseDir(storeId, dir);
105 (void) KvStoreDelegateManager::SetProcessLabel(appId, userId);
106 bool syncDualTupleMode = static_cast<bool>(*data);
107 (void) KvStoreDelegateManager::GetKvStoreIdentifier(userId, appId, storeId, syncDualTupleMode);
108 AutoLaunchOption option;
109 GetRandomAutoLaunchOption(data, size, option);
110 option.dataDir = path;
111 (void) KvStoreDelegateManager::EnableKvStoreAutoLaunch(userId, appId, storeId, option, nullptr);
112 (void) KvStoreDelegateManager::DisableKvStoreAutoLaunch(userId, appId, storeId);
113 CallbackFuzz(data, storeId);
114 std::string targetDev = GetRandomString(data, size, len, start);
115 bool isCheckOk = static_cast<bool>(*data);
116 auto databaseStatusNotifyCallback = [userId, appId, storeId, targetDev, &isCheckOk] (
117 const std::string ¬ifyUserId, const std::string ¬ifyAppId, const std::string ¬ifyStoreId,
118 const std::string &deviceId, bool onlineStatus) -> void {
119 if (notifyUserId == userId && notifyAppId == appId && notifyStoreId == storeId &&
120 deviceId == targetDev && onlineStatus == true) {
121 isCheckOk = true;
122 }
123 };
124 g_mgr.SetStoreStatusNotifier(databaseStatusNotifyCallback);
125 RuntimeConfigFuzz();
126 }
127 KvStoreDelegateManager::SetKvStoreCorruptionHandler(nullptr);
128 DistributedDBToolsTest::RemoveTestDbFiles(path);
129 LOGD("End KvDelegateManagerFuzzer");
130 }
131 }
132
133 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)134 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
135 {
136 OHOS::CombineTest(data, size);
137 return 0;
138 }
139