BtRejectMessageTest.cc 5.5 KB

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