From 99654e41602787e0083740bf8c88eee426ce4a76 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 29 Sep 2009 14:52:42 +0000 Subject: [PATCH] 2009-09-29 Tatsuhiro Tsujikawa 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 --- ChangeLog | 62 ++++++ src/BtBitfieldMessage.cc | 9 +- src/BtBitfieldMessageValidator.h | 7 +- src/BtExtendedMessage.cc | 8 +- src/BtHandshakeMessage.cc | 4 +- src/BtHandshakeMessageValidator.h | 1 - src/BtPieceMessage.cc | 18 +- src/BtPieceMessageValidator.h | 6 +- src/BtPortMessage.cc | 12 +- src/DHTFindNodeReplyMessage.cc | 6 +- src/DHTGetPeersReplyMessage.cc | 7 +- src/DHTMessageFactoryImpl.cc | 6 +- src/DHTRoutingTableDeserializer.cc | 5 +- src/DHTRoutingTableSerializer.cc | 13 +- src/DHTTokenTracker.cc | 8 +- src/DefaultBtMessageFactory.cc | 4 +- src/IndexBtMessage.cc | 5 +- src/IndexBtMessage.h | 8 +- src/IndexBtMessageValidator.h | 4 +- src/Makefile.am | 3 +- src/Makefile.in | 9 +- src/PeerMessageUtil.cc | 216 --------------------- src/PeerMessageUtil.h | 98 ---------- src/RangeBtMessage.cc | 9 +- src/RangeBtMessage.h | 12 +- src/RangeBtMessageValidator.h | 14 +- src/UTPexExtensionMessage.cc | 4 +- src/ZeroBtMessage.cc | 3 +- src/ZeroBtMessage.h | 6 +- src/bittorrent_helper.cc | 183 ++++++++++++++++- src/bittorrent_helper.h | 59 ++++++ test/BittorrentHelperTest.cc | 37 ++++ test/BtAllowedFastMessageTest.cc | 20 +- test/BtBitfieldMessageTest.cc | 10 +- test/BtCancelMessageTest.cc | 22 +-- test/BtChokeMessageTest.cc | 10 +- test/BtExtendedMessageTest.cc | 11 +- test/BtHandshakeMessageTest.cc | 1 - test/BtHaveAllMessageTest.cc | 10 +- test/BtHaveMessageTest.cc | 14 +- test/BtHaveNoneMessageTest.cc | 15 +- test/BtInterestedMessageTest.cc | 10 +- test/BtNotInterestedMessageTest.cc | 10 +- test/BtPieceMessageTest.cc | 18 +- test/BtPortMessageTest.cc | 14 +- test/BtRejectMessageTest.cc | 22 +-- test/BtRequestMessageTest.cc | 22 +-- test/BtSuggestPieceMessageTest.cc | 16 +- test/BtUnchokeMessageTest.cc | 10 +- test/DHTFindNodeReplyMessageTest.cc | 5 +- test/DHTGetPeersReplyMessageTest.cc | 7 +- test/DHTMessageFactoryImplTest.cc | 10 +- test/DHTRoutingTableDeserializerTest.cc | 12 +- test/DHTRoutingTableSerializerTest.cc | 6 +- test/DefaultBtMessageFactoryTest.cc | 8 +- test/DefaultExtensionMessageFactoryTest.cc | 10 +- test/Makefile.am | 1 - test/Makefile.in | 14 +- test/PeerListProcessorTest.cc | 10 +- test/PeerMessageUtilTest.cc | 55 ------ test/UTPexExtensionMessageTest.cc | 18 +- 61 files changed, 607 insertions(+), 630 deletions(-) delete mode 100644 src/PeerMessageUtil.cc delete mode 100644 src/PeerMessageUtil.h delete mode 100644 test/PeerMessageUtilTest.cc diff --git a/ChangeLog b/ChangeLog index a8426c04..1b31f964 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,65 @@ +2009-09-29 Tatsuhiro Tsujikawa + + 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 Limit the size of values list when sending get_peers reply. diff --git a/src/BtBitfieldMessage.cc b/src/BtBitfieldMessage.cc index 9ee9a8f4..4ea2b69e 100644 --- a/src/BtBitfieldMessage.cc +++ b/src/BtBitfieldMessage.cc @@ -36,7 +36,7 @@ #include -#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; diff --git a/src/BtBitfieldMessageValidator.h b/src/BtBitfieldMessageValidator.h index cd8058e1..710dcd7b 100644 --- a/src/BtBitfieldMessageValidator.h +++ b/src/BtBitfieldMessageValidator.h @@ -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; } }; diff --git a/src/BtExtendedMessage.cc b/src/BtExtendedMessage.cc index 39d61028..913085ca 100644 --- a/src/BtExtendedMessage.cc +++ b/src/BtExtendedMessage.cc @@ -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& 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); diff --git a/src/BtHandshakeMessage.cc b/src/BtHandshakeMessage.cc index aada5137..3c0aa3dd 100644 --- a/src/BtHandshakeMessage.cc +++ b/src/BtHandshakeMessage.cc @@ -36,7 +36,6 @@ #include -#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) { diff --git a/src/BtHandshakeMessageValidator.h b/src/BtHandshakeMessageValidator.h index 990d331e..5a6c8e3d 100644 --- a/src/BtHandshakeMessageValidator.h +++ b/src/BtHandshakeMessageValidator.h @@ -41,7 +41,6 @@ #include "BtHandshakeMessage.h" #include "Util.h" -#include "PeerMessageUtil.h" #include "StringFormat.h" namespace aria2 { diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index 9381d6e4..6953f2b6 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -38,7 +38,7 @@ #include #include -#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; } diff --git a/src/BtPieceMessageValidator.h b/src/BtPieceMessageValidator.h index c21c36f8..dd270fbf 100644 --- a/src/BtPieceMessageValidator.h +++ b/src/BtPieceMessageValidator.h @@ -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; } }; diff --git a/src/BtPortMessage.cc b/src/BtPortMessage.cc index 978bed1a..5ceaa0c7 100644 --- a/src/BtPortMessage.cc +++ b/src/BtPortMessage.cc @@ -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::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 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; } diff --git a/src/DHTFindNodeReplyMessage.cc b/src/DHTFindNodeReplyMessage.cc index 99831796..fd6108ca 100644 --- a/src/DHTFindNodeReplyMessage.cc +++ b/src/DHTFindNodeReplyMessage.cc @@ -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 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; } } diff --git a/src/DHTGetPeersReplyMessage.cc b/src/DHTGetPeersReplyMessage.cc index 41fe9227..2d69237b 100644 --- a/src/DHTGetPeersReplyMessage.cc +++ b/src/DHTGetPeersReplyMessage.cc @@ -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 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 = *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)); } } diff --git a/src/DHTMessageFactoryImpl.cc b/src/DHTMessageFactoryImpl.cc index 9c4f836d..0d995ff8 100644 --- a/src/DHTMessageFactoryImpl.cc +++ b/src/DHTMessageFactoryImpl.cc @@ -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 node(new DHTNode(src+offset)); std::pair 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 addr = - PeerMessageUtil::unpackcompact(data.uc()); + bittorrent::unpackcompact(data.uc()); PeerHandle peer(new Peer(addr.first, addr.second)); peers.push_back(peer); } diff --git a/src/DHTRoutingTableDeserializer.cc b/src/DHTRoutingTableDeserializer.cc index 73345ad5..eae6d5d4 100644 --- a/src/DHTRoutingTableDeserializer.cc +++ b/src/DHTRoutingTableDeserializer.cc @@ -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 peer = - PeerMessageUtil::unpackcompact(buf); + std::pair peer = bittorrent::unpackcompact(buf); if(peer.first.empty()) { // skip this entry readBytes(buf, buf.size(), in, 42); diff --git a/src/DHTRoutingTableSerializer.cc b/src/DHTRoutingTableSerializer.cc index 9228bbb4..763e3c61 100644 --- a/src/DHTRoutingTableSerializer.cc +++ b/src/DHTRoutingTableSerializer.cc @@ -33,17 +33,19 @@ */ /* copyright --> */ #include "DHTRoutingTableSerializer.h" + +#include +#include +#include + #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 -#include -#include 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 diff --git a/src/DHTTokenTracker.cc b/src/DHTTokenTracker.cc index f6650b51..adbd4608 100644 --- a/src/DHTTokenTracker.cc +++ b/src/DHTTokenTracker.cc @@ -33,13 +33,15 @@ */ /* copyright --> */ #include "DHTTokenTracker.h" + +#include + #include "DHTUtil.h" -#include "PeerMessageUtil.h" +#include "bittorrent_helper.h" #include "DlAbortEx.h" #include "DHTConstants.h" #include "MessageDigestHelper.h" #include "StringFormat.h" -#include 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()); diff --git a/src/DefaultBtMessageFactory.cc b/src/DefaultBtMessageFactory.cc index 17bc280c..be5a7b5f 100644 --- a/src/DefaultBtMessageFactory.cc +++ b/src/DefaultBtMessageFactory.cc @@ -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); diff --git a/src/IndexBtMessage.cc b/src/IndexBtMessage.cc index 5c3b45d4..3c23666a 100644 --- a/src/IndexBtMessage.cc +++ b/src/IndexBtMessage.cc @@ -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; } diff --git a/src/IndexBtMessage.h b/src/IndexBtMessage.h index 3230baf2..1bc73f03 100644 --- a/src/IndexBtMessage.h +++ b/src/IndexBtMessage.h @@ -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 static SharedHandle 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 message(new T()); - message->setIndex(PeerMessageUtil::getIntParam(data, 1)); + message->setIndex(bittorrent::getIntParam(data, 1)); return message; } public: diff --git a/src/IndexBtMessageValidator.h b/src/IndexBtMessageValidator.h index 17bf4fd3..5dc91e82 100644 --- a/src/IndexBtMessageValidator.h +++ b/src/IndexBtMessageValidator.h @@ -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; } diff --git a/src/Makefile.am b/src/Makefile.am index 52317171..3fe04d63 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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\ diff --git a/src/Makefile.in b/src/Makefile.in index 5890c150..625d4170 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -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@ diff --git a/src/PeerMessageUtil.cc b/src/PeerMessageUtil.cc deleted file mode 100644 index 2c1ae636..00000000 --- a/src/PeerMessageUtil.cc +++ /dev/null @@ -1,216 +0,0 @@ -/* */ -#include "PeerMessageUtil.h" - -#include -#include - -#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(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(length), - MAX_BLOCK_LENGTH/1024).str()); - } - if(length == 0) { - throw DL_ABORT_EX(StringFormat("Invalid length: %lu", - static_cast(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(length)).str()); - } - uint32_t end = begin+length; - if(!(end <= pieceLength)) { - throw DL_ABORT_EX(StringFormat("Invalid range: begin=%u, length=%lu", - begin, - static_cast(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(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(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 -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(compact); - in.sin_port = 0; - char host[NI_MAXHOST]; - int s; - s = getnameinfo(reinterpret_cast(&in), sizeof(in), - host, NI_MAXHOST, 0, NI_MAXSERV, - NI_NUMERICHOST); - if(s) { - return std::pair(); - } - uint16_t port = ntohs(*(uint16_t*)(compact+sizeof(uint32_t))); - return std::pair(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 diff --git a/src/PeerMessageUtil.h b/src/PeerMessageUtil.h deleted file mode 100644 index 1df121b0..00000000 --- a/src/PeerMessageUtil.h +++ /dev/null @@ -1,98 +0,0 @@ -/* */ -#ifndef _D_PEER_MESSAGE_UTIL_H_ -#define _D_PEER_MESSAGE_UTIL_H_ - -#include "common.h" -#include -#include - -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 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_ diff --git a/src/RangeBtMessage.cc b/src/RangeBtMessage.cc index 64ce1bf5..0e4fcc6d 100644 --- a/src/RangeBtMessage.cc +++ b/src/RangeBtMessage.cc @@ -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; } diff --git a/src/RangeBtMessage.h b/src/RangeBtMessage.h index a0b12df4..a91ee7a5 100644 --- a/src/RangeBtMessage.h +++ b/src/RangeBtMessage.h @@ -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 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 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: diff --git a/src/RangeBtMessageValidator.h b/src/RangeBtMessageValidator.h index a6d6cdfa..196c9748 100644 --- a/src/RangeBtMessageValidator.h +++ b/src/RangeBtMessageValidator.h @@ -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; } }; diff --git a/src/UTPexExtensionMessage.cc b/src/UTPexExtensionMessage.cc index b437c80e..92e71947 100644 --- a/src/UTPexExtensionMessage.cc +++ b/src/UTPexExtensionMessage.cc @@ -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"; } diff --git a/src/ZeroBtMessage.cc b/src/ZeroBtMessage.cc index 153731ae..77b38737 100644 --- a/src/ZeroBtMessage.cc +++ b/src/ZeroBtMessage.cc @@ -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; } diff --git a/src/ZeroBtMessage.h b/src/ZeroBtMessage.h index ad2bfc3e..d22c2afd 100644 --- a/src/ZeroBtMessage.h +++ b/src/ZeroBtMessage.h @@ -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 static SharedHandle 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 message(new T()); return message; } diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 9f91c04d..95707e39 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -34,9 +34,9 @@ /* copyright --> */ #include "bittorrent_helper.h" +#include #include #include -#include #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(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(length), + MAX_BLOCK_LENGTH/1024).str()); + } + if(length == 0) { + throw DL_ABORT_EX + (StringFormat("Invalid length: %lu", + static_cast(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(length)).str()); + } + uint32_t end = begin+length; + if(!(end <= pieceLength)) { + throw DL_ABORT_EX + (StringFormat("Invalid range: begin=%u, length=%lu", + begin, + static_cast(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(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(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 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(compact); + in.sin_port = 0; + char host[NI_MAXHOST]; + int s; + s = getnameinfo(reinterpret_cast(&in), sizeof(in), + host, NI_MAXHOST, 0, NI_MAXSERV, + NI_NUMERICHOST); + if(s) { + return std::pair(); + } + uint16_t port = ntohs(*(uint16_t*)(compact+sizeof(uint32_t))); + return std::pair(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 diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h index 43160c04..e92a0b0c 100644 --- a/src/bittorrent_helper.h +++ b/src/bittorrent_helper.h @@ -40,6 +40,7 @@ #include #include #include +#include #include "SharedHandle.h" #include "AnnounceTier.h" @@ -151,6 +152,64 @@ getInfoHash(const SharedHandle& downloadContext); std::string getInfoHashString(const SharedHandle& 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 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 diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 0a156967..1cef6a5f 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -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 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 diff --git a/test/BtAllowedFastMessageTest.cc b/test/BtAllowedFastMessageTest.cc index 69a2639d..8bd7b162 100644 --- a/test/BtAllowedFastMessageTest.cc +++ b/test/BtAllowedFastMessageTest.cc @@ -1,10 +1,12 @@ #include "BtAllowedFastMessage.h" -#include "PeerMessageUtil.h" -#include "Util.h" -#include "Peer.h" + #include #include +#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 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); } diff --git a/test/BtBitfieldMessageTest.cc b/test/BtBitfieldMessageTest.cc index 4264c086..13acdace 100644 --- a/test/BtBitfieldMessageTest.cc +++ b/test/BtBitfieldMessageTest.cc @@ -4,7 +4,7 @@ #include -#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); } diff --git a/test/BtCancelMessageTest.cc b/test/BtCancelMessageTest.cc index 70654ea1..b5127a16 100644 --- a/test/BtCancelMessageTest.cc +++ b/test/BtCancelMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtChokeMessageTest.cc b/test/BtChokeMessageTest.cc index fae43412..f9a0d6fd 100644 --- a/test/BtChokeMessageTest.cc +++ b/test/BtChokeMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtExtendedMessageTest.cc b/test/BtExtendedMessageTest.cc index f7fb3027..c1d29daf 100644 --- a/test/BtExtendedMessageTest.cc +++ b/test/BtExtendedMessageTest.cc @@ -5,7 +5,7 @@ #include -#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 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); diff --git a/test/BtHandshakeMessageTest.cc b/test/BtHandshakeMessageTest.cc index 9c529e53..d787a654 100644 --- a/test/BtHandshakeMessageTest.cc +++ b/test/BtHandshakeMessageTest.cc @@ -4,7 +4,6 @@ #include -#include "PeerMessageUtil.h" #include "Util.h" #include "BtConstants.h" diff --git a/test/BtHaveAllMessageTest.cc b/test/BtHaveAllMessageTest.cc index d7a6e1e4..d8f0b8f2 100644 --- a/test/BtHaveAllMessageTest.cc +++ b/test/BtHaveAllMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtHaveMessageTest.cc b/test/BtHaveMessageTest.cc index c4435d15..be408564 100644 --- a/test/BtHaveMessageTest.cc +++ b/test/BtHaveMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtHaveNoneMessageTest.cc b/test/BtHaveNoneMessageTest.cc index d5bbded2..36024670 100644 --- a/test/BtHaveNoneMessageTest.cc +++ b/test/BtHaveNoneMessageTest.cc @@ -1,9 +1,12 @@ #include "BtHaveNoneMessage.h" -#include "PeerMessageUtil.h" -#include "Peer.h" + #include + #include +#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 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); } diff --git a/test/BtInterestedMessageTest.cc b/test/BtInterestedMessageTest.cc index 37c73040..0c9a7244 100644 --- a/test/BtInterestedMessageTest.cc +++ b/test/BtInterestedMessageTest.cc @@ -4,7 +4,7 @@ #include -#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); } diff --git a/test/BtNotInterestedMessageTest.cc b/test/BtNotInterestedMessageTest.cc index b23c0fec..f0f5127d 100644 --- a/test/BtNotInterestedMessageTest.cc +++ b/test/BtNotInterestedMessageTest.cc @@ -4,7 +4,7 @@ #include -#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); } diff --git a/test/BtPieceMessageTest.cc b/test/BtPieceMessageTest.cc index 05703542..ac460679 100644 --- a/test/BtPieceMessageTest.cc +++ b/test/BtPieceMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtPortMessageTest.cc b/test/BtPortMessageTest.cc index f72087e0..b2ebe841 100644 --- a/test/BtPortMessageTest.cc +++ b/test/BtPortMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtRejectMessageTest.cc b/test/BtRejectMessageTest.cc index 5264d39e..ad5f9f2b 100644 --- a/test/BtRejectMessageTest.cc +++ b/test/BtRejectMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtRequestMessageTest.cc b/test/BtRequestMessageTest.cc index e7a86fcc..dc77e1f3 100644 --- a/test/BtRequestMessageTest.cc +++ b/test/BtRequestMessageTest.cc @@ -4,7 +4,7 @@ #include -#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 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); } diff --git a/test/BtSuggestPieceMessageTest.cc b/test/BtSuggestPieceMessageTest.cc index 744ec11d..2f53fc67 100644 --- a/test/BtSuggestPieceMessageTest.cc +++ b/test/BtSuggestPieceMessageTest.cc @@ -1,8 +1,10 @@ #include "BtSuggestPieceMessage.h" -#include "PeerMessageUtil.h" + #include #include +#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 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); } diff --git a/test/BtUnchokeMessageTest.cc b/test/BtUnchokeMessageTest.cc index 99d833f8..4dd18bb8 100644 --- a/test/BtUnchokeMessageTest.cc +++ b/test/BtUnchokeMessageTest.cc @@ -1,5 +1,5 @@ #include "BtUnchokeMessage.h" -#include "PeerMessageUtil.h" +#include "bittorrent_helper.h" #include "Peer.h" #include #include @@ -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 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); } diff --git a/test/DHTFindNodeReplyMessageTest.cc b/test/DHTFindNodeReplyMessageTest.cc index fb40dc7e..14ddbc79 100644 --- a/test/DHTFindNodeReplyMessageTest.cc +++ b/test/DHTFindNodeReplyMessageTest.cc @@ -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)]); diff --git a/test/DHTGetPeersReplyMessageTest.cc b/test/DHTGetPeersReplyMessageTest.cc index 9cacf237..34cbcd7e 100644 --- a/test/DHTGetPeersReplyMessageTest.cc +++ b/test/DHTGetPeersReplyMessageTest.cc @@ -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(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); } diff --git a/test/DHTMessageFactoryImplTest.cc b/test/DHTMessageFactoryImplTest.cc index b9f671eb..521c8a52 100644 --- a/test/DHTMessageFactoryImplTest.cc +++ b/test/DHTMessageFactoryImplTest.cc @@ -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(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); } diff --git a/test/DHTRoutingTableDeserializerTest.cc b/test/DHTRoutingTableDeserializerTest.cc index 714dde6b..be824161 100644 --- a/test/DHTRoutingTableDeserializerTest.cc +++ b/test/DHTRoutingTableDeserializerTest.cc @@ -1,16 +1,18 @@ #include "DHTRoutingTableDeserializer.h" + +#include +#include +#include + +#include + #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 -#include -#include -#include namespace aria2 { diff --git a/test/DHTRoutingTableSerializerTest.cc b/test/DHTRoutingTableSerializerTest.cc index 338bb4f4..fc8e31be 100644 --- a/test/DHTRoutingTableSerializerTest.cc +++ b/test/DHTRoutingTableSerializerTest.cc @@ -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 #include @@ -112,7 +112,7 @@ void DHTRoutingTableSerializerTest::testSerialize() ss.read(buf, 6); { std::pair peer = - PeerMessageUtil::unpackcompact(reinterpret_cast(buf)); + bittorrent::unpackcompact(reinterpret_cast(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 peer = - PeerMessageUtil::unpackcompact(reinterpret_cast(buf)); + bittorrent::unpackcompact(reinterpret_cast(buf)); CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peer.first); CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peer.second); } diff --git a/test/DefaultBtMessageFactoryTest.cc b/test/DefaultBtMessageFactoryTest.cc index 304573a7..1d9a2c93 100644 --- a/test/DefaultBtMessageFactoryTest.cc +++ b/test/DefaultBtMessageFactoryTest.cc @@ -7,7 +7,7 @@ #include #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 m (dynamic_pointer_cast diff --git a/test/DefaultExtensionMessageFactoryTest.cc b/test/DefaultExtensionMessageFactoryTest.cc index 740004de..23b71fcc 100644 --- a/test/DefaultExtensionMessageFactoryTest.cc +++ b/test/DefaultExtensionMessageFactoryTest.cc @@ -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") }; diff --git a/test/Makefile.am b/test/Makefile.am index e1f8aaa6..e1e2b0c2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -128,7 +128,6 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\ ByteArrayDiskWriterTest.cc\ PeerTest.cc\ PeerSessionResourceTest.cc\ - PeerMessageUtilTest.cc\ ShareRatioSeedCriteriaTest.cc\ BtRegistryTest.cc\ BtDependencyTest.cc\ diff --git a/test/Makefile.in b/test/Makefile.in index af666e19..a63f13eb 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -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@ diff --git a/test/PeerListProcessorTest.cc b/test/PeerListProcessorTest.cc index 6d07f99a..59ceb594 100644 --- a/test/PeerListProcessorTest.cc +++ b/test/PeerListProcessorTest.cc @@ -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); diff --git a/test/PeerMessageUtilTest.cc b/test/PeerMessageUtilTest.cc deleted file mode 100644 index 3a2314be..00000000 --- a/test/PeerMessageUtilTest.cc +++ /dev/null @@ -1,55 +0,0 @@ -#include "common.h" -#include "PeerMessageUtil.h" -#include "a2netcompat.h" -#include -#include - -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 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 diff --git a/test/UTPexExtensionMessageTest.cc b/test/UTPexExtensionMessageTest.cc index 29a07520..8c26caba 100644 --- a/test/UTPexExtensionMessageTest.cc +++ b/test/UTPexExtensionMessageTest.cc @@ -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 };