2006-12-24 06:25:21 +00:00
|
|
|
#include "BtHaveAllMessage.h"
|
2009-07-06 15:22:06 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
2008-05-11 01:22:32 +00:00
|
|
|
#include "MockPieceStorage.h"
|
2009-07-06 15:22:06 +00:00
|
|
|
#include "DlAbortEx.h"
|
2010-06-11 12:49:14 +00:00
|
|
|
#include "FileEntry.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
|
|
|
|
|
|
|
class BtHaveAllMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtHaveAllMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
2010-03-04 16:24:03 +00:00
|
|
|
CPPUNIT_TEST(testCreateMessage);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST(testDoReceivedAction);
|
2009-07-06 15:22:06 +00:00
|
|
|
CPPUNIT_TEST(testDoReceivedAction_goodByeSeeder);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testCreate();
|
2010-03-04 16:24:03 +00:00
|
|
|
void testCreateMessage();
|
2006-12-24 06:25:21 +00:00
|
|
|
void testDoReceivedAction();
|
2009-07-06 15:22:06 +00:00
|
|
|
void testDoReceivedAction_goodByeSeeder();
|
2006-12-24 06:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtHaveAllMessageTest);
|
|
|
|
|
|
|
|
void BtHaveAllMessageTest::testCreate() {
|
|
|
|
unsigned char msg[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 14);
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<BtHaveAllMessage> pm = BtHaveAllMessage::create(&msg[4], 1);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)14, pm->getId());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
// case: payload size is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[6];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 14);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveAllMessage::create(&msg[4], 2);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2006-12-24 06:25:21 +00:00
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
// case: id is wrong
|
|
|
|
try {
|
|
|
|
unsigned char msg[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 15);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveAllMessage::create(&msg[4], 1);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2006-12-24 06:25:21 +00:00
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-04 16:24:03 +00:00
|
|
|
void BtHaveAllMessageTest::testCreateMessage() {
|
2006-12-24 06:25:21 +00:00
|
|
|
BtHaveAllMessage msg;
|
|
|
|
unsigned char data[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 14);
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* rawmsg = msg.createMessage();
|
|
|
|
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
|
|
|
delete [] rawmsg;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtHaveAllMessageTest::testDoReceivedAction() {
|
|
|
|
BtHaveAllMessage msg;
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Peer> peer(new Peer("host", 6969));
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->allocateSessionResource(16*1024, 256*1024);
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
2008-05-11 01:22:32 +00:00
|
|
|
SharedHandle<MockPieceStorage> pieceStorage(new MockPieceStorage());
|
|
|
|
msg.setPieceStorage(pieceStorage);
|
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.doReceivedAction();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(peer->isSeeder());
|
|
|
|
|
|
|
|
peer->setFastExtensionEnabled(false);
|
|
|
|
|
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2006-12-24 06:25:21 +00:00
|
|
|
} catch(...) {}
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-07-06 15:22:06 +00:00
|
|
|
void BtHaveAllMessageTest::testDoReceivedAction_goodByeSeeder()
|
|
|
|
{
|
|
|
|
BtHaveAllMessage msg;
|
|
|
|
SharedHandle<Peer> peer(new Peer("ip", 6000));
|
|
|
|
peer->allocateSessionResource(1024, 1024);
|
|
|
|
peer->setFastExtensionEnabled(true);
|
|
|
|
msg.setPeer(peer);
|
|
|
|
SharedHandle<MockPieceStorage> pieceStorage(new MockPieceStorage());
|
|
|
|
msg.setPieceStorage(pieceStorage);
|
|
|
|
|
|
|
|
pieceStorage->setDownloadFinished(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
|
|
} catch(DlAbortEx& e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|