2009-09-29 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Move all functions in PeerMessageUtil to bittorrent_helper.
	* src/BtBitfieldMessage.cc
	* src/BtBitfieldMessageValidator.h
	* src/BtExtendedMessage.cc
	* src/BtHandshakeMessage.cc
	* src/BtHandshakeMessageValidator.h
	* src/BtPieceMessage.cc
	* src/BtPieceMessageValidator.h
	* src/BtPortMessage.cc
	* src/DHTFindNodeReplyMessage.cc
	* src/DHTGetPeersReplyMessage.cc
	* src/DHTMessageFactoryImpl.cc
	* src/DHTRoutingTableDeserializer.cc
	* src/DHTRoutingTableSerializer.cc
	* src/DHTTokenTracker.cc
	* src/DefaultBtMessageFactory.cc
	* src/IndexBtMessage.cc
	* src/IndexBtMessage.h
	* src/IndexBtMessageValidator.h
	* src/Makefile.am
	* src/PeerMessageUtil.cc: Removed.
	* src/PeerMessageUtil.h: Removed.
	* src/RangeBtMessage.cc
	* src/RangeBtMessage.h
	* src/RangeBtMessageValidator.h
	* src/UTPexExtensionMessage.cc
	* src/ZeroBtMessage.cc
	* src/ZeroBtMessage.h
	* src/bittorrent_helper.cc
	* src/bittorrent_helper.h
	* test/BittorrentHelperTest.cc
	* test/BtAllowedFastMessageTest.cc
	* test/BtBitfieldMessageTest.cc
	* test/BtCancelMessageTest.cc
	* test/BtChokeMessageTest.cc
	* test/BtExtendedMessageTest.cc
	* test/BtHandshakeMessageTest.cc
	* test/BtHaveAllMessageTest.cc
	* test/BtHaveMessageTest.cc
	* test/BtHaveNoneMessageTest.cc
	* test/BtInterestedMessageTest.cc
	* test/BtNotInterestedMessageTest.cc
	* test/BtPieceMessageTest.cc
	* test/BtPortMessageTest.cc
	* test/BtRejectMessageTest.cc
	* test/BtRequestMessageTest.cc
	* test/BtSuggestPieceMessageTest.cc
	* test/BtUnchokeMessageTest.cc
	* test/DHTFindNodeReplyMessageTest.cc
	* test/DHTGetPeersReplyMessageTest.cc
	* test/DHTMessageFactoryImplTest.cc
	* test/DHTRoutingTableDeserializerTest.cc
	* test/DHTRoutingTableSerializerTest.cc
	* test/DefaultBtMessageFactoryTest.cc
	* test/DefaultExtensionMessageFactoryTest.cc
	* test/Makefile.am
	* test/PeerListProcessorTest.cc
	* test/PeerMessageUtilTest.cc: Removed.
	* test/UTPexExtensionMessageTest.cc
This commit is contained in:
Tatsuhiro Tsujikawa 2009-09-29 14:52:42 +00:00
parent 2e8b0d785e
commit 99654e4160
61 changed files with 607 additions and 630 deletions

View File

@ -1,3 +1,65 @@
2009-09-29 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Move all functions in PeerMessageUtil to bittorrent_helper.
* src/BtBitfieldMessage.cc
* src/BtBitfieldMessageValidator.h
* src/BtExtendedMessage.cc
* src/BtHandshakeMessage.cc
* src/BtHandshakeMessageValidator.h
* src/BtPieceMessage.cc
* src/BtPieceMessageValidator.h
* src/BtPortMessage.cc
* src/DHTFindNodeReplyMessage.cc
* src/DHTGetPeersReplyMessage.cc
* src/DHTMessageFactoryImpl.cc
* src/DHTRoutingTableDeserializer.cc
* src/DHTRoutingTableSerializer.cc
* src/DHTTokenTracker.cc
* src/DefaultBtMessageFactory.cc
* src/IndexBtMessage.cc
* src/IndexBtMessage.h
* src/IndexBtMessageValidator.h
* src/Makefile.am
* src/PeerMessageUtil.cc: Removed.
* src/PeerMessageUtil.h: Removed.
* src/RangeBtMessage.cc
* src/RangeBtMessage.h
* src/RangeBtMessageValidator.h
* src/UTPexExtensionMessage.cc
* src/ZeroBtMessage.cc
* src/ZeroBtMessage.h
* src/bittorrent_helper.cc
* src/bittorrent_helper.h
* test/BittorrentHelperTest.cc
* test/BtAllowedFastMessageTest.cc
* test/BtBitfieldMessageTest.cc
* test/BtCancelMessageTest.cc
* test/BtChokeMessageTest.cc
* test/BtExtendedMessageTest.cc
* test/BtHandshakeMessageTest.cc
* test/BtHaveAllMessageTest.cc
* test/BtHaveMessageTest.cc
* test/BtHaveNoneMessageTest.cc
* test/BtInterestedMessageTest.cc
* test/BtNotInterestedMessageTest.cc
* test/BtPieceMessageTest.cc
* test/BtPortMessageTest.cc
* test/BtRejectMessageTest.cc
* test/BtRequestMessageTest.cc
* test/BtSuggestPieceMessageTest.cc
* test/BtUnchokeMessageTest.cc
* test/DHTFindNodeReplyMessageTest.cc
* test/DHTGetPeersReplyMessageTest.cc
* test/DHTMessageFactoryImplTest.cc
* test/DHTRoutingTableDeserializerTest.cc
* test/DHTRoutingTableSerializerTest.cc
* test/DefaultBtMessageFactoryTest.cc
* test/DefaultExtensionMessageFactoryTest.cc
* test/Makefile.am
* test/PeerListProcessorTest.cc
* test/PeerMessageUtilTest.cc: Removed.
* test/UTPexExtensionMessageTest.cc
2009-09-29 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Limit the size of values list when sending get_peers reply.

View File

@ -36,7 +36,7 @@
#include <cstring>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Util.h"
#include "DlAbortEx.h"
#include "message.h"
@ -63,8 +63,8 @@ void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfie
BtBitfieldMessageHandle
BtBitfieldMessage::create(const unsigned char* data, size_t dataLength)
{
PeerMessageUtil::assertPayloadLengthGreater(1,dataLength, NAME);
PeerMessageUtil::assertID(ID, data, NAME);
bittorrent::assertPayloadLengthGreater(1,dataLength, NAME);
bittorrent::assertID(ID, data, NAME);
BtBitfieldMessageHandle message(new BtBitfieldMessage());
message->setBitfield((unsigned char*)data+1, dataLength-1);
return message;
@ -88,8 +88,7 @@ const unsigned char* BtBitfieldMessage::getMessage() {
*/
msgLength = 5+bitfieldLength;
msg = new unsigned char[msgLength];
PeerMessageUtil::createPeerMessageString(msg, msgLength,
1+bitfieldLength, ID);
bittorrent::createPeerMessageString(msg, msgLength, 1+bitfieldLength, ID);
memcpy(msg+5, bitfield, bitfieldLength);
}
return msg;

View File

@ -37,6 +37,7 @@
#include "BtMessageValidator.h"
#include "BtBitfieldMessage.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -52,9 +53,9 @@ public:
virtual bool validate(Errors& error) {
// TODO
PeerMessageUtil::checkBitfield(message->getBitfield(),
message->getBitfieldLength(),
numPiece);
bittorrent::checkBitfield(message->getBitfield(),
message->getBitfieldLength(),
numPiece);
return true;
}
};

View File

@ -39,7 +39,7 @@
#include "ExtensionMessage.h"
#include "ExtensionMessageFactory.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "DlAbortEx.h"
#include "message.h"
@ -76,7 +76,7 @@ const unsigned char* BtExtendedMessage::getMessage() {
std::string payload = _extensionMessage->getBencodedData();
_msgLength = 6+payload.size();
_msg = new unsigned char[_msgLength];
PeerMessageUtil::createPeerMessageString(_msg, _msgLength, 2+payload.size(), ID);
bittorrent::createPeerMessageString(_msg, _msgLength, 2+payload.size(), ID);
*(_msg+5) = _extensionMessage->getExtensionMessageID();
memcpy(_msg+6, payload.c_str(), payload.size());
}
@ -102,8 +102,8 @@ BtExtendedMessage::create(const SharedHandle<ExtensionMessageFactory>& factory,
const PeerHandle& peer,
const unsigned char* data, size_t dataLength)
{
PeerMessageUtil::assertPayloadLengthGreater(1, dataLength, NAME);
PeerMessageUtil::assertID(ID, data, NAME);
bittorrent::assertPayloadLengthGreater(1, dataLength, NAME);
bittorrent::assertID(ID, data, NAME);
assert(!factory.isNull());
ExtensionMessageHandle extmsg = factory->createMessage(data+1,
dataLength-1);

View File

@ -36,7 +36,6 @@
#include <cstring>
#include "PeerMessageUtil.h"
#include "Util.h"
#include "BtConstants.h"
#include "a2functional.h"
@ -45,7 +44,8 @@ namespace aria2 {
const std::string BtHandshakeMessage::NAME("handshake");
const unsigned char* BtHandshakeMessage::BT_PSTR = (const unsigned char*)"BitTorrent protocol";
const unsigned char* BtHandshakeMessage::BT_PSTR =
(const unsigned char*)"BitTorrent protocol";
BtHandshakeMessage::BtHandshakeMessage():SimpleBtMessage(ID, NAME)
{

View File

@ -41,7 +41,6 @@
#include "BtHandshakeMessage.h"
#include "Util.h"
#include "PeerMessageUtil.h"
#include "StringFormat.h"
namespace aria2 {

View File

@ -38,7 +38,7 @@
#include <cstdlib>
#include <cassert>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Util.h"
#include "message.h"
#include "DlAbortEx.h"
@ -67,11 +67,11 @@ void BtPieceMessage::setBlock(const unsigned char* block, size_t blockLength) {
}
BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, size_t dataLength) {
PeerMessageUtil::assertPayloadLengthGreater(9, dataLength, NAME);
PeerMessageUtil::assertID(ID, data, NAME);
bittorrent::assertPayloadLengthGreater(9, dataLength, NAME);
bittorrent::assertID(ID, data, NAME);
BtPieceMessageHandle message(new BtPieceMessage());
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
message->setIndex(bittorrent::getIntParam(data, 1));
message->setBegin(bittorrent::getIntParam(data, 5));
message->setBlock(data+9, dataLength-9);
return message;
}
@ -120,10 +120,10 @@ const unsigned char* BtPieceMessage::getMessageHeader() {
* total: 13bytes
*/
msgHeader = new unsigned char[MESSAGE_HEADER_LENGTH];
PeerMessageUtil::createPeerMessageString(msgHeader, MESSAGE_HEADER_LENGTH,
9+blockLength, ID);
PeerMessageUtil::setIntParam(&msgHeader[5], index);
PeerMessageUtil::setIntParam(&msgHeader[9], begin);
bittorrent::createPeerMessageString(msgHeader, MESSAGE_HEADER_LENGTH,
9+blockLength, ID);
bittorrent::setIntParam(&msgHeader[5], index);
bittorrent::setIntParam(&msgHeader[9], begin);
}
return msgHeader;
}

View File

@ -37,7 +37,7 @@
#include "BtMessageValidator.h"
#include "BtPieceMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -56,8 +56,8 @@ public:
virtual bool validate(Errors& error) {
// TODO
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
PeerMessageUtil::checkBegin(message->getBegin(), pieceLength);
bittorrent::checkIndex(message->getIndex(), numPiece);
bittorrent::checkBegin(message->getBegin(), pieceLength);
return true;
}
};

View File

@ -33,7 +33,7 @@
*/
/* copyright --> */
#include "BtPortMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "DlAbortEx.h"
#include "Util.h"
#include "message.h"
@ -61,9 +61,9 @@ BtPortMessage::~BtPortMessage()
SharedHandle<BtPortMessage> BtPortMessage::create(const unsigned char* data, size_t dataLength)
{
PeerMessageUtil::assertPayloadLengthEqual(3, dataLength, NAME);
PeerMessageUtil::assertID(ID, data, NAME);
uint16_t port = PeerMessageUtil::getShortIntParam(data, 1);
bittorrent::assertPayloadLengthEqual(3, dataLength, NAME);
bittorrent::assertID(ID, data, NAME);
uint16_t port = bittorrent::getShortIntParam(data, 1);
SharedHandle<BtPortMessage> message(new BtPortMessage(port));
return message;
}
@ -99,8 +99,8 @@ const unsigned char* BtPortMessage::getMessage() {
* total: 7bytes
*/
_msg = new unsigned char[MESSAGE_LENGTH];
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 3, ID);
PeerMessageUtil::setShortIntParam(&_msg[5], _port);
bittorrent::createPeerMessageString(_msg, MESSAGE_LENGTH, 3, ID);
bittorrent::setShortIntParam(&_msg[5], _port);
}
return _msg;
}

View File

@ -42,7 +42,7 @@
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageCallback.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Util.h"
#include "bencode.h"
@ -80,8 +80,8 @@ BDE DHTFindNodeReplyMessage::getResponse()
i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) {
SharedHandle<DHTNode> node = *i;
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
if(PeerMessageUtil::createcompact(buffer+20+offset, node->getIPAddress(),
node->getPort())) {
if(bittorrent::createcompact(buffer+20+offset, node->getIPAddress(),
node->getPort())) {
offset += 26;
}
}

View File

@ -42,7 +42,7 @@
#include "DHTMessageFactory.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageCallback.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "DHTUtil.h"
#include "Util.h"
@ -86,7 +86,8 @@ BDE DHTGetPeersReplyMessage::getResponse()
i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) {
SharedHandle<DHTNode> node = *i;
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);
if(PeerMessageUtil::createcompact(buffer+20+offset, node->getIPAddress(), node->getPort())) {
if(bittorrent::createcompact
(buffer+20+offset, node->getIPAddress(), node->getPort())) {
offset += 26;
}
}
@ -109,7 +110,7 @@ BDE DHTGetPeersReplyMessage::getResponse()
i != _values.end() && valuesList.size() < MAX_VALUES_SIZE; ++i) {
const SharedHandle<Peer>& peer = *i;
unsigned char buffer[6];
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
if(bittorrent::createcompact(buffer, peer->ipaddr, peer->port)) {
valuesList << BDE(buffer, sizeof(buffer));
}
}

View File

