mirror of
https://github.com/aria2/aria2.git
synced 2025-04-06 05:57:36 +03:00
Rewritten a portion of bittorrent implementation: * src/BtMessageValidator.h: New class. * src/BtBitfieldMessageValidator.h: New class. * src/BtHandshakeMessageValidator.h: New class. * src/BtRequestMessageValidator.h: New class. * src/BtSuggestPieceMessageValidator.h: New class. * src/BtAllowedFastMessageValidator.h: New class. * src/BtRejectMessageValidator.h: New class. * src/BtCancelMessageValidator.h: New class. * src/BtPieceMessageValidator.h: New class. * src/BtHaveMessageValidator.h: New class. * src/BtEventListener.h: New class. * src/AbstractBtEventListener.h: New class. * src/BtEvent.h: New class. * src/BtChokingEvent.h: New class. * src/BtChokedEvent.h: New class. * src/BtCancelSendingPieceEvent.h: New class. * src/BtAbortOutstandingRequestEvent.h: New class. * src/Randomizer.h: New class. * src/SimpleRandomizer.h: New class. * src/BtMessage.h: New class. * src/AbstractBtMessage.h: New class. * src/SimpleBtMessage.h: New class. * src/BtHaveMessage.h: New class. * src/BtInterestedMessage.h: New class. * src/BtAllowedFastMessage.h: New class. * src/BtUnchokeMessage.h: New class. * src/BtCancelMessage.h: New class. * src/BtNotInterestedMessage.h: New class. * src/BtChokeMessage.h: New class. * src/BtHaveNoneMessage.h: New class. * src/BtHandshakeMessage.h: New class. * src/BtSuggestPieceMessage.h: New class. * src/BtHaveMessage.h: New class. * src/BtPieceMessage.h: New class. * src/BtHaveAllMessage.h: New class. * src/BtKeepAliveMessage.h: New class. * src/BtPortMessage.h: New class. * src/BtRejectMessage.h: New class. * src/BtBitfieldMessage.h: New class. * src/BtRequestMessage.h: New class. * src/DefaultBtRequestFactory.h: New class. * src/DefaultBtMessageReceiver.h: New class. * src/BtInteractive.h: New class. * src/BtMessageDispatcher.h: New class. * src/DefaultBtMessageDispatcher.h: New class. * src/DefaultBtInteractive.h: New class. * src/BitfieldManFactory.h: New class. * src/HandleRegistry.h: New class. * src/BtMessageFactory.h: New class. * src/BtMessageReceiver.h: New class. * src/DefaultBtMessageFactory.h: New class. * src/PeerObject.h: New class. * src/BtRequestFactory.h: New class.
102 lines
3.5 KiB
C++
102 lines
3.5 KiB
C++
#include "BtHandshakeMessage.h"
|
|
#include "PeerMessageUtil.h"
|
|
#include "Util.h"
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
using namespace std;
|
|
|
|
class BtHandshakeMessageTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(BtHandshakeMessageTest);
|
|
CPPUNIT_TEST(testCreate);
|
|
CPPUNIT_TEST(testGetMessage);
|
|
CPPUNIT_TEST(testToString);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
private:
|
|
|
|
public:
|
|
void setUp() {
|
|
}
|
|
|
|
void testCreate();
|
|
void testGetMessage();
|
|
void testToString();
|
|
|
|
static string BTPSTR;
|
|
};
|
|
|
|
|
|
string BtHandshakeMessageTest::BTPSTR = "BitTorrent protocol";
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtHandshakeMessageTest);
|
|
|
|
void createHandshakeMessageData(unsigned char* msg) {
|
|
msg[0] = 19;
|
|
memcpy(&msg[1], BtHandshakeMessageTest::BTPSTR.c_str(),
|
|
BtHandshakeMessageTest::BTPSTR.size());
|
|
unsigned char reserved[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04 };
|
|
memcpy(&msg[20], reserved, sizeof(reserved));
|
|
unsigned char infoHash[] = { 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
0xff, 0xff, 0xff, 0xff, 0xff,
|
|
0xff, 0xff, 0xff, 0xff, 0xff,
|
|
0xff, 0xff, 0xff, 0xff, 0xff };
|
|
memcpy(&msg[28], infoHash, sizeof(infoHash));
|
|
unsigned char peerId[] = { 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0 };
|
|
memcpy(&msg[48], peerId, sizeof(peerId));
|
|
}
|
|
|
|
void BtHandshakeMessageTest::testCreate() {
|
|
unsigned char msg[68];
|
|
createHandshakeMessageData(msg);
|
|
BtHandshakeMessageHandle message = BtHandshakeMessage::create(&msg[0], sizeof(msg));
|
|
CPPUNIT_ASSERT_EQUAL(INT32_MAX, message->getId());
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)19, message->getPstrlen());
|
|
CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
|
|
Util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
|
|
CPPUNIT_ASSERT_EQUAL(string("0000000000000004"),
|
|
Util::toHex(message->getReserved(), BtHandshakeMessage::RESERVED_LENGTH));
|
|
CPPUNIT_ASSERT_EQUAL(string("ffffffffffffffffffffffffffffffffffffffff"),
|
|
Util::toHex(message->getInfoHash(), INFO_HASH_LENGTH));
|
|
CPPUNIT_ASSERT_EQUAL(string("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0"),
|
|
Util::toHex(message->getPeerId(), PEER_ID_LENGTH));
|
|
}
|
|
|
|
void BtHandshakeMessageTest::testGetMessage() {
|
|
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 };
|
|
|
|
BtHandshakeMessageHandle msg = new BtHandshakeMessage();
|
|
msg->setInfoHash(infoHash);
|
|
msg->setPeerId(peerId);
|
|
|
|
unsigned char data[68];
|
|
createHandshakeMessageData(data);
|
|
CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)data, 68),
|
|
Util::toHex((const unsigned char*)msg->getMessage(), 68));
|
|
}
|
|
|
|
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 };
|
|
|
|
BtHandshakeMessage msg;
|
|
msg.setInfoHash(infoHash);
|
|
msg.setPeerId(peerId);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(string("handshake peerId=%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0%f0, reserved=0000000000000004"), msg.toString());
|
|
}
|