mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-10 04:33:59 +01:00
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:
@@ -6,6 +6,9 @@
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <string>
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(base64_tests, BasicTestingSetup)
|
||||
|
||||
@@ -23,14 +26,14 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
|
||||
|
||||
// Decoding strings with embedded NUL characters should fail
|
||||
bool failure;
|
||||
(void)DecodeBase64(std::string("invalid", 7), &failure);
|
||||
BOOST_CHECK_EQUAL(failure, true);
|
||||
(void)DecodeBase64(std::string("nQB/pZw=", 8), &failure);
|
||||
BOOST_CHECK_EQUAL(failure, false);
|
||||
(void)DecodeBase64(std::string("nQB/pZw=\0invalid", 16), &failure);
|
||||
BOOST_CHECK_EQUAL(failure, true);
|
||||
(void)DecodeBase64(std::string("nQB/pZw=invalid", 15), &failure);
|
||||
BOOST_CHECK_EQUAL(failure, true);
|
||||
(void)DecodeBase64("invalid\0"s, &failure);
|
||||
BOOST_CHECK(failure);
|
||||
(void)DecodeBase64("nQB/pZw="s, &failure);
|
||||
BOOST_CHECK(!failure);
|
||||
(void)DecodeBase64("nQB/pZw=\0invalid"s, &failure);
|
||||
BOOST_CHECK(failure);
|
||||
(void)DecodeBase64("nQB/pZw=invalid\0"s, &failure);
|
||||
BOOST_CHECK(failure);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user