@ -55,7 +55,7 @@
#include "DHTPeerAnnounceStorage.h"
#include "DHTTokenTracker.h"
#include "DHTMessageCallback.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "BtRuntime.h"
#include "Util.h"
#include "Peer.h"
@ -332,7 +332,7 @@ DHTMessageFactoryImpl::extractNodes(const unsigned char* src, size_t length)
for(size_t offset = 0; offset < length; offset += 26) {
SharedHandle<DHTNode> node(new DHTNode(src+offset));
std::pair<std::string, uint16_t> addr =
PeerMessageUtil::unpackcompact(src+offset+DHT_ID_LENGTH);
bittorrent::unpackcompact(src+offset+DHT_ID_LENGTH);
if(addr.first.empty()) {
continue;
}
@ -416,7 +416,7 @@ DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues
const BDE& data = *i;
if(data.isString() && data.s().size() == 6) {
std::pair<std::string, uint16_t> addr =
PeerMessageUtil::unpackcompact(data.uc());
bittorrent::unpackcompact(data.uc());
PeerHandle peer(new Peer(addr.first, addr.second));
peers.push_back(peer);
}

View File

@ -42,7 +42,7 @@
#include "DHTNode.h"
#include "DHTConstants.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "DlAbortEx.h"
#include "Logger.h"
#include "a2netcompat.h"
@ -182,8 +182,7 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
CHECK_STREAM(in, 42);
continue;
}
std::pair<std::string, uint16_t> peer =
PeerMessageUtil::unpackcompact(buf);
std::pair<std::string, uint16_t> peer = bittorrent::unpackcompact(buf);
if(peer.first.empty()) {
// skip this entry
readBytes(buf, buf.size(), in, 42);

View File

@ -33,17 +33,19 @@
*/
/* copyright --> */
#include "DHTRoutingTableSerializer.h"
#include <cerrno>
#include <cstring>
#include <ostream>
#include "DHTNode.h"
#include "DlAbortEx.h"
#include "DHTConstants.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Logger.h"
#include "a2netcompat.h"
#include "StringFormat.h"
#include "Util.h"
#include <cerrno>
#include <cstring>
#include <ostream>
namespace aria2 {
@ -102,7 +104,8 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
// Currently, only IPv4 address and IPv4-mapped address are saved.
// 6bytes: write IP address + port in Compact IP-address/port info form.
unsigned char compactPeer[6];
if(!PeerMessageUtil::createcompact(compactPeer, node->getIPAddress(), node->getPort())) {
if(!bittorrent::createcompact
(compactPeer, node->getIPAddress(), node->getPort())) {
memset(compactPeer, 0, 6);
}
// 1byte compact peer format length

View File

@ -33,13 +33,15 @@
*/
/* copyright --> */
#include "DHTTokenTracker.h"
#include <cstring>
#include "DHTUtil.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "DlAbortEx.h"
#include "DHTConstants.h"
#include "MessageDigestHelper.h"
#include "StringFormat.h"
#include <cstring>
namespace aria2 {
@ -62,7 +64,7 @@ std::string DHTTokenTracker::generateToken(const unsigned char* infoHash,
const unsigned char* secret) const
{
unsigned char src[DHT_ID_LENGTH+6+SECRET_SIZE];
if(!PeerMessageUtil::createcompact(src+DHT_ID_LENGTH, ipaddr, port)) {
if(!bittorrent::createcompact(src+DHT_ID_LENGTH, ipaddr, port)) {
throw DL_ABORT_EX
(StringFormat("Token generation failed: ipaddr=%s, port=%u",
ipaddr.c_str(), port).str());

View File

@ -34,7 +34,7 @@
/* copyright --> */
#include "DefaultBtMessageFactory.h"
#include "DlAbortEx.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "BtKeepAliveMessage.h"
#include "BtChokeMessage.h"
#include "BtUnchokeMessage.h"
@ -84,7 +84,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
// keep-alive
msg.reset(new BtKeepAliveMessage());
} else {
uint8_t id = PeerMessageUtil::getId(data);
uint8_t id = bittorrent::getId(data);
switch(id) {
case BtChokeMessage::ID:
msg = BtChokeMessage::create(data, dataLength);

View File

@ -35,6 +35,7 @@
#include "IndexBtMessage.h"
#include "Util.h"
#include "a2functional.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -48,8 +49,8 @@ const unsigned char* IndexBtMessage::getMessage()
* total: 9bytes
*/
_msg = new unsigned char[MESSAGE_LENGTH];
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 5, getId());
PeerMessageUtil::setIntParam(&_msg[5], _index);
bittorrent::createPeerMessageString(_msg, MESSAGE_LENGTH, 5, getId());
bittorrent::setIntParam(&_msg[5], _index);
}
return _msg;
}

View File

@ -36,7 +36,7 @@
#define _D_INDEX_BT_MESSAGE_H_
#include "SimpleBtMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -50,10 +50,10 @@ protected:
template<typename T>
static SharedHandle<T> create(const unsigned char* data, size_t dataLength)
{
PeerMessageUtil::assertPayloadLengthEqual(5, dataLength, T::NAME);
PeerMessageUtil::assertID(T::ID, data, T::NAME);
bittorrent::assertPayloadLengthEqual(5, dataLength, T::NAME);
bittorrent::assertID(T::ID, data, T::NAME);
SharedHandle<T> message(new T());
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
message->setIndex(bittorrent::getIntParam(data, 1));
return message;
}
public:

View File

@ -37,7 +37,7 @@
#include "BtMessageValidator.h"
#include "IndexBtMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -54,7 +54,7 @@ public:
virtual bool validate(Errors& errors)
{
// TODO
PeerMessageUtil::checkIndex(_message->getIndex(), _numPiece);
bittorrent::checkIndex(_message->getIndex(), _numPiece);
return true;
}

View File

@ -275,8 +275,7 @@ SRCS += IteratableChunkChecksumValidator.cc IteratableChunkChecksumValidator.h\
endif # ENABLE_MESSAGE_DIGEST
if ENABLE_BITTORRENT
SRCS += PeerMessageUtil.cc PeerMessageUtil.h\
PeerAbstractCommand.cc PeerAbstractCommand.h\
SRCS += PeerAbstractCommand.cc PeerAbstractCommand.h\
PeerInitiateConnectionCommand.cc PeerInitiateConnectionCommand.h\
PeerInteractionCommand.cc PeerInteractionCommand.h\
Peer.cc Peer.h\

View File

@ -72,8 +72,7 @@ bin_PROGRAMS = aria2c$(EXEEXT)
@ENABLE_MESSAGE_DIGEST_TRUE@ Checksum.h\
@ENABLE_MESSAGE_DIGEST_TRUE@ ChunkChecksum.h
@ENABLE_BITTORRENT_TRUE@am__append_13 = PeerMessageUtil.cc PeerMessageUtil.h\
@ENABLE_BITTORRENT_TRUE@ PeerAbstractCommand.cc PeerAbstractCommand.h\
@ENABLE_BITTORRENT_TRUE@am__append_13 = PeerAbstractCommand.cc PeerAbstractCommand.h\
@ENABLE_BITTORRENT_TRUE@ PeerInitiateConnectionCommand.cc PeerInitiateConnectionCommand.h\
@ENABLE_BITTORRENT_TRUE@ PeerInteractionCommand.cc PeerInteractionCommand.h\
@ENABLE_BITTORRENT_TRUE@ Peer.cc Peer.h\
@ -455,8 +454,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
CheckIntegrityCommand.h ChecksumCheckIntegrityEntry.cc \
ChecksumCheckIntegrityEntry.h messageDigest.cc messageDigest.h \
MessageDigestHelper.cc MessageDigestHelper.h Checksum.h \
ChunkChecksum.h PeerMessageUtil.cc PeerMessageUtil.h \
PeerAbstractCommand.cc PeerAbstractCommand.h \
ChunkChecksum.h PeerAbstractCommand.cc PeerAbstractCommand.h \
PeerInitiateConnectionCommand.cc \
PeerInitiateConnectionCommand.h PeerInteractionCommand.cc \
PeerInteractionCommand.h Peer.cc Peer.h PeerListenCommand.cc \
@ -627,7 +625,7 @@ am__objects_6 =
@ENABLE_MESSAGE_DIGEST_TRUE@ ChecksumCheckIntegrityEntry.$(OBJEXT) \
@ENABLE_MESSAGE_DIGEST_TRUE@ messageDigest.$(OBJEXT) \
@ENABLE_MESSAGE_DIGEST_TRUE@ MessageDigestHelper.$(OBJEXT)
@ENABLE_BITTORRENT_TRUE@am__objects_13 = PeerMessageUtil.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@am__objects_13 = \
@ENABLE_BITTORRENT_TRUE@ PeerAbstractCommand.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ PeerInitiateConnectionCommand.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ PeerInteractionCommand.$(OBJEXT) \
@ -1510,7 +1508,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerInitiateConnectionCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerInteractionCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerListenCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerMessageUtil.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerReceiveHandshakeCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerSessionResource.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Piece.Po@am__quote@

View File

@ -1,216 +0,0 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "PeerMessageUtil.h"
#include <cassert>
#include <cstring>
#include "DlAbortEx.h"
#include "a2netcompat.h"
#include "StringFormat.h"
#include "BtConstants.h"
#include "message.h"
#include "StringFormat.h"
namespace aria2 {
uint8_t PeerMessageUtil::getId(const unsigned char* msg) {
return msg[0];
}
uint32_t PeerMessageUtil::getIntParam(const unsigned char* msg, size_t pos)
{
uint32_t nParam;
memcpy(&nParam, msg+pos, sizeof(nParam));
return ntohl(nParam);
}
uint16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, size_t pos) {
uint16_t nParam;
memcpy(&nParam, msg+pos, sizeof(nParam));
return ntohs(nParam);
}
void PeerMessageUtil::checkIndex(size_t index, size_t pieces) {
if(!(index < pieces)) {
throw DL_ABORT_EX(StringFormat("Invalid index: %lu",
static_cast<unsigned long>(index)).str());
}
}
void PeerMessageUtil::checkBegin(uint32_t begin, size_t pieceLength) {
if(!(begin < pieceLength)) {
throw DL_ABORT_EX(StringFormat("Invalid begin: %u", begin).str());
}
}
void PeerMessageUtil::checkLength(size_t length) {
if(length > MAX_BLOCK_LENGTH) {
throw DL_ABORT_EX(StringFormat("Length too long: %lu > %uKB",
static_cast<unsigned long>(length),
MAX_BLOCK_LENGTH/1024).str());
}
if(length == 0) {
throw DL_ABORT_EX(StringFormat("Invalid length: %lu",
static_cast<unsigned long>(length)).str());
}
}
void PeerMessageUtil::checkRange(uint32_t begin, size_t length, size_t pieceLength) {
if(!(0 < length)) {
throw DL_ABORT_EX(StringFormat("Invalid range: begin=%u, length=%lu",
begin,
static_cast<unsigned long>(length)).str());
}
uint32_t end = begin+length;
if(!(end <= pieceLength)) {
throw DL_ABORT_EX(StringFormat("Invalid range: begin=%u, length=%lu",
begin,
static_cast<unsigned long>(length)).str());
}
}
void PeerMessageUtil::checkBitfield(const unsigned char* bitfield,
size_t bitfieldLength,
size_t pieces) {
if(!(bitfieldLength == (pieces+7)/8)) {
throw DL_ABORT_EX
(StringFormat("Invalid bitfield length: %lu",
static_cast<unsigned long>(bitfieldLength)).str());
}
char lastbyte = bitfield[bitfieldLength-1];
for(size_t i = 0; i < 8-pieces%8 && pieces%8 != 0; ++i) {
if(!(((lastbyte >> i) & 1) == 0)) {
throw DL_ABORT_EX("Invalid bitfield");
}
}
}
void PeerMessageUtil::setIntParam(unsigned char* dest, uint32_t param) {
uint32_t nParam = htonl(param);
memcpy(dest, &nParam, sizeof(nParam));
}
void PeerMessageUtil::setShortIntParam(unsigned char* dest, uint16_t param) {
uint16_t nParam = htons(param);
memcpy(dest, &nParam, sizeof(nParam));
}
void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
size_t msgLength,
size_t payloadLength,
uint8_t messageId) {
assert(msgLength >= 5);
memset(msg, 0, msgLength);
setIntParam(msg, payloadLength);
msg[4] = messageId;
}
bool
PeerMessageUtil::createcompact(unsigned char* compact, const std::string& addr, uint16_t port)
{
struct addrinfo hints;
struct addrinfo* res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // since compact peer format is ipv4 only.
hints.ai_flags = AI_NUMERICHOST;
if(getaddrinfo(addr.c_str(), 0, &hints, &res)) {
return false;
}
struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
uint32_t* addrp = (uint32_t*)compact;
*addrp = in->sin_addr.s_addr;
uint16_t* portp = (uint16_t*)(compact+4);
*portp = htons(port);
freeaddrinfo(res);
return true;
}
std::pair<std::string, uint16_t>
PeerMessageUtil::unpackcompact(const unsigned char* compact)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
#ifdef HAVE_SOCKADDR_IN_SIN_LEN
// For netbsd
in.sin_len = sizeof(in);
#endif // HAVE_SOCKADDR_IN_SIN_LEN
in.sin_family = AF_INET;
in.sin_addr.s_addr = *reinterpret_cast<const uint32_t*>(compact);
in.sin_port = 0;
char host[NI_MAXHOST];
int s;
s = getnameinfo(reinterpret_cast<const struct sockaddr*>(&in), sizeof(in),
host, NI_MAXHOST, 0, NI_MAXSERV,
NI_NUMERICHOST);
if(s) {
return std::pair<std::string, uint16_t>();
}
uint16_t port = ntohs(*(uint16_t*)(compact+sizeof(uint32_t)));
return std::pair<std::string, uint16_t>(host, port);
}
void PeerMessageUtil::assertPayloadLengthGreater
(size_t threshold, size_t actual, const std::string& msgName)
{
if(actual <= threshold) {
throw DL_ABORT_EX
(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(), actual).str());
}
}
void PeerMessageUtil::assertPayloadLengthEqual
(size_t expected, size_t actual, const std::string& msgName)
{
if(expected != actual) {
throw DL_ABORT_EX
(StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(),
actual, expected).str());
}
}
void PeerMessageUtil::assertID
(uint8_t expected, const unsigned char* data, const std::string& msgName)
{
uint8_t id = getId(data);
if(expected != id) {
throw DL_ABORT_EX
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(),
expected).str());
}
}
} // namespace aria2

