diff --git a/build_msvc/test_bitcoin/test_bitcoin.vcxproj b/build_msvc/test_bitcoin/test_bitcoin.vcxproj index 494cd8b0ed..ae52c2661e 100644 --- a/build_msvc/test_bitcoin/test_bitcoin.vcxproj +++ b/build_msvc/test_bitcoin/test_bitcoin.vcxproj @@ -84,7 +84,7 @@ - + diff --git a/src/Makefile.test.include b/src/Makefile.test.include index af6e889d88..2a554dbe60 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -440,8 +440,9 @@ endif %.json.h: %.json @$(MKDIR_P) $(@D) $(AM_V_GEN) { \ + echo "#include " && \ echo "namespace json_tests{" && \ - echo "static unsigned const char $(*F)[] = {" && \ + echo "static const std::string $(*F){" && \ $(HEXDUMP) -v -e '8/1 "0x%02x, "' -e '"\n"' $< | $(SED) -e 's/0x ,//g' && \ echo "};};"; \ } > "$@.new" && mv -f "$@.new" "$@" diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp index 7f3ca6bf93..bb3defb93e 100644 --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -23,7 +23,7 @@ BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup) // Goal: test low-level base58 encoding functionality BOOST_AUTO_TEST_CASE(base58_EncodeBase58) { - UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode))); + UniValue tests = read_json(json_tests::base58_encode_decode); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; std::string strTest = test.write(); @@ -43,7 +43,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58) // Goal: test low-level base58 decoding functionality BOOST_AUTO_TEST_CASE(base58_DecodeBase58) { - UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode))); + UniValue tests = read_json(json_tests::base58_encode_decode); std::vector result; for (unsigned int idx = 0; idx < tests.size(); idx++) { diff --git a/src/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp index e8b535ad93..38274a9801 100644 --- a/src/test/blockfilter_tests.cpp +++ b/src/test/blockfilter_tests.cpp @@ -129,9 +129,7 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test) BOOST_AUTO_TEST_CASE(blockfilters_json_test) { UniValue json; - std::string json_data(json_tests::blockfilters, - json_tests::blockfilters + sizeof(json_tests::blockfilters)); - if (!json.read(json_data) || !json.isArray()) { + if (!json.read(json_tests::blockfilters) || !json.isArray()) { BOOST_ERROR("Parse error."); return; } diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index 60a9a55406..681899f40f 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -22,7 +22,7 @@ BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup) // Goal: check that parsed keys match test payload BOOST_AUTO_TEST_CASE(key_io_valid_parse) { - UniValue tests = read_json(std::string(json_tests::key_io_valid, json_tests::key_io_valid + sizeof(json_tests::key_io_valid))); + UniValue tests = read_json(json_tests::key_io_valid); CKey privkey; CTxDestination destination; SelectParams(ChainType::MAIN); @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse) // Goal: check that generated keys match test vectors BOOST_AUTO_TEST_CASE(key_io_valid_gen) { - UniValue tests = read_json(std::string(json_tests::key_io_valid, json_tests::key_io_valid + sizeof(json_tests::key_io_valid))); + UniValue tests = read_json(json_tests::key_io_valid); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; @@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_gen) // Goal: check that base58 parsing code is robust against a variety of corrupted data BOOST_AUTO_TEST_CASE(key_io_invalid) { - UniValue tests = read_json(std::string(json_tests::key_io_invalid, json_tests::key_io_invalid + sizeof(json_tests::key_io_invalid))); // Negative testcases + UniValue tests = read_json(json_tests::key_io_invalid); // Negative testcases CKey privkey; CTxDestination destination; diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 9255a73014..3500fe6b6a 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -20,7 +20,7 @@ static UniValue JSON(std::string_view json) { UniValue value; - BOOST_CHECK(value.read(json.data(), json.size())); + BOOST_CHECK(value.read(json)); return value; } diff --git a/src/test/script_standard_tests.cpp b/src/test/script_standard_tests.cpp index fc66632b10..c0b533eed9 100644 --- a/src/test/script_standard_tests.cpp +++ b/src/test/script_standard_tests.cpp @@ -394,7 +394,7 @@ BOOST_AUTO_TEST_CASE(bip341_spk_test_vectors) // using control_set = decltype(TaprootSpendData::scripts)::mapped_type; UniValue tests; - tests.read((const char*)json_tests::bip341_wallet_vectors, sizeof(json_tests::bip341_wallet_vectors)); + tests.read(json_tests::bip341_wallet_vectors); const auto& vectors = tests["scriptPubKey"]; diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index 171dae0002..509d238dfa 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -932,7 +932,7 @@ BOOST_AUTO_TEST_CASE(script_build) std::set tests_set; { - UniValue json_tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests))); + UniValue json_tests = read_json(json_tests::script_tests); for (unsigned int idx = 0; idx < json_tests.size(); idx++) { const UniValue& tv = json_tests[idx]; @@ -971,7 +971,7 @@ BOOST_AUTO_TEST_CASE(script_json_test) // scripts. // If a witness is given, then the last value in the array should be the // amount (nValue) to use in the crediting tx - UniValue tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests))); + UniValue tests = read_json(json_tests::script_tests); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; @@ -1809,7 +1809,7 @@ BOOST_AUTO_TEST_CASE(script_assets_test) BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors) { UniValue tests; - tests.read((const char*)json_tests::bip341_wallet_vectors, sizeof(json_tests::bip341_wallet_vectors)); + tests.read(json_tests::bip341_wallet_vectors); const auto& vectors = tests["keyPathSpending"]; diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp index 3e559626ff..cca5325ff8 100644 --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -163,7 +163,7 @@ BOOST_AUTO_TEST_CASE(sighash_test) // Goal: check that SignatureHash generates correct hash BOOST_AUTO_TEST_CASE(sighash_from_data) { - UniValue tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash))); + UniValue tests = read_json(json_tests::sighash); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 40f91ade63..d031775385 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(tx_valid) { BOOST_CHECK_MESSAGE(CheckMapFlagNames(), "mapFlagNames is missing a script verification flag"); // Read tests from test/data/tx_valid.json - UniValue tests = read_json(std::string(json_tests::tx_valid, json_tests::tx_valid + sizeof(json_tests::tx_valid))); + UniValue tests = read_json(json_tests::tx_valid); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; @@ -281,7 +281,7 @@ BOOST_AUTO_TEST_CASE(tx_valid) BOOST_AUTO_TEST_CASE(tx_invalid) { // Read tests from test/data/tx_invalid.json - UniValue tests = read_json(std::string(json_tests::tx_invalid, json_tests::tx_invalid + sizeof(json_tests::tx_invalid))); + UniValue tests = read_json(json_tests::tx_invalid); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h index 94f80f9c27..acbce25741 100644 --- a/src/univalue/include/univalue.h +++ b/src/univalue/include/univalue.h @@ -7,13 +7,15 @@ #define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H #include +#include #include -#include #include #include #include #include +#include #include +#include #include class UniValue { @@ -94,9 +96,7 @@ public: std::string write(unsigned int prettyIndent = 0, unsigned int indentLevel = 0) const; - bool read(const char *raw, size_t len); - bool read(const char *raw) { return read(raw, strlen(raw)); } - bool read(std::string_view raw) { return read(raw.data(), raw.size()); } + bool read(std::string_view raw); private: UniValue::VType typ; diff --git a/src/univalue/lib/univalue_read.cpp b/src/univalue/lib/univalue_read.cpp index 2f2385383c..40d465f438 100644 --- a/src/univalue/lib/univalue_read.cpp +++ b/src/univalue/lib/univalue_read.cpp @@ -5,10 +5,11 @@ #include #include -#include #include +#include #include #include +#include #include /* @@ -259,7 +260,7 @@ enum expect_bits : unsigned { #define setExpect(bit) (expectMask |= EXP_##bit) #define clearExpect(bit) (expectMask &= ~EXP_##bit) -bool UniValue::read(const char *raw, size_t size) +bool UniValue::read(std::string_view str_in) { clear(); @@ -270,7 +271,8 @@ bool UniValue::read(const char *raw, size_t size) unsigned int consumed; enum jtokentype tok = JTOK_NONE; enum jtokentype last_tok = JTOK_NONE; - const char* end = raw + size; + const char* raw{str_in.data()}; + const char* end{raw + str_in.size()}; do { last_tok = tok; diff --git a/src/univalue/test/unitester.cpp b/src/univalue/test/unitester.cpp index 6344a5a0ab..d27e647e5c 100644 --- a/src/univalue/test/unitester.cpp +++ b/src/univalue/test/unitester.cpp @@ -153,7 +153,7 @@ void no_nul_test() { char buf[] = "___[1,2,3]___"; UniValue val; - assert(val.read(buf + 3, 7)); + assert(val.read({buf + 3, 7})); } int main (int argc, char *argv[])