load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library")
load("//:helper.bzl", "santa_unit_test")

# Common is the folder containing all the code that's shared between multiple
# components, so every library in this package is visible to the whole group.
package(
    default_visibility = ["//visibility:public"],
)

licenses(["notice"])

proto_library(
    name = "santa_proto",
    srcs = ["santa.proto"],
    deps = [
        "//Source/common/processtree:process_tree_proto",
        "@protobuf//:any_proto",
        "@protobuf//:timestamp_proto",
    ],
)

cc_proto_library(
    name = "santa_cc_proto",
    deps = [":santa_proto"],
)

objc_library(
    name = "PowerMonitor",
    srcs = ["PowerMonitor.mm"],
    hdrs = ["PowerMonitor.h"],
    sdk_frameworks = [
        "IOKit",
    ],
    deps = [
        ":PassKey",
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "PowerMonitorTest",
    srcs = ["PowerMonitorTest.mm"],
    deps = [
        ":PowerMonitor",
    ],
)

objc_library(
    name = "SystemResources",
    srcs = ["SystemResources.mm"],
    hdrs = ["SystemResources.h"],
    deps = [
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "SystemResourcesTest",
    srcs = ["SystemResourcesTest.mm"],
    deps = [
        ":SystemResources",
    ],
)

objc_library(
    name = "Pinning",
    srcs = ["Pinning.mm"],
    hdrs = ["Pinning.h"],
)

objc_library(
    name = "NKeyTokenValidator",
    srcs = ["NKeyTokenValidator.mm"],
    hdrs = ["NKeyTokenValidator.h"],
    deps = [
        ":SNTLogging",
        ":String",
        "@boringssl//:crypto",
        "@nats_c//:nats",
    ],
)

santa_unit_test(
    name = "NKeyTokenValidatorTest",
    srcs = ["NKeyTokenValidatorTest.mm"],
    deps = [
        ":NKeyTokenValidator",
        "@boringssl//:crypto",
    ],
)

objc_library(
    name = "NSData+Zlib",
    srcs = ["NSData+Zlib.mm"],
    hdrs = ["NSData+Zlib.h"],
    deps = [
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "NSDataZlibTest",
    srcs = [
        "NSDataZlibTest.mm",
    ],
    resources = glob(["testdata/compression_test_*.*"]),
    sdk_dylibs = ["libz"],
    deps = [
        ":NSData+Zlib",
    ],
)

objc_library(
    name = "SNTDeepCopy",
    srcs = ["SNTDeepCopy.mm"],
    hdrs = ["SNTDeepCopy.h"],
)

cc_library(
    name = "SantaCache",
    hdrs = ["SantaCache.h"],
    deps = [
        ":BranchPrediction",
        "@abseil-cpp//absl/hash",
    ],
)

santa_unit_test(
    name = "SantaCacheTest",
    srcs = ["SantaCacheTest.mm"],
    deps = [
        ":SantaCache",
    ],
)

cc_library(
    name = "SantaSetCache",
    hdrs = ["SantaSetCache.h"],
    deps = [
        ":SantaCache",
        "@abseil-cpp//absl/container:flat_hash_map",
        "@abseil-cpp//absl/container:flat_hash_set",
        "@abseil-cpp//absl/hash",
        "@abseil-cpp//absl/synchronization",
    ],
)

santa_unit_test(
    name = "SantaSetCacheTest",
    srcs = ["SantaSetCacheTest.mm"],
    deps = [
        ":SantaSetCache",
    ],
)

objc_library(
    name = "AccountLookup",
    srcs = ["AccountLookup.mm"],
    hdrs = ["AccountLookup.h"],
)

santa_unit_test(
    name = "AccountLookupTest",
    srcs = ["AccountLookupTest.mm"],
    deps = [":AccountLookup"],
)

objc_library(
    name = "AuditUtilities",
    srcs = ["AuditUtilities.mm"],
    hdrs = ["AuditUtilities.h"],
    sdk_dylibs = ["bsm"],
    deps = [
        ":ScopedMachPort",
    ],
)

santa_unit_test(
    name = "AuditUtilitiesTest",
    srcs = ["AuditUtilitiesTest.mm"],
    deps = [
        ":AuditUtilities",
        "@abseil-cpp//absl/container:flat_hash_map",
    ],
)

# This target shouldn't be used directly.
# Use a more specific scoped type instead.
cc_library(
    name = "ScopedTypeRef",
    hdrs = ["ScopedTypeRef.h"],
    visibility = ["//Source/common:__pkg__"],
)

# ScopedTypeRef.h is intentionally free of Objective-C and Foundation deps so it
# can be used from pure C++ TUs. This library compiles as C++ (not
# Objective-C++) to keep it so.
cc_library(
    name = "ScopedTypeRefPureCXX",
    testonly = 1,
    srcs = ["ScopedTypeRefPureCXX.cc"],
    visibility = ["//Source/common:__pkg__"],
    deps = [
        ":ScopedTypeRef",
    ],
)

objc_library(
    name = "ScopedCFTypeRef",
    hdrs = ["ScopedCFTypeRef.h"],
    deps = [
        ":ScopedTypeRef",
    ],
)

santa_unit_test(
    name = "ScopedCFTypeRefTest",
    srcs = ["ScopedCFTypeRefTest.mm"],
    sdk_frameworks = [
        "Security",
    ],
    deps = [
        ":ScopedCFTypeRef",
        ":ScopedTypeRefPureCXX",
    ],
)

objc_library(
    name = "ScopedIOObjectRef",
    hdrs = ["ScopedIOObjectRef.h"],
    sdk_frameworks = [
        "IOKit",
    ],
    deps = [
        ":ScopedTypeRef",
    ],
)

santa_unit_test(
    name = "ScopedIOObjectRefTest",
    srcs = ["ScopedIOObjectRefTest.mm"],
    sdk_frameworks = [
        "IOKit",
    ],
    deps = [
        ":ScopedIOObjectRef",
    ],
)

objc_library(
    name = "ScopedMachPort",
    hdrs = ["ScopedMachPort.h"],
    deps = [
        ":ScopedTypeRef",
    ],
)

santa_unit_test(
    name = "ScopedMachPortTest",
    srcs = ["ScopedMachPortTest.mm"],
    deps = [
        ":ScopedMachPort",
    ],
)

objc_library(
    name = "ScopedFile",
    hdrs = ["ScopedFile.h"],
    deps = [
        ":SNTLogging",
        "@abseil-cpp//absl/status:statusor",
    ],
)

santa_unit_test(
    name = "ScopedFileTest",
    srcs = ["ScopedFileTest.mm"],
    deps = [
        ":ScopedFile",
        ":TestUtils",
    ],
)

objc_library(
    name = "BranchPrediction",
    hdrs = ["BranchPrediction.h"],
)

objc_library(
    name = "SantaVnode",
    hdrs = ["SantaVnode.h"],
    deps = [
        "@abseil-cpp//absl/hash",
    ],
)

objc_library(
    name = "Memoizer",
    hdrs = ["Memoizer.h"],
)

santa_unit_test(
    name = "MemoizerTest",
    srcs = ["MemoizerTest.mm"],
    deps = [":Memoizer"],
)

objc_library(
    name = "Platform",
    hdrs = ["Platform.h"],
)

objc_library(
    name = "String",
    hdrs = ["String.h"],
)

objc_library(
    name = "CSOpsHelper",
    srcs = ["CSOpsHelper.mm"],
    hdrs = ["CSOpsHelper.h"],
    sdk_dylibs = ["bsm"],
    deps = [
        ":String",
    ],
)

santa_unit_test(
    name = "CSOpsHelperTest",
    srcs = ["CSOpsHelperTest.mm"],
    deps = [
        ":AuditUtilities",
        ":CSOpsHelper",
        ":String",
    ],
)

objc_library(
    name = "CertificateHelpers",
    srcs = ["CertificateHelpers.mm"],
    hdrs = ["CertificateHelpers.h"],
    module_name = "santa_common_CertificateHelpers",
    deps = [
        ":MOLCertificate",
        ":MOLCodesignChecker",
        ":SNTCommonEnums",
    ],
)

objc_library(
    name = "EncodeEntitlements",
    srcs = ["EncodeEntitlements.mm"],
    hdrs = ["EncodeEntitlements.h"],
    deps = [
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "EncodeEntitlementsTest",
    srcs = ["EncodeEntitlementsTest.mm"],
    deps = [
        ":EncodeEntitlements",
    ],
)

objc_library(
    name = "SigningIDHelpers",
    srcs = ["SigningIDHelpers.mm"],
    hdrs = ["SigningIDHelpers.h"],
    module_name = "santa_common_SigningIDHelpers",
    deps = [
        ":MOLCodesignChecker",
        ":SNTLogging",
    ],
)

objc_library(
    name = "CodeSigningIdentifierUtils",
    srcs = ["CodeSigningIdentifierUtils.mm"],
    hdrs = ["CodeSigningIdentifierUtils.h"],
    deps = [
        ":String",
    ],
)

santa_unit_test(
    name = "CodeSigningIdentifierUtilsTest",
    srcs = ["CodeSigningIdentifierUtilsTest.mm"],
    deps = [
        ":CodeSigningIdentifierUtils",
    ],
)

objc_library(
    name = "RingBuffer",
    hdrs = ["RingBuffer.h"],
    deps = [
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "RingBufferTest",
    srcs = ["RingBufferTest.mm"],
    deps = [":RingBuffer"],
)

objc_library(
    name = "Glob",
    srcs = ["Glob.mm"],
    hdrs = ["Glob.h"],
    deps = [
        ":SNTLogging",
        "@abseil-cpp//absl/cleanup:cleanup",
    ],
)

objc_library(
    name = "Keychain",
    srcs = ["Keychain.mm"],
    hdrs = ["Keychain.h"],
    sdk_frameworks = [
        "Security",
    ],
    deps = [
        ":SNTLogging",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
    ],
)

santa_unit_test(
    name = "KeychainTest",
    srcs = ["KeychainTest.mm"],
    deps = [
        ":Keychain",
        ":TestUtils",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/status:statusor",
    ],
)

objc_library(
    name = "TelemetryEventMap",
    srcs = ["TelemetryEventMap.mm"],
    hdrs = ["TelemetryEventMap.h"],
    deps = [
        ":Platform",
        ":String",
        "@abseil-cpp//absl/container:flat_hash_map",
    ],
)

objc_library(
    name = "PassKey",
    hdrs = ["PassKey.h"],
)

objc_library(
    name = "Timer",
    hdrs = ["Timer.h"],
    deps = [
        ":SNTLogging",
    ],
)

objc_library(
    name = "SNTTimer",
    srcs = ["SNTTimer.mm"],
    hdrs = ["SNTTimer.h"],
    deps = [
        ":SNTLogging",
        ":Timer",
    ],
)

santa_unit_test(
    name = "SNTTimerTest",
    srcs = ["SNTTimerTest.mm"],
    deps = [
        ":SNTTimer",
        ":TestUtils",
        ":Timer",
    ],
)

objc_library(
    name = "SNTXxhash",
    hdrs = ["SNTXxhash.h"],
    deps = [
        "@xxhash",
    ],
)

santa_unit_test(
    name = "SNTXxhashTest",
    srcs = ["SNTXxhashTest.mm"],
    deps = [
        ":SNTXxhash",
        ":String",
        ":TestUtils",
    ],
)

santa_unit_test(
    name = "TelemetryEventMapTest",
    srcs = ["TelemetryEventMapTest.mm"],
    deps = [
        ":Platform",
        ":TelemetryEventMap",
    ],
)

objc_library(
    name = "SNTBlockMessage",
    srcs = ["SNTBlockMessage.mm"],
    hdrs = ["SNTBlockMessage.h"],
    deps = [
        ":SNTConfigurator",
        ":SNTDeviceEvent",
        ":SNTLogging",
        ":SNTStoredExecutionEvent",
        ":SNTStoredFileAccessEvent",
        ":SNTStoredNetworkFlowEvent",
        ":SNTSystemInfo",
    ],
)

objc_library(
    name = "SNTBlockMessage_SantaGUI",
    srcs = ["SNTBlockMessage.mm"],
    hdrs = ["SNTBlockMessage.h"],
    defines = ["SANTAGUI"],
    module_name = "santa_common_SNTBlockMessage",
    deps = [
        ":SNTConfigurator",
        ":SNTDeviceEvent",
        ":SNTLogging",
        ":SNTStoredExecutionEvent",
        ":SNTStoredFileAccessEvent",
        ":SNTStoredNetworkFlowEvent",
        ":SNTSystemInfo",
    ],
)

objc_library(
    name = "SNTCachedDecision",
    srcs = ["SNTCachedDecision.mm"],
    hdrs = ["SNTCachedDecision.h"],
    deps = [
        ":SNTCommonEnums",
        ":SantaVnode",
    ],
)

objc_library(
    name = "SNTDeviceEvent",
    srcs = ["SNTDeviceEvent.mm"],
    hdrs = ["SNTDeviceEvent.h"],
    module_name = "santa_common_SNTDeviceEvent",
    sdk_frameworks = [
        "Foundation",
    ],
    deps = [
        ":CoderMacros",
        ":SNTCommonEnums",
    ],
)

objc_library(
    name = "SNTCommonEnums",
    module_name = "santa_common_SNTCommonEnums",
    textual_hdrs = ["SNTCommonEnums.h"],
)

objc_library(
    name = "SNTLiteDetector",
    srcs = ["SNTLiteDetector.mm"],
    hdrs = ["SNTLiteDetector.h"],
    module_name = "santa_common_SNTLiteDetector",
    deps = [
        ":SNTCommonEnums",
    ],
)

objc_library(
    name = "SNTConfigurator",
    srcs = ["SNTConfigurator.mm"],
    hdrs = ["SNTConfigurator.h"],
    module_name = "santa_common_SNTConfigurator",
    sdk_frameworks = [
        "Foundation",
    ],
    deps = [
        ":NKeyTokenValidator",
        ":Pinning",
        ":SNTCELFallbackRule",
        ":SNTCommonEnums",
        ":SNTExportConfiguration",
        ":SNTLiteDetector",
        ":SNTLogging",
        ":SNTModeTransition",
        ":SNTRule",
        ":SNTStrengthify",
        ":SNTSystemInfo",
        ":SNTTemporaryAdminPolicy",
        "//Source/common/faa:WatchItems",
        "//Source/common/ne:SNTSyncNetworkExtensionSettings",
    ],
)

objc_library(
    name = "SNTConfigBundle",
    srcs = ["SNTConfigBundle.mm"],
    hdrs = ["SNTConfigBundle.h"],
    module_name = "santa_common_SNTConfigBundle",
    deps = [
        ":CoderMacros",
        ":SNTCELFallbackRule",
        ":SNTCommonEnums",
        ":SNTExportConfiguration",
        ":SNTModeTransition",
        ":SNTTemporaryAdminPolicy",
        "//Source/common/ne:SNTSyncNetworkExtensionSettings",
    ],
)

santa_unit_test(
    name = "SNTConfigBundleTest",
    srcs = ["SNTConfigBundleTest.mm"],
    deps = [
        ":SNTCommonEnums",
        ":SNTConfigBundle",
        ":SNTExportConfiguration",
        ":SNTModeTransition",
        ":SNTTemporaryAdminPolicy",
        "//Source/common/ne:SNTSyncNetworkExtensionSettings",
    ],
)

objc_library(
    name = "SNTExportConfiguration",
    srcs = ["SNTExportConfiguration.mm"],
    hdrs = ["SNTExportConfiguration.h"],
    deps = [
        ":CoderMacros",
        ":SNTCommonEnums",
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "SNTExportConfigurationTest",
    srcs = ["SNTExportConfigurationTest.mm"],
    deps = [
        ":SNTExportConfiguration",
    ],
)

objc_library(
    name = "SNTModeTransition",
    srcs = ["SNTModeTransition.mm"],
    hdrs = ["SNTModeTransition.h"],
    deps = [
        ":CoderMacros",
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "SNTModeTransitionTest",
    srcs = ["SNTModeTransitionTest.mm"],
    deps = [
        ":SNTModeTransition",
    ],
)

objc_library(
    name = "SNTTemporaryAdminPolicy",
    srcs = ["SNTTemporaryAdminPolicy.mm"],
    hdrs = ["SNTTemporaryAdminPolicy.h"],
    deps = [
        ":CoderMacros",
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "SNTTemporaryAdminPolicyTest",
    srcs = ["SNTTemporaryAdminPolicyTest.mm"],
    deps = [
        ":SNTTemporaryAdminPolicy",
    ],
)

objc_library(
    name = "SNTKVOManager",
    srcs = ["SNTKVOManager.mm"],
    hdrs = ["SNTKVOManager.h"],
    deps = [
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "SNTKVOManagerTest",
    srcs = ["SNTKVOManagerTest.mm"],
    deps = [
        ":SNTKVOManager",
    ],
)

objc_library(
    name = "SNTDropRootPrivs",
    srcs = ["SNTDropRootPrivs.mm"],
    hdrs = ["SNTDropRootPrivs.h"],
)

objc_library(
    name = "SNTFileInfo",
    srcs = ["SNTFileInfo.mm"],
    hdrs = ["SNTFileInfo.h"],
    module_name = "santa_common_SNTFileInfo",
    deps = [
        ":AccountLookup",
        ":CertificateHelpers",
        ":MOLCodesignChecker",
        ":SNTError",
        ":SNTLogging",
        ":SantaVnode",
        ":SystemResources",
        "@FMDB",
    ],
)

objc_library(
    name = "SNTLogging",
    hdrs = ["SNTLogging.h"],
)

objc_library(
    name = "PrefixTree",
    hdrs = ["PrefixTree.h"],
    deps = [
        ":SNTLogging",
        "@abseil-cpp//absl/synchronization",
    ],
)

objc_library(
    name = "Unit",
    hdrs = ["Unit.h"],
)

objc_library(
    name = "SNTCELFallbackRule",
    srcs = ["SNTCELFallbackRule.mm"],
    hdrs = ["SNTCELFallbackRule.h"],
    sdk_frameworks = [
        "Foundation",
    ],
    deps = [
        ":CoderMacros",
        ":SNTLogging",
    ],
)

santa_unit_test(
    name = "SNTCELFallbackRuleTest",
    srcs = ["SNTCELFallbackRuleTest.mm"],
    deps = [
        ":SNTCELFallbackRule",
    ],
)

objc_library(
    name = "SNTRule",
    srcs = ["SNTRule.mm"],
    hdrs = ["SNTRule.h"],
    sdk_frameworks = [
        "Foundation",
    ],
    deps = [
        ":CoderMacros",
        ":SNTCommonEnums",
        ":SNTError",
        ":SNTSyncConstants",
    ],
)

objc_library(
    name = "SNTFileAccessRule",
    srcs = ["SNTFileAccessRule.mm"],
    hdrs = ["SNTFileAccessRule.h"],
    deps = [
        ":CoderMacros",
    ],
)

objc_library(
    name = "SNTNetworkFlowRule",
    srcs = ["SNTNetworkFlowRule.mm"],
    hdrs = ["SNTNetworkFlowRule.h"],
    deps = [
        ":CoderMacros",
    ],
)

objc_library(
    name = "SNTSignal",
    srcs = ["SNTSignal.mm"],
    hdrs = ["SNTSignal.h"],
    deps = [
        ":CoderMacros",
    ],
)

santa_unit_test(
    name = "SNTNetworkFlowRuleTest",
    srcs = ["SNTNetworkFlowRuleTest.mm"],
    deps = [
        ":SNTNetworkFlowRule",
    ],
)

santa_unit_test(
    name = "SNTRuleTest",
    srcs = ["SNTRuleTest.mm"],
    deps = [
        ":SNTCommonEnums",
        ":SNTError",
        ":SNTRule",
        ":SNTSyncConstants",
    ],
)

objc_library(
    name = "SNTRuleTimeWindow",
    srcs = ["SNTRuleTimeWindow.mm"],
    hdrs = ["SNTRuleTimeWindow.h"],
    module_name = "santa_common_SNTRuleTimeWindow",
    deps = [
        ":CoderMacros",
    ],
)

santa_unit_test(
    name = "SNTRuleTimeWindowTest",
    srcs = ["SNTRuleTimeWindowTest.mm"],
    deps = [
        ":SNTRuleTimeWindow",
    ],
)

objc_library(
    name = "SNTTimedRuleKillDetails",
    srcs = ["SNTTimedRuleKillDetails.mm"],
    hdrs = ["SNTTimedRuleKillDetails.h"],
    module_name = "santa_common_SNTTimedRuleKillDetails",
    deps = [
        ":CoderMacros",
        ":SNTCommonEnums",
        ":SNTRuleTimeWindow",
    ],
)

santa_unit_test(
    name = "SNTTimedRuleKillDetailsTest",
    srcs = ["SNTTimedRuleKillDetailsTest.mm"],
    deps = [
        ":SNTCommonEnums",
        ":SNTRuleTimeWindow",
        ":SNTTimedRuleKillDetails",
    ],
)

objc_library(
    name = "SNTRuleIdentifiers",
    srcs = ["SNTRuleIdentifiers.mm"],
    hdrs = ["SNTRuleIdentifiers.h"],
    deps = [
        ":CoderMacros",
        ":SNTCommonEnums",
        ":String",
    ],
)

objc_library(
    name = "SNTKillCommand",
    srcs = ["SNTKillCommand.mm"],
    hdrs = ["SNTKillCommand.h"],
    deps = [
        ":CodeSigningIdentifierUtils",
        ":CoderMacros",
    ],
)

santa_unit_test(
    name = "SNTKillCommandTest",
    srcs = ["SNTKillCommandTest.mm"],
    deps = [
        ":CodeSigningIdentifierUtils",
        ":SNTKillCommand",
    ],
)

objc_library(
    name = "SNTSandboxExecRequest",
    srcs = ["SNTSandboxExecRequest.mm"],
    hdrs = ["SNTSandboxExecRequest.h"],
    deps = [
        ":CoderMacros",
        ":SNTRuleIdentifiers",
    ],
)

santa_unit_test(
    name = "SNTSandboxExecRequestTest",
    srcs = ["SNTSandboxExecRequestTest.mm"],
    deps = [":SNTSandboxExecRequest"],
)

objc_library(
    name = "CoderMacros",
    hdrs = ["CoderMacros.h"],
)

objc_library(
    name = "SNTStoredEvent",
    srcs = ["SNTStoredEvent.mm"],
    hdrs = ["SNTStoredEvent.h"],
    module_name = "santa_common_SNTStoredEvent",
    deps = [
        ":CoderMacros",
    ],
)

objc_library(
    name = "SNTStoredSignalReport",
    srcs = ["SNTStoredSignalReport.mm"],
    hdrs = ["SNTStoredSignalReport.h"],
    module_name = "santa_common_SNTStoredSignalReport",
    deps = [
        ":CoderMacros",
    ],
)

objc_library(
    name = "SNTStoredExecutionEvent",
    srcs = ["SNTStoredExecutionEvent.mm"],
    hdrs = ["SNTStoredExecutionEvent.h"],
    module_name = "santa_common_SNTStoredExecutionEvent",
    deps = [
        ":CertificateHelpers",
        ":CoderMacros",
        ":MOLCertificate",
        ":MOLCodesignChecker",
        ":SNTCommonEnums",
        ":SNTFileInfo",
        ":SNTStoredEvent",
        ":SigningIDHelpers",
    ],
)

objc_library(
    name = "SNTStoredProcess",
    srcs = ["SNTStoredProcess.mm"],
    hdrs = ["SNTStoredProcess.h"],
    module_name = "santa_common_SNTStoredProcess",
    deps = [
        ":CoderMacros",
        ":MOLCertificate",
    ],
)

objc_library(
    name = "SNTStoredFileAccessEvent",
    srcs = ["SNTStoredFileAccessEvent.mm"],
    hdrs = ["SNTStoredFileAccessEvent.h"],
    module_name = "santa_common_SNTStoredFileAccessEvent",
    deps = [
        ":CertificateHelpers",
        ":CoderMacros",
        ":SNTCommonEnums",
        ":SNTStoredEvent",
        ":SNTStoredProcess",
    ],
)

objc_library(
    name = "SNTTimedSessionAuditEvent",
    srcs = ["SNTTimedSessionAuditEvent.mm"],
    hdrs = ["SNTTimedSessionAuditEvent.h"],
    deps = [
        ":CoderMacros",
        ":SNTStoredEvent",
    ],
)

objc_library(
    name = "SNTStoredTemporaryMonitorModeAuditEvent",
    srcs = ["SNTStoredTemporaryMonitorModeAuditEvent.mm"],
    hdrs = ["SNTStoredTemporaryMonitorModeAuditEvent.h"],
    deps = [
        ":CoderMacros",
        ":SNTTimedSessionAuditEvent",
    ],
)

objc_library(
    name = "SNTStoredTemporaryAdminModeAuditEvent",
    srcs = ["SNTStoredTemporaryAdminModeAuditEvent.mm"],
    hdrs = ["SNTStoredTemporaryAdminModeAuditEvent.h"],
    deps = [
        ":CoderMacros",
        ":SNTTimedSessionAuditEvent",
    ],
)

santa_unit_test(
    name = "SNTStoredEventTest",
    srcs = ["SNTStoredEventTest.mm"],
    deps = [
        ":SNTStoredEvent",
        ":SNTStoredExecutionEvent",
        ":SNTStoredFileAccessEvent",
        ":SNTStoredProcess",
    ],
)

santa_unit_test(
    name = "SNTStoredExecutionEventTest",
    srcs = ["SNTStoredExecutionEventTest.mm"],
    resources = [
        "testdata/signed-with-teamid",
    ],
    structured_resources = glob([
        "testdata/BundleExample.app/**",
    ]),
    deps = [
        ":SNTFileInfo",
        ":SNTStoredExecutionEvent",
    ],
)

santa_unit_test(
    name = "SNTStoredProcessTest",
    srcs = ["SNTStoredProcessTest.mm"],
    deps = [
        ":SNTStoredProcess",
    ],
)

santa_unit_test(
    name = "SNTStoredTemporaryMonitorModeAuditEventTest",
    srcs = ["SNTStoredTemporaryMonitorModeAuditEventTest.mm"],
    deps = [
        ":SNTStoredEvent",
        ":SNTStoredTemporaryMonitorModeAuditEvent",
        ":SNTTimedSessionAuditEvent",
    ],
)

santa_unit_test(
    name = "SNTStoredTemporaryAdminModeAuditEventTest",
    srcs = ["SNTStoredTemporaryAdminModeAuditEventTest.mm"],
    deps = [
        ":SNTStoredTemporaryAdminModeAuditEvent",
    ],
)

objc_library(
    name = "SNTProcessChain",
    srcs = ["SNTProcessChain.mm"],
    hdrs = ["SNTProcessChain.h"],
    deps = [
        ":CoderMacros",
        ":MOLCertificate",
        ":SNTStoredEvent",
    ],
)

santa_unit_test(
    name = "SNTProcessChainTest",
    srcs = ["SNTProcessChainTest.mm"],
    deps = [
        ":MOLCertificate",
        ":SNTProcessChain",
    ],
)

objc_library(
    name = "SNTStoredNetworkFlowEvent",
    srcs = ["SNTStoredNetworkFlowEvent.mm"],
    hdrs = ["SNTStoredNetworkFlowEvent.h"],
    module_name = "santa_common_SNTStoredNetworkFlowEvent",
    deps = [
        ":CoderMacros",
        ":SNTStoredEvent",
        ":SNTStoredProcess",
    ],
)

santa_unit_test(
    name = "SNTStoredNetworkFlowEventTest",
    srcs = ["SNTStoredNetworkFlowEventTest.mm"],
    deps = [
        ":SNTStoredNetworkFlowEvent",
        ":SNTStoredProcess",
    ],
)

objc_library(
    name = "SNTStoredNetworkMountEvent",
    srcs = ["SNTStoredNetworkMountEvent.mm"],
    hdrs = ["SNTStoredNetworkMountEvent.h"],
    module_name = "santa_common_SNTStoredNetworkMountEvent",
    deps = [
        ":CoderMacros",
        ":SNTProcessChain",
        ":SNTStoredEvent",
    ],
)

santa_unit_test(
    name = "SNTStoredNetworkMountEventTest",
    srcs = ["SNTStoredNetworkMountEventTest.mm"],
    deps = [
        ":SNTProcessChain",
        ":SNTStoredEvent",
        ":SNTStoredNetworkMountEvent",
    ],
)

objc_library(
    name = "SNTStoredUSBMountEvent",
    srcs = ["SNTStoredUSBMountEvent.mm"],
    hdrs = ["SNTStoredUSBMountEvent.h"],
    module_name = "santa_common_SNTStoredUSBMountEvent",
    deps = [
        ":CoderMacros",
        ":SNTStoredEvent",
    ],
)

santa_unit_test(
    name = "SNTStoredUSBMountEventTest",
    srcs = ["SNTStoredUSBMountEventTest.mm"],
    deps = [
        ":CoderMacros",
        ":SNTStoredEvent",
        ":SNTStoredUSBMountEvent",
    ],
)

objc_library(
    name = "SNTConfigState",
    srcs = ["SNTConfigState.mm"],
    hdrs = ["SNTConfigState.h"],
    module_name = "santa_common_SNTConfigState",
    deps = [
        ":CoderMacros",
        ":SNTCommonEnums",
        ":SNTConfigurator",
    ],
)

cc_library(
    name = "SNTStrengthify",
    hdrs = ["SNTStrengthify.h"],
)

objc_library(
    name = "SNTSyncConstants",
    srcs = ["SNTSyncConstants.mm"],
    hdrs = ["SNTSyncConstants.h"],
    sdk_frameworks = [
        "Foundation",
    ],
)

objc_library(
    name = "SNTSystemInfo",
    srcs = ["SNTSystemInfo.mm"],
    hdrs = ["SNTSystemInfo.h"],
    sdk_frameworks = [
        "Foundation",
        "IOKit",
    ],
    deps = [
        ":SNTCommonEnums",
        ":SNTFileInfo",
    ],
)

objc_library(
    name = "SNTSIPStatus",
    srcs = ["SNTSIPStatus.mm"],
    hdrs = ["SNTSIPStatus.h"],
    sdk_frameworks = [
        "Foundation",
    ],
    deps = [
        ":SNTLogging",
    ],
)

objc_library(
    name = "SNTXPCBundleServiceInterface",
    srcs = ["SNTXPCBundleServiceInterface.mm"],
    hdrs = ["SNTXPCBundleServiceInterface.h"],
    deps = [
        ":MOLXPCConnection",
        ":SNTStoredEvent",
        ":SNTStoredExecutionEvent",
    ],
)

objc_library(
    name = "SNTXPCMetricServiceInterface",
    srcs = ["SNTXPCMetricServiceInterface.mm"],
    hdrs = ["SNTXPCMetricServiceInterface.h"],
    deps = [
        ":MOLXPCConnection",
    ],
)

objc_library(
    name = "SNTXPCControlInterface",
    srcs = ["SNTXPCControlInterface.mm"],
    hdrs = ["SNTXPCControlInterface.h"],
    defines = select({
        "//:adhoc_build": ["SANTAADHOC"],
        "//conditions:default": None,
    }),
    deps = [
        ":MOLCodesignChecker",
        ":MOLXPCConnection",
        ":SNTCommonEnums",
        ":SNTConfigBundle",
        ":SNTConfigurator",
        ":SNTFileAccessRule",
        ":SNTKillCommand",
        ":SNTNetworkFlowRule",
        ":SNTRule",
        ":SNTRuleIdentifiers",
        ":SNTSignal",
        ":SNTStoredEvent",
        ":SNTStoredExecutionEvent",
        ":SNTStoredSignalReport",
        ":SNTXPCUnprivilegedControlInterface",
        "//Source/common/ne:SNTNetworkExtensionSettings",
        "@santanetd//src/santanetd:SNDFlowInfo",
        "@santanetd//src/santanetd:SNDNetworkFlowDecision",
        "@santanetd//src/santanetd:SNDProcessFlows",
        "@santanetd//src/santanetd:SNDProcessInfo",
    ],
)

objc_library(
    name = "SNTXPCNotifierInterface",
    srcs = ["SNTXPCNotifierInterface.mm"],
    hdrs = ["SNTXPCNotifierInterface.h"],
    deps = [
        ":SNTCommonEnums",
        ":SNTConfigBundle",
        ":SNTConfigState",
        ":SNTXPCBundleServiceInterface",
    ],
)

objc_library(
    name = "SNTMetricSet",
    srcs = ["SNTMetricSet.mm"],
    hdrs = ["SNTMetricSet.h"],
    deps = [":SNTCommonEnums"],
)

objc_library(
    name = "SNTXPCSyncServiceInterface",
    srcs = ["SNTXPCSyncServiceInterface.mm"],
    hdrs = ["SNTXPCSyncServiceInterface.h"],
    module_name = "santa_common_SNTXPCSyncServiceInterface",
    deps = [
        ":MOLCertificate",
        ":MOLXPCConnection",
        ":SNTCommonEnums",
        ":SNTExportConfiguration",
        ":SNTStoredEvent",
        ":SNTStoredExecutionEvent",
        ":SNTStoredFileAccessEvent",
        ":SNTStoredNetworkFlowEvent",
        ":SNTStoredSignalReport",
    ],
)

objc_library(
    name = "SNTXPCUnprivilegedControlInterface",
    srcs = ["SNTXPCUnprivilegedControlInterface.mm"],
    hdrs = ["SNTXPCUnprivilegedControlInterface.h"],
    deps = [
        ":MOLCertificate",
        ":MOLXPCConnection",
        ":SNTCommonEnums",
        ":SNTRule",
        ":SNTRuleIdentifiers",
        ":SNTStoredEvent",
        ":SNTStoredExecutionEvent",
        ":SNTXPCBundleServiceInterface",
        ":SantaVnode",
        "//Source/common/faa:WatchItems",
    ],
)

objc_library(
    name = "SNTError",
    srcs = ["SNTError.mm"],
    hdrs = ["SNTError.h"],
)

santa_unit_test(
    name = "SNTFileInfoTest",
    srcs = ["SNTFileInfoTest.mm"],
    resources = [
        "testdata/32bitplist",
        "testdata/bad_pagezero",
        "testdata/cal-yikes-universal",
        "testdata/missing_pagezero",
        "testdata/yikes-universal_adhoc",
    ],
    structured_resources = glob([
        "testdata/BundleExample.app/**",
        "testdata/DirectoryBundle/**",
    ]),
    deps = [
        ":SNTError",
        ":SNTFileInfo",
        ":SystemResources",
    ],
)

santa_unit_test(
    name = "PrefixTreeTest",
    srcs = ["PrefixTreeTest.mm"],
    deps = [
        ":PrefixTree",
        ":Unit",
    ],
)

santa_unit_test(
    name = "SNTMetricSetTest",
    srcs = ["SNTMetricSetTest.mm"],
    deps = [":SNTMetricSet"],
)

santa_unit_test(
    name = "SNTCachedDecisionTest",
    srcs = ["SNTCachedDecisionTest.mm"],
    deps = [
        ":SNTCachedDecision",
        ":TestUtils",
        "@OCMock",
    ],
)

santa_unit_test(
    name = "SNTBlockMessageTest",
    srcs = ["SNTBlockMessageTest.mm"],
    sdk_frameworks = [
        "AppKit",
    ],
    deps = [
        ":SNTBlockMessage_SantaGUI",
        ":SNTConfigurator",
        ":SNTStoredExecutionEvent",
        ":SNTStoredFileAccessEvent",
        ":SNTStoredNetworkFlowEvent",
        ":SNTSystemInfo",
        "@OCMock",
    ],
)

santa_unit_test(
    name = "SNTConfiguratorTest",
    srcs = ["SNTConfiguratorTest.mm"],
    deps = [
        ":SNTCommonEnums",
        ":SNTConfigurator",
        "@OCMock",
    ],
)

test_suite(
    name = "unit_tests",
    tests = [
        ":AccountLookupTest",
        ":AuditUtilitiesTest",
        ":CSOpsHelperTest",
        ":CodeSigningIdentifierUtilsTest",
        ":EncodeEntitlementsTest",
        ":KeychainTest",
        ":MOLAuthenticatingURLSessionTest",
        ":MOLCertificateTest",
        ":MOLCodesignCheckerTest",
        ":MOLXPCConnectionTest",
        ":MemoizerTest",
        ":NKeyTokenValidatorTest",
        ":NSDataZlibTest",
        ":PowerMonitorTest",
        ":PrefixTreeTest",
        ":RingBufferTest",
        ":SNTBlockMessageTest",
        ":SNTCELFallbackRuleTest",
        ":SNTCachedDecisionTest",
        ":SNTConfigBundleTest",
        ":SNTConfiguratorTest",
        ":SNTExportConfigurationTest",
        ":SNTFileInfoTest",
        ":SNTKVOManagerTest",
        ":SNTKillCommandTest",
        ":SNTMetricSetTest",
        ":SNTModeTransitionTest",
        ":SNTNetworkFlowRuleTest",
        ":SNTProcessChainTest",
        ":SNTRuleTest",
        ":SNTRuleTimeWindowTest",
        ":SNTSandboxExecRequestTest",
        ":SNTStoredEventTest",
        ":SNTStoredExecutionEventTest",
        ":SNTStoredNetworkFlowEventTest",
        ":SNTStoredNetworkMountEventTest",
        ":SNTStoredProcessTest",
        ":SNTStoredTemporaryAdminModeAuditEventTest",
        ":SNTStoredTemporaryMonitorModeAuditEventTest",
        ":SNTStoredUSBMountEventTest",
        ":SNTTemporaryAdminPolicyTest",
        ":SNTTimedRuleKillDetailsTest",
        ":SNTTimerTest",
        ":SNTXxhashTest",
        ":SantaCacheTest",
        ":SantaSetCacheTest",
        ":ScopedCFTypeRefTest",
        ":ScopedFileTest",
        ":ScopedIOObjectRefTest",
        ":ScopedMachPortTest",
        ":SystemResourcesTest",
        ":TelemetryEventMapTest",
        "//Source/common/cel:ArenaGrowthTest",
        "//Source/common/cel:CELPlanCacheTest",
        "//Source/common/cel:CELTest",
        "//Source/common/faa:unit_tests",
        "//Source/common/ne:unit_tests",
    ],
    visibility = ["//:santa_package_group"],
)

objc_library(
    name = "TestUtils",
    testonly = 1,
    srcs = ["TestUtils.mm"],
    hdrs = ["TestUtils.h"],
    sdk_dylibs = [
        "bsm",
    ],
    deps = [
        ":Platform",
        ":SystemResources",
        "@OCMock",
        "@googletest//:gtest",
    ],
)

genrule(
    name = "CommitHash",
    outs = ["CommitHash.plist"],
    cmd = """
    STABLE_GIT_COMMIT=$$(awk '/STABLE_GIT_COMMIT/ { print $$2 }' bazel-out/stable-status.txt)
    defaults write $${PWD}/$(@) SNTCommitHash -string $${STABLE_GIT_COMMIT:-unset}
    """,
    local = True,
    message = "Generating CommitHash.plist",
    stamp = 1,
)

objc_library(
    name = "MOLCertificate",
    srcs = ["MOLCertificate.mm"],
    hdrs = ["MOLCertificate.h"],
    sdk_frameworks = [
        "Foundation",
        "Security",
    ],
)

santa_unit_test(
    name = "MOLCertificateTest",
    srcs = ["MOLCertificateTest.mm"],
    resources = glob([
        "testdata/*.pem",
        "testdata/*.crt",
    ]),
    deps = [":MOLCertificate"],
)

objc_library(
    name = "MOLCodesignChecker",
    srcs = ["MOLCodesignChecker.mm"],
    hdrs = ["MOLCodesignChecker.h"],
    module_name = "santa_common_MOLCodesignChecker",
    sdk_frameworks = [
        "Foundation",
        "Security",
    ],
    deps = [
        ":MOLCertificate",
        "//Source/common:ScopedCFTypeRef",
        "//Source/common:String",
    ],
)

santa_unit_test(
    name = "MOLCodesignCheckerTest",
    srcs = ["MOLCodesignCheckerTest.mm"],
    resources = [
        "testdata/cal-yikes-universal",
        "testdata/cal-yikes-universal_adhoc",
        "testdata/cal-yikes-universal_signed",
        "testdata/signed-with-teamid",
        "testdata/yikes-universal",
        "testdata/yikes-universal_adhoc",
        "testdata/yikes-universal_signed",
    ],
    deps = [":MOLCodesignChecker"],
)

objc_library(
    name = "MOLXPCConnection",
    srcs = ["MOLXPCConnection.mm"],
    hdrs = ["MOLXPCConnection.h"],
    module_name = "santa_common_MOLXPCConnection",
    sdk_frameworks = ["Security"],
    deps = [
        ":ScopedCFTypeRef",
    ],
)

santa_unit_test(
    name = "MOLXPCConnectionTest",
    srcs = ["MOLXPCConnectionTest.mm"],
    deps = [
        ":MOLXPCConnection",
        "@OCMock",
    ],
)

objc_library(
    name = "MOLDERDecoder",
    srcs = ["MOLDERDecoder.mm"],
    hdrs = ["MOLDERDecoder.h"],
    sdk_frameworks = ["Security"],
)

objc_library(
    name = "MOLAuthenticatingURLSession",
    srcs = ["MOLAuthenticatingURLSession.mm"],
    hdrs = ["MOLAuthenticatingURLSession.h"],
    sdk_frameworks = ["Security"],
    deps = [
        ":MOLCertificate",
        ":MOLDERDecoder",
        ":ScopedCFTypeRef",
    ],
)

santa_unit_test(
    name = "MOLAuthenticatingURLSessionTest",
    srcs = [
        "MOLAuthenticatingURLSessionTest.mm",
        "MOLDERDecoderTest.mm",
    ],
    resources = [
        "testdata/dn.plist",
        "testdata/example_org_client_cert.pem",
        "testdata/example_org_client_cert_old.pem",
        "testdata/internet_widgits_client_cert.pem",
    ],
    deps = [
        ":MOLAuthenticatingURLSession",
        ":MOLDERDecoder",
    ],
)
