aboutsummaryrefslogtreecommitdiff
path: root/src/test/base58_tests.cpp
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2020-09-23 08:40:38 +0200
committerVasil Dimov <vd@FreeBSD.org>2020-10-31 16:02:49 +0100
commitecc6cf1a3b097b9b5b047282063a0b6779631b83 (patch)
tree742e8def9e830937968e79dcdb6cefc270dbf67e /src/test/base58_tests.cpp
parentb1291b2e8fc39b366765d905200f022823e3d50b (diff)
downloadbitcoin-ecc6cf1a3b097b9b5b047282063a0b6779631b83.tar.xz
test: fix creation of std::string objects with \0s
A string literal `"abc"` contains a terminating `\0`, so that is 4 bytes. There is no need to write `"abc\0"` unless two terminating `\0`s are necessary. `std::string` objects do not internally contain a terminating `\0`, so `std::string("abc")` creates a string with size 3 and is the same as `std::string("abc", 3)`. In `"\01"` the `01` part is interpreted as one number (1) and that is the same as `"\1"` which is a string like `{1, 0}` whereas `"\0z"` is a string like `{0, 'z', 0}`. To create a string like `{0, '1', 0}` one must use `"\0" "1"`. Adjust the tests accordingly.
Diffstat (limited to 'src/test/base58_tests.cpp')
-rw-r--r--src/test/base58_tests.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp
index 6a636f2574..e55d6b3b19 100644
--- a/src/test/base58_tests.cpp
+++ b/src/test/base58_tests.cpp
@@ -12,7 +12,9 @@
#include <univalue.h>
#include <boost/test/unit_test.hpp>
+#include <string>
+using namespace std::literals;
extern UniValue read_json(const std::string& jsondata);
@@ -58,14 +60,14 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
}
- BOOST_CHECK(!DecodeBase58("invalid", result, 100));
- BOOST_CHECK(!DecodeBase58(std::string("invalid"), result, 100));
- BOOST_CHECK(!DecodeBase58(std::string("\0invalid", 8), result, 100));
+ BOOST_CHECK(!DecodeBase58("invalid"s, result, 100));
+ BOOST_CHECK(!DecodeBase58("invalid\0"s, result, 100));
+ BOOST_CHECK(!DecodeBase58("\0invalid"s, result, 100));
- BOOST_CHECK(DecodeBase58(std::string("good", 4), result, 100));
- BOOST_CHECK(!DecodeBase58(std::string("bad0IOl", 7), result, 100));
- BOOST_CHECK(!DecodeBase58(std::string("goodbad0IOl", 11), result, 100));
- BOOST_CHECK(!DecodeBase58(std::string("good\0bad0IOl", 12), result, 100));
+ BOOST_CHECK(DecodeBase58("good"s, result, 100));
+ BOOST_CHECK(!DecodeBase58("bad0IOl"s, result, 100));
+ BOOST_CHECK(!DecodeBase58("goodbad0IOl"s, result, 100));
+ BOOST_CHECK(!DecodeBase58("good\0bad0IOl"s, result, 100));
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
@@ -73,10 +75,10 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
std::vector<unsigned char> expected = ParseHex("971a55");
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
- BOOST_CHECK(DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh", 21), result, 100));
- BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oi", 21), result, 100));
- BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh0IOl", 25), result, 100));
- BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh\00IOl", 26), result, 100));
+ BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));
+ BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oi"s, result, 100));
+ BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh0IOl"s, result, 100));
+ BOOST_CHECK(!DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh\0" "0IOl"s, result, 100));
}
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)