Skip to content

Commit 861c470

Browse files
committed
Add validation test tool to verify load-save byte likeness
1 parent d9c4a10 commit 861c470

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ jobs:
4141
if [ "$RUNNER_OS" == "Windows" ]; then
4242
export CMAKE_GENERATOR=Ninja
4343
fi
44-
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_INSTALL_PREFIX=install -DLIBBNDL_BUILD_TOOLS=ON -DLIBBNDL_BUILD_UI=ON -DLIBBNDL_INSTALL_UI=ON
44+
cmake -S. -Bbuild \
45+
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
46+
-DCMAKE_INSTALL_PREFIX=install \
47+
-DLIBBNDL_BUILD_TOOLS=ON \
48+
-DLIBBNDL_BUILD_UI=ON \
49+
-DLIBBNDL_INSTALL_UI=ON \
50+
-DLIBBNDL_BUILD_VALIDATIONTEST=ON
4551
cmake --build build
4652
cmake --install build
4753

CMakePresets.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"installDir": "${sourceDir}/out/install/${presetName}",
2121
"cacheVariables": {
2222
"LIBBNDL_BUILD_TOOLS": "ON",
23-
"LIBBNDL_INSTALL_TOOLS": "ON"
23+
"LIBBNDL_INSTALL_TOOLS": "ON",
24+
"LIBBNDL_BUILD_VALIDATIONTEST": "ON"
2425
}
2526
},
2627
{
@@ -414,7 +415,7 @@
414415
],
415416
"vendor": {
416417
"qt-project.org/Presets": {
417-
"checksum": "YMZgAov16/vL9KShMbHsAB8IkfY="
418+
"checksum": "Vvury/6B6y8sAEcGfFRgwMMpPdE="
418419
}
419420
}
420421
}

tools/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ option(LIBBNDL_BUILD_UI "Build UI tools" OFF)
66
if(LIBBNDL_BUILD_UI)
77
add_subdirectory(bndl_edit)
88
endif()
9+
10+
option(LIBBNDL_BUILD_VALIDATIONTEST "Build validation test tool" OFF)
11+
if(LIBBNDL_BUILD_VALIDATIONTEST)
12+
add_subdirectory(bndl_validationtest)
13+
endif()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
add_executable(bndl_validationtest main.cpp)
2+
3+
target_link_libraries(bndl_validationtest PRIVATE libbndl cxxopts::cxxopts pugixml::pugixml)
4+
target_compile_definitions(bndl_validationtest PRIVATE PUGIXML_HEADER_ONLY PUGIXML_NO_XPATH)
5+
6+
set_property(TARGET bndl_validationtest PROPERTY CXX_STANDARD 20)
7+
8+
if(MSVC)
9+
target_compile_options(bndl_validationtest PRIVATE "/W4" "/Zc:throwingNew" "/EHsc")
10+
endif()
11+
12+
add_custom_command(TARGET bndl_validationtest POST_BUILD
13+
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:libbndl> $<TARGET_FILE_DIR:bndl_validationtest>)
14+
15+
if(LIBBNDL_INSTALL_TOOLS)
16+
install(TARGETS bndl_validationtest RUNTIME DESTINATION bin
17+
LIBRARY DESTINATION lib
18+
ARCHIVE DESTINATION lib)
19+
endif()

tools/bndl_validationtest/main.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <libbndl/bundle.hpp>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
6+
static bool identicalFiles(const std::filesystem::path &path1, const std::filesystem::path &path2)
7+
{
8+
std::ifstream file1(path1, std::ifstream::binary | std::ifstream::ate);
9+
std::ifstream file2(path2, std::ifstream::binary | std::ifstream::ate);
10+
11+
if (file1.fail() || file2.fail()) {
12+
return false;
13+
}
14+
15+
if (file1.tellg() != file2.tellg()) {
16+
return false;
17+
}
18+
19+
file1.seekg(0, std::ifstream::beg);
20+
file2.seekg(0, std::ifstream::beg);
21+
return std::equal(std::istreambuf_iterator<char>(file1.rdbuf()), std::istreambuf_iterator<char>(), std::istreambuf_iterator<char>(file2.rdbuf()));
22+
}
23+
24+
int main(int argc, char **argv)
25+
{
26+
if (argc != 2)
27+
{
28+
std::cerr << "Invalid arguments\n";
29+
return 1;
30+
}
31+
32+
const auto dir = std::filesystem::path(argv[1]);
33+
if (!std::filesystem::is_directory(dir))
34+
{
35+
std::cerr << "Invalid directory\n";
36+
return 2;
37+
}
38+
39+
// This is just a test tool so not to bothered about the scenario where this already exists.
40+
const auto outputFile = std::filesystem::temp_directory_path() / "bndl_validationtest.bndl";
41+
42+
std::filesystem::remove(outputFile);
43+
44+
for (const auto &entry : std::filesystem::recursive_directory_iterator(dir))
45+
{
46+
if (!std::filesystem::is_regular_file(entry))
47+
continue;
48+
49+
const auto path = entry.path();
50+
51+
libbndl::Bundle bundle;
52+
53+
if (!bundle.Load(path.string()))
54+
{
55+
auto ext = path.extension().string();
56+
std::transform(ext.begin(), ext.end(), ext.begin(), [](auto c) { return std::tolower(c, std::locale::classic()); });
57+
58+
// If it looks like a bundle let's be vocal about it
59+
if (ext == ".bndl" || ext == ".bundle")
60+
std::cerr << "Failed to load likely bundle: " << path << '\n';
61+
62+
continue;
63+
}
64+
65+
if (!bundle.Save(outputFile.string()))
66+
{
67+
std::cerr << "Failed to save bundle: " << path << '\n';
68+
}
69+
else if (!identicalFiles(path, outputFile))
70+
{
71+
std::cerr << "Did not produce identical output on save: " << path << '\n';
72+
}
73+
74+
std::filesystem::remove(outputFile);
75+
}
76+
}

0 commit comments

Comments
 (0)