CMakeLists.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
  2. # Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 2 or (at your option)
  7. # version 3 of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. include_directories(
  17. ${CMAKE_CURRENT_SOURCE_DIR}
  18. ${CMAKE_CURRENT_BINARY_DIR}
  19. ${CMAKE_SOURCE_DIR}/src
  20. ${CMAKE_CURRENT_BINARY_DIR}/../src)
  21. add_definitions(-DQT_TEST_LIB)
  22. set(KEEPASSX_TEST_DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)
  23. configure_file(config-keepassx-tests.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-keepassx-tests.h)
  24. macro(parse_arguments prefix arg_names option_names)
  25. set(DEFAULT_ARGS)
  26. foreach(arg_name ${arg_names})
  27. set(${prefix}_${arg_name})
  28. endforeach(arg_name)
  29. foreach(option ${option_names})
  30. set(${prefix}_${option} FALSE)
  31. endforeach(option)
  32. set(current_arg_name DEFAULT_ARGS)
  33. set(current_arg_list)
  34. foreach(arg ${ARGN})
  35. set(larg_names ${arg_names})
  36. list(FIND larg_names "${arg}" is_arg_name)
  37. if(is_arg_name GREATER -1)
  38. set(${prefix}_${current_arg_name} ${current_arg_list})
  39. set(current_arg_name ${arg})
  40. set(current_arg_list)
  41. else()
  42. set(loption_names ${option_names})
  43. list(FIND loption_names "${arg}" is_option)
  44. if(is_option GREATER -1)
  45. set(${prefix}_${arg} TRUE)
  46. else(is_option GREATER -1)
  47. set(current_arg_list ${current_arg_list} ${arg})
  48. endif()
  49. endif()
  50. endforeach(arg)
  51. set(${prefix}_${current_arg_name} ${current_arg_list})
  52. endmacro(parse_arguments)
  53. macro(add_unit_test)
  54. parse_arguments(TEST "NAME;SOURCES;LIBS;LAUNCHER" "" ${ARGN})
  55. set(_test_NAME ${TEST_NAME})
  56. set(_test_LAUNCHER ${TEST_LAUNCHER})
  57. set(_srcList ${TEST_SOURCES})
  58. add_executable(${_test_NAME} ${_srcList})
  59. target_link_libraries(${_test_NAME} ${TEST_LIBS})
  60. if(NOT TEST_OUTPUT)
  61. set(TEST_OUTPUT plaintext)
  62. endif(NOT TEST_OUTPUT)
  63. set(TEST_OUTPUT ${TEST_OUTPUT} CACHE STRING "The output to generate when running the QTest unit tests")
  64. if(KDE4_TEST_OUTPUT STREQUAL "xml")
  65. add_test(${_test_NAME} ${_test_LAUNCHER} ${_test_NAME} -xml -o ${_test_NAME}.tml)
  66. else(KDE4_TEST_OUTPUT STREQUAL "xml")
  67. add_test(${_test_NAME} ${_test_LAUNCHER} ${_test_NAME})
  68. endif(KDE4_TEST_OUTPUT STREQUAL "xml")
  69. set_tests_properties(${_test_NAME} PROPERTIES ENVIRONMENT "LANG=en_US.UTF-8")
  70. if(NOT MSVC_IDE) #not needed for the ide
  71. # if the tests are EXCLUDE_FROM_ALL, add a target "buildtests" to build all tests
  72. if(NOT WITH_TESTS)
  73. get_directory_property(_buildtestsAdded BUILDTESTS_ADDED)
  74. if(NOT _buildtestsAdded)
  75. add_custom_target(buildtests)
  76. set_directory_properties(PROPERTIES BUILDTESTS_ADDED TRUE)
  77. endif()
  78. add_dependencies(buildtests ${_test_NAME})
  79. endif()
  80. endif()
  81. endmacro(add_unit_test)
  82. set(TEST_LIBRARIES keepassxc_gui Qt5::Test)
  83. set(testsupport_SOURCES
  84. modeltest.cpp
  85. FailDevice.cpp
  86. mock/MockClock.cpp
  87. util/TemporaryFile.cpp)
  88. add_library(testsupport STATIC ${testsupport_SOURCES})
  89. target_link_libraries(testsupport Qt5::Core Qt5::Concurrent Qt5::Widgets Qt5::Test)
  90. add_unit_test(NAME testgroup SOURCES TestGroup.cpp
  91. LIBS testsupport ${TEST_LIBRARIES})
  92. add_unit_test(NAME testkdbx2 SOURCES TestKdbx2.cpp
  93. LIBS ${TEST_LIBRARIES})
  94. add_unit_test(NAME testkdbx3 SOURCES TestKeePass2Format.cpp FailDevice.cpp mock/MockChallengeResponseKey.cpp TestKdbx3.cpp
  95. LIBS testsupport ${TEST_LIBRARIES})
  96. add_unit_test(NAME testkdbx4 SOURCES TestKeePass2Format.cpp FailDevice.cpp mock/MockChallengeResponseKey.cpp TestKdbx4.cpp
  97. LIBS testsupport ${TEST_LIBRARIES})
  98. add_unit_test(NAME testkeys SOURCES TestKeys.cpp mock/MockChallengeResponseKey.cpp
  99. LIBS ${TEST_LIBRARIES})
  100. add_unit_test(NAME testgroupmodel SOURCES TestGroupModel.cpp
  101. LIBS testsupport ${TEST_LIBRARIES})
  102. add_unit_test(NAME testentrymodel SOURCES TestEntryModel.cpp
  103. LIBS testsupport ${TEST_LIBRARIES})
  104. add_unit_test(NAME testcryptohash SOURCES TestCryptoHash.cpp
  105. LIBS ${TEST_LIBRARIES})
  106. add_unit_test(NAME testsymmetriccipher SOURCES TestSymmetricCipher.cpp
  107. LIBS ${TEST_LIBRARIES})
  108. add_unit_test(NAME testhashedblockstream SOURCES TestHashedBlockStream.cpp
  109. LIBS testsupport ${TEST_LIBRARIES})
  110. add_unit_test(NAME testkeepass2randomstream SOURCES TestKeePass2RandomStream.cpp
  111. LIBS ${TEST_LIBRARIES})
  112. add_unit_test(NAME testmodified SOURCES TestModified.cpp
  113. LIBS testsupport ${TEST_LIBRARIES})
  114. add_unit_test(NAME testdeletedobjects SOURCES TestDeletedObjects.cpp
  115. LIBS ${TEST_LIBRARIES})
  116. add_unit_test(NAME testkeepass1reader SOURCES TestKeePass1Reader.cpp
  117. LIBS ${TEST_LIBRARIES})
  118. add_unit_test(NAME testimports SOURCES TestImports.cpp
  119. LIBS ${TEST_LIBRARIES})
  120. if(WITH_XC_NETWORKING)
  121. add_unit_test(NAME testupdatecheck SOURCES TestUpdateCheck.cpp
  122. LIBS ${TEST_LIBRARIES})
  123. add_unit_test(NAME testicondownloader SOURCES TestIconDownloader.cpp LIBS ${TEST_LIBRARIES})
  124. endif()
  125. if(WITH_XC_AUTOTYPE)
  126. add_unit_test(NAME testautotype SOURCES TestAutoType.cpp
  127. LIBS ${TEST_LIBRARIES})
  128. set_target_properties(testautotype PROPERTIES ENABLE_EXPORTS ON)
  129. endif()
  130. if(WITH_XC_SSHAGENT)
  131. add_unit_test(NAME testopensshkey SOURCES TestOpenSSHKey.cpp
  132. LIBS sshagent ${TEST_LIBRARIES})
  133. if(NOT WIN32)
  134. add_unit_test(NAME testsshagent SOURCES TestSSHAgent.cpp
  135. LIBS sshagent ${TEST_LIBRARIES})
  136. endif()
  137. endif()
  138. add_unit_test(NAME testentry SOURCES TestEntry.cpp
  139. LIBS ${TEST_LIBRARIES})
  140. add_unit_test(NAME testmerge SOURCES TestMerge.cpp
  141. LIBS testsupport ${TEST_LIBRARIES})
  142. add_unit_test(NAME testpasswordgenerator SOURCES TestPasswordGenerator.cpp
  143. LIBS ${TEST_LIBRARIES})
  144. add_unit_test(NAME testpasswordhealth SOURCES TestPasswordHealth.cpp
  145. LIBS ${TEST_LIBRARIES})
  146. add_unit_test(NAME testpassphrasegenerator SOURCES TestPassphraseGenerator.cpp
  147. LIBS ${TEST_LIBRARIES})
  148. add_unit_test(NAME testhibp SOURCES TestHibp.cpp
  149. LIBS ${TEST_LIBRARIES})
  150. add_unit_test(NAME testtotp SOURCES TestTotp.cpp
  151. LIBS ${TEST_LIBRARIES})
  152. add_unit_test(NAME testbase32 SOURCES TestBase32.cpp
  153. LIBS ${TEST_LIBRARIES})
  154. add_unit_test(NAME testcsvparser SOURCES TestCsvParser.cpp
  155. LIBS ${TEST_LIBRARIES})
  156. add_unit_test(NAME testrandomgenerator SOURCES TestRandomGenerator.cpp
  157. LIBS testsupport ${TEST_LIBRARIES})
  158. add_unit_test(NAME testentrysearcher SOURCES TestEntrySearcher.cpp
  159. LIBS ${TEST_LIBRARIES})
  160. add_unit_test(NAME testcsvexporter SOURCES TestCsvExporter.cpp
  161. LIBS ${TEST_LIBRARIES})
  162. if(WITH_XC_YUBIKEY)
  163. add_unit_test(NAME testykchallengeresponsekey
  164. SOURCES TestYkChallengeResponseKey.cpp
  165. LIBS ${TEST_LIBRARIES})
  166. endif()
  167. if(WITH_XC_KEESHARE)
  168. add_unit_test(NAME testsharing SOURCES TestSharing.cpp
  169. LIBS testsupport ${TEST_LIBRARIES})
  170. endif()
  171. add_unit_test(NAME testdatabase SOURCES TestDatabase.cpp
  172. LIBS testsupport ${TEST_LIBRARIES})
  173. add_unit_test(NAME testtools SOURCES TestTools.cpp
  174. LIBS ${TEST_LIBRARIES})
  175. add_unit_test(NAME testconfig SOURCES TestConfig.cpp
  176. LIBS testsupport ${TEST_LIBRARIES})
  177. if(WITH_XC_FDOSECRETS)
  178. add_unit_test(NAME testfdosecrets SOURCES TestFdoSecrets.cpp
  179. LIBS testsupport fdosecrets ${TEST_LIBRARIES})
  180. endif()
  181. if(WITH_XC_BROWSER)
  182. add_unit_test(NAME testbrowser SOURCES TestBrowser.cpp
  183. LIBS browser ${TEST_LIBRARIES})
  184. if(WITH_XC_BROWSER_PASSKEYS)
  185. add_unit_test(NAME testpasskeys SOURCES TestPasskeys.cpp
  186. LIBS browser ${TEST_LIBRARIES})
  187. endif()
  188. endif()
  189. if(WITH_XC_NETWORKING OR WITH_XC_BROWSER)
  190. add_unit_test(NAME testurltools SOURCES TestUrlTools.cpp LIBS ${TEST_LIBRARIES})
  191. endif()
  192. add_unit_test(NAME testcli SOURCES TestCli.cpp
  193. LIBS testsupport cli ${ZXCVBN_LIBRARIES} ${TEST_LIBRARIES})
  194. target_compile_definitions(testcli PRIVATE KEEPASSX_CLI_PATH="$<TARGET_FILE:keepassxc-cli>")
  195. if(WITH_GUI_TESTS)
  196. add_subdirectory(gui)
  197. endif(WITH_GUI_TESTS)