Browse Source

Tests: add hex2bin() / bin2hex()

LIB-18
heck 4 years ago
parent
commit
f03f679360
  1. 1
      .gitignore
  2. 39
      test/test_hexbin.cc

1
.gitignore

@ -59,3 +59,4 @@ test_pEpLog_basic
/test/test_swarm_tofu
/test/test_file_rw_bin
/test/test_rw.bin
/test/test_hexbin

39
test/test_hexbin.cc

@ -0,0 +1,39 @@
#include "../src/std_utils.hh"
#include "../src/pEpLog.hh"
#include <iostream>
#include <cassert>
#include <pEp/pitytest11/PityTest.hh>
using namespace pEp::Utils;
int main()
{
pEp::Adapter::pEpLog::set_enabled(true);
{
// Valid hex string
std::string str_in{ "FFABCD00EF123200" };
std::vector<unsigned char> bin = pEp::Utils::hex2bin(str_in);
PITYASSERT(str_in.length() == bin.size() * 2, "Size error");
std::string str_out = pEp::Utils::bin2hex(bin);
pEpLog("Hex_IN : '" + to_lower(str_in) + "'");
pEpLog("Hex_OUT : '" + to_lower(str_out) + "'");
PITYASSERT(to_lower(str_in) == to_lower(str_out), "roundtrip failed");
}
{
// Uneven string throws
std::string str_in{ "FFA" };
PITYASSERT_THROWS(pEp::Utils::hex2bin(str_in), "Uneven string should throw");
}
{
// Non-hex chars
std::string str_in{ "pEp!" };
PITYASSERT_THROWS(pEp::Utils::hex2bin(str_in), "Invalid hex chars should throw");
}
pEpLog("All tests passsed");
}
Loading…
Cancel
Save