• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "autolaunch_fuzzer.h"
17 #include "distributeddb_data_generate_unit_test.h"
18 #include "distributeddb_tools_test.h"
19 
20 using namespace DistributedDB;
21 using namespace DistributedDBTest;
22 using namespace DistributedDBUnitTest;
23 namespace OHOS {
24 static auto g_kvManager = KvStoreDelegateManager(APP_ID, USER_ID);
25 
EnableAutoLaunchFuzz(const uint8_t * data,size_t size)26 void EnableAutoLaunchFuzz(const uint8_t *data, size_t size)
27 {
28     KvStoreConfig config;
29     DistributedDBToolsTest::TestDirInit(config.dataDir);
30     g_kvManager.SetKvStoreConfig(config);
31     std::string rawString(reinterpret_cast<const char *>(data), size);
32     CipherPassword passwd;
33     passwd.SetValue(data, size);
34     AutoLaunchOption launchOption;
35     KvStoreDelegateManager::EnableKvStoreAutoLaunch(USER_ID, APP_ID, rawString, launchOption, nullptr);
36     AutoLaunchOption launchOption1 {true, false, CipherType::DEFAULT, passwd, "", false, config.dataDir, nullptr};
37     KvStoreDelegateManager::EnableKvStoreAutoLaunch(USER_ID, APP_ID, "StoreId1", launchOption1, nullptr);
38     AutoLaunchOption launchOption2 {true, true, CipherType::DEFAULT, passwd, "", false, config.dataDir, nullptr};
39     KvStoreDelegateManager::EnableKvStoreAutoLaunch(USER_ID, APP_ID, "StoreId2", launchOption2, nullptr);
40     AutoLaunchOption launchOption3 {false, true, CipherType::DEFAULT, passwd, "", false, config.dataDir, nullptr};
41     KvStoreDelegateManager::EnableKvStoreAutoLaunch(USER_ID, APP_ID, "StoreId3", launchOption3, nullptr);
42     AutoLaunchOption launchOption4 {false, false, CipherType::DEFAULT, passwd, "", false, config.dataDir, nullptr};
43     KvStoreDelegateManager::EnableKvStoreAutoLaunch(USER_ID, APP_ID, "StoreId4", launchOption4, nullptr);
44 }
45 
DisableAutoLaunchFUzz(const uint8_t * data,size_t size)46 void DisableAutoLaunchFUzz(const uint8_t *data, size_t size)
47 {
48     KvStoreConfig config;
49     DistributedDBToolsTest::TestDirInit(config.dataDir);
50     g_kvManager.SetKvStoreConfig(config);
51     std::string rawString(reinterpret_cast<const char *>(data), size);
52     CipherPassword passwd;
53     passwd.SetValue(data, size);
54     KvStoreDelegateManager::DisableKvStoreAutoLaunch(USER_ID, APP_ID, rawString);
55     passwd.SetValue(data, size);
56     KvStoreNbDelegate::Option option {true, true, false, CipherType::DEFAULT, passwd};
57     KvStoreNbDelegate *kvNbDelegatePtr = nullptr;
58     g_kvManager.GetKvStore(rawString, option,
59         [&kvNbDelegatePtr](DBStatus status, KvStoreNbDelegate* kvNbDelegate) {
60             if (status == DBStatus::OK) {
61                 kvNbDelegatePtr = kvNbDelegate;
62             }
63     });
64     KvStoreDelegateManager::DisableKvStoreAutoLaunch(USER_ID, APP_ID, rawString);
65 }
66 }
67 
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71     /* Run your code on data */
72     OHOS::EnableAutoLaunchFuzz(data, size);
73     OHOS::DisableAutoLaunchFUzz(data, size);
74     return 0;
75 }
76 
77