2006-12-24 06:25:21 +00:00
|
|
|
#include "BtInterestedMessage.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
#include "Peer.h"
|
2008-11-03 12:04:57 +00:00
|
|
|
#include "MockPeerStorage.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class BtInterestedMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtInterestedMessageTest);
|
|
|
|
CPPUNIT_TEST(testCreate);
|
|
|
|
CPPUNIT_TEST(testGetMessage);
|
|
|
|
CPPUNIT_TEST(testDoReceivedAction);
|
|
|
|
CPPUNIT_TEST(testOnSendComplete);
|
|
|
|
CPPUNIT_TEST(testToString);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
public:
|
|
|
|
void testCreate();
|
|
|
|
void testGetMessage();
|
|
|
|
void testDoReceivedAction();
|
|
|
|
void testOnSendComplete();
|
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtInterestedMessageTest);
|
|
|
|
|
|
|
|
void BtInterestedMessageTest::testCreate() {
|
|
|
|
unsigned char msg[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessageHandle pm = BtInterestedMessage::create(&msg[4], 1);
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)2, 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, 2);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage::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, 3);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtInterestedMessage::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(...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtInterestedMessageTest::testGetMessage() {
|
|
|
|
BtInterestedMessage msg;
|
|
|
|
unsigned char data[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtInterestedMessageTest::testDoReceivedAction() {
|
|
|
|
BtInterestedMessage 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(1024, 1024*1024);
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.setPeer(peer);
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2008-11-03 12:04:57 +00:00
|
|
|
SharedHandle<MockPeerStorage> peerStorage(new MockPeerStorage());
|
|
|
|
|
|
|
|
msg.setPeerStorage(peerStorage);
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(!peer->peerInterested());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.doReceivedAction();
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(peer->peerInterested());
|
2008-11-03 12:04:57 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0, peerStorage->getNumChokeExecuted());
|
|
|
|
|
|
|
|
peer->amChoking(false);
|
|
|
|
msg.doReceivedAction();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtInterestedMessageTest::testOnSendComplete() {
|
|
|
|
BtInterestedMessage 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(1024, 1024*1024);
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.setPeer(peer);
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(!peer->amInterested());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.onSendComplete();
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(peer->amInterested());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtInterestedMessageTest::testToString() {
|
|
|
|
BtInterestedMessage msg;
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("interested"), msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|