BtRejectMessageTest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "BtRejectMessage.h"
  2. #include "PeerMessageUtil.h"
  3. #include "Peer.h"
  4. #include "FileEntry.h"
  5. #include "BtRegistry.h"
  6. #include "PeerObject.h"
  7. #include "BtMessageFactory.h"
  8. #include "BtRequestFactory.h"
  9. #include "BtMessageReceiver.h"
  10. #include "ExtensionMessageFactory.h"
  11. #include "PeerConnection.h"
  12. #include "MockBtMessageDispatcher.h"
  13. #include "MockBtContext.h"
  14. #include <cstring>
  15. #include <cppunit/extensions/HelperMacros.h>
  16. namespace aria2 {
  17. class BtRejectMessageTest:public CppUnit::TestFixture {
  18. CPPUNIT_TEST_SUITE(BtRejectMessageTest);
  19. CPPUNIT_TEST(testCreate);
  20. CPPUNIT_TEST(testGetMessage);
  21. CPPUNIT_TEST(testDoReceivedAction);
  22. CPPUNIT_TEST(testDoReceivedActionNoMatch);
  23. CPPUNIT_TEST(testDoReceivedActionFastExtensionDisabled);
  24. CPPUNIT_TEST(testToString);
  25. CPPUNIT_TEST_SUITE_END();
  26. private:
  27. public:
  28. void testCreate();
  29. void testGetMessage();
  30. void testDoReceivedAction();
  31. void testDoReceivedActionNoMatch();
  32. void testDoReceivedActionFastExtensionDisabled();
  33. void testToString();
  34. class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
  35. public:
  36. RequestSlot slot;
  37. public:
  38. MockBtMessageDispatcher2():slot(RequestSlot::nullSlot) {}
  39. void setRequestSlot(const RequestSlot& slot) {
  40. this->slot = slot;
  41. }
  42. virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin,
  43. int32_t length) {
  44. if(slot.getIndex() == index && slot.getBegin() == begin &&
  45. slot.getLength() == length) {
  46. return slot;
  47. } else {
  48. return RequestSlot::nullSlot;
  49. }
  50. }
  51. virtual void removeOutstandingRequest(const RequestSlot& slot) {
  52. if(this->slot.getIndex() == slot.getIndex() &&
  53. this->slot.getBegin() == slot.getBegin() &&
  54. this->slot.getLength() == slot.getLength()) {
  55. this->slot = RequestSlot::nullSlot;
  56. }
  57. }
  58. };
  59. typedef SharedHandle<MockBtMessageDispatcher2> MockBtMessageDispatcher2Handle;
  60. SharedHandle<Peer> peer;
  61. SharedHandle<MockBtMessageDispatcher2> dispatcher;
  62. SharedHandle<BtRejectMessage> msg;
  63. BtRejectMessageTest():peer(0), dispatcher(0), msg(0) {}
  64. void setUp() {
  65. BtRegistry::unregisterAll();
  66. peer = new Peer("host", 6969);
  67. peer->allocateSessionResource(1024, 1024*1024);
  68. SharedHandle<MockBtContext> btContext = new MockBtContext();
  69. btContext->setInfoHash((const unsigned char*)"12345678901234567890");
  70. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  71. new PeerObjectCluster());
  72. PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject());
  73. dispatcher = new MockBtMessageDispatcher2();
  74. PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
  75. msg = new BtRejectMessage();
  76. msg->setPeer(peer);
  77. msg->setBtContext(btContext);
  78. msg->setIndex(1);
  79. msg->setBegin(16);
  80. msg->setLength(32);
  81. msg->setBtMessageDispatcher(dispatcher);
  82. }
  83. };
  84. CPPUNIT_TEST_SUITE_REGISTRATION(BtRejectMessageTest);
  85. void BtRejectMessageTest::testCreate() {
  86. unsigned char msg[17];
  87. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 16);
  88. PeerMessageUtil::setIntParam(&msg[5], 12345);
  89. PeerMessageUtil::setIntParam(&msg[9], 256);
  90. PeerMessageUtil::setIntParam(&msg[13], 1024);
  91. SharedHandle<BtRejectMessage> pm = BtRejectMessage::create(&msg[4], 13);
  92. CPPUNIT_ASSERT_EQUAL((int8_t)16, pm->getId());
  93. CPPUNIT_ASSERT_EQUAL((int32_t)12345, pm->getIndex());
  94. CPPUNIT_ASSERT_EQUAL((int32_t)256, pm->getBegin());
  95. CPPUNIT_ASSERT_EQUAL((int32_t)1024, pm->getLength());
  96. // case: payload size is wrong
  97. try {
  98. unsigned char msg[18];
  99. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 16);
  100. BtRejectMessage::create(&msg[4], 14);
  101. CPPUNIT_FAIL("exception must be thrown.");
  102. } catch(...) {
  103. }
  104. // case: id is wrong
  105. try {
  106. unsigned char msg[17];
  107. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 17);
  108. BtRejectMessage::create(&msg[4], 13);
  109. CPPUNIT_FAIL("exception must be thrown.");
  110. } catch(...) {
  111. }
  112. }
  113. void BtRejectMessageTest::testGetMessage() {
  114. BtRejectMessage msg;
  115. msg.setIndex(12345);
  116. msg.setBegin(256);
  117. msg.setLength(1024);
  118. unsigned char data[17];
  119. PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 16);
  120. PeerMessageUtil::setIntParam(&data[5], 12345);
  121. PeerMessageUtil::setIntParam(&data[9], 256);
  122. PeerMessageUtil::setIntParam(&data[13], 1024);
  123. CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
  124. }
  125. void BtRejectMessageTest::testDoReceivedAction() {
  126. peer->setFastExtensionEnabled(true);
  127. RequestSlot slot(1, 16, 32, 2);
  128. dispatcher->setRequestSlot(slot);
  129. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
  130. msg->doReceivedAction();
  131. CPPUNIT_ASSERT(RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
  132. }
  133. void BtRejectMessageTest::testDoReceivedActionNoMatch() {
  134. peer->setFastExtensionEnabled(true);
  135. RequestSlot slot(2, 16, 32, 2);
  136. dispatcher->setRequestSlot(slot);
  137. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32)));
  138. msg->doReceivedAction();
  139. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32)));
  140. }
  141. void BtRejectMessageTest::testDoReceivedActionFastExtensionDisabled() {
  142. RequestSlot slot(1, 16, 32, 2);
  143. dispatcher->setRequestSlot(slot);
  144. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
  145. try {
  146. msg->doReceivedAction();
  147. CPPUNIT_FAIL("exception must be thrown.");
  148. } catch(...) {}
  149. }
  150. void BtRejectMessageTest::testToString() {
  151. CPPUNIT_ASSERT_EQUAL(std::string("reject index=1, begin=16, length=32"),
  152. msg->toString());
  153. }
  154. } // namespace aria2