Skip to content

Commit

Permalink
tools: add gentestdata
Browse files Browse the repository at this point in the history
Using gentestdata you can generate a pseudo-random binary data that you
can use during testing.
This will produce the same data every time, but it looks random and the
data is neither sparse nor compressible.
  • Loading branch information
arogge committed Jun 7, 2023
1 parent ad45b1c commit 80e673f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/tools/CMakeLists.txt
Expand Up @@ -56,6 +56,10 @@ if(NOT HAVE_WIN32)
target_link_libraries(bpluginfo bareos ${DL_LIBRARIES} CLI11::CLI11)
endif()

add_executable(gentestdata)
target_sources(gentestdata PRIVATE gentestdata.cc)
target_link_libraries(gentestdata PRIVATE CLI11::CLI11)

set(TOOLS_BIN bsmtp bwild bregex)

set(TOOLS_SBIN bscrypto bwild bregex)
Expand Down
56 changes: 56 additions & 0 deletions core/src/tools/gentestdata.cc
@@ -0,0 +1,56 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2023-2023 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "CLI/App.hpp"
#include "CLI/Formatter.hpp"
#include "CLI/Config.hpp"
#include <random>
#include <cstring>

static void print_random(long);

int main(int argc, char** argv)
{
CLI::App app{"Generate a stream of pseudo-random testdata"};

long bytes{1024};
app.add_option("-s,--size", bytes, "Number of bytes to create");

CLI11_PARSE(app, argc, argv);

print_random(bytes);
return 0;
}


void print_random(long bytes)
{
std::mt19937_64 generator{};
using val_type = decltype(generator());

char buf[sizeof(val_type)];
for (auto remaining = bytes; remaining > 0; remaining -= sizeof(val_type)) {
auto value = generator();
memcpy(buf, &value, sizeof(val_type));
for (auto i = 0u; i < sizeof(val_type) && remaining - i > 0; i++) {
std::putchar(buf[i]);
}
}
}

0 comments on commit 80e673f

Please sign in to comment.