diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h index a3a2b61d889f66a939804d262e82ccae25b2395a..952676334409a0b78534737a37f094ce2ed9c4ed 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/include/json_common.h @@ -34,7 +34,6 @@ public: std::vector> &resultPath, bool isFirstLevel); static std::vector> ParsePath(const JsonObject &node, int &errCode); static std::vector GetLeafValue(const JsonObject &node); - static bool isValueEqual(const ValueObject &srcValue, const ValueObject &targetValue); static int Append(const JsonObject &src, const JsonObject &add, bool isReplace); static bool IsJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &errCode); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp index 171c1fcbbcde6c15e7cc720c9818215c632ce099..d02d6af5d2fd2d79bf3f4e1d642cfaaa72f43041 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/json_common.cpp @@ -349,7 +349,7 @@ bool AddSpliteHitField(const JsonObject &src, const JsonObject &item, JsonFieldP externErrCode = -E_DATA_CONFLICT; return false; } - errCode = (i == 0) ? hitItem.AddItemToObject(abandonPath[i], item) : hitItem.AddItemToObject(abandonPath[i]); + errCode = hitItem.AddItemToObject(abandonPath[i], item); externErrCode = (externErrCode == E_OK ? errCode : externErrCode); } return false; @@ -391,7 +391,7 @@ bool AddSpliteField(const JsonObject &src, const JsonObject &item, const JsonFie externErrCode = -E_DATA_CONFLICT; return false; } - errCode = (i == 0 ? hitItem.AddItemToObject(abandonPath[i], item) : hitItem.AddItemToObject(abandonPath[i])); + errCode = hitItem.AddItemToObject(abandonPath[i], item); externErrCode = (externErrCode == E_OK ? errCode : externErrCode); newHitPath.emplace_back(abandonPath[i]); hitItem = hitItem.FindItem(newHitPath, errCode); @@ -565,23 +565,6 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add, bool isRepl return externErrCode; } -bool JsonCommon::isValueEqual(const ValueObject &srcValue, const ValueObject &targetValue) -{ - if (srcValue.GetValueType() == targetValue.GetValueType()) { - switch (srcValue.GetValueType()) { - case ValueObject::ValueType::VALUE_NULL: - return true; - case ValueObject::ValueType::VALUE_BOOL: - return srcValue.GetBoolValue() == targetValue.GetBoolValue(); - case ValueObject::ValueType::VALUE_NUMBER: - return srcValue.GetDoubleValue() == targetValue.GetDoubleValue(); - case ValueObject::ValueType::VALUE_STRING: - return srcValue.GetStringValue() == targetValue.GetStringValue(); - } - } - return false; -} - bool JsonCommon::IsArrayMatch(const JsonObject &src, const JsonObject &target, int &isAlreadyMatched) { JsonObject srcChild = src.GetChild(); @@ -603,7 +586,8 @@ bool JsonCommon::IsArrayMatch(const JsonObject &src, const JsonObject &target, i bool JsonCommon::IsObjectItemMatch(const JsonObject &srcItem, const JsonObject &item, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) { - if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY && item.GetType() == JsonObject::Type::JSON_ARRAY && + if (((srcItem.GetType() == JsonObject::Type::JSON_ARRAY && item.GetType() == JsonObject::Type::JSON_ARRAY) || + (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF)) && !isAlreadyMatched) { bool isEqual = (srcItem == item); if (!isEqual) { // Filter value is No equal with src @@ -611,15 +595,6 @@ bool JsonCommon::IsObjectItemMatch(const JsonObject &srcItem, const JsonObject & } isAlreadyMatched = isMatchFlag; return false; // Both leaf node, no need iterate - } - if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF && - !isAlreadyMatched) { - bool isEqual = isValueEqual(srcItem.GetItemValue(), item.GetItemValue()); - if (!isEqual) { // Filter value is No equal with src - isMatchFlag = isEqual; - } - isAlreadyMatched = isMatchFlag; - return false; // Both leaf node, no need iterate } else if (srcItem.GetType() != item.GetType()) { if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { // srcItem Type is ARRAY, item Type is not ARRAY bool isEqual = IsArrayMatch(srcItem, item, isAlreadyMatched); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/log_print.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/log_print.cpp index 398dbc33173e530a338a2170c896e14cf809eb1d..0231663e8b8d07a849f5ae7581b208c98b7a8064 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/log_print.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/common/src/log_print.cpp @@ -31,18 +31,12 @@ void PrintLog(LogPrint::Level level, const char *tag, const std::string &msg) case LogPrint::Level::LEVEL_DEBUG: (void)OHOS::HiviewDFX::HiLog::Debug(label, format.c_str(), msg.c_str()); break; - case LogPrint::Level::LEVEL_INFO: - (void)OHOS::HiviewDFX::HiLog::Info(label, format.c_str(), msg.c_str()); - break; case LogPrint::Level::LEVEL_WARN: (void)OHOS::HiviewDFX::HiLog::Warn(label, format.c_str(), msg.c_str()); break; case LogPrint::Level::LEVEL_ERROR: (void)OHOS::HiviewDFX::HiLog::Error(label, format.c_str(), msg.c_str()); break; - case LogPrint::Level::LEVEL_FATAL: - (void)OHOS::HiviewDFX::HiLog::Fatal(label, format.c_str(), msg.c_str()); - break; default: break; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp index 6f852abe0ce0f4e947bd889b7d05c8dee76466fc..d06b0943d59c0519231b5f7c03481cf405b1a479 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/interface/src/collection.cpp @@ -22,8 +22,6 @@ #include "log_print.h" namespace DocumentDB { -constexpr int JSON_LENS_MAX = 1024 * 1024; - Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor) { std::string lowerCaseName = name; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h index 664bd439aa231c9ac99c118a581a4ebf6398805c..3be7b9587f18b9e87ef728f0686885e7068322f0 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/include/json_object.h @@ -37,12 +37,10 @@ public: explicit ValueObject(bool val); explicit ValueObject(double val); explicit ValueObject(const char *val); - explicit ValueObject(const std::string &val); ValueType GetValueType() const; bool GetBoolValue() const; int64_t GetIntValue() const; - double GetDoubleValue() const; std::string GetStringValue() const; private: @@ -74,7 +72,6 @@ public: int DeleteItemFromObject(const std::string &field); int AddItemToObject(const std::string &fieldName, const JsonObject &item); - int AddItemToObject(const std::string &fieldName); ValueObject GetItemValue() const; void ReplaceItemInArray(const int &index, const JsonObject &newItem, int &errCode); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp index a61824e6bb9bfa344170b036612c2a0e0c6a3ccf..8ec195439fa6ff56102e3e280f6dbfa9d3eb19e6 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/src/oh_adapter/src/json_object.cpp @@ -43,7 +43,7 @@ ValueObject::ValueObject(double val) doubleValue = val; } -ValueObject::ValueObject(const std::string &val) +ValueObject::ValueObject(const char *val) { valueType = ValueType::VALUE_STRING; stringValue = val; @@ -64,11 +64,6 @@ int64_t ValueObject::GetIntValue() const return static_cast(std::llround(doubleValue)); } -double ValueObject::GetDoubleValue() const -{ - return doubleValue; -} - std::string ValueObject::GetStringValue() const { return stringValue; @@ -335,29 +330,6 @@ int JsonObject::AddItemToObject(const std::string &fieldName, const JsonObject & return E_OK; } -int JsonObject::AddItemToObject(const std::string &fieldName) -{ - if (cjson_->type == cJSON_Array) { - int n = 0; - cJSON *child = cjson_->child; - while (child != nullptr) { - child = child->next; - n++; - } - if (IsNumber(fieldName) && n <= std::stoi(fieldName)) { - GLOGE("Add item object to array over size."); - return -E_NO_DATA; - } - } - if (cjson_->type != cJSON_Object) { - GLOGE("type conflict."); - return -E_DATA_CONFLICT; - } - cJSON *emptyitem = cJSON_CreateObject(); - cJSON_AddItemToObject(cjson_, fieldName.c_str(), emptyitem); - return E_OK; -} - ValueObject JsonObject::GetItemValue() const { if (cjson_ == nullptr) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/CMakeLists.txt b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9b60d9f91b61b190a8691340c859c1e9da5d382 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.2) + +project(gaussdb_rd VERSION 1.0.0) + +find_program(CLANG_COMPILER clang) +find_program(CLANGXX_COMPILER clang++) + +set(CMAKE_C_COMPILER ${CLANG_COMPILER}) +set(CMAKE_CXX_COMPILER ${CLANGXX_COMPILER}) +set(CMAKE_BUILD_TYPE "Debug") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=fuzzer,address") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -std=c++17 -pthread") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer,address -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation") + +add_compile_options(-Werror=sign-compare) +add_compile_options(-Werror=unused-variable) +add_compile_options(-Wno-conversion-null) +add_compile_options(-Wuninitialized) +add_compile_options(-Wformat) +add_compile_options(-Wno-deprecated-declarations) + +add_definitions(-DRUNNING_ON_SIMULATED_ENV) + +find_package(Threads) +enable_testing() + +file(GLOB_RECURSE DOCUMENT_SRC "${PROJECT_SOURCE_DIR}/../../src/*.cpp") +file(GLOB_RECURSE FUZZ_TEST "${PROJECT_SOURCE_DIR}/json_fuzzer/*.cpp") + +include_directories( + ${PROJECT_SOURCE_DIR}/../../include + ${PROJECT_SOURCE_DIR}/../../src/common/src + ${PROJECT_SOURCE_DIR}/../../src/common/include + ${PROJECT_SOURCE_DIR}/../../src/executor/include + ${PROJECT_SOURCE_DIR}/../../src/executor/base + ${PROJECT_SOURCE_DIR}/../../src/executor/document + ${PROJECT_SOURCE_DIR}/../../src/oh_adapter/include + ${PROJECT_SOURCE_DIR}/../../src/oh_adapter/src + ${PROJECT_SOURCE_DIR}/../../src/interface/include + ${PROJECT_SOURCE_DIR}/../../src/interface/src +) + +include_directories( + ${PROJECT_SOURCE_DIR}/json_fuzzer +) + +set(DISTRIBUTEDDB_PATH ${PROJECT_SOURCE_DIR}/../../third_party/distributeddatamgr_kv_store/frameworks/libs/distributeddb/) +include_directories( + ${DISTRIBUTEDDB_PATH}/include + ${DISTRIBUTEDDB_PATH}/interfaces/include +) + +include_directories( + ${PROJECT_SOURCE_DIR}/../../third_party/third_party_cJSON + ${PROJECT_SOURCE_DIR}/../../third_party/third_party_sqlite/include + ${PROJECT_SOURCE_DIR}/../../third_party/utils_native/base/include + ${PROJECT_SOURCE_DIR}/../../third_party/utils_native/base/src/securec + ${PROJECT_SOURCE_DIR}/../../third_party/kate/log/include +) + +link_directories( + ${PROJECT_SOURCE_DIR}/../../third_party/third_party_cJSON/build + ${PROJECT_SOURCE_DIR}/../../third_party/third_party_sqlite + ${PROJECT_SOURCE_DIR}/../../third_party/utils_native +) + +add_executable(fuzztest ${DOCUMENT_SRC} ${FUZZ_TEST}) + +target_link_libraries(fuzztest + cjson + sqlite3 + securec + crypto + gcov +) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/BUILD.gn b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ed25e380153f7ca2108407b2d39383744dde3061 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/BUILD.gn @@ -0,0 +1,148 @@ +# Copyright (c) 2023 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. + +import("//build/config/features.gni") +import("//build/test.gni") + +module_output_path = "datamgr_service/gaussdb_rd" + +############################################################################### +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "../../include", + "../../src/common/include", + "../../src/executor/include", + "../../src/executor/document", + "../../src/oh_adapter/include", + "../../src/oh_adapter/src", + "../../src/interface/include", + ] + + defines = [ + "SQLITE_ENABLE_SNAPSHOT", + "SQLITE_HAS_CODEC", + "SQLITE_ENABLE_JSON1", + "USING_HILOG_LOGGER", + "USE_SQLITE_SYMBOLS", + "SQLITE_ENABLE_DROPTABLE_CALLBACK", + ] +} + +############################################################################### +ohos_source_set("src_file") { + testonly = true + + sources = [ + "../../src/common/src/collection_option.cpp", + "../../src/common/src/db_config.cpp", + "../../src/common/src/db_constant.cpp", + "../../src/common/src/json_common.cpp", + "../../src/common/src/log_print.cpp", + "../../src/common/src/os_api.cpp", + "../../src/executor/base/grd_db_api.cpp", + "../../src/executor/document/check_common.cpp", + "../../src/executor/document/grd_document_api.cpp", + "../../src/executor/document/grd_resultset_api.cpp", + "../../src/interface/src/collection.cpp", + "../../src/interface/src/doc_errno.cpp", + "../../src/interface/src/document_store.cpp", + "../../src/interface/src/document_store_manager.cpp", + "../../src/interface/src/projection_tree.cpp", + "../../src/interface/src/result_set.cpp", + "../../src/interface/src/result_set_common.cpp", + "../../src/oh_adapter/src/json_object.cpp", + "../../src/oh_adapter/src/kv_store_manager.cpp", + "../../src/oh_adapter/src/sqlite_store_executor_impl.cpp", + "../../src/oh_adapter/src/sqlite_utils.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gtest_main", + "//third_party/sqlite:sqlite", + ] + + configs += [ "/third_party/cJSON:cJSON_config" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + deps += [ "/third_party/cJSON:cjson" ] + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "distributeddatamgr" + part_name = "datamgr_service" +} + +ohos_fuzztest("JsonFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//foundation/datamgr_service/gaussdb_rd/test/fuzz_test/json_fuzzer" + + if (!defined(deps)) { + deps = [] + } + if (!defined(external_deps)) { + external_deps = [] + } + configs = [ ":module_private_config" ] + deps += [ + ":src_file", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/sqlite:sqlite", + ] + configs += [ "//third_party/cJSON:cJSON_config" ] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + deps += [ + "//third_party/cJSON:cjson", + "//third_party/openssl:libcrypto_shared", + ] + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", + "hiviewdfx_hilog_native:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "--coverage", + ] + + ldflags = [ "--coverage" ] + + cflags_cc = [ "--coverage" ] + + sources = [ "json_fuzzer.cpp" ] +} + +############################################################################### + +group("fuzztest") { + testonly = true + deps = [ "//third_party/googletest:gmock" ] + + deps += [ + # deps file + ":JsonFuzzTest", + ] +} +############################################################################### diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/json_fuzzer.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/json_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f7681d4a605a04c2a450969aaf394f79bdce6b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/json_fuzzer.cpp @@ -0,0 +1,1722 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "grd_base/grd_db_api.h" +#include "grd_base/grd_resultset_api.h" +#include "grd_document/grd_document_api.h" +#include "grd_resultset_inner.h" + +const char *TEST_DB = "./data"; +const char *TEST_DB_FILE = "./data/testfile"; +const char *COLLECTION_NAME = "collectionname"; +const char *OPTION_STR = "{ \"maxdoc\" : 1000}"; +const char *CONFIG_STR = "{}"; +const char *DB_DIR_PATH = "./data/"; +const char *NO_EXIST_COLLECTION_NAME = "no_exisit"; + +static GRD_DB *g_db = nullptr; + +namespace OHOS { +namespace { +int RemoveDir(const char *dir) +{ + if (dir == nullptr) { + return -1; + } + if (access(dir, F_OK) != 0) { + return 0; + } + struct stat dir_stat; + if (stat(dir, &dir_stat) < 0) { + return -1; + } + + char dir_name[PATH_MAX]; + DIR *dirPtr = nullptr; + struct dirent *dr = nullptr; + if (S_ISREG(dir_stat.st_mode)) { // normal file + remove(dir); + } else if (S_ISDIR(dir_stat.st_mode)) { + dirPtr = opendir(dir); + while ((dr = readdir(dirPtr)) != nullptr) { + // ignore . and .. + if ((strcmp(".", dr->d_name) == 0) || (strcmp("..", dr->d_name) == 0)) { + continue; + } + sprintf(dir_name, "%s / %s", dir, dr->d_name); + (void)RemoveDir(dir_name); + } + closedir(dirPtr); + rmdir(dir); // remove empty dir + } else { + return -1; + } + return 0; +} + +void MakeDir(const char *dir) +{ + std::string tmpPath; + const char *pcur = dir; + + while (*pcur++ != '\0') { + tmpPath.push_back(*(pcur - 1)); + if ((*pcur == '/' || *pcur == '\0') && access(tmpPath.c_str(), 0) != 0 && !tmpPath.empty()) { + if (mkdir(tmpPath.c_str(), (S_IRUSR | S_IWUSR | S_IXUSR)) != 0) { + return; + } + } + } +} + +std::string getMaxString() +{ + std::string str = "{"; + for (int i = 0; i <= 1000; i++) { + for (int j = 0; j <= 600; j++) { + str += "a"; + } + } + return str + "}"; +} +} // namespace + +void InsertDocFuzz(const uint8_t *data, size_t size) +{ + std::string collectionName_data(reinterpret_cast(data), size); + const char *collectionName_val = collectionName_data.data(); + std::string optionStr_data(reinterpret_cast(data), size); + const char *optionStr_val = optionStr_data.data(); + GRD_CreateCollection(g_db, collectionName_val, optionStr_val, 0); + std::string document_data(reinterpret_cast(data), size); + const char *document_val = document_data.data(); + GRD_InsertDoc(g_db, collectionName_val, document_val, 0); + GRD_InsertDoc(g_db, collectionName_val, document_val, 0); + std::string stringJson = "{" + document_data + "}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, stringJson.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, stringJson.c_str(), 0); + std::string stringJson2 = "{\"_id\":\"1\",\"field\":\"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, stringJson2.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, stringJson2.c_str(), 0); + std::string longId = "1"; + for (int i = 0; i < 1000; i++) { + longId += "1"; + } + longId = "{\"_id\":\"" + longId + "\",\"field\":\"aab\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, longId.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, "{\"_id\":123}", 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, "{\"field1.field2.field&*^&3\":\"anc\"}", 0); + + std::string document2 = "{\"_id\":2,\"field\":\"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document2.c_str(), 0); + std::string document3 = "{\"_id\":true,\"field\":\"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document3.c_str(), 0); + std::string document4 = "{\"_id\" : null, \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document4.c_str(), 0); + std::string document5 = "{\"_id\" : [\"2\"], \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document5.c_str(), 0); + std::string document6 = "{\"_id\" : {\"val\" : \"2\"}, \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document6.c_str(), 0); + std::string document7 = "{\"_id\" : \"3\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document7.c_str(), 0); + std::string document9 = "{\"_id\" : \"4\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(NULL, COLLECTION_NAME, document9.c_str(), 0); + std::string document10 = "{\"_id\" : \"5\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, NULL, document10.c_str(), 0); + std::string document12 = "{\"_id\" : \"6\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document12.c_str(), 1); + std::string document13 = "{\"_id\" : \"7\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document13.c_str(), INT_MAX); + std::string document14 = "{\"_id\" : \"8\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document14.c_str(), INT_MIN); + GRD_InsertDoc(g_db, NULL, NULL, 0); + std::string document15 = "{\"_id\" : \"9\", \"name\" : \"" + document_data + "\"}"; + std::string collectionName2(512, 'a'); + GRD_InsertDoc(g_db, collectionName2.c_str(), document15.c_str(), 0); + const char *collectionName = "collction@!#"; + std::string document16 = "{\"_id\" : \"10\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, collectionName, document16.c_str(), GRD_OK); + std::string collectionName1(257, 'k'); + std::string document17 = "{\"_id\" : \"10\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, collectionName1.c_str(), document17.c_str(), GRD_OK); + std::string document18 = "({\"level1\" : {\"level2\" : {\"level3\" : {\"level4\": {\"level5\" : 1}},\ + \"level3_2\" : \"" + + document_data + "\"}},\"_id\":\"14\"})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document18.c_str(), 0); + std::string document19 = "({\"level1\" : {\"level2\" : {\"level3\" : [{ \"level5\" : \"" + document_data + "\",\ + \"level5_2\":\"" + + document_data + "\"}, \"" + document_data + "\",\"" + document_data + "\"], \"level3_2\":\ + \"" + + document_data + "\"}}, \"_id\":\"14\"})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document19.c_str(), 0); + std::string document20 = "({\"level1\" : {\"level2\" : {\"level3\" : { \"level4\" : \"" + document_data + "\"},\ + \"level3_2\" : \"" + + document_data + "\"}}, \"_id\":\"14\"})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document20.c_str(), 0); + std::string documentPart1 = "{ \"_id\" : \"15\", \"textVal\" : \" "; + std::string documentPart2 = "\" }"; + std::string jsonVal = std::string(512 * 1024 - documentPart1.size() - documentPart2.size(), 'k'); + std::string document = documentPart1 + jsonVal + documentPart2; + std::string jsonVal2 = std::string(512 * 1024 - 1 - documentPart1.size() - documentPart2.size(), 'k'); + std::string document21 = documentPart1 + jsonVal2 + documentPart2; + GRD_InsertDoc(g_db, COLLECTION_NAME, document21.c_str(), 0); + std::string document22 = "{\"_id\" : \"16\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document22.c_str(), 0); + std::string document23 = + "{\"_id\" : \"17\", \"level1\" : {\"level2\" : {\"level3\" : {\"level4\" : " + document_data + "\ + } } }, \"level1_2\" : \"" + + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document23.c_str(), 0); + std::string document24 = document_data; + GRD_InsertDoc(g_db, COLLECTION_NAME, document24.c_str(), 0); + std::string document25 = "{\"name\" : \"" + document_data + "\", \"age\" : " + document_data + ",\ + \"friend\" : {\"name\" : \" " + + document_data + "\"}, \"_id\" : \"19\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document25.c_str(), 0); + std::string collectionName3 = std::string(256, 'k'); + std::string document26 = "{\"_id\" : \"22\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, collectionName3.c_str(), document26.c_str(), GRD_OK); + std::string collectionName4 = "Aads_sd__23Asb_"; + std::string document27 = "{\"_id\" : \"23\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, collectionName4.c_str(), document27.c_str(), GRD_OK); + std::string collectionName5 = "GRD_collectionName"; + std::string document28 = "{\"_id\" : \"24\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document28.c_str(), 0); + collectionName = "GM_SYS__collectionName"; + std::string document29 = "{\"_id\" : \"24_2\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document29.c_str(), 0); + + collectionName = "grd_collectionName"; + std::string document30 = "{\"_id\" : \"24_3\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document30.c_str(), 0); + + collectionName = "gm_sys_collectionName"; + std::string document31 = "{\"_id\" : \"24_4\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document31.c_str(), 0); + + collectionName = "gM_sYs_collectionName"; + std::string document32 = "{\"_id\" : \"24_5\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document32.c_str(), 0); + + collectionName = "gRd_collectionName"; + std::string document33 = "{\"_id\" : \"24_6\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document33.c_str(), 0); + + collectionName = "gRd@collectionName"; + std::string document34 = "{\"_id\" : \"24_7\", \"name\" : \"" + document_data + "\"}"; + GRD_CreateCollection(g_db, collectionName5.c_str(), "", 0); + GRD_InsertDoc(g_db, collectionName5.c_str(), document34.c_str(), 0); + + std::string document35 = "{\"_id\" : \"25_0\", \"level1\" : {\"level2\" : {\"level3\" :\ + {\"level4\" : \"" + + document_data + "\"}}} , \"level1_2\" : \"" + document_data + "\" }"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document35.c_str(), 0); + + std::string document36 = "{\"_id\" : \"25_1\", \"class_name\" : \"" + document_data + + "\", \"signed_info\" : true, \ + \"student_info\" : [{\"name\":\"" + + document_data + "\", \"age\" : " + document_data + ", \"sex\" : \"男\"}, \ + { \"newName1\" : [\"" + + document_data + "\", \"" + document_data + "\", 0, \"" + document_data + "\"] }]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document36.c_str(), 0); + + std::string document37 = "{\"_id\" : \"25_2\", \"class_name\" : \"" + document_data + + "\", \"signed_info\" : true, \ + \"student_info\" : [{\"name\":\"" + + document_data + "\", \"age\" : " + document_data + ", \"sex\" : \"男\"}, \ + [\"" + + document_data + "\", \"" + document_data + "\", 0, \"" + document_data + "\", {\"0ab\" : null}]]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document37.c_str(), 0); + + std::string document38 = "{\"_id\" : \"25_3\", \"class_name\" : \"" + document_data + + "\", \"signed_info\" : true, \ + \"student_info\" : [{\"name\":\"" + + document_data + "\", \"age\" : " + document_data + ", \"sex\" : \"男\"}, \ + { \"newName1\" : [\"" + + document_data + "\", \"" + document_data + "\", 0, \"" + document_data + "\", {\"level5\" : 1}] }]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document38.c_str(), 0); + + std::string document39 = + "{\"_id\" : \"35\", \"A_aBdk_324_\" : \"" + document_data + "\", \"name\" : \"" + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document39.c_str(), 0); + + std::string document40 = "{\"_id\" : \"35_2\", \"1A_aBdk_324_\" : \"" + document_data + + "\", " + "\"name\" : \"" + + document_data + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document40.c_str(), 0); + std::string document41 = "{\"_id\" : \"36_0\", \"stringType\" : \"" + document_data + + "\", \"numType\" : " + document_data + ", \"BoolType\" : true,\ + \"nullType\" : null, \"arrayType\" : " + + document_data + ", \"objectType\" : {\"A\" : 3}}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document41.c_str(), 0); + + std::string document42 = "({\"_id\" : \"38_0\", \"field2\" : 1.797693" + longId + "13486232e308})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document42.c_str(), 0); + std::string document43 = "({\"_id\" : \"38_1\", \"t1\" : {\"field2\" : 1.797" + longId + "69313486232e308}})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document43.c_str(), 0); + std::string document44 = "({\"_id\" : \"38_2\", \"t1\" : [1, 2, 1.79769313486" + longId + "232e308]})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document44.c_str(), 0); + std::string document45 = "({\"_id\" : \"38_3\", \"t1\" : [1, 2, -1.7976931348623167" + longId + "E+308]})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document45.c_str(), 0); + std::string document46 = "({\"_id\" : \"38_4\", \"t1\" : [1, 2, -1." + longId + "79769313486231570E+308]})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document46.c_str(), 0); + std::string document47 = "({\"_id\" : \"38_5\", \"t1\" : [1, 2, 1.79769313486" + longId + "231570E+308]})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document47.c_str(), 0); + + std::string doc1 = "{\"_id\" : "; + std::string doc2 = "\""; + std::string doc4 = "\""; + std::string doc5 = ", \"name\" : \"" + document_data + "\"}"; + std::string document_midlle(900, 'k'); + std::string document48 = doc1 + doc2 + document_midlle + doc4 + doc5; + GRD_InsertDoc(g_db, COLLECTION_NAME, document48.c_str(), 0); + std::string document_midlle2(1000, 'k'); + document48 = doc1 + doc2 + document_midlle2 + doc4 + doc5; + GRD_InsertDoc(g_db, COLLECTION_NAME, document48.c_str(), 0); + + std::string document49 = "({\"_id\":\"0123\", \"num\":\"" + document_data + "\"})"; + std::string document50 = "({\"_id\":\"0123\", \"NUM\":\"" + document_data + "\"})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document49.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, document50.c_str(), 0); + + std::string document51 = "({\"_id\":\"0123\", \"num.\":\"" + document_data + "\"})"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document51.c_str(), 0); + + const char *document52 = R""({})""; + GRD_InsertDoc(g_db, COLLECTION_NAME, document52, 0); + + const char *document53 = R""({"empty" : null})""; + GRD_InsertDoc(g_db, COLLECTION_NAME, document53, 0); + GRD_DropCollection(g_db, collectionName_val, 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + + std::string documentNew1 = "{\"_id\" : "; + std::string documentNew2 = "\""; + std::string documentNew3 = "\""; + std::string documentNew4 = ", \"name\" : \"Ori\"}"; + document = documentNew1 + documentNew2 + document_data + documentNew3 + documentNew4; + GRD_InsertDoc(g_db, COLLECTION_NAME, document.c_str(), 0); +} + +namespace { +static const char *g_document1 = "{\"_id\" : \"1\", \"name\":\"doc1\",\"item\":\"journal\",\"personInfo\":\ + {\"school\":\"AB\", \"age\" : 51}}"; +static const char *g_document2 = "{\"_id\" : \"2\", \"name\":\"doc2\",\"item\": 1, \"personInfo\":\ + [1, \"my string\", {\"school\":\"AB\", \"age\" : 51}, true, {\"school\":\"CD\", \"age\" : 15}, false]}"; +static const char *g_document3 = "{\"_id\" : \"3\", \"name\":\"doc3\",\"item\":\"notebook\",\"personInfo\":\ + [{\"school\":\"C\", \"age\" : 5}]}"; +static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\"paper\",\"personInfo\":\ + {\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}"; +static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ + [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; +static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ + [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15},\ + {\"school\":\"C\", \"teacher\" : \"moon\", \"age\" : 20}]}"; + +static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ + [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; +static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ + [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; +static const char *g_document9 = "{\"_id\" : \"9\", \"name\":\"doc9\",\"item\": true}"; +static const char *g_document10 = "{\"_id\" : \"10\", \"name\":\"doc10\", \"parent\" : \"kate\"}"; +static const char *g_document11 = "{\"_id\" : \"11\", \"name\":\"doc11\", \"other\" : \"null\"}"; +static const char *g_document12 = "{\"_id\" : \"12\", \"name\":\"doc12\",\"other\" : null}"; +static const char *g_document13 = "{\"_id\" : \"13\", \"name\":\"doc13\",\"item\" : \"shoes\",\"personInfo\":\ + {\"school\":\"AB\", \"age\" : 15}}"; +static const char *g_document14 = "{\"_id\" : \"14\", \"name\":\"doc14\",\"item\" : true,\"personInfo\":\ + [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 85}]}"; +static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : " + "5}]}"; +static const char *g_document16 = "{\"_id\" : \"16\", \"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ + {\"nested4\":\"ABC\", \"field2\":\"CCC\"}}}}"; +static const char *g_document17 = "{\"_id\" : \"17\", \"name\":\"doc17\",\"personInfo\":\"oh,ok\"}"; +static const char *g_document18 = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ + {\"school\":\"DD\", \"age\":66}, \"color\":\"blue\"}"; +static const char *g_document19 = "{\"_id\" : \"19\", \"name\":\"doc19\",\"ITEM\" : true,\"PERSONINFO\":\ + {\"school\":\"AB\", \"age\":15}}"; +static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\ + [{\"SCHOOL\":\"B\", \"AGE\":15}, {\"SCHOOL\":\"C\", \"AGE\":35}]}"; +static const char *g_document23 = "{\"_id\" : \"23\", \"name\":\"doc22\",\"ITEM\" : " + "true,\"personInfo\":[{\"school\":\"b\", \"age\":15}, [{\"school\":\"doc23\"}, 10, " + "{\"school\":\"doc23\"}, true, {\"school\":\"y\"}], {\"school\":\"b\"}]}"; +static std::vector g_data = { g_document1, g_document2, g_document3, g_document4, g_document5, + g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, + g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20, g_document23 }; + +static void InsertData(GRD_DB *g_db, const char *collectionName) +{ + for (const auto &item : g_data) { + GRD_InsertDoc(g_db, collectionName, item, 0); + } +} + +void SetUpTestCase() +{ + (void)RemoveDir(TEST_DB); + MakeDir(TEST_DB); + GRD_DBOpen(TEST_DB_FILE, CONFIG_STR, GRD_DB_OPEN_CREATE, &g_db); + InsertData(g_db, COLLECTION_NAME); + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); +} + +void TearDownTestCase() +{ + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + GRD_DBClose(g_db, GRD_DB_CLOSE); + (void)RemoveDir(TEST_DB); + g_db = nullptr; +} + +void FindDocFuzz_Plus(const std::string &input) +{ + GRD_ResultSet *resultSet = nullptr; + std::string filter = "{\"_id\" : \"6\"}"; + Query query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"6\", \"name\":\"" + input + "\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"name\":\"" + input + "\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"" + input + "\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : 1}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : [\"" + input + "\", 1]}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : {\"" + input + "\" : \"1\"}}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : true}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : null}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + const char *colName1 = "grd_type"; + const char *colName2 = "GM_SYS_sysfff"; + resultSet = nullptr; + filter = "{\"_id\" : \"1\"}"; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, colName1, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + GRD_FindDoc(g_db, colName2, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"100\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"6\"}"; + resultSet = nullptr; + GRD_ResultSet *resultSet2 = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet2); + if (resultSet2 != nullptr) { + GRD_Next(resultSet2); + char *value = nullptr; + GRD_GetValue(resultSet2, &value); + } + GRD_FreeResultSet(resultSet2); + + resultSet = nullptr; + resultSet2 = nullptr; + query = { filter.c_str(), "{}" }; + const char *collectionName = "DocumentDBFindTest024"; + GRD_CreateCollection(g_db, collectionName, "", 0); + InsertData(g_db, collectionName); + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FindDoc(g_db, collectionName, query, 1, &resultSet2); + + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + + GRD_Next(resultSet2); + char *value2 = nullptr; + GRD_GetValue(resultSet2, &value2); + GRD_FreeValue(value2); + + GRD_Next(resultSet); + GRD_Next(resultSet2); + GRD_GetValue(resultSet, &value); + GRD_GetValue(resultSet2, &value); + GRD_FreeResultSet(resultSet); + GRD_FreeResultSet(resultSet2); + GRD_DropCollection(g_db, collectionName, 0); + + filter = "{\"_id\" : \"16\"}"; + resultSet = nullptr; + std::string projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4\":true}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": true, \"nested1\":{\"nested2\":{\"nested3\":{\"nested4\":true}}}}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": 0, \"nested1.nested2.nested3.nested4\":0}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": \"" + input + "\", \"nested1.nested2.nested3.nested4\":\"" + input + "\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"7\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\": true, \"other_Info\":true, \"non_exist_field\":true}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": true, \"other_Info\":true, \" item \":true}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"7\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\": true, \"other_Info.non_exist_field\":true}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": true, \"other_Info\":{\"non_exist_field\":true}}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": true, \"other_Info.0\": true}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"4\"}"; + resultSet = nullptr; + projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + filter = "{\"_id\" : \"7\"}"; + resultSet = nullptr; + projectionInfo = "{\"non_exist_field\":true}"; + int flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + flag = 1; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"7\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\":true, \"item\":true}"; + flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + flag = 1; + projectionInfo = "{\"name\": " + input + ", \"item\": " + input + "}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"7\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\":false, \"item\":false}"; + flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + flag = 1; + projectionInfo = "{\"name\": " + input + ", \"item\": " + input + "}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"4\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\": " + input + ", \"personInfo.grade1\": " + input + ", \ + \"personInfo.shool1\": " + + input + ", \"personInfo.age1\": " + input + "}"; + flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + projectionInfo = "{\"name\": false, \"personInfo.grade1\": false, \ + \"personInfo.shool1\": false, \"personInfo.age1\": false}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + projectionInfo = + "{\"name\": " + input + ", \"personInfo.school\": " + input + ", \"personInfo.age\": " + input + "}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + if (resultSet != nullptr) { + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + } + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"17\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, GRD_DOC_ID_DISPLAY, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, "", query, 0, &resultSet); + GRD_FindDoc(g_db, nullptr, query, 0, &resultSet); + + filter = "{\"_id\" : \"4\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\":" + input + ", \"personInfo\":0, \"item\":" + input + "}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + projectionInfo = "{\"name\":true, \"personInfo\":0, \"item\":true}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"item\":\"\"}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + projectionInfo = "{\"name\":false, \"personInfo\":1, \"item\":false"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"item\":false"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + projectionInfo = "{\"name\":false, \"personInfo\":true, \"item\":false"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + filter = "{\"_id\" : \"6\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\":false, \"personInfo\": 0, \"item\":0}"; + flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"18\"}"; + resultSet = nullptr; + projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : " + "-100}"; + flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + projectionInfo = "{\"personInfo\":[true, " + input + "]}"; + flag = 1; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + projectionInfo = "{\"personInfo\":null}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + resultSet = nullptr; + projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":" + input + ", \"COLOR\":" + input + + ", \"nonExist\" : " + "" + + input + "}"; + flag = 0; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet); + GRD_Next(resultSet); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + projectionInfo = "{\"Name\":" + input + ", \"personInfo.age\": false, \"personInfo.SCHOOL\": false, \"item\":\ + false, \"COLOR\":false, \"nonExist\" : false}"; + query = { filter.c_str(), projectionInfo.c_str() }; + value = nullptr; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_Next(resultSet); + GRD_FreeResultSet(resultSet); + + filter = "{\"_id\" : \"18\"}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + std::string collectionName1(511, 'a'); + GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet); + GRD_FreeResultSet(resultSet); + + std::string collectionName2(512, 'a'); + GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet); + GRD_FindDoc(g_db, "", query, 1, &resultSet); + + resultSet = nullptr; + projectionInfo = "{}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 3, &resultSet); + GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MAX, &resultSet); + GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MIN, &resultSet); + GRD_FindDoc(nullptr, COLLECTION_NAME, query, 0, &resultSet); + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, nullptr); + query = { nullptr, nullptr }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + + std::string document1 = "{\"_id\" : "; + std::string document2 = "\""; + std::string document4 = "\""; + std::string document5 = "}"; + std::string document_midlle(900, 'k'); + filter = document1 + document2 + document_midlle + document4 + document5; + resultSet = nullptr; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + std::string document_midlle2(899, 'k'); + filter = document1 + document2 + document_midlle2 + document4 + document5; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_FreeResultSet(resultSet); + + filter = "{\"personInfo\" : {\"school\":\"" + input + "\"}}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + projectionInfo = "{\"version\": " + input + "}"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + projectionInfo = "({\"a00001\":" + input + ", \"a00001\":" + input + "})"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + resultSet = nullptr; + projectionInfo = "({\"abc123_.\":" + input + "})"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + filter = "({\"abc123_.\":" + input + "})"; + resultSet = nullptr; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + std::string document064 = "{\"_id\" : \"64\", \"a\":" + input + ", \"doc64\" : " + input + "}"; + std::string document063 = "{\"_id\" : \"63\", \"a\":" + input + ", \"doc63\" : " + input + "}"; + std::string document062 = "{\"_id\" : \"62\", \"a\":" + input + ", \"doc62\" : " + input + "}"; + std::string document061 = "{\"_id\" : \"61\", \"a\":" + input + ", \"doc61\" : " + input + "}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document064.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, document063.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, document062.c_str(), 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, document061.c_str(), 0); + filter = "{\"a\":" + input + "}"; + resultSet = nullptr; + projectionInfo = R"({})"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + std::string document = "{\"a\":" + input + ", \"doc64\" : " + input + "}"; + filter = "{\"b\":" + input + "}"; + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, filter.c_str(), document.c_str(), 0); + filter = "{\"a\":" + input + "}"; + resultSet = nullptr; + projectionInfo = R"({})"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + GRD_FreeResultSet(resultSet); + + filter = "{\"a\":" + input + "}"; + resultSet = nullptr; + projectionInfo = R"({})"; + query = { filter.c_str(), projectionInfo.c_str() }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + GRD_Next(resultSet); + char *valueNew = nullptr; + GRD_GetValue(resultSet, &valueNew); + GRD_FreeValue(valueNew); + GRD_FreeResultSet(resultSet); + + filter = "{\"personInfo\" : {\"school\":" + input + "}" + "}"; + resultSet = nullptr; + query = { filter.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_Next(resultSet); + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); +} +} // namespace + +void FindDocFuzz(const uint8_t *data, size_t size) +{ + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); + std::string input(reinterpret_cast(data), size); + std::string inputJson = "{\"field\":\"" + input + "\"}"; + const char *document1 = inputJson.c_str(); + GRD_InsertDoc(g_db, COLLECTION_NAME, document1, 0); + Query query = { inputJson.c_str(), inputJson.c_str() }; + GRD_ResultSet *resultSet = nullptr; + // ResultSet conflict + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FreeResultSet(resultSet); + resultSet = nullptr; + GRD_FindDoc(g_db, input.c_str(), query, size, &resultSet); + GRD_FreeResultSet(resultSet); + GRD_FindDoc(nullptr, input.c_str(), query, 1, &resultSet); + query.filter = nullptr; + GRD_FindDoc(g_db, input.c_str(), query, 1, &resultSet); + inputJson = "{\"field\": 0, \"field2\":" + input + "}"; + query.filter = "{}"; + query.projection = inputJson.c_str(); + resultSet = nullptr; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FreeResultSet(resultSet); + inputJson = "{\"" + input + "\": 0}"; + query.projection = inputJson.c_str(); + resultSet = nullptr; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FreeResultSet(resultSet); + inputJson = "{\"field\":[\"aaa\"," + input + "]}"; + resultSet = nullptr; + query.projection = inputJson.c_str(); + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FreeResultSet(resultSet); + query.filter = "{\"field\": false}"; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_FreeResultSet(resultSet); + FindDocFuzz_Plus(input); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); +} + +void UpdateDocFuzz(const uint8_t *data, size_t size) +{ + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); + std::string input(reinterpret_cast(data), size); + std::string inputJson = "{\"_id\":\"2\", \"field\": \"aaa\", " + "\"subject\":\"aaaaaaaaaaa\", \"test1\": true, " + "\"test2\": null}"; + + GRD_UpdateDoc(g_db, inputJson.c_str(), "{}", "{}", 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, input.c_str(), "{}", 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{}", input.c_str(), 0); + GRD_UpdateDoc(nullptr, COLLECTION_NAME, "{}", "{}", 0); + inputJson = "{\"_id\":\"2\", \"field\":" + input + "}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field\":" + input + "}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1.field2.field3.field4.field5.field6\":" + input + "}"; + inputJson = "{\"field1\": {\"field2\": {\"field3\": {\"field4\": {\"field5\":" + input + "}}}}}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + std::string s; + for (int i = 0; i < ((1024 * 1024) - 1) - 15; i++) { + s += 'a'; + } + inputJson = "{\"field5\": \"" + s + "\"}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field5\": \"" + s + s + "\"}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1\": [\"field2\", {\"field3\":\"" + input + "\"}]}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"name\":\"doc6\", \"c0\" : {\"c1\" : true } }"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"name\":\"doc6\"}", "{\"c0.c1\":false}", 1); + inputJson = "{\"name\":\"doc7\", \"c0\" : {\"c1\" : null } }"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"name\":\"doc7\"}", "{\"c0.c1\":null}", 1); + inputJson = "{\"name\":\"doc8\", \"c0\" : [\"" + input + "\", 123]}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"name\":\"doc8\"}", "{\"c0.0\":\"ac\"}", 1); + + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, inputJson.c_str(), "{}", "{}", 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, input.c_str(), "{}", 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{}", input.c_str(), 0); + GRD_UpdateDoc(nullptr, COLLECTION_NAME, "{}", "{}", 0); + inputJson = "{\"_id\":\"2\", \"field\":" + input + "}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field\":" + input + "}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1.field2.field3.field4.field5.field6\":" + input + "}"; + inputJson = "{\"field1\": {\"field2\": {\"field3\": {\"field4\": {\"field5\":" + input + "}}}}}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + s.clear(); + for (int i = 0; i < ((1024 * 1024) - 1) - 15; i++) { + s += 'a'; + } + inputJson = "{\"field5\": \"" + s + "\"}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field5\": \"" + s + s + "\"}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1\": [\"field2\", {\"field3\":\"" + input + "\"}]}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"name\":\"doc6\", \"c0\" : {\"c1\" : true } }"; + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"name\":\"doc6\"}", "{\"c0.c1\":false}", 1); + inputJson = "{\"name\":\"doc7\", \"c0\" : {\"c1\" : null } }"; + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"name\":\"doc7\"}", "{\"c0.c1\":null}", 1); + inputJson = "{\"name\":\"doc8\", \"c0\" : [\"" + input + "\", 123]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, "{\"name\":\"doc8\"}", "{\"c0.0\":\"ac\"}", 1); + + const char *newCollName = "./student"; + GRD_UpdateDoc(g_db, inputJson.c_str(), "{}", "{}", 0); + GRD_UpdateDoc(g_db, newCollName, input.c_str(), "{}", 0); + GRD_UpdateDoc(g_db, newCollName, "{}", input.c_str(), 0); + GRD_UpdateDoc(nullptr, newCollName, "{}", "{}", 0); + inputJson = "{\"_id\":\"2\", \"field\":" + input + "}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field\":" + input + "}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1.field2.field3.field4.field5.field6\":" + input + "}"; + inputJson = "{\"field1\": {\"field2\": {\"field3\": {\"field4\": {\"field5\":" + input + "}}}}}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + s.clear(); + for (int i = 0; i < ((1024 * 1024) - 1) - 15; i++) { + s += 'a'; + } + inputJson = "{\"field5\": \"" + s + "\"}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field5\": \"" + s + s + "\"}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1\": [\"field2\", {\"field3\":\"" + input + "\"}]}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"name\":\"doc6\", \"c0\" : {\"c1\" : true } }"; + GRD_UpdateDoc(g_db, newCollName, "{\"name\":\"doc6\"}", "{\"c0.c1\":false}", 1); + inputJson = "{\"name\":\"doc7\", \"c0\" : {\"c1\" : null } }"; + GRD_UpdateDoc(g_db, newCollName, "{\"name\":\"doc7\"}", "{\"c0.c1\":null}", 1); + inputJson = "{\"name\":\"doc8\", \"c0\" : [\"" + input + "\", 123]}"; + GRD_UpdateDoc(g_db, newCollName, "{\"name\":\"doc8\"}", "{\"c0.0\":\"ac\"}", 1); + + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, inputJson.c_str(), "{}", "{}", 0); + GRD_UpdateDoc(g_db, newCollName, input.c_str(), "{}", 0); + GRD_UpdateDoc(g_db, newCollName, "{}", input.c_str(), 0); + GRD_UpdateDoc(nullptr, newCollName, "{}", "{}", 0); + inputJson = "{\"_id\":\"2\", \"field\":" + input + "}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field\":" + input + "}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1.field2.field3.field4.field5.field6\":" + input + "}"; + inputJson = "{\"field1\": {\"field2\": {\"field3\": {\"field4\": {\"field5\":" + input + "}}}}}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + s.clear(); + for (int i = 0; i < ((1024 * 1024) - 1) - 15; i++) { + s += 'a'; + } + inputJson = "{\"field5\": \"" + s + "\"}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field5\": \"" + s + s + "\"}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"field1\": [\"field2\", {\"field3\":\"" + input + "\"}]}"; + GRD_UpdateDoc(g_db, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + inputJson = "{\"name\":\"doc6\", \"c0\" : {\"c1\" : true } }"; + GRD_InsertDoc(g_db, newCollName, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, newCollName, "{\"name\":\"doc6\"}", "{\"c0.c1\":false}", 1); + inputJson = "{\"name\":\"doc7\", \"c0\" : {\"c1\" : null } }"; + GRD_InsertDoc(g_db, newCollName, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, newCollName, "{\"name\":\"doc7\"}", "{\"c0.c1\":null}", 1); + inputJson = "{\"name\":\"doc8\", \"c0\" : [\"" + input + "\", 123]}"; + GRD_InsertDoc(g_db, newCollName, inputJson.c_str(), 0); + GRD_UpdateDoc(g_db, newCollName, "{\"name\":\"doc8\"}", "{\"c0.0\":\"ac\"}", 1); + + const char *filter = "{\"_id\" : \"1\"}"; + const char *updata2 = "{\"objectInfo.child.child\" : {\"child\":{\"child\":null}}}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter, updata2, 0); + + const char *filter1 = "{\"_id\" : \"1\"}"; + const char *updata1 = "{\"_id\" : \"6\"}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter1, updata1, 0); + + const char *filter2 = "{\"_id\" : \"1\"}"; + const char *updata3 = "{\"age$\" : \"21\"}"; + const char *updata4 = "{\"bonus..traffic\" : 100}"; + const char *updata5 = "{\"0item\" : 100}"; + const char *updata6 = "{\"item\" : 1.79769313486232e308}"; + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter2, updata3, 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter2, updata4, 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter2, updata5, 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter2, updata6, 0); + + const char *filter3 = "{\"_id\" : \"1\"}"; + const char *updata7 = "{\"age\" : 21}"; + GRD_UpdateDoc(g_db, NULL, filter3, updata7, 0); + GRD_UpdateDoc(g_db, "", filter3, updata7, 0); + GRD_UpdateDoc(NULL, COLLECTION_NAME, filter3, updata7, 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, NULL, updata7, 0); + GRD_UpdateDoc(g_db, COLLECTION_NAME, filter3, NULL, 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); +} + +void UpsertDocFuzz(const uint8_t *data, size_t size) +{ + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); + std::string input(reinterpret_cast(data), size); + std::string inputJsonNoId = "{\"name\":\"doc8\", \"c0\" : [\"" + input + "\", 123]}"; + std::string inputJson = "{\"_id\":\"1\", \"field\": " + input + "}"; + const char *document1 = inputJson.c_str(); + + GRD_InsertDoc(g_db, COLLECTION_NAME, document1, 0); + GRD_UpsertDoc(g_db, input.c_str(), "{}", "{}", 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), "{}", 0); + inputJson = "{\"_id\":\"1\", \"field\":" + input + "}"; + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"1\"}", inputJson.c_str(), 0); + inputJson = "{\"_id\":\"1\", \"field\":\"" + input + "\"}"; + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"1\"}", inputJson.c_str(), 0); + inputJson = "{\"field\":" + input + "}"; + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"1\"}", inputJson.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"1\"}", inputJson.c_str(), 1); + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 1); + GRD_UpsertDoc(nullptr, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 1); + GRD_UpsertDoc(g_db, COLLECTION_NAME, nullptr, inputJson.c_str(), 1); + GRD_UpsertDoc(g_db, COLLECTION_NAME, "{\"_id\":\"2\"}", inputJson.c_str(), 100); + + const char *newCollName = "./student"; + GRD_DB *g_db1 = nullptr; + GRD_CreateCollection(g_db1, newCollName, "", 0); + GRD_InsertDoc(g_db1, newCollName, document1, 0); + GRD_UpsertDoc(g_db1, input.c_str(), "{}", "{}", 0); + GRD_UpsertDoc(g_db1, newCollName, inputJson.c_str(), "{}", 0); + inputJson = "{\"_id\":\"1\", \"field\":" + input + "}"; + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"1\"}", inputJson.c_str(), 0); + inputJson = "{\"_id\":\"1\", \"field\":\"" + input + "\"}"; + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"1\"}", inputJson.c_str(), 0); + inputJson = "{\"field\":" + input + "}"; + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"1\"}", inputJson.c_str(), 0); + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 0); + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"1\"}", inputJson.c_str(), 1); + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 1); + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"newID1999\"}", inputJsonNoId.c_str(), 0); + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"newID1999a\"}", inputJsonNoId.c_str(), 1); + GRD_UpsertDoc(nullptr, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 1); + GRD_UpsertDoc(g_db1, newCollName, nullptr, inputJson.c_str(), 1); + GRD_UpsertDoc(g_db1, newCollName, "{\"_id\":\"2\"}", inputJson.c_str(), 100); + GRD_DropCollection(g_db1, newCollName, 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + + GRD_CreateCollection(g_db, "student", "", 0); + std::string documentNew = + "{\"name\": {\"first\":[\"XXX\", \"BBB\", \"CCC\"], \"last\":\"moray\", \"field\":" + input + "}"; + std::string updateDocNew = R""({"name.last.AA.B":"Mnado"})""; + GRD_UpsertDoc(g_db, "student", R""({"_id":"10001"})"", documentNew.c_str(), 0); + GRD_UpsertDoc(g_db1, "student", R""({"_id":"10001"})"", updateDocNew.c_str(), 1); + GRD_DropCollection(g_db, "student", 0); + + GRD_CreateCollection(g_db, "student", "", 0); + documentNew = "{\"name\":[\"Tmn\", \"BB\", \"Alice\"], \"field\":" + input + "}"; + updateDocNew = R""({"name.first":"GG"})""; + GRD_UpsertDoc(g_db, "student", R""({"_id":"10002"})"", documentNew.c_str(), 0); + GRD_UpsertDoc(g_db1, "student", R""({"_id":"10002"})"", updateDocNew.c_str(), 1); +} + +void DeleteDocFuzz(const uint8_t *data, size_t size) +{ + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); + std::string input(reinterpret_cast(data), size); + std::string inputJson = "{\"field\":" + input + "}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + GRD_DeleteDoc(g_db, input.c_str(), "{}", 0); + GRD_DeleteDoc(g_db, nullptr, "{}", 0); + std::string longName = ""; + for (int i = 0; i < 513; i++) { + longName += 'a'; + } + GRD_DeleteDoc(g_db, "grd_name", "{}", 0); + GRD_DeleteDoc(g_db, longName.c_str(), "{}", 0); + GRD_DeleteDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + inputJson = "{\"field1\":" + input + "}"; + GRD_DeleteDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + inputJson = "{\"field\":\"" + input + "\"}"; + GRD_DeleteDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + inputJson = "{\"field1\":\"" + input + "\"}"; + GRD_DeleteDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + + const char *document1 = "{ \ + \"_id\" : \"1\", \ + \"name\": \"xiaoming\", \ + \"address\": \"beijing\", \ + \"age\" : 15, \ + \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ + \"subject\": [\"math\", \"English\", \"music\", {\"info\" : \"exam\"}] \ + }"; + const char *document2 = "{ \ + \"_id\" : \"2\", \ + \"name\": \"ori\", \ + \"address\": \"beijing\", \ + \"age\" : 15, \ + \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ + \"subject\": [\"math\", \"English\", \"music\"] \ + }"; + const char *document3 = "{ \ + \"_id\" : \"3\", \ + \"name\": \"David\", \ + \"address\": \"beijing\", \ + \"age\" : 15, \ + \"friend\" : {\"name\" : \"David\", \"sex\" : \"female\", \"age\" : 90}, \ + \"subject\": [\"Sing\", \"Jump\", \"Rap\", \"BasketBall\"] \ + }"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document1, 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, document2, 0); + GRD_InsertDoc(g_db, COLLECTION_NAME, document3, 0); + + GRD_DeleteDoc(g_db, COLLECTION_NAME, "{}", 0); + + const char *filter = "{\"age\" : 15}"; + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0); + const char *filter1 = "{\"_id\" : \"1\", \"age\" : 15}"; + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter1, 0); + std::string filter2 = "{\"_id\" : \"1\", \"name\" : " + input + "}"; + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter2.c_str(), 1); + GRD_DeleteDoc(g_db, NULL, filter2.c_str(), 0); + GRD_DeleteDoc(g_db, "", filter2.c_str(), 1); + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter2.c_str(), 0); + + const char *filter3 = "{\"_id\" : \"1\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter3, "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter3, 0); + + const char *filter4 = "{\"_id\" : \"1\"}"; + std::string collectionName1(511, 'a'); + GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0); + GRD_DeleteDoc(g_db, collectionName1.c_str(), filter4, 0); + GRD_DropCollection(g_db, collectionName1.c_str(), 0); + GRD_FreeResultSet(resultSet); + + GRD_DeleteDoc(NULL, COLLECTION_NAME, filter2.c_str(), 0); + GRD_DeleteDoc(g_db, NULL, filter2.c_str(), 0); + GRD_DeleteDoc(g_db, "", filter2.c_str(), 0); + GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL, 0); + GRD_DeleteDoc(g_db, COLLECTION_NAME, "", 0); + GRD_DeleteDoc(g_db, "notExisted", filter2.c_str(), 0); + + std::vector filterVec = { R"({"_id" : 1})", R"({"_id":[1, 2]})", R"({"_id" : {"t1" : 1}})", + R"({"_id":null})", R"({"_id":true})", R"({"_id" : 1.333})", R"({"_id" : -2.0})" }; + for (const auto &item : filterVec) { + GRD_DeleteDoc(g_db, COLLECTION_NAME, item.c_str(), 0); + } + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter2.c_str(), 0); + + std::string filter5 = "{\"subject.info\" : \"" + input + "\"}"; + resultSet = nullptr; + query = { filter2.c_str(), "{}" }; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + GRD_DeleteDoc(g_db, COLLECTION_NAME, filter5.c_str(), 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + GRD_FreeResultSet(resultSet); +} + +namespace { +void FindAndRelease(Query query) +{ + GRD_ResultSet *resultSet = nullptr; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + while (GRD_Next(resultSet) == 0) + ; + GRD_Next(resultSet); + char *value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + resultSet = nullptr; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet); + while (GRD_Next(resultSet) == 0) + ; + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + + const char *newCollName = "./student"; + resultSet = nullptr; + GRD_FindDoc(g_db, newCollName, query, 1, &resultSet); + while (GRD_Next(resultSet) == 0) + ; + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + resultSet = nullptr; + GRD_FindDoc(g_db, newCollName, query, 0, &resultSet); + while (GRD_Next(resultSet) == 0) + ; + GRD_Next(resultSet); + value = nullptr; + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); +} +} // namespace + +void NextFuzz(const uint8_t *data, size_t size) +{ + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); + std::string input(reinterpret_cast(data), size); + std::string inputJson = "{" + input + "}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, inputJson.c_str(), 0); + Query query = { inputJson.c_str(), "{}" }; + FindAndRelease(query); + std::string stringJson2 = "{ \"_id\":\"1\", \"field\":\"" + input + "\"}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, stringJson2.c_str(), 0); + query.filter = "{\"_id\": \"1\"}"; + FindAndRelease(query); + std::string filter2 = "{ \"_id\":\"1\", \"field\":\"" + input + " invalid\"}"; + query.filter = filter2.c_str(); + FindAndRelease(query); + query.filter = "{\"_id\": \"3\"}"; + FindAndRelease(query); + std::string stringJson3 = "{ \"_id\":\"2\", \"field\": [\"field2\", null, \"abc\", 123]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, stringJson3.c_str(), 0); + query.filter = "{\"field\": [\"field2\", null, \"abc\", 123]}"; + FindAndRelease(query); + std::string document = "{\"field\": [\"" + input + "\",\"" + input + "\",\"" + input + "\"]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document.c_str(), 0); + std::string filter = "{\"field." + std::to_string(size) + "\":\"" + input + "\"}"; + query.filter = filter.c_str(); + query.projection = "{\"field\": 0}"; + FindAndRelease(query); + document = "{\"field1.field2\" [false], \"field1.field2.field3\": [3], " + "\"field1.field4\": [null]}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document.c_str(), 0); + query.filter = document.c_str(); + query.projection = "{\"field\": 0}"; + FindAndRelease(query); + query.projection = "{\"field\": 1}"; + FindAndRelease(query); + query.projection = "{\"field.field2\": 1}"; + FindAndRelease(query); + document = "{\"field1\": {\"field2\": [{\"field3\":\"" + input + "\"}]}}"; + GRD_InsertDoc(g_db, COLLECTION_NAME, document.c_str(), 0); + filter = "{\"field1.field2.field3\":\"" + input + "\"}"; + query.filter = filter.c_str(); + query.projection = "{\"field1.field2\": 1}"; + FindAndRelease(query); + query.projection = "{\"field1.field2\": 0}"; + FindAndRelease(query); + query.projection = "{\"field1.field3\": 1}"; + FindAndRelease(query); + query.projection = "{\"field1.field3\": 0}"; + FindAndRelease(query); + query.projection = "{\"field1.field2.field3\": 0, \"field1.field2.field3\": 0}"; + FindAndRelease(query); + std::string projection = "{\"field1\": \"" + input + "\"}"; + query.projection = projection.c_str(); + FindAndRelease(query); + projection = "{\"" + input + "\": true, \"" + input + "\": false}"; + query.projection = projection.c_str(); + FindAndRelease(query); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); +} + +void GetValueFuzz(const uint8_t *data, size_t size) +{ + GRD_CreateCollection(g_db, COLLECTION_NAME, OPTION_STR, 0); + std::string input(reinterpret_cast(data), size); + std::string inputJson = "{" + input + "}"; + const char *document1 = inputJson.c_str(); + GRD_InsertDoc(g_db, COLLECTION_NAME, document1, 0); + char *value = nullptr; + Query query = { inputJson.c_str(), "{}" }; + GRD_ResultSet *resultSet = nullptr; + GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet); + GRD_GetValue(resultSet, &value); + GRD_Next(resultSet); + GRD_GetValue(resultSet, &value); + GRD_FreeValue(value); + GRD_FreeResultSet(resultSet); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); +} + +void FreeResultSetFuzz() +{ + GRD_ResultSet *resultSet = nullptr; + GRD_FreeResultSet(resultSet); + resultSet = new GRD_ResultSet; + resultSet->resultSet_ = DocumentDB::ResultSet(); + GRD_FreeResultSet(resultSet); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); +} + +void DbOpenFuzz(const uint8_t *data, size_t size) +{ + std::string dbFile_data(reinterpret_cast(data), size); + std::string realDbFile_data = DB_DIR_PATH + dbFile_data; + const char *dbFile_val = realDbFile_data.data(); + std::string configStr_data(reinterpret_cast(data), size); + const char *configStr_val = configStr_data.data(); + GRD_DB *db_val = nullptr; + + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + std::string string_max = getMaxString(); + const char *configStr_MaxLen = string_max.data(); + GRD_DBOpen(dbFile_val, configStr_MaxLen, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_stringValue = "{\"bufferPoolSize\": \"1024.5\"}"; + const char *configStr_stringValue = field_stringValue.data(); + GRD_DBOpen(dbFile_val, configStr_stringValue, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_boolValue = "{\"bufferPoolSize\":}"; + const char *configStr_bool = field_boolValue.data(); + GRD_DBOpen(dbFile_val, configStr_bool, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_stringValue_append = "{\"bufferPoolSize\":\"8192\"}"; + const char *configStr_str = field_stringValue_append.data(); + GRD_DBOpen(dbFile_val, configStr_str, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_stringValue_flush = "{\"bufferPoolSize\":\"8192\",\"redoFlushBtTrx\":\"1\"}"; + const char *configStr_flush = field_stringValue_flush.data(); + GRD_DBOpen(dbFile_val, configStr_flush, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_stringValue_redo_bufsize = "{\"bufferPoolSize\":\"8192\",\"redoBufSize\":\"16384\"}"; + const char *configStr_bs = field_stringValue_redo_bufsize.data(); + GRD_DBOpen(dbFile_val, configStr_bs, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_stringValue_MaxConnNum = "{\"bufferPoolSize\":\"8192\",\"maxConnNum\":\"1024\"}"; + const char *configStr_mcn = field_stringValue_MaxConnNum.data(); + GRD_DBOpen(dbFile_val, configStr_mcn, GRD_DB_OPEN_CREATE, &db_val); + + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + std::string field_stringValue_all = "{\"bufferPoolSize\":\"8192\",\"redoFlushBtTrx\":\"1\",\"redoBufSize\":" + "\"16384\",\"maxConnNum\":\"1024\"}"; + const char *configStr_all = field_stringValue_all.data(); + GRD_DBOpen(dbFile_val, configStr_all, GRD_DB_OPEN_CREATE, &db_val); + + std::string field_lowNumber = "{\"bufferPoolSize\": 102}"; + const char *configStr_lowNumber = field_lowNumber.data(); + GRD_DBOpen(dbFile_val, configStr_lowNumber, GRD_DB_OPEN_CREATE, &db_val); + + GRD_DBOpen(nullptr, configStr_val, GRD_DB_OPEN_CREATE, &db_val); + GRD_DBOpen(dbFile_val, configStr_val, GRD_DB_OPEN_CREATE, &db_val); + GRD_DBClose(nullptr, GRD_DB_CLOSE); + GRD_DBClose(db_val, GRD_DB_CLOSE); + + std::string path = "./documentFuzz.db"; + GRD_DBOpen(path.c_str(), "", GRD_DB_OPEN_ONLY, &db_val); + (void)remove(path.c_str()); + + path = "./document.db"; + GRD_DBOpen(path.c_str(), R""({"pageSize":64, "redopubbufsize":4033})"", GRD_DB_OPEN_CREATE, &db_val); + GRD_DBClose(db_val, 0); + GRD_DBOpen(path.c_str(), R""({"pageSize":64, "redopubbufsize":4032})"", GRD_DB_OPEN_CREATE, &db_val); + GRD_DBOpen(path.c_str(), R""({"redopubbufsize":255})"", GRD_DB_OPEN_CREATE, &db_val); + GRD_DBOpen(path.c_str(), R""({"redopubbufsize":16385})"", GRD_DB_OPEN_CREATE, &db_val); +} + +void DbCloseFuzz(const uint8_t *data, size_t size) +{ + std::string dbFile_data(reinterpret_cast(data), size); + std::string realDbFile_data = DB_DIR_PATH + dbFile_data; + const char *dbFile_Val = realDbFile_data.data(); + std::string configStr_data(reinterpret_cast(data), size); + const char *configStr_val = configStr_data.data(); + GRD_DB *db_val = nullptr; + GRD_DBOpen(dbFile_Val, configStr_val, GRD_DB_OPEN_CREATE, &db_val); + GRD_DBClose(db_val, GRD_DB_CLOSE); +} + +void DbCloseResultSetFuzz() +{ + GRD_DB *db = nullptr; + GRD_DB *db2 = nullptr; + GRD_DBOpen(TEST_DB_FILE, CONFIG_STR, GRD_DB_OPEN_CREATE, &db); + GRD_DBOpen(TEST_DB_FILE, CONFIG_STR, GRD_DB_OPEN_CREATE, &db2); + GRD_CreateCollection(db, "collection1", "{\"maxdoc\" : 5}", 0); + + GRD_ResultSet *resultSet = nullptr; + Query query = { "{}", "{}" }; + GRD_FindDoc(db, "collection1", query, 0, &resultSet); + + GRD_FreeResultSet(resultSet); + + GRD_DBClose(db, GRD_DB_CLOSE); + + GRD_DBClose(db2, GRD_DB_CLOSE); +} + +void CreateCollectionFuzz(const uint8_t *data, size_t size) +{ + std::string collectionName_data(reinterpret_cast(data), size); + const char *collectionName_val = collectionName_data.data(); + std::string optionStr_data(reinterpret_cast(data), size); + const char *optionStr_val = optionStr_data.data(); + GRD_CreateCollection(nullptr, collectionName_val, optionStr_val, 0); + GRD_CreateCollection(g_db, collectionName_val, optionStr_val, 0); + const char *optionStr = nullptr; + GRD_CreateCollection(g_db, COLLECTION_NAME, optionStr, 0); + GRD_CreateCollection(g_db, COLLECTION_NAME, "{\"maxdoc\":5, \"unexpected_max_doc\":32}", 0); + GRD_CreateCollection(g_db, COLLECTION_NAME, "{}", 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + std::string optStr = "{\"maxdoc\":" + optionStr_data + "}"; + GRD_CreateCollection(g_db, COLLECTION_NAME, optStr.c_str(), 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + GRD_CreateCollection(g_db, COLLECTION_NAME, optStr.c_str(), 1000); + optStr = "{\"maxdoc\": 5}"; + GRD_CreateCollection(g_db, COLLECTION_NAME, optStr.c_str(), 0); + GRD_CreateCollection(g_db, COLLECTION_NAME, optStr.c_str(), 1); + GRD_DropCollection(g_db, COLLECTION_NAME, 0); + GRD_DropCollection(g_db, COLLECTION_NAME, 1000); + GRD_DropCollection(g_db, collectionName_val, 0); +} + +void DropCollectionFuzz(const uint8_t *data, size_t size) +{ + std::string collectionName_data(reinterpret_cast(data), size); + const char *collectionName_val = collectionName_data.data(); + std::string optionStr_data(reinterpret_cast(data), size); + const char *optionStr_val = optionStr_data.data(); + GRD_CreateCollection(g_db, collectionName_val, optionStr_val, 0); + GRD_DropCollection(nullptr, collectionName_val, 0); + GRD_DropCollection(g_db, collectionName_val, 0); +} + +void DbFlushFuzz(const uint8_t *data, size_t size) +{ + GRD_DB *db = nullptr; + GRD_DB *db2 = nullptr; + GRD_DBOpen(TEST_DB_FILE, CONFIG_STR, GRD_DB_OPEN_CREATE, &db); + GRD_Flush(db, 0); + GRD_Flush(db, 1); + GRD_Flush(db2, 0); + GRD_Flush(db2, 1); + GRD_DBClose(db, GRD_DB_CLOSE); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" { +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::SetUpTestCase(); + + OHOS::DbOpenFuzz(data, size); + OHOS::CreateCollectionFuzz(data, size); + OHOS::DropCollectionFuzz(data, size); + OHOS::InsertDocFuzz(data, size); + OHOS::FindDocFuzz(data, size); + OHOS::UpdateDocFuzz(data, size); + OHOS::UpsertDocFuzz(data, size); + OHOS::DeleteDocFuzz(data, size); + OHOS::NextFuzz(data, size); + OHOS::GetValueFuzz(data, size); + OHOS::FreeResultSetFuzz(); + OHOS::DbCloseFuzz(data, size); + OHOS::DbCloseResultSetFuzz(); + OHOS::DbFlushFuzz(data, size); + + OHOS::TearDownTestCase(); + return 0; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/project.xml b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..ceb0812f10b8e9c1a8f53f6c65a3094b2dd66be5 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/fuzz_test/json_fuzzer/project.xml @@ -0,0 +1,28 @@ + + + + + + + 1000 + + 7200 + + 4096 + + 1 + + \ No newline at end of file