From 5360ca1548b51b4a368608063b6b5e05b20b50b7 Mon Sep 17 00:00:00 2001 From: subtly Date: Sat, 20 Jun 2015 08:34:53 -0400 Subject: [PATCH] Initial reader test. Something broken with encframes vector of bytes. --- libp2p/RLPXFrameWriter.cpp | 5 ++++- libp2p/RLPXFrameWriter.h | 12 ++++++------ test/libp2p/rlpx.cpp | 21 +++++++++++++++++---- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/libp2p/RLPXFrameWriter.cpp b/libp2p/RLPXFrameWriter.cpp index c8c1e8ceb..2c00256e4 100644 --- a/libp2p/RLPXFrameWriter.cpp +++ b/libp2p/RLPXFrameWriter.cpp @@ -25,6 +25,9 @@ using namespace std; using namespace dev; using namespace dev::p2p; +const uint16_t RLPXFrameWriter::EmptyFrameLength = h128::size * 3; // header + headerMAC + frameMAC +const uint16_t RLPXFrameWriter::MinFrameDequeLength = h128::size * 4; // header + headerMAC + padded-block + frameMAC + void RLPXFrameWriter::enque(unsigned _packetType, RLPStream& _payload, PacketPriority _priority) { QueueState& qs = _priority ? m_q.first : m_q.second; @@ -113,7 +116,7 @@ size_t RLPXFrameWriter::mux(RLPXFrameCoder& _coder, unsigned _size, vector= 0); frameLen -= payload.size(); - o_toWrite.push_back(move(payload)); + o_toWrite.push_back(payload); payload.resize(0); if (!qs.remaining) diff --git a/libp2p/RLPXFrameWriter.h b/libp2p/RLPXFrameWriter.h index 3bbcb7a16..e4a4a1953 100644 --- a/libp2p/RLPXFrameWriter.h +++ b/libp2p/RLPXFrameWriter.h @@ -49,9 +49,9 @@ public: RLPXFrameReader(uint16_t _protocolType): m_protocolType(_protocolType) {} /// Processes a single frame returning complete packets. - std::vector demux(RLPXFrameCoder& _coder, bytes& _frame, bool _sequence = false, uint16_t _seq = 0, uint32_t _totalSize = 0) + std::vector demux(RLPXFrameCoder& _coder, bytesRef _frame, bool _sequence = false, uint16_t _seq = 0, uint32_t _totalSize = 0) { - if (!_coder.authAndDecryptFrame(&_frame)) + if (!_coder.authAndDecryptFrame(_frame)) BOOST_THROW_EXCEPTION(RLPXFrameDecrytFailed()); std::vector ret; @@ -66,7 +66,7 @@ public: RLPXPacket& p = m_incomplete.at(_seq).first; if (_frame.size() > remaining) return ret; - else if(p.streamIn(&_frame)) + else if(p.streamIn(_frame)) { ret.push_back(std::move(p)); m_incomplete.erase(_seq); @@ -79,7 +79,7 @@ public: m_incomplete.erase(_seq); } - bytesConstRef buffer(&_frame); + bytesConstRef buffer(_frame); while (!buffer.empty()) { auto type = RLPXPacket::nextRLP(buffer); @@ -125,8 +125,8 @@ class RLPXFrameWriter public: enum PacketPriority { PriorityLow = 0, PriorityHigh }; - static const uint16_t EmptyFrameLength = h128::size * 3; // header + headerMAC + frameMAC - static const uint16_t MinFrameDequeLength = h128::size * 4; // header + headerMAC + padded-block + frameMAC + static const uint16_t EmptyFrameLength; + static const uint16_t MinFrameDequeLength; RLPXFrameWriter(uint16_t _protocolType): m_protocolType(_protocolType) {} RLPXFrameWriter(RLPXFrameWriter const& _s): m_protocolType(_s.m_protocolType) {} diff --git a/test/libp2p/rlpx.cpp b/test/libp2p/rlpx.cpp index f2c602554..da582b988 100644 --- a/test/libp2p/rlpx.cpp +++ b/test/libp2p/rlpx.cpp @@ -497,15 +497,28 @@ BOOST_AUTO_TEST_CASE(readerWriter) BOOST_REQUIRE(encframes.size() == drains); for (auto const& c: encframes) { - BOOST_REQUIRE(c.size() == RLPXFrameWriter::MinFrameDequeLength); + BOOST_REQUIRE_EQUAL(c.size(), RLPXFrameWriter::MinFrameDequeLength); } // read and assemble dequed encframes vector packets; RLPXFrameReader r(0); -// for (auto const& b: encframes) -// packets.push_back(r.demux()); - + for (auto i = 0; i < encframes.size(); i++) + { + auto size = encframes[i].size(); + auto p = encframes[i].data(); + bytesRef frameWithHeader(encframes[i].data(), encframes[i].size()); + bytesRef h = frameWithHeader.cropped(0, 16); + bool decryptedHeader = coder.authAndDecryptHeader(h); + BOOST_REQUIRE(decryptedHeader); + bytesRef frame = frameWithHeader.cropped(16); + auto packets = r.demux(coder, frame); + if (packets.size()) + packets += move(packets); + } + BOOST_REQUIRE_EQUAL(packets.size(), 1); + BOOST_REQUIRE_EQUAL(packets.front().size(), payload.size()); + BOOST_REQUIRE_EQUAL(sha3(packets.front().data()), sha3(payload)); } BOOST_AUTO_TEST_SUITE_END()