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.
This commit is contained in:
Vasil Dimov
2020-09-23 08:40:38 +02:00
parent b1291b2e8f
commit ecc6cf1a3b
6 changed files with 59 additions and 44 deletions

View File

@@ -23,6 +23,7 @@
#include <array>
#include <stdint.h>
#include <string.h>
#include <thread>
#include <univalue.h>
#include <utility>
@@ -35,6 +36,8 @@
#include <boost/test/unit_test.hpp>
using namespace std::literals;
/* defined in logging.cpp */
namespace BCLog {
std::string LogEscapeMessage(const std::string& str);
@@ -1235,9 +1238,9 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney)
BOOST_CHECK(!ParseMoney("-1", ret));
// Parsing strings with embedded NUL characters should fail
BOOST_CHECK(!ParseMoney(std::string("\0-1", 3), ret));
BOOST_CHECK(!ParseMoney(std::string("\01", 2), ret));
BOOST_CHECK(!ParseMoney(std::string("1\0", 2), ret));
BOOST_CHECK(!ParseMoney("\0-1"s, ret));
BOOST_CHECK(!ParseMoney("\0" "1"s, ret));
BOOST_CHECK(!ParseMoney("1\0"s, ret));
}
BOOST_AUTO_TEST_CASE(util_IsHex)