Changeset View
Changeset View
Standalone View
Standalone View
src/test/util_tests.cpp
// Copyright (c) 2011-2019 The Bitcoin Core developers | // Copyright (c) 2011-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <chainparams.h> | |||||
#include <clientversion.h> | #include <clientversion.h> | ||||
#include <hash.h> // For Hash() | |||||
#include <key.h> // For CKey | |||||
#include <optional.h> | #include <optional.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <test/util/logging.h> | #include <test/util/logging.h> | ||||
#include <test/util/str.h> | #include <test/util/str.h> | ||||
#include <uint256.h> | |||||
#include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC | |||||
#include <util/moneystr.h> | #include <util/moneystr.h> | ||||
#include <util/spanparsing.h> | #include <util/spanparsing.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/string.h> | #include <util/string.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <util/vector.h> | #include <util/vector.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <univalue.h> | #include <univalue.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <array> | |||||
#include <cstdint> | #include <cstdint> | ||||
#include <utility> | #include <utility> | ||||
#ifndef WIN32 | #ifndef WIN32 | ||||
#include <csignal> | #include <csignal> | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/wait.h> | #include <sys/wait.h> | ||||
#endif | #endif | ||||
#include <thread> | #include <thread> | ||||
▲ Show 20 Lines • Show All 2,276 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(test_tracked_vector) { | ||||
BOOST_CHECK(v8[0].origin == &t2); | BOOST_CHECK(v8[0].origin == &t2); | ||||
BOOST_CHECK(v8[1].origin == &t1); | BOOST_CHECK(v8[1].origin == &t1); | ||||
BOOST_CHECK(v8[2].origin == &t2); | BOOST_CHECK(v8[2].origin == &t2); | ||||
BOOST_CHECK_EQUAL(v8[0].copies, 0); | BOOST_CHECK_EQUAL(v8[0].copies, 0); | ||||
BOOST_CHECK_EQUAL(v8[1].copies, 1); | BOOST_CHECK_EQUAL(v8[1].copies, 1); | ||||
BOOST_CHECK_EQUAL(v8[2].copies, 0); | BOOST_CHECK_EQUAL(v8[2].copies, 0); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(message_sign) { | |||||
const std::array<uint8_t, 32> privkey_bytes = { | |||||
{// just some random data | |||||
// derived address from this private key: | |||||
// 15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs | |||||
0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E, 0xEE, 0xBA, 0xAA, | |||||
0x42, 0x0F, 0xEF, 0x07, 0x26, 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, | |||||
0x48, 0x9F, 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66}}; | |||||
const std::string message = "Trust no one"; | |||||
const std::string expected_signature = | |||||
"IED/" | |||||
"JtZs3huKX9JEQIBPZPSZwOiMyDQ+" | |||||
"yNWQvL7YcFzCOGTcfleWOWSvfggenKCinqvOX8t1Iw+HYZqQjxzXQm0="; | |||||
CKey privkey; | |||||
std::string generated_signature; | |||||
BOOST_REQUIRE_MESSAGE(!privkey.IsValid(), | |||||
"Confirm the private key is invalid"); | |||||
BOOST_CHECK_MESSAGE(!MessageSign(privkey, message, generated_signature), | |||||
"Sign with an invalid private key"); | |||||
privkey.Set(privkey_bytes.begin(), privkey_bytes.end(), true); | |||||
BOOST_REQUIRE_MESSAGE(privkey.IsValid(), | |||||
"Confirm the private key is valid"); | |||||
BOOST_CHECK_MESSAGE(MessageSign(privkey, message, generated_signature), | |||||
"Sign with a valid private key"); | |||||
BOOST_CHECK_EQUAL(expected_signature, generated_signature); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(message_verify) { | |||||
const auto params = CreateChainParams(CBaseChainParams::MAIN); | |||||
BOOST_CHECK_EQUAL(MessageVerify(*params, "invalid address", | |||||
"signature should be irrelevant", | |||||
"message too"), | |||||
MessageVerificationResult::ERR_INVALID_ADDRESS); | |||||
BOOST_CHECK_EQUAL( | |||||
MessageVerify(*params, "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV", | |||||
"signature should be irrelevant", "message too"), | |||||
MessageVerificationResult::ERR_ADDRESS_NO_KEY); | |||||
BOOST_CHECK_EQUAL(MessageVerify(*params, | |||||
"1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm", | |||||
"invalid signature, not in base64 encoding", | |||||
"message should be irrelevant"), | |||||
MessageVerificationResult::ERR_MALFORMED_SIGNATURE); | |||||
BOOST_CHECK_EQUAL( | |||||
MessageVerify(*params, "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm", | |||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" | |||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", | |||||
"message should be irrelevant"), | |||||
MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED); | |||||
BOOST_CHECK_EQUAL( | |||||
MessageVerify(*params, "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs", | |||||
"IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/" | |||||
"WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=", | |||||
"I never signed this"), | |||||
MessageVerificationResult::ERR_NOT_SIGNED); | |||||
BOOST_CHECK_EQUAL( | |||||
MessageVerify(*params, "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs", | |||||
"IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/" | |||||
"WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=", | |||||
"Trust no one"), | |||||
MessageVerificationResult::OK); | |||||
BOOST_CHECK_EQUAL( | |||||
MessageVerify(*params, "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3", | |||||
"IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQg" | |||||
"Zm9yIGJhbmtzIAaHRtbCeDZINyavx14=", | |||||
"Trust me"), | |||||
MessageVerificationResult::OK); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(message_hash) { | |||||
const std::string unsigned_tx = "..."; | |||||
const std::string prefixed_message = | |||||
std::string(1, (char)MESSAGE_MAGIC.length()) + MESSAGE_MAGIC + | |||||
std::string(1, (char)unsigned_tx.length()) + unsigned_tx; | |||||
const uint256 signature_hash = Hash(unsigned_tx.begin(), unsigned_tx.end()); | |||||
const uint256 message_hash1 = | |||||
Hash(prefixed_message.begin(), prefixed_message.end()); | |||||
const uint256 message_hash2 = MessageHash(unsigned_tx); | |||||
BOOST_CHECK_EQUAL(message_hash1, message_hash2); | |||||
BOOST_CHECK_NE(message_hash1, signature_hash); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |