aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
authorluigi1111 <luigi1111w@gmail.com>2020-04-04 12:55:02 -0500
committerluigi1111 <luigi1111w@gmail.com>2020-04-04 12:55:02 -0500
commitcfc0f4a7fa67dd880d51c0aa35baa1e73e5db423 (patch)
tree932e460664a6329d5b9906506d1d6951378768a2 /tests/unit_tests
parentMerge pull request #6347 (diff)
parentReduce template bloat in hex->bin for ZMQ json (diff)
downloadmonero-cfc0f4a7fa67dd880d51c0aa35baa1e73e5db423.tar.xz
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)
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/epee_utils.cpp46
1 files changed, 43 insertions, 3 deletions
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index 513c2227c..42bbb26bb 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -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();