2006-12-24 06:25:21 +00:00
|
|
|
#include "BtUnchokeMessage.h"
|
2013-01-11 05:20:34 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
2006-12-24 06:25:21 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2013-01-11 05:20:34 +00:00
|
|
|
#include "bittorrent_helper.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "SocketBuffer.h"
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class BtUnchokeMessageTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(BtUnchokeMessageTest);
|
|
|
|
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);
|
|
|
|
CPPUNIT_TEST(testOnSendComplete);
|
|
|
|
CPPUNIT_TEST(testToString);
|
|
|
|
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();
|
|
|
|
void testOnSendComplete();
|
|
|
|
void testToString();
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(BtUnchokeMessageTest);
|
|
|
|
|
|
|
|
void BtUnchokeMessageTest::testCreate() {
|
|
|
|
unsigned char msg[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 1);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<BtUnchokeMessage> pm(BtUnchokeMessage::create(&msg[4], 1));
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint8_t)1, 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, 1);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtUnchokeMessage::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, 2);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtUnchokeMessage::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 BtUnchokeMessageTest::testCreateMessage() {
|
2006-12-24 06:25:21 +00:00
|
|
|
BtUnchokeMessage msg;
|
|
|
|
unsigned char data[5];
|
2009-09-29 14:52:42 +00:00
|
|
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 1);
|
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 BtUnchokeMessageTest::testDoReceivedAction() {
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("host", 6969));
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->allocateSessionResource(1024, 1024*1024);
|
|
|
|
peer->peerChoking(true);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtUnchokeMessage msg;
|
|
|
|
msg.setPeer(peer);
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(peer->peerChoking());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg.doReceivedAction();
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(!peer->peerChoking());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtUnchokeMessageTest::testOnSendComplete() {
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer(new Peer("host", 6969));
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->allocateSessionResource(1024, 1024*1024);
|
|
|
|
peer->amChoking(true);
|
2006-12-24 06:25:21 +00:00
|
|
|
BtUnchokeMessage msg;
|
|
|
|
msg.setPeer(peer);
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(peer->amChoking());
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
|
2013-01-11 05:20:34 +00:00
|
|
|
pu->update(0, true);
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT(!peer->amChoking());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtUnchokeMessageTest::testToString() {
|
|
|
|
BtUnchokeMessage msg;
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("unchoke"), msg.toString());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|