BtRejectMessageTest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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(size_t index, uint32_t begin,
  43. size_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. void setUp() {
  64. BtRegistry::unregisterAll();
  65. peer.reset(new Peer("host", 6969));
  66. peer->allocateSessionResource(1024, 1024*1024);
  67. SharedHandle<MockBtContext> btContext(new MockBtContext());
  68. btContext->setInfoHash((const unsigned char*)"12345678901234567890");
  69. SharedHandle<PeerObjectCluster> cluster(new PeerObjectCluster());
  70. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  71. cluster);
  72. SharedHandle<PeerObject> po(new PeerObject());
  73. PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po);
  74. dispatcher.reset(new MockBtMessageDispatcher2());
  75. PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
  76. msg.reset(new BtRejectMessage());
  77. msg->setPeer(peer);
  78. msg->setBtContext(btContext);
  79. msg->setIndex(1);
  80. msg->setBegin(16);
  81. msg->setLength(32);
  82. msg->setBtMessageDispatcher(dispatcher);
  83. }
  84. void tearDown() {
  85. BtRegistry::unregisterAll();
  86. }
  87. };
  88. CPPUNIT_TEST_SUITE_REGISTRATION(BtRejectMessageTest);
  89. void BtRejectMessageTest::testCreate() {
  90. unsigned char msg[17];
  91. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 16);
  92. PeerMessageUtil::setIntParam(&msg[5], 12345);
  93. PeerMessageUtil::setIntParam(&msg[9], 256);
  94. PeerMessageUtil::setIntParam(&msg[13], 1024);
  95. SharedHandle<BtRejectMessage> pm = BtRejectMessage::create(&msg[4], 13);
  96. CPPUNIT_ASSERT_EQUAL((uint8_t)16, pm->getId());
  97. CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
  98. CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
  99. CPPUNIT_ASSERT_EQUAL((size_t)1024, pm->getLength());
  100. // case: payload size is wrong
  101. try {
  102. unsigned char msg[18];
  103. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 14, 16);
  104. BtRejectMessage::create(&msg[4], 14);
  105. CPPUNIT_FAIL("exception must be thrown.");
  106. } catch(...) {
  107. }
  108. // case: id is wrong
  109. try {
  110. unsigned char msg[17];
  111. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, 17);
  112. BtRejectMessage::create(&msg[4], 13);
  113. CPPUNIT_FAIL("exception must be thrown.");
  114. } catch(...) {
  115. }
  116. }
  117. void BtRejectMessageTest::testGetMessage() {
  118. BtRejectMessage msg;
  119. msg.setIndex(12345);
  120. msg.setBegin(256);
  121. msg.setLength(1024);
  122. unsigned char data[17];
  123. PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 16);
  124. PeerMessageUtil::setIntParam(&data[5], 12345);
  125. PeerMessageUtil::setIntParam(&data[9], 256);
  126. PeerMessageUtil::setIntParam(&data[13], 1024);
  127. CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
  128. }
  129. void BtRejectMessageTest::testDoReceivedAction() {
  130. peer->setFastExtensionEnabled(true);
  131. RequestSlot slot(1, 16, 32, 2);
  132. dispatcher->setRequestSlot(slot);
  133. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
  134. msg->doReceivedAction();
  135. CPPUNIT_ASSERT(RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
  136. }
  137. void BtRejectMessageTest::testDoReceivedActionNoMatch() {
  138. peer->setFastExtensionEnabled(true);
  139. RequestSlot slot(2, 16, 32, 2);
  140. dispatcher->setRequestSlot(slot);
  141. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32)));
  142. msg->doReceivedAction();
  143. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32)));
  144. }
  145. void BtRejectMessageTest::testDoReceivedActionFastExtensionDisabled() {
  146. RequestSlot slot(1, 16, 32, 2);
  147. dispatcher->setRequestSlot(slot);
  148. CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32)));
  149. try {
  150. msg->doReceivedAction();
  151. CPPUNIT_FAIL("exception must be thrown.");
  152. } catch(...) {}
  153. }
  154. void BtRejectMessageTest::testToString() {
  155. CPPUNIT_ASSERT_EQUAL(std::string("reject index=1, begin=16, length=32"),
  156. msg->toString());
  157. }
  158. } // namespace aria2