|
8 | 8 | #include <net.h>
|
9 | 9 | #include <netaddress.h>
|
10 | 10 | #include <netbase.h>
|
| 11 | +#include <netmessagemaker.h> |
11 | 12 | #include <serialize.h>
|
12 | 13 | #include <span.h>
|
13 | 14 | #include <streams.h>
|
@@ -704,4 +705,85 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
|
704 | 705 | BOOST_CHECK(!IsLocal(addr));
|
705 | 706 | }
|
706 | 707 |
|
| 708 | +void message_serialize_deserialize_test(bool v2, const std::vector<CSerializedNetMsg>& test_msgs) |
| 709 | +{ |
| 710 | + // use 32 byte keys with all zeros |
| 711 | + CPrivKey k1(32, 0); |
| 712 | + CPrivKey k2(32, 0); |
| 713 | + |
| 714 | + // construct the serializers |
| 715 | + std::unique_ptr<TransportSerializer> serializer; |
| 716 | + std::unique_ptr<TransportDeserializer> deserializer; |
| 717 | + |
| 718 | + if (v2) { |
| 719 | + serializer = std::make_unique<V2TransportSerializer>(V2TransportSerializer(k1, k2)); |
| 720 | + deserializer = std::make_unique<V2TransportDeserializer>(V2TransportDeserializer((NodeId)0, k1, k2)); |
| 721 | + } else { |
| 722 | + serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer()); |
| 723 | + deserializer = std::make_unique<V1TransportDeserializer>(V1TransportDeserializer(Params(), (NodeId)0, SER_NETWORK, INIT_PROTO_VERSION)); |
| 724 | + } |
| 725 | + // run a couple of times through all messages with the same AEAD instance |
| 726 | + for (unsigned int i = 0; i < 100; i++) { |
| 727 | + for (const CSerializedNetMsg& msg_orig : test_msgs) { |
| 728 | + // bypass the copy protection |
| 729 | + CSerializedNetMsg msg; |
| 730 | + msg.data = msg_orig.data; |
| 731 | + msg.m_type = msg_orig.m_type; |
| 732 | + size_t raw_msg_size{msg.data.size()}; |
| 733 | + |
| 734 | + std::vector<unsigned char> serialized_header; |
| 735 | + serializer->prepareForTransport(msg, serialized_header); |
| 736 | + |
| 737 | + // read two times |
| 738 | + // first: read header |
| 739 | + size_t read_bytes{0}; |
| 740 | + Span<const uint8_t> span_header(serialized_header.data(), serialized_header.size()); |
| 741 | + if (serialized_header.size() > 0) read_bytes += deserializer->Read(span_header); |
| 742 | + // second: read the encrypted payload (if required) |
| 743 | + Span<const uint8_t> span_msg(msg.data.data(), msg.data.size()); |
| 744 | + if (msg.data.size() > 0) read_bytes += deserializer->Read(span_msg); |
| 745 | + if (msg.data.size() > read_bytes) { |
| 746 | + Span<const uint8_t> span_msg(msg.data.data() + read_bytes, msg.data.size() - read_bytes); |
| 747 | + read_bytes += deserializer->Read(span_msg); |
| 748 | + } |
| 749 | + BOOST_CHECK(deserializer->Complete()); |
| 750 | + BOOST_CHECK_EQUAL(read_bytes, msg.data.size() + serialized_header.size()); |
| 751 | + // message must be complete |
| 752 | + bool reject_message{true}; |
| 753 | + bool disconnect{true}; |
| 754 | + CNetMessage result{deserializer->GetMessage(GetTime<std::chrono::microseconds>(), reject_message, disconnect)}; |
| 755 | + BOOST_CHECK(!reject_message); |
| 756 | + BOOST_CHECK(!disconnect); |
| 757 | + BOOST_CHECK_EQUAL(result.m_type, msg.m_type); |
| 758 | + BOOST_CHECK_EQUAL(raw_msg_size, result.m_message_size); |
| 759 | + } |
| 760 | + } |
| 761 | +} |
| 762 | + |
| 763 | +BOOST_AUTO_TEST_CASE(net_v2) |
| 764 | +{ |
| 765 | + // create some messages where we perform serialization and deserialization |
| 766 | + std::vector<CSerializedNetMsg> test_msgs; |
| 767 | + test_msgs.push_back(CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK)); |
| 768 | + test_msgs.push_back(CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (int)NODE_NETWORK, 123, CAddress(CService(), NODE_NONE), CAddress(CService(), NODE_NONE), 123, "foobar", 500000, true)); |
| 769 | + test_msgs.push_back(CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::PING, 123456)); |
| 770 | + CDataStream stream(ParseHex("020000000001013107ca31e1950a9b44b75ce3e8f30127e4d823ed8add1263a1cc8adcc8e49164000000001716001487835ecf51ea0351ef266d216a7e7a3e74b84b4efeffffff02082268590000000017a9144a94391b99e672b03f56d3f60800ef28bc304c4f8700ca9a3b0000000017a9146d5df9e79f752e3c53fc468db89cafda4f7d00cb87024730440220677de5b11a5617d541ba06a1fa5921ab6b4509f8028b23f18ab8c01c5eb1fcfb02202fe382e6e87653f60ff157aeb3a18fc888736720f27ced546b0b77431edabdb0012102608c772598e9645933a86bcd662a3b939e02fb3e77966c9713db5648d5ba8a0006010000"), SER_NETWORK, PROTOCOL_VERSION); |
| 771 | + test_msgs.push_back(CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::TX, CTransaction(deserialize, stream))); |
| 772 | + std::vector<CInv> vInv; |
| 773 | + for (unsigned int i = 0; i < 1000; i++) { |
| 774 | + vInv.push_back(CInv(MSG_BLOCK, Params().GenesisBlock().GetHash())); |
| 775 | + } |
| 776 | + test_msgs.push_back(CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::INV, vInv)); |
| 777 | + |
| 778 | + // add a dummy message |
| 779 | + std::string dummy; |
| 780 | + for (unsigned int i = 0; i < 100; i++) { |
| 781 | + dummy += "020000000001013107ca31e1950a9b44b75ce3e8f30127e4d823ed8add1263a1cc8adcc8e49164000000001716001487835ecf51ea0351ef266d216a7e7a3e74b84b4efeffffff02082268590000000017a9144a94391b99e672b03f56d3f60800ef28bc304c4f8700ca9a3b0000000017a9146d5df9e79f752e3c53fc468db89cafda4f7d00cb87024730440220677de5b11a5617d541ba06a1fa5921ab6b4509f8028b23f18ab8c01c5eb1fcfb02202fe382e6e87653f60ff157aeb3a18fc888736720f27ced546b0b77431edabdb0012102608c772598e9645933a86bcd662a3b939e02fb3e77966c9713db5648d5ba8a0006010000"; |
| 782 | + } |
| 783 | + test_msgs.push_back(CNetMsgMaker(INIT_PROTO_VERSION).Make("foobar", dummy)); |
| 784 | + |
| 785 | + message_serialize_deserialize_test(true, test_msgs); |
| 786 | + message_serialize_deserialize_test(false, test_msgs); |
| 787 | +} |
| 788 | + |
707 | 789 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments