1
0
mirror of synced 2025-01-18 00:56:49 +01:00

tests: Improved testing environment, added helpers tests

This commit is contained in:
WerWolv 2022-01-29 17:14:15 +01:00
parent da16be7ad8
commit e6f1dd0490
16 changed files with 225 additions and 38 deletions

7
.idea/vcs.xml generated
View File

@ -2,13 +2,6 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/capstone" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/curl" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/fmt" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/libromfs" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/nativefiledialog" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/xdgpp" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/yara/yara" vcs="Git" />
<mapping directory="$PROJECT_DIR$/lib/external/capstone" vcs="Git" />
<mapping directory="$PROJECT_DIR$/lib/external/curl" vcs="Git" />
<mapping directory="$PROJECT_DIR$/lib/external/fmt" vcs="Git" />

View File

@ -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<decltype(value)>;
ValueType mask = (std::numeric_limits<ValueType>::max() >> (((sizeof(value) * 8) - 1) - (from - to))) << to;

View File

@ -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
)

View File

@ -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})

View File

@ -1,7 +1,7 @@
#include <hex/helpers/crypto.hpp>
#include "hex/helpers/logger.hpp"
#include "test_provider.hpp"
#include "tests.hpp"
#include <hex/test/test_provider.hpp>
#include <hex/test/tests.hpp>
#include <random>
#include <vector>

View File

@ -1,6 +1,6 @@
#include <hex/helpers/utils.hpp>
#include "test_provider.hpp"
#include "tests.hpp"
#include <hex/test/test_provider.hpp>
#include <hex/test/tests.hpp>
TEST_SEQUENCE("32BitIntegerEndianSwap") {
TEST_ASSERT(hex::changeEndianess<u32>(0xAABBCCDD, std::endian::big) == 0xDDCCBBAA);

View File

@ -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)

View File

@ -4,6 +4,7 @@
#include <utility>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <string>
#include <map>

View File

@ -1,7 +1,7 @@
#include <hex.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>
#include "tests.hpp"
#include <hex/test/tests.hpp>
#include <cstdlib>

View File

@ -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})

View File

@ -1,9 +1,10 @@
#include <hex/helpers/crypto.hpp>
#include "test_provider.hpp"
#include "tests.hpp"
#include <hex/test/tests.hpp>
#include <hex/test/test_provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <vector>
#include <algorithm>
#include <vector>
TEST_SEQUENCE("TestSucceeding") {
TEST_SUCCESS();

View File

@ -0,0 +1,42 @@
#include <hex/test/tests.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/paths.hpp>
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();
};

View File

@ -0,0 +1,54 @@
#include <hex/test/tests.hpp>
#include <hex/helpers/net.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/paths.hpp>
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();
};

View File

@ -0,0 +1,32 @@
#include <hex/test/tests.hpp>
#include <hex/helpers/utils.hpp>
using namespace std::literals::string_literals;
TEST_SEQUENCE("SplitStringAtChar") {
const std::string TestString = "Hello|World|ABCD|Test|";
const std::vector<std::string> 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<std::string> 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();
};

View File

@ -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 ()
endforeach ()
add_dependencies(unit_tests ${PROJECT_NAME})