Browse Source

Initial reader test. Something broken with encframes vector of bytes.

cl-refactor
subtly 10 years ago
parent
commit
5360ca1548
  1. 5
      libp2p/RLPXFrameWriter.cpp
  2. 12
      libp2p/RLPXFrameWriter.h
  3. 21
      test/libp2p/rlpx.cpp

5
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<bytes
_coder.writeFrame(m_protocolType, &payload, payload);
assert((int)frameLen - payload.size() >= 0);
frameLen -= payload.size();
o_toWrite.push_back(move(payload));
o_toWrite.push_back(payload);
payload.resize(0);
if (!qs.remaining)

12
libp2p/RLPXFrameWriter.h

@ -49,9 +49,9 @@ public:
RLPXFrameReader(uint16_t _protocolType): m_protocolType(_protocolType) {}
/// Processes a single frame returning complete packets.
std::vector<RLPXPacket> demux(RLPXFrameCoder& _coder, bytes& _frame, bool _sequence = false, uint16_t _seq = 0, uint32_t _totalSize = 0)
std::vector<RLPXPacket> 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<RLPXPacket> 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) {}

21
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<RLPXPacket> 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()

Loading…
Cancel
Save