# Copyright (c) 2024 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/ohos.gni")
import("//build/test.gni")
import("../../../services/samgr/var.gni")
module_output_path = "samgr/samgr"
samgr_dir = "../../../"
samgr_services_dir = "${samgr_dir}/services/samgr/native"

config("sam_fuzz_test_config") {
  include_dirs = [
    "${samgr_dir}/services/dfx/include",
    "${samgr_dir}/services/lsamgr/include",
    "${samgr_dir}/services/samgr/native/test/unittest/include",
    "${samgr_dir}/test/fuzztest/samgr_fuzzer/include",
  ]
}

##############################fuzztest##########################################
cancelunloadsystemability_test = {
  targetName = "CancelUnloadSystemAbilityFuzzTest"
  configFuzzer = "cancelunloadsystemability_fuzzer"
  source = "src/cancelunloadsystemability_fuzzer.cpp"
}

checkremotesystemability_test = {
  targetName = "CheckRemoteSystemAbilityFuzzTest"
  configFuzzer = "checkremotesystemability_fuzzer"
  source = "src/checkremotesystemability_fuzzer.cpp"
}

getextensionsaids_test = {
  targetName = "GetExtensionSaIdsFuzzTest"
  configFuzzer = "getextensionsaids_fuzzer"
  source = "src/getextensionsaids_fuzzer.cpp"
}

getextensionsalist_test = {
  targetName = "GetExtensionSaListFuzzTest"
  configFuzzer = "getextensionsalist_fuzzer"
  source = "src/getextensionsalist_fuzzer.cpp"
}

getondemandpolicy_test = {
  targetName = "GetOnDemandPolicyFuzzTest"
  configFuzzer = "getondemandpolicy_fuzzer"
  source = "src/getondemandpolicy_fuzzer.cpp"
}

getsystemability_test = {
  targetName = "GetSystemAbilityFuzzTest"
  configFuzzer = "getsystemability_fuzzer"
  source = "src/getsystemability_fuzzer.cpp"
}

getsaextensioninfo_test = {
  targetName = "GetSaExtensionInfoFuzzTest"
  configFuzzer = "getsaextensioninfo_fuzzer"
  source = "src/getsaextensioninfo_fuzzer.cpp"
}

loadremotesystemability_test = {
  targetName = "LoadRemoteSystemAbilityFuzzTest"
  configFuzzer = "loadremotesystemability_fuzzer"
  source = "src/loadremotesystemability_fuzzer.cpp"
}

unloadallidlesystemability_test = {
  targetName = "UnloadAllIdleSystemAbilityFuzzTest"
  configFuzzer = "unloadallidlesystemability_fuzzer"
  source = "src/unloadallidlesystemability_fuzzer.cpp"
}

unsubscribesystemprocess_test = {
  targetName = "UnSubscribeSystemProcessFuzzTest"
  configFuzzer = "unsubscribesystemprocess_fuzzer"
  source = "src/unsubscribesystemprocess_fuzzer.cpp"
}

unsubscribesystemability_test = {
  targetName = "UnSubscribeSystemAbilityFuzzTest"
  configFuzzer = "unsubscribesystemability_fuzzer"
  source = "src/unsubscribesystemability_fuzzer.cpp"
}

addsystemability_test = {
  targetName = "AddSystemAbilityFuzzTest"
  configFuzzer = "addsystemability_fuzzer"
  source = "src/addsystemability_fuzzer.cpp"
}

addsystemprocess_test = {
  targetName = "AddSystemProcessFuzzTest"
  configFuzzer = "addsystemprocess_fuzzer"
  source = "src/addsystemprocess_fuzzer.cpp"
}