View File

@ -1,98 +0,0 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#ifndef _D_PEER_MESSAGE_UTIL_H_
#define _D_PEER_MESSAGE_UTIL_H_
#include "common.h"
#include <string>
#include <utility>
namespace aria2 {
class PeerMessageUtil {
private:
PeerMessageUtil() {}
public:
static uint32_t getIntParam(const unsigned char* msg, size_t pos);
static uint16_t getShortIntParam(const unsigned char* msg, size_t pos);
static void setIntParam(unsigned char* dest, uint32_t param);
static void setShortIntParam(unsigned char* dest, uint16_t param);
static uint8_t getId(const unsigned char* msg);
static void checkIndex(size_t index, size_t pieces);
static void checkBegin(uint32_t begin, size_t pieceLength);
static void checkLength(size_t length);
static void checkRange(uint32_t begin, size_t length, size_t pieceLength);
static void checkBitfield(const unsigned char* bitfield,
size_t bitfieldLength,
size_t pieces);
static void createPeerMessageString(unsigned char* msg,
size_t msgLength,
size_t payloadLength,
uint8_t messageId);
/**
* Creates compact tracker format(6bytes for ipv4 address and port)
* and stores the results in compact.
* compact must be at least 6 bytes and pre-allocated.
* Returns true if creation is successful, otherwise returns false.
* The example of failure reason is that addr is not numbers-and-dots
* notation.
*/
static bool createcompact(unsigned char* compact, const std::string& addr, uint16_t port);
static std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact);
// Throws exception if threshold >= actual
static void assertPayloadLengthGreater(size_t threshold, size_t actual,
const std::string& msgName);
// Throws exception if expected != actual
static void assertPayloadLengthEqual(size_t expected, size_t actual,
const std::string& msgName);
// Throws exception if expected is not equal to id from data.
static void assertID(uint8_t expected, const unsigned char* data,
const std::string& msgName);
};
} // namespace aria2
#endif // _D_PEER_MESSAGE_UTIL_H_

View File

@ -35,6 +35,7 @@
#include "RangeBtMessage.h"
#include "Util.h"
#include "a2functional.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -64,10 +65,10 @@ const unsigned char* RangeBtMessage::getMessage()
* total: 17bytes
*/
_msg = new unsigned char[MESSAGE_LENGTH];
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 13, getId());
PeerMessageUtil::setIntParam(&_msg[5], _index);
PeerMessageUtil::setIntParam(&_msg[9], _begin);
PeerMessageUtil::setIntParam(&_msg[13], _length);
bittorrent::createPeerMessageString(_msg, MESSAGE_LENGTH, 13, getId());
bittorrent::setIntParam(&_msg[5], _index);
bittorrent::setIntParam(&_msg[9], _begin);
bittorrent::setIntParam(&_msg[13], _length);
}
return _msg;
}

View File

@ -36,7 +36,7 @@
#define _D_RANGE_BT_MESSAGE_H_
#include "SimpleBtMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -53,12 +53,12 @@ protected:
static SharedHandle<T> create
(const unsigned char* data, size_t dataLength)
{
PeerMessageUtil::assertPayloadLengthEqual(13, dataLength, T::NAME);
PeerMessageUtil::assertID(T::ID, data, T::NAME);
bittorrent::assertPayloadLengthEqual(13, dataLength, T::NAME);
bittorrent::assertID(T::ID, data, T::NAME);
SharedHandle<T> message(new T());
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
message->setLength(PeerMessageUtil::getIntParam(data, 9));
message->setIndex(bittorrent::getIntParam(data, 1));
message->setBegin(bittorrent::getIntParam(data, 5));
message->setLength(bittorrent::getIntParam(data, 9));
return message;
}
public:

View File

@ -37,7 +37,7 @@
#include "BtMessageValidator.h"
#include "RangeBtMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -57,12 +57,12 @@ public:
virtual bool validate(Errors& errors)
{
// TODO
PeerMessageUtil::checkIndex(_message->getIndex(), _numPiece);
PeerMessageUtil::checkBegin(_message->getBegin(), _pieceLength);
PeerMessageUtil::checkLength(_message->getLength());
PeerMessageUtil::checkRange(_message->getBegin(),
_message->getLength(),
_pieceLength);
bittorrent::checkIndex(_message->getIndex(), _numPiece);
bittorrent::checkBegin(_message->getBegin(), _pieceLength);
bittorrent::checkLength(_message->getLength());
bittorrent::checkRange(_message->getBegin(),
_message->getLength(),
_pieceLength);
return true;
}
};

View File

@ -35,7 +35,7 @@
#include "UTPexExtensionMessage.h"
#include "Peer.h"
#include "Util.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "PeerStorage.h"
#include "PeerListProcessor.h"
#include "DlAbortEx.h"
@ -77,7 +77,7 @@ UTPexExtensionMessage::createCompactPeerListAndFlag(const Peers& peers)
std::string flagstring;
for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); ++itr) {
unsigned char compact[6];
if(PeerMessageUtil::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
if(bittorrent::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
addrstring.append(&compact[0], &compact[6]);
flagstring += (*itr)->isSeeder() ? "2" : "0";
}

View File

@ -33,6 +33,7 @@
*/
/* copyright --> */
#include "ZeroBtMessage.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -45,7 +46,7 @@ const unsigned char* ZeroBtMessage::getMessage()
* total: 5bytes
*/
_msg = new unsigned char[MESSAGE_LENGTH];
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 1, getId());
bittorrent::createPeerMessageString(_msg, MESSAGE_LENGTH, 1, getId());
}
return _msg;
}

View File

@ -36,7 +36,7 @@
#define _D_ZERO_BT_MESSAGE_H_
#include "SimpleBtMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -49,8 +49,8 @@ protected:
template<typename T>
static SharedHandle<T> create(const unsigned char* data, size_t dataLength)
{
PeerMessageUtil::assertPayloadLengthEqual(1, dataLength, T::NAME);
PeerMessageUtil::assertID(T::ID, data, T::NAME);
bittorrent::assertPayloadLengthEqual(1, dataLength, T::NAME);
bittorrent::assertID(T::ID, data, T::NAME);
SharedHandle<T> message(new T());
return message;
}

View File

@ -34,9 +34,9 @@
/* copyright --> */
#include "bittorrent_helper.h"
#include <cassert>
#include <cstring>
#include <algorithm>
#include <deque>
#include "DownloadContext.h"
#include "Randomizer.h"
@ -48,8 +48,10 @@
#include "BtConstants.h"
#include "messageDigest.h"
#include "MessageDigestHelper.h"
#include "PeerMessageUtil.h"
#include "SimpleRandomizer.h"
#include "a2netcompat.h"
#include "BtConstants.h"
#include "bitfield.h"
namespace aria2 {
@ -582,7 +584,7 @@ void computeFastSet
size_t numPieces, const unsigned char* infoHash, size_t fastSetSize)
{
unsigned char compact[6];
if(!PeerMessageUtil::createcompact(compact, ipaddr, 0)) {
if(!createcompact(compact, ipaddr, 0)) {
return;
}
if(numPieces < fastSetSize) {
@ -655,6 +657,181 @@ const unsigned char* getStaticPeerId()
}
}
uint8_t getId(const unsigned char* msg)
{
return msg[0];
}
uint32_t getIntParam(const unsigned char* msg, size_t pos)
{
uint32_t nParam;
memcpy(&nParam, msg+pos, sizeof(nParam));
return ntohl(nParam);
}
uint16_t getShortIntParam(const unsigned char* msg, size_t pos)
{
uint16_t nParam;
memcpy(&nParam, msg+pos, sizeof(nParam));
return ntohs(nParam);
}
void checkIndex(size_t index, size_t pieces)
{
if(!(index < pieces)) {
throw DL_ABORT_EX(StringFormat("Invalid index: %lu",
static_cast<unsigned long>(index)).str());
}
}
void checkBegin(uint32_t begin, size_t pieceLength)
{
if(!(begin < pieceLength)) {
throw DL_ABORT_EX(StringFormat("Invalid begin: %u", begin).str());
}
}
void checkLength(size_t length)
{
if(length > MAX_BLOCK_LENGTH) {
throw DL_ABORT_EX
(StringFormat("Length too long: %lu > %uKB",
static_cast<unsigned long>(length),
MAX_BLOCK_LENGTH/1024).str());
}
if(length == 0) {
throw DL_ABORT_EX
(StringFormat("Invalid length: %lu",
static_cast<unsigned long>(length)).str());
}
}
void checkRange(uint32_t begin, size_t length, size_t pieceLength)
{
if(!(0 < length)) {
throw DL_ABORT_EX
(StringFormat("Invalid range: begin=%u, length=%lu",
begin,
static_cast<unsigned long>(length)).str());
}
uint32_t end = begin+length;
if(!(end <= pieceLength)) {
throw DL_ABORT_EX
(StringFormat("Invalid range: begin=%u, length=%lu",
begin,
static_cast<unsigned long>(length)).str());
}
}
void checkBitfield
(const unsigned char* bitfield, size_t bitfieldLength, size_t pieces)
{
if(!(bitfieldLength == (pieces+7)/8)) {
throw DL_ABORT_EX
(StringFormat("Invalid bitfield length: %lu",
static_cast<unsigned long>(bitfieldLength)).str());
}
// Check if last byte contains garbage set bit.
if(bitfield[bitfieldLength-1]&~bitfield::lastByteMask(pieces)) {
throw DL_ABORT_EX("Invalid bitfield");
}
}
void setIntParam(unsigned char* dest, uint32_t param)
{
uint32_t nParam = htonl(param);
memcpy(dest, &nParam, sizeof(nParam));
}
void setShortIntParam(unsigned char* dest, uint16_t param)
{
uint16_t nParam = htons(param);
memcpy(dest, &nParam, sizeof(nParam));
}
void createPeerMessageString
(unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId)
{
assert(msgLength >= 5);
memset(msg, 0, msgLength);
setIntParam(msg, payloadLength);
msg[4] = messageId;
}
bool createcompact
(unsigned char* compact, const std::string& addr, uint16_t port)
{
struct addrinfo hints;
struct addrinfo* res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // since compact peer format is ipv4 only.
hints.ai_flags = AI_NUMERICHOST;
if(getaddrinfo(addr.c_str(), 0, &hints, &res)) {
return false;
}
struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
uint32_t* addrp = (uint32_t*)compact;
*addrp = in->sin_addr.s_addr;
uint16_t* portp = (uint16_t*)(compact+4);
*portp = htons(port);
freeaddrinfo(res);
return true;
}
std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
#ifdef HAVE_SOCKADDR_IN_SIN_LEN
// For netbsd
in.sin_len = sizeof(in);
#endif // HAVE_SOCKADDR_IN_SIN_LEN
in.sin_family = AF_INET;
in.sin_addr.s_addr = *reinterpret_cast<const uint32_t*>(compact);
in.sin_port = 0;
char host[NI_MAXHOST];
int s;
s = getnameinfo(reinterpret_cast<const struct sockaddr*>(&in), sizeof(in),
host, NI_MAXHOST, 0, NI_MAXSERV,
NI_NUMERICHOST);
if(s) {
return std::pair<std::string, uint16_t>();
}
uint16_t port = ntohs(*(uint16_t*)(compact+sizeof(uint32_t)));
return std::pair<std::string, uint16_t>(host, port);
}
void assertPayloadLengthGreater
(size_t threshold, size_t actual, const std::string& msgName)
{
if(actual <= threshold) {
throw DL_ABORT_EX
(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(), actual).str());
}
}
void assertPayloadLengthEqual
(size_t expected, size_t actual, const std::string& msgName)
{
if(expected != actual) {
throw DL_ABORT_EX
(StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(),
actual, expected).str());
}
}
void assertID
(uint8_t expected, const unsigned char* data, const std::string& msgName)
{
uint8_t id = getId(data);
if(expected != id) {
throw DL_ABORT_EX
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(),
expected).str());
}
}
} // namespace bittorrent
} // namespace aria2

