2006-12-24 06:25:21 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2008-11-27 15:29:15 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "BtConstants.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class BtHandshakeMessageTest : public CppUnit::TestFixture {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtHandshakeMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
2010-03-04 16:24:03 +00:00
|
|
|
CPPUNIT_TEST(testCreateMessage);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST(testToString);
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
CPPUNIT_TEST(testSetDHTEnabled);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
private:
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
2015-12-27 09:39:47 +00:00
|
|
|
void setUp() {}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
void testCreate();
|
2010-03-04 16:24:03 +00:00
|
|
|
void testCreateMessage();
|
2006-12-24 06:25:21 +00:00
|
|
|
void testToString();
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
void testSetDHTEnabled();
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
static std::string BTPSTR;
|
2006-12-24 06:25:21 +00:00
|
|
|
};
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string BtHandshakeMessageTest::BTPSTR = "BitTorrent protocol";
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtHandshakeMessageTest);
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void createHandshakeMessageData(unsigned char* msg)
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
msg[0] = 19;
|
|
|
|
memcpy(&msg[1], BtHandshakeMessageTest::BTPSTR.c_str(),
|
2010-01-05 16:01:46 +00:00
|
|
|
BtHandshakeMessageTest::BTPSTR.size());
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char reserved[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x04};
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(&msg[20], reserved, sizeof(reserved));
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(&msg[28], infoHash, sizeof(infoHash));
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(&msg[48], peerId, sizeof(peerId));
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtHandshakeMessageTest::testCreate()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
unsigned char msg[68];
|
|
|
|
createHandshakeMessageData(msg);
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<BtHandshakeMessage> message =
|
|
|
|
BtHandshakeMessage::create(&msg[0], sizeof(msg));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)INT8_MAX, message->getId());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)19, message->getPstrlen());
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
|
|
|
util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
|
|
|
|
util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
|
|
|
std::string("0000000000100004"),
|
|
|
|
util::toHex(message->getReserved(), BtHandshakeMessage::RESERVED_LENGTH));
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("ffffffffffffffffffffffffffffffffffffffff"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::toHex(message->getInfoHash(), INFO_HASH_LENGTH));
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::toHex(message->getPeerId(), PEER_ID_LENGTH));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtHandshakeMessageTest::testCreateMessage()
|
|
|
|
{
|
2016-05-15 14:40:09 +00:00
|
|
|
constexpr unsigned char infoHash[] = {
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|
|
|
constexpr unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
|
|
|
|
|
|
|
auto msg = std::make_shared<BtHandshakeMessage>();
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->setInfoHash(infoHash);
|
|
|
|
msg->setPeerId(peerId);
|
|
|
|
|
|
|
|
unsigned char data[68];
|
|
|
|
createHandshakeMessageData(data);
|
2016-05-15 14:40:09 +00:00
|
|
|
auto rawmsg = msg->createMessage();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)68, rawmsg.size());
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(util::toHex((const unsigned char*)data, 68),
|
2016-05-15 14:40:09 +00:00
|
|
|
util::toHex(rawmsg.data(), 68));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void BtHandshakeMessageTest::testToString()
|
|
|
|
{
|
|
|
|
unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|
|
|
unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
BtHandshakeMessage msg;
|
|
|
|
msg.setInfoHash(infoHash);
|
|
|
|
msg.setPeerId(peerId);
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
|
|
|
std::string("handshake "
|
|
|
|
"peerId=%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%F0%"
|
|
|
|
"F0%F0%F0, reserved=0000000000100004"),
|
|
|
|
msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
|
|
|
|
void BtHandshakeMessageTest::testSetDHTEnabled()
|
|
|
|
{
|
|
|
|
BtHandshakeMessage msg;
|
|
|
|
CPPUNIT_ASSERT(!msg.isDHTEnabled());
|
|
|
|
msg.setDHTEnabled(false);
|
|
|
|
CPPUNIT_ASSERT(!msg.isDHTEnabled());
|
|
|
|
msg.setDHTEnabled(true);
|
|
|
|
CPPUNIT_ASSERT(msg.isDHTEnabled());
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|