samgr_fuzztests = [
  cancelunloadsystemability_test,
  checkremotesystemability_test,
  getextensionsaids_test,
  getextensionsalist_test,
  getondemandpolicy_test,
  getsystemability_test,
  getsaextensioninfo_test,
  loadremotesystemability_test,
  unloadallidlesystemability_test,
  unsubscribesystemprocess_test,
  unsubscribesystemability_test,
  addsystemability_test,
  addsystemprocess_test,
]
foreach(item, samgr_fuzztests) {
  ohos_fuzztest(item.targetName) {
    module_out_path = module_output_path

    fuzz_config_file =
        "${samgr_dir}/test/fuzztest/samgr_fuzzer/project/${item.configFuzzer}"

    configs = [
      ":sam_fuzz_test_config",
      "${samgr_dir}/services/samgr/native:sam_config",
      "${samgr_dir}/test/resource:coverage_flags",
    ]

    cflags = [
      "-g",
      "-O0",
      "-Wno-unused-variable",
      "-fno-omit-frame-pointer",
    ]
    sources = [
      "${samgr_dir}/services/dfx/source/hisysevent_adapter.cpp",
      "${samgr_dir}/utils/native/source/tools.cpp",
      "${samgr_services_dir}/source/ability_death_recipient.cpp",
      "${samgr_services_dir}/source/collect/device_param_collect.cpp",
      "${samgr_services_dir}/source/collect/device_status_collect_manager.cpp",
      "${samgr_services_dir}/source/collect/device_timed_collect.cpp",
      "${samgr_services_dir}/source/collect/icollect_plugin.cpp",
      "${samgr_services_dir}/source/collect/ref_count_collect.cpp",
      "${samgr_services_dir}/source/memory_guard.cpp",
      "${samgr_services_dir}/source/rpc_callback_imp.cpp",
      "${samgr_services_dir}/source/samgr_time_handler.cpp",
      "${samgr_services_dir}/source/schedule/system_ability_event_handler.cpp",
      "${samgr_services_dir}/source/schedule/system_ability_state_machine.cpp",
      "${samgr_services_dir}/source/schedule/system_ability_state_scheduler.cpp",
      "${samgr_services_dir}/source/base_system_ability_manager.cpp",
    "${samgr_services_dir}/source/system_ability_manager.cpp",
      "${samgr_services_dir}/source/system_ability_manager_dumper.cpp",
      "${samgr_services_dir}/source/system_ability_manager_stub.cpp",
      "${samgr_services_dir}/source/system_ability_manager_util.cpp",
      "${samgr_services_dir}/source/pi_mutex.cpp",
      "${samgr_services_dir}/test/unittest/src/mock_accesstoken_kit.cpp",
      "${samgr_services_dir}/source/ffrt_handler.cpp",
      "${samgr_services_dir}/test/unittest/src/mock_permission.cpp",
      "src/fuzztest_utils.cpp",
      item.source,
    ]
    deps = [ "//foundation/systemabilitymgr/samgr/interfaces/innerkits/dynamic_cache:dynamic_cache" ]

    external_deps = [
      "access_token:libaccesstoken_sdk",
      "c_utils:utils",
      "config_policy:configpolicy_util",
      "ffrt:libffrt",
      "hilog:libhilog",
      "hisysevent:libhisysevent",
      "hitrace:hitrace_meter",
      "init:libbeget_proxy",
      "init:libbegetutil",
      "ipc:ipc_core",
      "ipc:libdbinder",
      "json:nlohmann_json_static",
      "safwk:system_ability_fwk",
      "samgr:samgr_common",
      "samgr:samgr_proxy",
      "qos_manager:qos",
      "qos_manager:concurrent_task_client",
    ]
    defines = []
    if (hicollie_able) {
      external_deps += [ "hicollie:libhicollie" ]
      defines += [ "HICOLLIE_ENABLE" ]
    }

    if (samgr_support_access_token) {
      external_deps += [
        "access_token:libnativetoken_shared",
        "access_token:libtokensetproc_shared",
      ]
      defines += [ "SUPPORT_ACCESS_TOKEN" ]
    }

    if (support_device_manager) {
      sources += [
        "${samgr_services_dir}/source/collect/device_networking_collect.cpp",
      ]
      external_deps += [ "device_manager:devicemanagersdk" ]
      defines += [ "SUPPORT_DEVICE_MANAGER" ]
    }

    if (support_penglai_mode) {
      defines += [ "SUPPORT_PENGLAI_MODE" ]
    }
    defines += ["SAMGR_USE_FFRT"]
  }
}

###############################################################################
group("fuzztest") {
  testonly = true
  deps = []
  deps += [
    # deps file
    ":AddSystemAbilityFuzzTest",
    ":AddSystemProcessFuzzTest",
    ":CancelUnloadSystemAbilityFuzzTest",
    ":CheckRemoteSystemAbilityFuzzTest",
    ":GetExtensionSaIdsFuzzTest",
    ":GetExtensionSaListFuzzTest",
    ":GetOnDemandPolicyFuzzTest",
    ":GetSaExtensionInfoFuzzTest",
    ":GetSystemAbilityFuzzTest",
    ":LoadRemoteSystemAbilityFuzzTest",
    ":UnSubscribeSystemAbilityFuzzTest",
    ":UnSubscribeSystemProcessFuzzTest",
    ":UnloadAllIdleSystemAbilityFuzzTest",
  ]
}
###############################################################################