View File

@ -40,6 +40,7 @@
#include <string>
#include <vector>
#include <deque>
#include <utility>
#include "SharedHandle.h"
#include "AnnounceTier.h"
@ -151,6 +152,64 @@ getInfoHash(const SharedHandle<DownloadContext>& downloadContext);
std::string
getInfoHashString(const SharedHandle<DownloadContext>& downloadContext);
// Returns 4bytes unsigned integer located at offset pos. The integer
// in msg is network byte order. This function converts it into host
// byte order and returns it.
uint32_t getIntParam(const unsigned char* msg, size_t pos);
// Returns 2bytes unsigned integer located at offset pos. The integer
// in msg is network byte order. This function converts it into host
// byte order and returns it.
uint16_t getShortIntParam(const unsigned char* msg, size_t pos);
// Put param at location pointed by dest. param is converted into
// network byte order.
void setIntParam(unsigned char* dest, uint32_t param);
// Put param at location pointed by dest. param is converted into
// network byte order.
void setShortIntParam(unsigned char* dest, uint16_t param);
// Returns message ID located at first byte:msg[0]
uint8_t getId(const unsigned char* msg);
void checkIndex(size_t index, size_t pieces);
void checkBegin(uint32_t begin, size_t pieceLength);
void checkLength(size_t length);
void checkRange(uint32_t begin, size_t length, size_t pieceLength);
void checkBitfield
(const unsigned char* bitfield, size_t bitfieldLength, size_t pieces);
// Initialize msg with 0 and set payloadLength and messageId.
void createPeerMessageString
(unsigned char* msg, size_t msgLength, size_t payloadLength, uint8_t messageId);
/**
* Creates compact tracker format(6bytes for ipv4 address and port)
* and stores the results in compact.
* compact must be at least 6 bytes and pre-allocated.
* Returns true if creation is successful, otherwise returns false.
* The example of failure reason is that addr is not numbers-and-dots
* notation.
*/
bool createcompact
(unsigned char* compact, const std::string& addr, uint16_t port);
// Unpack compact into pair of IPv4 address and port.
std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact);
// Throws exception if threshold >= actual
void assertPayloadLengthGreater
(size_t threshold, size_t actual, const std::string& msgName);
// Throws exception if expected != actual
void assertPayloadLengthEqual
(size_t expected, size_t actual, const std::string& msgName);
// Throws exception if expected is not equal to id from data.
void assertID
(uint8_t expected, const unsigned char* data, const std::string& msgName);
} // namespace bittorrent
} // namespace aria2

View File

@ -13,6 +13,7 @@
#include "FileEntry.h"
#include "array_fun.h"
#include "messageDigest.h"
#include "a2netcompat.h"
namespace aria2 {
@ -52,6 +53,8 @@ class BittorrentHelperTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testSetFileFilter_multi);
CPPUNIT_TEST(testUTF8Torrent);
CPPUNIT_TEST(testMetaData);
CPPUNIT_TEST(testCreatecompact);
CPPUNIT_TEST(testCheckBitfield);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
@ -88,6 +91,8 @@ public:
void testSetFileFilter_multi();
void testUTF8Torrent();
void testMetaData();
void testCreatecompact();
void testCheckBitfield();
};
@ -643,6 +648,38 @@ void BittorrentHelperTest::testMetaData()
dctx->getAttribute(BITTORRENT)[CREATION_DATE].i());
}
void BittorrentHelperTest::testCreatecompact()
{
unsigned char compact[6];
// Note: bittorrent::createcompact() on linux can handle IPv4-mapped
// addresses like `ffff::127.0.0.1', but on cygwin, it doesn't.
CPPUNIT_ASSERT(createcompact(compact, "127.0.0.1", 6881));
std::pair<std::string, uint16_t> p = unpackcompact(compact);
CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), p.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
}
void BittorrentHelperTest::testCheckBitfield()
{
unsigned char bitfield[] = { 0xff, 0xe0 };
checkBitfield(bitfield, sizeof(bitfield), 11);
try {
checkBitfield(bitfield, sizeof(bitfield), 17);
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// success
}
// Change last byte
bitfield[1] = 0xf0;
try {
checkBitfield(bitfield, sizeof(bitfield), 11);
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// success
}
}
} // namespace bittorrent
} // namespace aria2

View File

@ -1,10 +1,12 @@
#include "BtAllowedFastMessage.h"
#include "PeerMessageUtil.h"
#include "Util.h"
#include "Peer.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "bittorrent_helper.h"
#include "Util.h"
#include "Peer.h"
namespace aria2 {
class BtAllowedFastMessageTest:public CppUnit::TestFixture {
@ -34,8 +36,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtAllowedFastMessageTest);
void BtAllowedFastMessageTest::testCreate() {
unsigned char msg[9];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 17);
PeerMessageUtil::setIntParam(&msg[5], 12345);
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 17);
bittorrent::setIntParam(&msg[5], 12345);
SharedHandle<BtAllowedFastMessage> pm = BtAllowedFastMessage::create(&msg[4], 5);
CPPUNIT_ASSERT_EQUAL((uint8_t)17, pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
@ -43,7 +45,7 @@ void BtAllowedFastMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[10];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 6, 17);
bittorrent::createPeerMessageString(msg, sizeof(msg), 6, 17);
BtAllowedFastMessage::create(&msg[4], 6);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -51,7 +53,7 @@ void BtAllowedFastMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[9];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 18);
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 18);
BtAllowedFastMessage::create(&msg[4], 5);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -62,8 +64,8 @@ void BtAllowedFastMessageTest::testGetMessage() {
BtAllowedFastMessage msg;
msg.setIndex(12345);
unsigned char data[9];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 5, 17);
PeerMessageUtil::setIntParam(&data[5], 12345);
bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
bittorrent::setIntParam(&data[5], 12345);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 9) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Util.h"
#include "Peer.h"
#include "MockPieceStorage.h"
@ -39,7 +39,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtBitfieldMessageTest);
void BtBitfieldMessageTest::testCreate() {
unsigned char msg[5+2];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 5);
bittorrent::createPeerMessageString(msg, sizeof(msg), 3, 5);
unsigned char bitfield[2];
memset(bitfield, 0xff, sizeof(bitfield));
memcpy(&msg[5], bitfield, sizeof(bitfield));
@ -50,7 +50,7 @@ void BtBitfieldMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 5);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 5);
BtBitfieldMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -58,7 +58,7 @@ void BtBitfieldMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5+2];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 6);
bittorrent::createPeerMessageString(msg, sizeof(msg), 3, 6);
BtBitfieldMessage::create(&msg[4], 3);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -71,7 +71,7 @@ void BtBitfieldMessageTest::testGetMessage() {
memset(bitfield, 0xff, sizeof(bitfield));
msg.setBitfield(bitfield, sizeof(bitfield));
unsigned char data[5+2];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 5);
bittorrent::createPeerMessageString(data, sizeof(data), 3, 5);
memcpy(&data[5], bitfield, sizeof(bitfield));
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 7) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "MockBtMessageDispatcher.h"
#include "Peer.h"
#include "FileEntry.h"
@ -53,10 +53,10 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtCancelMessageTest);
void BtCancelMessageTest::testCreate() {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 8);
PeerMessageUtil::setIntParam(&msg[5], 12345);
PeerMessageUtil::setIntParam(&msg[9], 256);
PeerMessageUtil::setIntParam(&msg[13], 1024);
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 8);
bittorrent::setIntParam(&msg[5], 12345);
bittorrent::setIntParam(&msg[9], 256);
bittorrent::setIntParam(&msg[13], 1024);
SharedHandle<BtCancelMessage> pm = BtCancelMessage::create(&msg[4], 13);
CPPUNIT_ASSERT_EQUAL((uint8_t)8, pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
@ -66,7 +66,7 @@ void BtCancelMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[18];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 8);
bittorrent::createPeerMessageString(msg, sizeof(msg), 14, 8);
BtCancelMessage::create(&msg[4], 14);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -74,7 +74,7 @@ void BtCancelMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 9);
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 9);
BtCancelMessage::create(&msg[4], 13);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -87,10 +87,10 @@ void BtCancelMessageTest::testGetMessage() {
msg.setBegin(256);
msg.setLength(1024);
unsigned char data[17];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 8);
PeerMessageUtil::setIntParam(&data[5], 12345);
PeerMessageUtil::setIntParam(&data[9], 256);
PeerMessageUtil::setIntParam(&data[13], 1024);
bittorrent::createPeerMessageString(data, sizeof(data), 13, 8);
bittorrent::setIntParam(&data[5], 12345);
bittorrent::setIntParam(&data[9], 256);
bittorrent::setIntParam(&data[13], 1024);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "MockBtMessageDispatcher.h"
#include "MockBtRequestFactory.h"
#include "Peer.h"
@ -72,14 +72,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtChokeMessageTest);
void BtChokeMessageTest::testCreate() {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 0);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 0);
SharedHandle<BtChokeMessage> pm = BtChokeMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL((uint8_t)0, pm->getId());
// case: payload size is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 0);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 0);
BtChokeMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -87,7 +87,7 @@ void BtChokeMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 1);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 1);
BtChokeMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -97,7 +97,7 @@ void BtChokeMessageTest::testCreate() {
void BtChokeMessageTest::testGetMessage() {
BtChokeMessage msg;
unsigned char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 0);
bittorrent::createPeerMessageString(data, sizeof(data), 1, 0);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}

