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