diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 4ff7122dd..e47a4f3d0 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,13 +2,6 @@ - - - - - - - diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 4d829b6cd..0878ed1f3 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -42,6 +42,8 @@ namespace hex { void openWebpage(std::string url); [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) { + if (from < to) std::swap(from, to); + using ValueType = std::remove_cvref_t; ValueType mask = (std::numeric_limits::max() >> (((sizeof(value) * 8) - 1) - (from - to))) << to; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 711a0ee61..6d96a1d21 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,8 +1,10 @@ project(unit_tests) -add_subdirectory(pattern_language) -add_subdirectory(algorithms) +add_custom_target(unit_tests) +add_subdirectory(common) + +add_subdirectory(helpers) +add_subdirectory(algorithms) +add_subdirectory(pattern_language) + -add_custom_target(unit_tests - DEPENDS pattern_language_tests algorithms_test -) diff --git a/tests/algorithms/CMakeLists.txt b/tests/algorithms/CMakeLists.txt index 11641dc3f..476f0f06a 100644 --- a/tests/algorithms/CMakeLists.txt +++ b/tests/algorithms/CMakeLists.txt @@ -1,16 +1,10 @@ cmake_minimum_required(VERSION 3.16) project(algorithms_test) - +set(TEST_CATEGORY Algorithms) # Add new tests here # set(AVAILABLE_TESTS - # Common - TestSucceeding - TestFailing - TestProvider_read - TestProvider_write - # Endian 32BitIntegerEndianSwap 64BitFloatEndianSwap @@ -33,18 +27,20 @@ set(AVAILABLE_TESTS ) -add_executable(algorithms_test - source/main.cpp - - source/common.cpp +add_executable(${PROJECT_NAME} source/endian.cpp source/crypto.cpp ) -target_include_directories(algorithms_test PRIVATE include) -target_link_libraries(algorithms_test libimhex) -set_target_properties(algorithms_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + +# ---- No need to change anything from here downwards unless you know what you're doing ---- # + +target_include_directories(${PROJECT_NAME} PRIVATE include) +target_link_libraries(${PROJECT_NAME} libimhex tests_common) + +set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) foreach (test IN LISTS AVAILABLE_TESTS) - add_test(NAME "Algorithms/${test}" COMMAND algorithms_test "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) + add_test(NAME "${TEST_CATEGORY}/${test}" COMMAND ${PROJECT_NAME} "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) endforeach () +add_dependencies(unit_tests ${PROJECT_NAME}) \ No newline at end of file diff --git a/tests/algorithms/source/crypto.cpp b/tests/algorithms/source/crypto.cpp index 4b0658096..9f277e35a 100644 --- a/tests/algorithms/source/crypto.cpp +++ b/tests/algorithms/source/crypto.cpp @@ -1,7 +1,7 @@ #include #include "hex/helpers/logger.hpp" -#include "test_provider.hpp" -#include "tests.hpp" +#include +#include #include #include diff --git a/tests/algorithms/source/endian.cpp b/tests/algorithms/source/endian.cpp index e025b06c3..0b354772c 100644 --- a/tests/algorithms/source/endian.cpp +++ b/tests/algorithms/source/endian.cpp @@ -1,6 +1,6 @@ #include -#include "test_provider.hpp" -#include "tests.hpp" +#include +#include TEST_SEQUENCE("32BitIntegerEndianSwap") { TEST_ASSERT(hex::changeEndianess(0xAABBCCDD, std::endian::big) == 0xDDCCBBAA); diff --git a/tests/common/CMakeLists.txt b/tests/common/CMakeLists.txt new file mode 100644 index 000000000..0b25d5123 --- /dev/null +++ b/tests/common/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.16) + +project(tests_common) + +add_library(tests_common STATIC + source/main.cpp +) +target_include_directories(tests_common PUBLIC include) +target_link_libraries(tests_common PUBLIC libimhex) \ No newline at end of file diff --git a/tests/algorithms/include/test_provider.hpp b/tests/common/include/hex/test/test_provider.hpp similarity index 100% rename from tests/algorithms/include/test_provider.hpp rename to tests/common/include/hex/test/test_provider.hpp diff --git a/tests/algorithms/include/tests.hpp b/tests/common/include/hex/test/tests.hpp similarity index 98% rename from tests/algorithms/include/tests.hpp rename to tests/common/include/hex/test/tests.hpp index 1315d77d4..1b68fa287 100644 --- a/tests/algorithms/include/tests.hpp +++ b/tests/common/include/hex/test/tests.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include diff --git a/tests/algorithms/source/main.cpp b/tests/common/source/main.cpp similarity index 97% rename from tests/algorithms/source/main.cpp rename to tests/common/source/main.cpp index 8c2b75bdd..66e0e4512 100644 --- a/tests/algorithms/source/main.cpp +++ b/tests/common/source/main.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "tests.hpp" +#include #include diff --git a/tests/helpers/CMakeLists.txt b/tests/helpers/CMakeLists.txt new file mode 100644 index 000000000..abb271b9b --- /dev/null +++ b/tests/helpers/CMakeLists.txt @@ -0,0 +1,47 @@ +cmake_minimum_required(VERSION 3.16) + +project(helpers_test) +set(TEST_CATEGORY Helpers) + +# Add new tests here # +set(AVAILABLE_TESTS + # Common + TestSucceeding + TestFailing + TestProvider_read + TestProvider_write + + # Net + #StoreAPI + #TipsAPI + #ContentAPI + + # File + FileAccess + + # Utils + SplitStringAtChar + SplitStringAtString + ExtractBits +) + + +add_executable(${PROJECT_NAME} + source/common.cpp + source/file.cpp + source/net.cpp + source/utils.cpp +) + + +# ---- No need to change anything from here downwards unless you know what you're doing ---- # + +target_include_directories(${PROJECT_NAME} PRIVATE include) +target_link_libraries(${PROJECT_NAME} libimhex tests_common) + +set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + +foreach (test IN LISTS AVAILABLE_TESTS) + add_test(NAME "${TEST_CATEGORY}/${test}" COMMAND ${PROJECT_NAME} "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +endforeach () +add_dependencies(unit_tests ${PROJECT_NAME}) \ No newline at end of file diff --git a/tests/algorithms/source/common.cpp b/tests/helpers/source/common.cpp similarity index 97% rename from tests/algorithms/source/common.cpp rename to tests/helpers/source/common.cpp index c799b1ae0..e912b048f 100644 --- a/tests/algorithms/source/common.cpp +++ b/tests/helpers/source/common.cpp @@ -1,9 +1,10 @@ -#include -#include "test_provider.hpp" -#include "tests.hpp" +#include +#include + +#include -#include #include +#include TEST_SEQUENCE("TestSucceeding") { TEST_SUCCESS(); diff --git a/tests/helpers/source/file.cpp b/tests/helpers/source/file.cpp new file mode 100644 index 000000000..03260ba7d --- /dev/null +++ b/tests/helpers/source/file.cpp @@ -0,0 +1,42 @@ +#include + +#include +#include + +using namespace std::literals::string_literals; + +TEST_SEQUENCE("FileAccess") { + const auto FilePath = hex::fs::current_path() / "file.txt"; + const auto FileContent = "Hello World"; + + { + hex::File file(FilePath, hex::File::Mode::Create); + TEST_ASSERT(file.isValid()); + + file.write(FileContent); + } + + { + hex::File file(FilePath, hex::File::Mode::Read); + TEST_ASSERT(file.isValid()); + + TEST_ASSERT(file.readString() == FileContent); + } + + { + hex::File file(FilePath, hex::File::Mode::Write); + TEST_ASSERT(file.isValid()); + + + file.remove(); + TEST_ASSERT(!file.isValid()); + } + + { + hex::File file(FilePath, hex::File::Mode::Read); + if (file.isValid()) + TEST_FAIL(); + } + + TEST_SUCCESS(); +}; \ No newline at end of file diff --git a/tests/helpers/source/net.cpp b/tests/helpers/source/net.cpp new file mode 100644 index 000000000..4c746aa05 --- /dev/null +++ b/tests/helpers/source/net.cpp @@ -0,0 +1,54 @@ +#include + +#include +#include +#include + +using namespace std::literals::string_literals; + +TEST_SEQUENCE("StoreAPI") { + hex::Net net; + + auto result = net.getString(ImHexApiURL + "/store"s).get(); + + if (result.code != 200) + TEST_FAIL(); + + if (result.body.empty()) + TEST_FAIL(); + + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("TipsAPI") { + hex::Net net; + + auto result = net.getString(ImHexApiURL + "/tip"s).get(); + + if (result.code != 200) + TEST_FAIL(); + + if (result.body.empty()) + TEST_FAIL(); + + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("ContentAPI") { + hex::Net net; + + const auto FilePath = hex::fs::current_path() / "elf.hexpat"; + + auto result = net.downloadFile("https://api.werwolv.net/content/imhex/patterns/elf.hexpat", FilePath).get(); + + TEST_ASSERT(result.code == 200); + + hex::File file(FilePath, hex::File::Mode::Read); + if (!file.isValid()) + TEST_FAIL(); + + if (file.getSize() == 0) + TEST_FAIL(); + + TEST_SUCCESS(); +}; \ No newline at end of file diff --git a/tests/helpers/source/utils.cpp b/tests/helpers/source/utils.cpp new file mode 100644 index 000000000..51b195228 --- /dev/null +++ b/tests/helpers/source/utils.cpp @@ -0,0 +1,32 @@ +#include + +#include + +using namespace std::literals::string_literals; + +TEST_SEQUENCE("SplitStringAtChar") { + const std::string TestString = "Hello|World|ABCD|Test|"; + const std::vector TestSplitVector = { "Hello", "World", "ABCD", "Test", "" }; + + TEST_ASSERT(hex::splitString(TestString, "|") == TestSplitVector); + + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("SplitStringAtString") { + const std::string TestString = "Hello|DELIM|World|DELIM|ABCD|DELIM|Test|DELIM|"; + const std::vector TestSplitVector = { "Hello", "World", "ABCD", "Test", "" }; + + TEST_ASSERT(hex::splitString(TestString, "|DELIM|") == TestSplitVector); + + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("ExtractBits") { + TEST_ASSERT(hex::extract(11, 4, 0xAABBU) == 0xAB); + TEST_ASSERT(hex::extract(15, 0, 0xAABBU) == 0xAABB); + TEST_ASSERT(hex::extract(35, 20, 0x8899AABBCCDDEEFFU) == 0xBCCD); + TEST_ASSERT(hex::extract(20, 35, 0x8899AABBCCDDEEFFU) == 0xBCCD); + + TEST_SUCCESS(); +}; \ No newline at end of file diff --git a/tests/pattern_language/CMakeLists.txt b/tests/pattern_language/CMakeLists.txt index 10c89e8e3..be58ffe8d 100644 --- a/tests/pattern_language/CMakeLists.txt +++ b/tests/pattern_language/CMakeLists.txt @@ -22,7 +22,14 @@ set(AVAILABLE_TESTS ) -add_executable(pattern_language_tests source/main.cpp source/tests.cpp) +add_executable(pattern_language_tests + source/main.cpp + source/tests.cpp +) + + +# ---- No need to change anything from here downwards unless you know what you're doing ---- # + target_include_directories(pattern_language_tests PRIVATE include) target_link_libraries(pattern_language_tests libimhex) @@ -34,4 +41,5 @@ add_custom_command(TARGET pattern_language_tests foreach (test IN LISTS AVAILABLE_TESTS) add_test(NAME "PatternLanguage/${test}" COMMAND pattern_language_tests "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) -endforeach () \ No newline at end of file +endforeach () +add_dependencies(unit_tests ${PROJECT_NAME}) \ No newline at end of file