Signed-off-by: wangbin <wangbin341@huawei.com>

Changes to be committed:
This commit is contained in:
wangbin 2022-04-25 11:27:42 +08:00
parent e23137b0a8
commit df04810241
7 changed files with 320 additions and 1 deletions

View File

@ -116,7 +116,8 @@
}
],
"test": [
"//foundation/distributeddatamgr/distributeddatamgr:build_native_test"
"//foundation/distributeddatamgr/distributeddatamgr:build_native_test",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/test:fuzztest"
]
}
}

View File

@ -739,3 +739,13 @@ group("unittest") {
]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
"fuzztest/query_fuzzer:fuzztest",
]
}
###############################################################################

View File

@ -0,0 +1,103 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#####################hydra-fuzz###################
import("//build/config/features.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("QueryFuzzTest") {
module_out_path = "distributeddatamgr/distributeddb"
include_dirs = [
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/test/unittest/common/common",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/test/unittest/common/syncer",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/test/unittest/common/storage",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/interfaces/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/interfaces/src",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/storage/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/storage/src",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/storage/src/sqlite",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/storage/src/multiver",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/communicator/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/communicator/src",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/syncer/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/syncer/src",
"//utils/native/base/include",
"//third_party/sqlite/include",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/include/relational",
"//third_party/jsoncpp/include/json",
"//third_party/skia/third_party/externals/spirv-headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/skia/third_party/externals/swiftshader/third_party/SPIRV-Headers/tools/buildHeaders/jsoncpp/dist/json",
"//third_party/jsoncpp/include/json",
"//third_party/grpc/src/core/lib/json",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src/db_common.cpp",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src/json_object.cpp",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src/log_print.cpp",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src/platform_specific.cpp",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src/schema_object.cpp",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/common/src/schema_utils.cpp",
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb/test/unittest/common/common/distributeddb_data_generate_unit_test.cpp",
"query_fuzzer.cpp",
]
defines = [
"SQLITE_ENABLE_SNAPSHOT",
"_LARGEFILE64_SOURCE",
"_FILE_OFFSET_BITS=64",
"SQLITE_HAS_CODEC",
"SQLITE_ENABLE_JSON1",
"USING_HILOG_LOGGER",
"USE_SQLITE_SYMBOLS",
"USING_DB_JSON_EXTRACT_AUTOMATICALLY",
"LOW_LEVEL_MEM_DEV",
"JSONCPP_USE_BUILDER",
"OMIT_FLATBUFFER",
"RELATIONAL_STORE",
"SQLITE_DISTRIBUTE_RELATIONAL",
]
deps = [
"//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb:distributeddb",
"//third_party/jsoncpp:jsoncpp",
"//utils/native/base:utils",
"//third_party/openssl:libcrypto_shared",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":QueryFuzzTest",
]
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2021 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>30</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,139 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "query_fuzzer.h"
#include "get_query_info.h"
using namespace DistributedDB;
using namespace std;
namespace {
const std::string TEST_FIELD_NAME = "$.test";
}
namespace OHOS {
void FuzzEqualTo(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().EqualTo(rawString, static_cast<int>(size));
}
void FuzzNotEqualTo(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().NotEqualTo(TEST_FIELD_NAME,rawString);
}
void FuzzGreaterThan(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().GreaterThan(rawString, static_cast<int>(U32_AT(data)));
}
void FuzzLessThan(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().LessThan(TEST_FIELD_NAME,rawString);
}
void FuzzGreaterThanOrEqualTo(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().GreaterThanOrEqualTo(rawString, static_cast<int>(size));
}
void FuzzLessThanOrEqualTo(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().LessThanOrEqualTo(TEST_FIELD_NAME,rawString);
}
void FuzzOrderBy(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().GreaterThanOrEqualTo(rawString, true);
query = Query::Select().GreaterThanOrEqualTo(rawString, false);
}
void FuzzLimit(const uint8_t* data, size_t size)
{
Query query = Query::Select().Limit(static_cast<int>(size), static_cast<int>(U32_AT(data)));
}
void FuzzLike(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().Like(rawString, rawString);
}
void FuzzNotLike(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().NotLike(TEST_FIELD_NAME,rawString);
}
void FuzzIn(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
std::vector<std::string> values;
//512 max size
for (int i = 0; i < static_cast<int>(U32_AT(data) % 512); i++) {
values.push_back(rawString);
}
Query query = Query::Select().In(TEST_FIELD_NAME, values);
}
void FuzzNotIn(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
std::vector<std::string> values;
//512 max size
for (int i = 0; i < static_cast<int>(size % 512); i++) {
values.push_back(rawString);
}
Query query = Query::Select().NotIn(TEST_FIELD_NAME, values);
}
void FuzzIsNull(const uint8_t* data, size_t size)
{
std::string rawString(reinterpret_cast<const char *>(data), size);
Query query = Query::Select().IsNull(rawString);
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
//u32 4 bytes
if (size < 4) {
return 0;
}
//Run your code on data
OHOS::FuzzEqualTo(data,size);
OHOS::FuzzNotEqualTo(data,size);
OHOS::FuzzGreaterThan(data,size);
OHOS::FuzzLessThan(data,size);
OHOS::FuzzGreaterThanOrEqualTo(data,size);
OHOS::FuzzLessThanOrEqualTo(data,size);
OHOS::FuzzOrderBy(data,size);
OHOS::FuzzLimit(data,size);
OHOS::FuzzLike(data,size);
OHOS::FuzzNotLike(data,size);
OHOS::FuzzIn(data,size);
OHOS::FuzzNotIn(data,size);
OHOS::FuzzIsNull(data,size);
return 0;
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef QUERY_FUZZER_H
#define QUERY_FUZZER_H
#define FUZZ_PROJECT_NAME "Query_fuzzer"
#include <cstdint>
#include <unistd.h>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
uint16_t U16_AT(const uint8_t * const &ptr)
{
// 8 - 0
return (ptr[0] << 8) | ptr[1];
}
uint32_t U32_AT(const uint8_t * const &ptr)
{
//24 - 16 - 8 - 0
return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
}
#endif //QUERY_FUZZER_H