View File

@ -5,7 +5,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "MockExtensionMessageFactory.h"
#include "Peer.h"
#include "Exception.h"
@ -42,8 +42,7 @@ void BtExtendedMessageTest::testCreate() {
// payload:{4:name3:foo}->11bytes
std::string payload = "4:name3:foo";
unsigned char msg[17];// 6+11bytes
PeerMessageUtil::createPeerMessageString((unsigned char*)msg,
sizeof(msg), 13, 20);
bittorrent::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20);
msg[5] = 1; // Set dummy extended message ID 1
memcpy(msg+6, payload.c_str(), payload.size());
SharedHandle<BtExtendedMessage> pm = BtExtendedMessage::create(exmsgFactory,
@ -54,7 +53,7 @@ void BtExtendedMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 20);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 20);
BtExtendedMessage::create(exmsgFactory, peer, &msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception& e) {
@ -63,7 +62,7 @@ void BtExtendedMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 21);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 21);
BtExtendedMessage::create(exmsgFactory, peer, &msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception& e) {
@ -79,7 +78,7 @@ void BtExtendedMessageTest::testGetMessage() {
BtExtendedMessage msg(exmsg);
unsigned char data[17];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 20);
bittorrent::createPeerMessageString(data, sizeof(data), 13, 20);
*(data+5) = extendedMessageID;
memcpy(data+6, payload.c_str(), payload.size());
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);

View File

@ -4,7 +4,6 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "Util.h"
#include "BtConstants.h"

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "MockPieceStorage.h"
#include "DlAbortEx.h"
@ -36,14 +36,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtHaveAllMessageTest);
void BtHaveAllMessageTest::testCreate() {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 14);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 14);
SharedHandle<BtHaveAllMessage> pm = BtHaveAllMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL((uint8_t)14, pm->getId());
// case: payload size is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 14);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 14);
BtHaveAllMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -51,7 +51,7 @@ void BtHaveAllMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 15);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 15);
BtHaveAllMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -61,7 +61,7 @@ void BtHaveAllMessageTest::testCreate() {
void BtHaveAllMessageTest::testGetMessage() {
BtHaveAllMessage msg;
unsigned char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 14);
bittorrent::createPeerMessageString(data, sizeof(data), 1, 14);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "MockPieceStorage.h"
#include "DlAbortEx.h"
@ -38,8 +38,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtHaveMessageTest);
void BtHaveMessageTest::testCreate() {
unsigned char msg[9];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 4);
PeerMessageUtil::setIntParam(&msg[5], 12345);
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 4);
bittorrent::setIntParam(&msg[5], 12345);
SharedHandle<BtHaveMessage> pm = BtHaveMessage::create(&msg[4], 5);
CPPUNIT_ASSERT_EQUAL((uint8_t)4, pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
@ -47,7 +47,7 @@ void BtHaveMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[10];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 6, 4);
bittorrent::createPeerMessageString(msg, sizeof(msg), 6, 4);
BtHaveMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -55,7 +55,7 @@ void BtHaveMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[9];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 5);
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 5);
BtHaveMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -66,8 +66,8 @@ void BtHaveMessageTest::testGetMessage() {
BtHaveMessage msg;
msg.setIndex(12345);
unsigned char data[9];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 5, 4);
PeerMessageUtil::setIntParam(&data[5], 12345);
bittorrent::createPeerMessageString(data, sizeof(data), 5, 4);
bittorrent::setIntParam(&data[5], 12345);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 9) == 0);
}

View File

@ -1,9 +1,12 @@
#include "BtHaveNoneMessage.h"
#include "PeerMessageUtil.h"
#include "Peer.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "bittorrent_helper.h"
#include "Peer.h"
namespace aria2 {
class BtHaveNoneMessageTest:public CppUnit::TestFixture {
@ -31,14 +34,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtHaveNoneMessageTest);
void BtHaveNoneMessageTest::testCreate() {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 15);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 15);
SharedHandle<BtHaveNoneMessage> pm = BtHaveNoneMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL((uint8_t)15, pm->getId());
// case: payload size is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 15);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 15);
BtHaveNoneMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -46,7 +49,7 @@ void BtHaveNoneMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 16);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 16);
BtHaveNoneMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -56,7 +59,7 @@ void BtHaveNoneMessageTest::testCreate() {
void BtHaveNoneMessageTest::testGetMessage() {
BtHaveNoneMessage msg;
unsigned char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 15);
bittorrent::createPeerMessageString(data, sizeof(data), 1, 15);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "MockPeerStorage.h"
@ -32,14 +32,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtInterestedMessageTest);
void BtInterestedMessageTest::testCreate() {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 2);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
BtInterestedMessageHandle pm = BtInterestedMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL((uint8_t)2, pm->getId());
// case: payload size is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 2);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 2);
BtInterestedMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -47,7 +47,7 @@ void BtInterestedMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 3);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 3);
BtInterestedMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -57,7 +57,7 @@ void BtInterestedMessageTest::testCreate() {
void BtInterestedMessageTest::testGetMessage() {
BtInterestedMessage msg;
unsigned char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 2);
bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "MockPeerStorage.h"
@ -32,14 +32,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtNotInterestedMessageTest);
void BtNotInterestedMessageTest::testCreate() {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 3);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 3);
BtNotInterestedMessageHandle pm = BtNotInterestedMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL((uint8_t)3, pm->getId());
// case: payload size is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 3);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 3);
BtNotInterestedMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -47,7 +47,7 @@ void BtNotInterestedMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 4);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 4);
BtNotInterestedMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -57,7 +57,7 @@ void BtNotInterestedMessageTest::testCreate() {
void BtNotInterestedMessageTest::testGetMessage() {
BtNotInterestedMessage msg;
unsigned char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 3);
bittorrent::createPeerMessageString(data, sizeof(data), 1, 3);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "MockBtMessage.h"
#include "MockBtMessageFactory.h"
#include "MockBtMessageDispatcher.h"
@ -106,9 +106,9 @@ void BtPieceMessageTest::testCreate() {
unsigned char msg[13+2];
unsigned char data[2];
memset(data, 0xff, sizeof(data));
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 11, 7);
PeerMessageUtil::setIntParam(&msg[5], 12345);
PeerMessageUtil::setIntParam(&msg[9], 256);
bittorrent::createPeerMessageString(msg, sizeof(msg), 11, 7);
bittorrent::setIntParam(&msg[5], 12345);
bittorrent::setIntParam(&msg[9], 256);
memcpy(&msg[13], data, sizeof(data));
SharedHandle<BtPieceMessage> pm = BtPieceMessage::create(&msg[4], 11);
CPPUNIT_ASSERT_EQUAL((uint8_t)7, pm->getId());
@ -120,7 +120,7 @@ void BtPieceMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[13];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 9, 7);
bittorrent::createPeerMessageString(msg, sizeof(msg), 9, 7);
BtPieceMessage::create(&msg[4], 9);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -128,7 +128,7 @@ void BtPieceMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[13+2];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 11, 8);
bittorrent::createPeerMessageString(msg, sizeof(msg), 11, 8);
BtPieceMessage::create(&msg[4], 11);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -141,9 +141,9 @@ void BtPieceMessageTest::testGetMessageHeader() {
msg.setBegin(256);
msg.setBlockLength(1024);
unsigned char data[13];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 9+1024, 7);
PeerMessageUtil::setIntParam(&data[5], 12345);
PeerMessageUtil::setIntParam(&data[9], 256);
bittorrent::createPeerMessageString(data, sizeof(data), 9+1024, 7);
bittorrent::setIntParam(&data[5], 12345);
bittorrent::setIntParam(&data[9], 256);
CPPUNIT_ASSERT(memcmp(msg.getMessageHeader(), data, 13) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Util.h"
#include "array_fun.h"
#include "Peer.h"
@ -60,8 +60,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtPortMessageTest);
void BtPortMessageTest::testCreate() {
unsigned char msg[7];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 9);
PeerMessageUtil::setShortIntParam(&msg[5], 12345);
bittorrent::createPeerMessageString(msg, sizeof(msg), 3, 9);
bittorrent::setShortIntParam(&msg[5], 12345);
SharedHandle<BtPortMessage> pm = BtPortMessage::create(&msg[4], 3);
CPPUNIT_ASSERT_EQUAL((uint8_t)9, pm->getId());
CPPUNIT_ASSERT_EQUAL((uint16_t)12345, pm->getPort());
@ -69,7 +69,7 @@ void BtPortMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[8];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 4, 9);
bittorrent::createPeerMessageString(msg, sizeof(msg), 4, 9);
BtPortMessage::create(&msg[4], 4);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -77,7 +77,7 @@ void BtPortMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[7];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 10);
bittorrent::createPeerMessageString(msg, sizeof(msg), 3, 10);
BtPortMessage::create(&msg[4], 3);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -92,8 +92,8 @@ void BtPortMessageTest::testToString() {
void BtPortMessageTest::testGetMessage() {
BtPortMessage msg(6881);
unsigned char data[7];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 9);
PeerMessageUtil::setShortIntParam(&data[5], 6881);
bittorrent::createPeerMessageString(data, sizeof(data), 3, 9);
bittorrent::setShortIntParam(&data[5], 6881);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 7) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "FileEntry.h"
#include "MockBtMessageDispatcher.h"
@ -86,10 +86,10 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtRejectMessageTest);
void BtRejectMessageTest::testCreate() {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 16);
PeerMessageUtil::setIntParam(&msg[5], 12345);
PeerMessageUtil::setIntParam(&msg[9], 256);
PeerMessageUtil::setIntParam(&msg[13], 1024);
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 16);
bittorrent::setIntParam(&msg[5], 12345);
bittorrent::setIntParam(&msg[9], 256);
bittorrent::setIntParam(&msg[13], 1024);
SharedHandle<BtRejectMessage> pm = BtRejectMessage::create(&msg[4], 13);
CPPUNIT_ASSERT_EQUAL((uint8_t)16, pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
@ -99,7 +99,7 @@ void BtRejectMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[18];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 16);
bittorrent::createPeerMessageString(msg, sizeof(msg), 14, 16);
BtRejectMessage::create(&msg[4], 14);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -107,7 +107,7 @@ void BtRejectMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 17);
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 17);
BtRejectMessage::create(&msg[4], 13);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -120,10 +120,10 @@ void BtRejectMessageTest::testGetMessage() {
msg.setBegin(256);
msg.setLength(1024);
unsigned char data[17];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 16);
PeerMessageUtil::setIntParam(&data[5], 12345);
PeerMessageUtil::setIntParam(&data[9], 256);
PeerMessageUtil::setIntParam(&data[13], 1024);
bittorrent::createPeerMessageString(data, sizeof(data), 13, 16);
bittorrent::setIntParam(&data[5], 12345);
bittorrent::setIntParam(&data[9], 256);
bittorrent::setIntParam(&data[13], 1024);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
}

