mirror of
https://github.com/monero-project/monero.git
synced 2025-05-08 10:55:10 -04:00
Merge pull request #6351
81c5943
Remove temporary std::string creation in some hex->bin calls (vtnerd)5fcc23a
Move hex->bin conversion to monero copyright files and with less includes (vtnerd)3387f0e
Reduce template bloat in hex->bin for ZMQ json (vtnerd)
This commit is contained in:
commit
cfc0f4a7fa
9 changed files with 135 additions and 62 deletions
|
@ -824,14 +824,14 @@ TEST(ToHex, String)
|
|||
|
||||
}
|
||||
|
||||
TEST(FromHex, String)
|
||||
TEST(HexLocale, String)
|
||||
{
|
||||
// the source data to encode and decode
|
||||
std::vector<uint8_t> source{{ 0x00, 0xFF, 0x0F, 0xF0 }};
|
||||
|
||||
// encode and decode the data
|
||||
auto hex = epee::to_hex::string({ source.data(), source.size() });
|
||||
auto decoded = epee::from_hex::vector(hex);
|
||||
auto decoded = epee::from_hex_locale::to_vector(hex);
|
||||
|
||||
// encoded should be twice the size and should decode to the exact same data
|
||||
EXPECT_EQ(source.size() * 2, hex.size());
|
||||
|
@ -840,7 +840,7 @@ TEST(FromHex, String)
|
|||
// we will now create a padded hex string, we want to explicitly allow
|
||||
// decoding it this way also, ignoring spaces and colons between the numbers
|
||||
hex.assign("00:ff 0f:f0");
|
||||
EXPECT_EQ(source, epee::from_hex::vector(hex));
|
||||
EXPECT_EQ(source, epee::from_hex_locale::to_vector(hex));
|
||||
}
|
||||
|
||||
TEST(ToHex, Array)
|
||||
|
@ -902,6 +902,46 @@ TEST(ToHex, Formatted)
|
|||
EXPECT_EQ(expected, out.str());
|
||||
}
|
||||
|
||||
TEST(FromHex, ToString)
|
||||
{
|
||||
static constexpr const char hex[] = u8"deadbeeffY";
|
||||
static constexpr const char binary[] = {
|
||||
char(0xde), char(0xad), char(0xbe), char(0xef), 0x00
|
||||
};
|
||||
|
||||
std::string out{};
|
||||
EXPECT_FALSE(epee::from_hex::to_string(out, hex));
|
||||
|
||||
boost::string_ref portion{hex};
|
||||
portion.remove_suffix(1);
|
||||
EXPECT_FALSE(epee::from_hex::to_string(out, portion));
|
||||
|
||||
portion.remove_suffix(1);
|
||||
EXPECT_TRUE(epee::from_hex::to_string(out, portion));
|
||||
EXPECT_EQ(std::string{binary}, out);
|
||||
}
|
||||
|
||||
TEST(FromHex, ToBuffer)
|
||||
{
|
||||
static constexpr const char hex[] = u8"deadbeeffY";
|
||||
static constexpr const std::uint8_t binary[] = {0xde, 0xad, 0xbe, 0xef};
|
||||
|
||||
std::vector<std::uint8_t> out{};
|
||||
out.resize(sizeof(binary));
|
||||
EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), hex));
|
||||
|
||||
boost::string_ref portion{hex};
|
||||
portion.remove_suffix(1);
|
||||
EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion));
|
||||
|
||||
portion.remove_suffix(1);
|
||||
EXPECT_FALSE(epee::from_hex::to_buffer({out.data(), out.size() - 1}, portion));
|
||||
|
||||
EXPECT_TRUE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion));
|
||||
const std::vector<std::uint8_t> expected{std::begin(binary), std::end(binary)};
|
||||
EXPECT_EQ(expected, out);
|
||||
}
|
||||
|
||||
TEST(StringTools, BuffToHex)
|
||||
{
|
||||
const std::vector<unsigned char> all_bytes = get_all_bytes();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue