aria2/test/BtRejectMessageTest.cc
Tatsuhiro Tsujikawa cd91e2ea4f 2007-01-28 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/Xml2MetalinkProcessor.h
	(xpathExists): New function.
	* src/Xml2MetalinkProcessor.cc
	(xpathExists): New function.

	Not to send HEAD request if filename and size are available in 
Metalink
	file:
	* src/UrlRequestInfo.h
	(filename): New variable.
	(totalLength): New variable.
	(setTotalLength): New function.
	(setFilename): New function.
	* src/MetalinkRequestInfo.cc
	(execute): Set filename and size to UrlRequestInfo.
	* src/MetalinkEntry.cc
	(MetalinkEntry): Initialize size with 0.
	* src/UrlRequestInfo.cc
	(execute): Set filename and size to SegmentMan.
	
	Not to download rest of the files after selected files are 
downloaded
	in BitTorrent:
	* src/PieceStorage.h
	(allDownloadFinished): New function.
	* src/DefaultBtAnnounce.cc
	(isCompleteAnnounceReady): Use allDownloadFinished instead of
	downloadFinished.
	(getAnnounceUrl): Use allDownloadFinished instead of 
downloadFinished.
	* src/DefaultPieceStorage.cc
	(completePiece): Use allDownloadFinished instead of 
downloadFinished.
	Commented out the call to finishSelectiveDownloadingMode().
	(downloadFinished): Call isFilteredAllBitSet() instead of
	isAllBitSet().
	(allDownloadFinished): New function.
	* src/DefaultBtInteractive.cc
	(addBitfieldMessageToQueue): Call allDownloadFinished() instead 
of
	downloadFinished().
	(checkHave): Call allDownloadFinished() instead of 
downloadFinished().
	* src/TorrentDownloadEngine.cc
	(onEndOfRun): Call allDownloadFinished() instead of 
downloadFinished().
	* src/BitfieldMan.h
	(isFilteredAllBitSet): New function.
	* src/ShareRatioSeedCriteria.h
	(PieceStorage.h): New include.
	(pieceStorage): New variable.
	(evaluate):
	btContext->getTotalLength() -> 
pieceStorage->getCompletedLength()
	* src/BitfieldMan.cc
	(isFilteredAllBitSet): New function.
	(isAllBitSet): Filter is not took into account.
	
	Rename --force-truncate as --allow-overwrite:
	* src/TorrentRequestInfo.cc
	(execute): PREF_FORCE_TRUNCATE -> PREF_ALLOW_OVERWRITE
	* src/main.cc
	(showUsage): --force-truncate -> --allow-overwrite
	* src/message.h
	(EX_FILE_ALREADY_EXISTS): --force-truncate -> --allow-overwrite
	* src/prefs.h
	(PREF_FORCE_TRUNCATE): Removed.
	(PREF_ALLOW_OVERWRITE): New definition.
	* src/SegmentMan.cc
	(shouldCancelDownloadForSafety): --force-truncate -> 
--allow-overwrite
	
	* src/MetalinkRequestInfo.cc
	(execute): Queueing message are now logged in info level.

	* src/common.h
	(LONG_LONG_MAX): Removed.
	(LONG_LONG_MIN): Removed.
	* src/HttpResponseCommand.cc
	(handleDefaultEncoding): LONG_LONG_MAX -> INT64_MAX
	* src/FtpNegotiateCommand.cc
	(recvSize): LONG_LONG_MAX -> INT64_MAX
	
	* src/main.cc
	(showUsage): Added --check-integiry and 
--realtime-chunk-checksum
	command-line option.
	(main): Added --check-integiry and --realtime-chunk-checksum
	command-line option.
	--force-truncate -> --allow-overwrite
	Set initial value of --check-integrity option to false.
	Don't show usage when error occurs while persing command-line 
options.
	Removed deprecated --upload-limit option.
2007-01-28 14:18:35 +00:00

176 lines
5.2 KiB
C++

#include "BtRejectMessage.h"
#include "PeerMessageUtil.h"
#include "Peer.h"
#include "MockBtMessageDispatcher.h"
#include "MockBtContext.h"
#include <cppunit/extensions/HelperMacros.h>
using namespace std;
class BtRejectMessageTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BtRejectMessageTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testGetMessage);
CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST(testDoReceivedActionNoMatch);
CPPUNIT_TEST(testDoReceivedActionFastExtensionDisabled);
CPPUNIT_TEST(testToString);
CPPUNIT_TEST_SUITE_END();
private:
public:
void testCreate();
void testGetMessage();
void testDoReceivedAction();
void testDoReceivedActionNoMatch();
void testDoReceivedActionFastExtensionDisabled();
void testToString();
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
public:
RequestSlot slot;
public:
MockBtMessageDispatcher2():slot(RequestSlot::nullSlot) {}
void setRequestSlot(const RequestSlot& slot) {
this->slot = slot;
}
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin,
int32_t length) {
if(slot.getIndex() == index && slot.getBegin() == begin &&
slot.getLength() == length) {
return slot;
} else {
return RequestSlot::nullSlot;
}
}
virtual void removeOutstandingRequest(const RequestSlot& slot) {
if(this->slot.getIndex() == slot.getIndex() &&
this->slot.getBegin() == slot.getBegin() &&
this->slot.getLength() == slot.getLength()) {
this->slot = RequestSlot::nullSlot;
}
}
};
typedef SharedHandle<MockBtMessageDispatcher2> MockBtMessageDispatcher2Handle;
PeerHandle peer;
MockBtMessageDispatcher2Handle dispatcher;
BtRejectMessageHandle msg;
BtRejectMessageTest():peer(0), dispatcher(0), msg(0) {}
void setUp() {
BtRegistry::clear();
peer = new Peer("host", 6969, 16*1024, 256*1024);
MockBtContextHandle btContext = new MockBtContext();
btContext->setInfoHash((const unsigned char*)"12345678901234567890");
BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
new PeerObjectCluster());
PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject());
dispatcher = new MockBtMessageDispatcher2();
PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
msg = new BtRejectMessage();
msg->setPeer(peer);
msg->setBtContext(btContext);
msg->setIndex(1);
msg->setBegin(16);
msg->setLength(32);
msg->setBtMessageDispatcher(dispatcher);
}
};
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);
BtRejectMessageHandle pm = BtRejectMessage::create(&msg[4], 13);
CPPUNIT_ASSERT_EQUAL((int8_t)16, pm->getId());
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
CPPUNIT_ASSERT_EQUAL(1024, pm->getLength());
// case: payload size is wrong
try {
unsigned char msg[18];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 16);
BtRejectMessage::create(&msg[4], 14);
CPPUNIT_FAIL("exception must be threw.");
} catch(...) {
}
// case: id is wrong
try {
unsigned char msg[17];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 17);
BtRejectMessage::create(&msg[4], 13);
CPPUNIT_FAIL("exception must be threw.");
} catch(...) {
}
}
void BtRejectMessageTest::testGetMessage() {
BtRejectMessage msg;
msg.setIndex(12345);
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);
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
}
void BtRejectMessageTest::testDoReceivedAction() {
peer->setFastExtensionEnabled(true);
RequestSlot slot(1, 16, 32, 2);
dispatcher->setRequestSlot(slot);
CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
msg->doReceivedAction();
CPPUNIT_ASSERT(RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
}
void BtRejectMessageTest::testDoReceivedActionNoMatch() {
peer->setFastExtensionEnabled(true);
RequestSlot slot(2, 16, 32, 2);
dispatcher->setRequestSlot(slot);
CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32)));
msg->doReceivedAction();
CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32)));
}
void BtRejectMessageTest::testDoReceivedActionFastExtensionDisabled() {
RequestSlot slot(1, 16, 32, 2);
dispatcher->setRequestSlot(slot);
CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
try {
msg->doReceivedAction();
CPPUNIT_FAIL("exception must be threw.");
} catch(...) {}
}
void BtRejectMessageTest::testToString() {
CPPUNIT_ASSERT_EQUAL(string("reject index=1, begin=16, length=32"),
msg->toString());
}