View File

@ -4,7 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "MockBtMessage.h"
#include "MockPieceStorage.h"
#include "MockBtMessageFactory.h"
@ -128,10 +128,10 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtRequestMessageTest);
void BtRequestMessageTest::testCreate() {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 6);
PeerMessageUtil::setIntParam(&msg[5], 12345);
PeerMessageUtil::setIntParam(&msg[9], 256);
PeerMessageUtil::setIntParam(&msg[13], 1024);
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 6);
bittorrent::setIntParam(&msg[5], 12345);
bittorrent::setIntParam(&msg[9], 256);
bittorrent::setIntParam(&msg[13], 1024);
SharedHandle<BtRequestMessage> pm = BtRequestMessage::create(&msg[4], 13);
CPPUNIT_ASSERT_EQUAL((uint8_t)6, pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
@ -141,7 +141,7 @@ void BtRequestMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[18];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 6);
bittorrent::createPeerMessageString(msg, sizeof(msg), 14, 6);
BtRequestMessage::create(&msg[4], 14);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -149,7 +149,7 @@ void BtRequestMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 7);
bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 7);
BtRequestMessage::create(&msg[4], 13);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -162,10 +162,10 @@ void BtRequestMessageTest::testGetMessage() {
msg.setBegin(256);
msg.setLength(1024);
unsigned char data[17];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 6);
PeerMessageUtil::setIntParam(&data[5], 12345);
PeerMessageUtil::setIntParam(&data[9], 256);
PeerMessageUtil::setIntParam(&data[13], 1024);
bittorrent::createPeerMessageString(data, sizeof(data), 13, 6);
bittorrent::setIntParam(&data[5], 12345);
bittorrent::setIntParam(&data[9], 256);
bittorrent::setIntParam(&data[13], 1024);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
}

View File

@ -1,8 +1,10 @@
#include "BtSuggestPieceMessage.h"
#include "PeerMessageUtil.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include "bittorrent_helper.h"
namespace aria2 {
class BtSuggestPieceMessageTest:public CppUnit::TestFixture {
@ -28,8 +30,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtSuggestPieceMessageTest);
void BtSuggestPieceMessageTest::testCreate() {
unsigned char msg[9];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 13);
PeerMessageUtil::setIntParam(&msg[5], 12345);
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 13);
bittorrent::setIntParam(&msg[5], 12345);
SharedHandle<BtSuggestPieceMessage> pm = BtSuggestPieceMessage::create(&msg[4], 5);
CPPUNIT_ASSERT_EQUAL((uint8_t)13, pm->getId());
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
@ -37,7 +39,7 @@ void BtSuggestPieceMessageTest::testCreate() {
// case: payload size is wrong
try {
unsigned char msg[10];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 6, 13);
bittorrent::createPeerMessageString(msg, sizeof(msg), 6, 13);
BtSuggestPieceMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -45,7 +47,7 @@ void BtSuggestPieceMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[9];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 14);
bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 14);
BtSuggestPieceMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -56,8 +58,8 @@ void BtSuggestPieceMessageTest::testGetMessage() {
BtSuggestPieceMessage msg;
msg.setIndex(12345);
unsigned char data[9];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 5, 13);
PeerMessageUtil::setIntParam(&data[5], 12345);
bittorrent::createPeerMessageString(data, sizeof(data), 5, 13);
bittorrent::setIntParam(&data[5], 12345);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 9) == 0);
}

View File

