#  Copyright (C) 2026 ChiPass Team <contact@chipass.org>
#  Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
#  Copyright (C) 2010, 2011 Felix Geyer <debfx@fobos.de>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 or (at your option)
#  version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_BINARY_DIR}
)

configure_file(app-config-tests.h.in ${CMAKE_CURRENT_BINARY_DIR}/app-config-tests.h)

add_library(ChiPass_test STATIC
  modeltest.cpp
  FailDevice.cpp
  mock/MockClock.cpp
  util/TemporaryFile.cpp
)
target_link_libraries(ChiPass_test PUBLIC
  ChiPass_all

  Qt6::Test
)

function (add_unit_test name)
  add_executable(test${name} ${ARGN})
  target_link_libraries(test${name} PRIVATE ChiPass_test)

  add_test(${name} test${name})
endfunction()

add_unit_test(group
  TestGroup.cpp
)

add_unit_test(kdbx2
  TestKdbx2.cpp
)

add_unit_test(kdbx3
  TestKeePass2Format.cpp
  FailDevice.cpp
  mock/MockChallengeResponseKey.cpp
  TestKdbx3.cpp
)

add_unit_test(kdbx4
  TestKeePass2Format.cpp
  FailDevice.cpp
  mock/MockChallengeResponseKey.cpp
  TestKdbx4.cpp
)

add_unit_test(keys
  TestKeys.cpp
  mock/MockChallengeResponseKey.cpp
)

add_unit_test(groupmodel
  TestGroupModel.cpp
)

add_unit_test(entrymodel
  TestEntryModel.cpp
)

add_unit_test(cryptohash
  TestCryptoHash.cpp
)

add_unit_test(symmetriccipher
  TestSymmetricCipher.cpp
)

add_unit_test(hashedblockstream
  TestHashedBlockStream.cpp
)

add_unit_test(keepass2randomstream
  TestKeePass2RandomStream.cpp
)

add_unit_test(modified
  TestModified.cpp
)

add_unit_test(deletedobjects
  TestDeletedObjects.cpp
)

add_unit_test(keepass1reader
  TestKeePass1Reader.cpp
)

add_unit_test(imports
  TestImports.cpp
)

add_unit_test(entry
  TestEntry.cpp
)

add_unit_test(merge
  TestMerge.cpp
)

add_unit_test(passwordgenerator
  TestPasswordGenerator.cpp
)

add_unit_test(passwordhealth
  TestPasswordHealth.cpp
)

add_unit_test(passphrasegenerator
  TestPassphraseGenerator.cpp
)

add_unit_test(hibp
  TestHibp.cpp
)

add_unit_test(totp
  TestTotp.cpp
)

add_unit_test(base32
  TestBase32.cpp
)

add_unit_test(csvparser
  TestCsvParser.cpp
)

add_unit_test(randomgenerator
  TestRandomGenerator.cpp
)

add_unit_test(entrysearcher
  TestEntrySearcher.cpp
)

add_unit_test(csvexporter
  TestCsvExporter.cpp
)

add_unit_test(database
  TestDatabase.cpp
)

add_unit_test(tools
  TestTools.cpp)

add_unit_test(config
  TestConfig.cpp
)

if (CHIPASS_ENABLE_AUTOTYPE)
  add_unit_test(autotype
    TestAutoType.cpp
  )
endif()

if (CHIPASS_ENABLE_BROWSER)
  add_unit_test(browser
    TestBrowser.cpp
  )

  if (CHIPASS_ENABLE_BROWSER_PASSKEYS)
    add_unit_test(passkeys
      TestPasskeys.cpp
    )
  endif()
endif()

if (CHIPASS_ENABLE_FDOSECRETS)
  add_unit_test(fdosecrets
    TestFdoSecrets.cpp
  )
endif()

if (CHIPASS_ENABLE_KEESHARE)
  add_unit_test(sharing
    TestSharing.cpp
  )
endif()

if (CHIPASS_ENABLE_NETWORKING OR CHIPASS_ENABLE_BROWSER)
  add_unit_test(urltools
    TestUrlTools.cpp
  )
endif()

if (CHIPASS_ENABLE_NETWORKING)
  add_unit_test(updatecheck
    TestUpdateCheck.cpp
  )

  add_unit_test(icondownloader
    TestIconDownloader.cpp
  )
endif()

if (CHIPASS_ENABLE_SSHAGENT)
  add_unit_test(opensshkey
    TestOpenSSHKey.cpp
  )
  if (NOT WIN32)
    add_unit_test(sshagent
      TestSSHAgent.cpp
    )
  endif()
endif()

if (CHIPASS_ENABLE_YUBIKEY)
  add_unit_test(ykchallengeresponsekey
    TestYkChallengeResponseKey.cpp
  )
endif()

add_unit_test(cli
  TestCli.cpp
)
target_link_libraries(testcli PRIVATE
  ChiPass_cli
)
target_compile_definitions(testcli PRIVATE
  CHIPASS_CLI_PATH="$<TARGET_FILE:ChiPass-cli>"
)

if (CHIPASS_ENABLE_GUI_TESTS)
  add_unit_test(gui
    gui/TestGui.cpp
  )
  add_unit_test(guipixmaps
    gui/TestGuiPixmaps.cpp
  )

  if(CHIPASS_ENABLE_BROWSER)
    add_unit_test(guibrowser
      gui/TestGuiBrowser.cpp
    )
  endif()

  if(CHIPASS_ENABLE_FDOSECRETS)
    add_executable(testguifdosecrets
      gui/TestGuiFdoSecrets.cpp
      util/FdoSecretsProxy.cpp
    )
    target_link_libraries(testguifdosecrets PRIVATE ChiPass_test)

    add_test(guifdosecrets
      dbus-run-session --config-file ${CMAKE_CURRENT_SOURCE_DIR}/data/dbus/session.conf --
      sh -c "exec $0" ${CMAKE_CURRENT_BINARY_DIR}/testguifdosecrets
    )
  endif()
endif()