@ -1,5 +1,5 @@
#include "BtUnchokeMessage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
@ -32,14 +32,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtUnchokeMessageTest);
void BtUnchokeMessageTest::testCreate() {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 1);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 1);
SharedHandle<BtUnchokeMessage> pm = BtUnchokeMessage::create(&msg[4], 1);
CPPUNIT_ASSERT_EQUAL((uint8_t)1, pm->getId());
// case: payload size is wrong
try {
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 1);
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 1);
BtUnchokeMessage::create(&msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -47,7 +47,7 @@ void BtUnchokeMessageTest::testCreate() {
// case: id is wrong
try {
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 2);
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
BtUnchokeMessage::create(&msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(...) {
@ -57,7 +57,7 @@ void BtUnchokeMessageTest::testCreate() {
void BtUnchokeMessageTest::testGetMessage() {
BtUnchokeMessage msg;
unsigned char data[5];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 1);
bittorrent::createPeerMessageString(data, sizeof(data), 1, 1);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
}

View File

@ -7,7 +7,7 @@
#include "Exception.h"
#include "Util.h"
#include "DHTBucket.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "bencode.h"
namespace aria2 {
@ -47,7 +47,8 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
CPPUNIT_ASSERT(bittorrent::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
std::string(&buf[0], &buf[sizeof(buf)]);

View File

@ -7,7 +7,7 @@
#include "Exception.h"
#include "Util.h"
#include "DHTBucket.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "Peer.h"
#include "bencode.h"
@ -58,7 +58,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact
CPPUNIT_ASSERT(bittorrent::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -81,7 +81,8 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
for(size_t i = 0; i < 4; ++i) {
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
unsigned char buffer[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
CPPUNIT_ASSERT(bittorrent::createcompact
(buffer, peer->ipaddr, peer->port));
valuesList << BDE(buffer, sizeof(buffer));
peers.push_back(peer);
}

View File

@ -10,7 +10,7 @@
#include "DHTNode.h"
#include "DHTRoutingTable.h"
#include "Peer.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "DHTBucket.h"
#include "DHTPingMessage.h"
#include "DHTPingReplyMessage.h"
@ -169,7 +169,7 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact
CPPUNIT_ASSERT(bittorrent::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -244,7 +244,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
nodes[i]->setPort(6881+i);
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact
CPPUNIT_ASSERT(bittorrent::createcompact
(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -291,8 +291,8 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
for(size_t i = 0; i < 4; ++i) {
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
unsigned char buffer[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr,
peer->port));
CPPUNIT_ASSERT(bittorrent::createcompact
(buffer, peer->ipaddr, peer->port));
valuesList << BDE(buffer, sizeof(buffer));
peers.push_back(peer);
}

View File

@ -1,16 +1,18 @@
#include "DHTRoutingTableDeserializer.h"
#include <cstring>
#include <sstream>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "DHTRoutingTableSerializer.h"
#include "Exception.h"
#include "Util.h"
#include "DHTNode.h"
#include "array_fun.h"
#include "DHTConstants.h"
#include "PeerMessageUtil.h"
#include "a2netcompat.h"
#include <cstring>
#include <sstream>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {

View File

@ -4,7 +4,7 @@
#include "DHTNode.h"
#include "array_fun.h"
#include "DHTConstants.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "a2netcompat.h"
#include <cstring>
#include <sstream>
@ -112,7 +112,7 @@ void DHTRoutingTableSerializerTest::testSerialize()
ss.read(buf, 6);
{
std::pair<std::string, uint16_t> peer =
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
bittorrent::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peer.second);
}
@ -171,7 +171,7 @@ void DHTRoutingTableSerializerTest::testSerialize()
ss.read(buf, 6);
{
std::pair<std::string, uint16_t> peer =
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
bittorrent::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peer.second);
}

View File

@ -7,7 +7,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "Peer.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "DownloadContext.h"
#include "MockExtensionMessageFactory.h"
#include "BtExtendedMessage.h"
@ -57,7 +57,7 @@ void DefaultBtMessageFactoryTest::testCreateBtMessage_BtExtendedMessage()
// payload:{4:name3:foo}->11bytes
std::string payload = "4:name3:foo";
char msg[17];// 6+11bytes
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg),
bittorrent::createPeerMessageString((unsigned char*)msg, sizeof(msg),
13, 20);
msg[5] = 1; // Set dummy extended message ID 1
memcpy(msg+6, payload.c_str(), payload.size());
@ -80,8 +80,8 @@ void DefaultBtMessageFactoryTest::testCreatePortMessage()
{
{
unsigned char data[7];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 9);
PeerMessageUtil::setShortIntParam(&data[5], 6881);
bittorrent::createPeerMessageString(data, sizeof(data), 3, 9);
bittorrent::setShortIntParam(&data[5], 6881);
try {
SharedHandle<BtPortMessage> m
(dynamic_pointer_cast<BtPortMessage>

View File

@ -6,7 +6,7 @@
#include "Peer.h"
#include "MockPeerStorage.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "HandshakeExtensionMessage.h"
#include "UTPexExtensionMessage.h"
#include "Exception.h"
@ -87,10 +87,10 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
unsigned char c2[6];
unsigned char c3[6];
unsigned char c4[6];
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
PeerMessageUtil::createcompact(c4, "10.1.1.3",10000);
bittorrent::createcompact(c1, "192.168.0.1", 6881);
bittorrent::createcompact(c2, "10.1.1.2", 9999);
bittorrent::createcompact(c3, "192.168.0.2", 6882);
bittorrent::createcompact(c4, "10.1.1.3",10000);
char id[1] = { _registry->getExtensionMessageID("ut_pex") };

View File

@ -128,7 +128,6 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\
ByteArrayDiskWriterTest.cc\
PeerTest.cc\
PeerSessionResourceTest.cc\
PeerMessageUtilTest.cc\
ShareRatioSeedCriteriaTest.cc\
BtRegistryTest.cc\
BtDependencyTest.cc\

View File

@ -76,7 +76,6 @@ check_PROGRAMS = $(am__EXEEXT_1)
@ENABLE_BITTORRENT_TRUE@ ByteArrayDiskWriterTest.cc\
@ENABLE_BITTORRENT_TRUE@ PeerTest.cc\
@ENABLE_BITTORRENT_TRUE@ PeerSessionResourceTest.cc\
@ENABLE_BITTORRENT_TRUE@ PeerMessageUtilTest.cc\
@ENABLE_BITTORRENT_TRUE@ ShareRatioSeedCriteriaTest.cc\
@ENABLE_BITTORRENT_TRUE@ BtRegistryTest.cc\
@ENABLE_BITTORRENT_TRUE@ BtDependencyTest.cc\
@ -219,12 +218,11 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
PeerListProcessorTest.cc AnnounceListTest.cc \
DefaultPeerStorageTest.cc MockPeerStorage.h \
ByteArrayDiskWriterTest.cc PeerTest.cc \
PeerSessionResourceTest.cc PeerMessageUtilTest.cc \
ShareRatioSeedCriteriaTest.cc BtRegistryTest.cc \
BtDependencyTest.cc BtPostDownloadHandlerTest.cc \
TimeSeedCriteriaTest.cc BtExtendedMessageTest.cc \
HandshakeExtensionMessageTest.cc UTPexExtensionMessageTest.cc \
DefaultBtMessageFactoryTest.cc \
PeerSessionResourceTest.cc ShareRatioSeedCriteriaTest.cc \
BtRegistryTest.cc BtDependencyTest.cc \
BtPostDownloadHandlerTest.cc TimeSeedCriteriaTest.cc \
BtExtendedMessageTest.cc HandshakeExtensionMessageTest.cc \
UTPexExtensionMessageTest.cc DefaultBtMessageFactoryTest.cc \
DefaultExtensionMessageFactoryTest.cc DHTNodeTest.cc \
DHTBucketTest.cc DHTRoutingTableTest.cc \
DHTMessageTrackerEntryTest.cc DHTMessageTrackerTest.cc \
@ -288,7 +286,6 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
@ENABLE_BITTORRENT_TRUE@ ByteArrayDiskWriterTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ PeerTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ PeerSessionResourceTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ PeerMessageUtilTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ ShareRatioSeedCriteriaTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ BtRegistryTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ BtDependencyTest.$(OBJEXT) \
@ -810,7 +807,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PStringBuildVisitorTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ParameterizedStringParserTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerListProcessorTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerMessageUtilTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerSessionResourceTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PieceStatManTest.Po@am__quote@

View File

@ -8,7 +8,6 @@
#include "Peer.h"
#include "bencode.h"
#include "TimeA2.h"
#include "PeerMessageUtil.h"
namespace aria2 {
@ -33,7 +32,9 @@ CPPUNIT_TEST_SUITE_REGISTRATION( PeerListProcessorTest );
void PeerListProcessorTest::testExtractPeerFromList() {
PeerListProcessor proc;
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
std::string peersString =
"d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
"4:porti2006eeee";
const BDE dict = bencode::decode(peersString);
@ -47,7 +48,10 @@ void PeerListProcessorTest::testExtractPeerFromList() {
void PeerListProcessorTest::testExtract2PeersFromList() {
PeerListProcessor proc;
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
std::string peersString =
"d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
"4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
"4:porti2007eeee";
const BDE dict = bencode::decode(peersString);

View File

@ -1,55 +0,0 @@
#include "common.h"
#include "PeerMessageUtil.h"
#include "a2netcompat.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class PeerMessageUtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(PeerMessageUtilTest);
CPPUNIT_TEST(testCreateCompact);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testCreateCompact();
};
CPPUNIT_TEST_SUITE_REGISTRATION( PeerMessageUtilTest );
void setIntParam(char* dest, int param) {
int nParam = htonl(param);
memcpy(dest, &nParam, 4);
}
void setShortIntParam(char* dest, int param) {
short int nParam = htons(param);
memcpy(dest, &nParam, 2);
}
void createNLengthMessage(char* msg, int msgLen, int payloadLen, int id) {
memset(msg, 0, msgLen);
setIntParam(msg, payloadLen);
msg[4] = (char)id;
}
void PeerMessageUtilTest::testCreateCompact()
{
unsigned char compact[6];
// Note: PeerMessageUtil::createcompact() on linux can handle IPv4-mapped
// addresses like `ffff::127.0.0.1', but on cygwin, it doesn't.
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(compact, "127.0.0.1", 6881));
std::pair<std::string, uint16_t> p = PeerMessageUtil::unpackcompact(compact);
CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), p.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, p.second);
}
} // namespace aria2

View File

@ -7,7 +7,7 @@
#include "Peer.h"
#include "a2netcompat.h"
#include "Util.h"
#include "PeerMessageUtil.h"
#include "bittorrent_helper.h"
#include "MockPeerStorage.h"
#include "Exception.h"
#include "FileEntry.h"
@ -83,10 +83,10 @@ void UTPexExtensionMessageTest::testGetBencodedData()
unsigned char c2[6];
unsigned char c3[6];
unsigned char c4[6];
PeerMessageUtil::createcompact(c1, p1->ipaddr, p1->port);
PeerMessageUtil::createcompact(c2, p2->ipaddr, p2->port);
PeerMessageUtil::createcompact(c3, p3->ipaddr, p3->port);
PeerMessageUtil::createcompact(c4, p4->ipaddr, p4->port);
bittorrent::createcompact(c1, p1->ipaddr, p1->port);
bittorrent::createcompact(c2, p2->ipaddr, p2->port);
bittorrent::createcompact(c3, p3->ipaddr, p3->port);
bittorrent::createcompact(c4, p4->ipaddr, p4->port);
std::string expected = "d5:added12:"+
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
@ -154,10 +154,10 @@ void UTPexExtensionMessageTest::testCreate()
unsigned char c2[6];
unsigned char c3[6];
unsigned char c4[6];
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
PeerMessageUtil::createcompact(c4, "10.1.1.3",10000);
bittorrent::createcompact(c1, "192.168.0.1", 6881);
bittorrent::createcompact(c2, "10.1.1.2", 9999);
bittorrent::createcompact(c3, "192.168.0.2", 6882);
bittorrent::createcompact(c4, "10.1.1.3",10000);
char id[1] = { 1 };