BtPieceMessageTest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "BtPieceMessage.h"
  2. #include "PeerMessageUtil.h"
  3. #include "MockBtContext.h"
  4. #include "MockBtMessage.h"
  5. #include "MockBtMessageFactory.h"
  6. #include "MockBtMessageDispatcher.h"
  7. #include "BtChokingEvent.h"
  8. #include "BtCancelSendingPieceEvent.h"
  9. #include "FileEntry.h"
  10. #include "Peer.h"
  11. #include "Piece.h"
  12. #include "BtRegistry.h"
  13. #include "PeerObject.h"
  14. #include "BtMessageReceiver.h"
  15. #include "BtRequestFactory.h"
  16. #include "PeerConnection.h"
  17. #include "ExtensionMessageFactory.h"
  18. #include <cstring>
  19. #include <cppunit/extensions/HelperMacros.h>
  20. namespace aria2 {
  21. class BtPieceMessageTest:public CppUnit::TestFixture {
  22. CPPUNIT_TEST_SUITE(BtPieceMessageTest);
  23. CPPUNIT_TEST(testCreate);
  24. CPPUNIT_TEST(testGetMessageHeader);
  25. CPPUNIT_TEST(testChokingEvent);
  26. CPPUNIT_TEST(testChokingEvent_allowedFastEnabled);
  27. CPPUNIT_TEST(testChokingEvent_inAmAllowedIndexSet);
  28. CPPUNIT_TEST(testChokingEvent_invalidate);
  29. CPPUNIT_TEST(testChokingEvent_sendingInProgress);
  30. CPPUNIT_TEST(testCancelSendingPieceEvent);
  31. CPPUNIT_TEST(testCancelSendingPieceEvent_noMatch);
  32. CPPUNIT_TEST(testCancelSendingPieceEvent_allowedFastEnabled);
  33. CPPUNIT_TEST(testCancelSendingPieceEvent_invalidate);
  34. CPPUNIT_TEST(testCancelSendingPieceEvent_sendingInProgress);
  35. CPPUNIT_TEST(testToString);
  36. CPPUNIT_TEST_SUITE_END();
  37. public:
  38. BtPieceMessageTest():btMessageDispatcher(0), peer(0), msg(0) {}
  39. void testCreate();
  40. void testGetMessageHeader();
  41. void testChokingEvent();
  42. void testChokingEvent_allowedFastEnabled();
  43. void testChokingEvent_inAmAllowedIndexSet();
  44. void testChokingEvent_invalidate();
  45. void testChokingEvent_sendingInProgress();
  46. void testCancelSendingPieceEvent();
  47. void testCancelSendingPieceEvent_noMatch();
  48. void testCancelSendingPieceEvent_allowedFastEnabled();
  49. void testCancelSendingPieceEvent_invalidate();
  50. void testCancelSendingPieceEvent_sendingInProgress();
  51. void testToString();
  52. class MockBtMessage2 : public MockBtMessage {
  53. public:
  54. int32_t index;
  55. int32_t begin;
  56. int32_t length;
  57. public:
  58. MockBtMessage2(int32_t index, int32_t begin, int32_t length):index(index), begin(begin), length(length) {}
  59. };
  60. class MockBtMessageFactory2 : public MockBtMessageFactory {
  61. public:
  62. virtual SharedHandle<BtMessage>
  63. createRejectMessage(int32_t index,
  64. int32_t begin,
  65. int32_t length) {
  66. SharedHandle<MockBtMessage2> msg = new MockBtMessage2(index, begin, length);
  67. return msg;
  68. }
  69. };
  70. SharedHandle<MockBtMessageDispatcher> btMessageDispatcher;
  71. SharedHandle<Peer> peer;
  72. SharedHandle<BtPieceMessage> msg;
  73. void setUp() {
  74. BtRegistry::unregisterAll();
  75. SharedHandle<MockBtContext> btContext;
  76. btContext->setInfoHash((const unsigned char*)"12345678901234567890");
  77. btContext->setPieceLength(16*1024);
  78. btContext->setTotalLength(256*1024);
  79. peer = new Peer("host", 6969);
  80. peer->allocateSessionResource(btContext->getPieceLength(),
  81. btContext->getTotalLength());
  82. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  83. new PeerObjectCluster());
  84. PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject());
  85. btMessageDispatcher = new MockBtMessageDispatcher();
  86. PEER_OBJECT(btContext, peer)->btMessageDispatcher = btMessageDispatcher;
  87. PEER_OBJECT(btContext, peer)->btMessageFactory = new MockBtMessageFactory2();
  88. msg = new BtPieceMessage();
  89. msg->setIndex(1);
  90. msg->setBegin(1024);
  91. msg->setBlockLength(16*1024);
  92. msg->setBtContext(btContext);
  93. msg->setPeer(peer);
  94. msg->setBtMessageDispatcher(btMessageDispatcher);
  95. msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer));
  96. }
  97. };
  98. CPPUNIT_TEST_SUITE_REGISTRATION(BtPieceMessageTest);
  99. void BtPieceMessageTest::testCreate() {
  100. unsigned char msg[13+2];
  101. unsigned char data[2];
  102. memset(data, 0xff, sizeof(data));
  103. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 11, 7);
  104. PeerMessageUtil::setIntParam(&msg[5], 12345);
  105. PeerMessageUtil::setIntParam(&msg[9], 256);
  106. memcpy(&msg[13], data, sizeof(data));
  107. SharedHandle<BtPieceMessage> pm = BtPieceMessage::create(&msg[4], 11);
  108. CPPUNIT_ASSERT_EQUAL((int8_t)7, pm->getId());
  109. CPPUNIT_ASSERT_EQUAL((int32_t)12345, pm->getIndex());
  110. CPPUNIT_ASSERT_EQUAL((int32_t)256, pm->getBegin());
  111. CPPUNIT_ASSERT(memcmp(data, pm->getBlock(), sizeof(data)) == 0);
  112. CPPUNIT_ASSERT_EQUAL((int32_t)2, pm->getBlockLength());
  113. // case: payload size is wrong
  114. try {
  115. unsigned char msg[13];
  116. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 9, 7);
  117. BtPieceMessage::create(&msg[4], 9);
  118. CPPUNIT_FAIL("exception must be thrown.");
  119. } catch(...) {
  120. }
  121. // case: id is wrong
  122. try {
  123. unsigned char msg[13+2];
  124. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 11, 8);
  125. BtPieceMessage::create(&msg[4], 11);
  126. CPPUNIT_FAIL("exception must be thrown.");
  127. } catch(...) {
  128. }
  129. }
  130. void BtPieceMessageTest::testGetMessageHeader() {
  131. BtPieceMessage msg;
  132. msg.setIndex(12345);
  133. msg.setBegin(256);
  134. msg.setBlockLength(1024);
  135. unsigned char data[13];
  136. PeerMessageUtil::createPeerMessageString(data, sizeof(data), 9+1024, 7);
  137. PeerMessageUtil::setIntParam(&data[5], 12345);
  138. PeerMessageUtil::setIntParam(&data[9], 256);
  139. CPPUNIT_ASSERT(memcmp(msg.getMessageHeader(), data, 13) == 0);
  140. }
  141. void BtPieceMessageTest::testChokingEvent() {
  142. CPPUNIT_ASSERT(!msg->isInvalidate());
  143. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  144. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  145. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  146. msg->handleEvent(new BtChokingEvent());
  147. CPPUNIT_ASSERT(msg->isInvalidate());
  148. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  149. }
  150. void BtPieceMessageTest::testChokingEvent_allowedFastEnabled() {
  151. peer->setFastExtensionEnabled(true);
  152. CPPUNIT_ASSERT(!msg->isInvalidate());
  153. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  154. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  155. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  156. msg->handleEvent(new BtChokingEvent());
  157. CPPUNIT_ASSERT(msg->isInvalidate());
  158. CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size());
  159. MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get();
  160. CPPUNIT_ASSERT_EQUAL((int32_t)1, rej->index);
  161. CPPUNIT_ASSERT_EQUAL((int32_t)1024, rej->begin);
  162. CPPUNIT_ASSERT_EQUAL((int32_t)16*1024, rej->length);
  163. }
  164. void BtPieceMessageTest::testChokingEvent_inAmAllowedIndexSet() {
  165. peer->setFastExtensionEnabled(true);
  166. peer->addAmAllowedIndex(1);
  167. CPPUNIT_ASSERT(!msg->isInvalidate());
  168. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  169. CPPUNIT_ASSERT(peer->isInAmAllowedIndexSet(1));
  170. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  171. msg->handleEvent(new BtChokingEvent());
  172. CPPUNIT_ASSERT(!msg->isInvalidate());
  173. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  174. }
  175. void BtPieceMessageTest::testChokingEvent_invalidate() {
  176. msg->setInvalidate(true);
  177. CPPUNIT_ASSERT(msg->isInvalidate());
  178. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  179. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  180. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  181. msg->handleEvent(new BtChokingEvent());
  182. CPPUNIT_ASSERT(msg->isInvalidate());
  183. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  184. }
  185. void BtPieceMessageTest::testChokingEvent_sendingInProgress() {
  186. msg->setSendingInProgress(true);
  187. CPPUNIT_ASSERT(!msg->isInvalidate());
  188. CPPUNIT_ASSERT(msg->isSendingInProgress());
  189. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  190. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  191. msg->handleEvent(new BtChokingEvent());
  192. CPPUNIT_ASSERT(!msg->isInvalidate());
  193. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  194. }
  195. void BtPieceMessageTest::testCancelSendingPieceEvent() {
  196. CPPUNIT_ASSERT(!msg->isInvalidate());
  197. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  198. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  199. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  200. CPPUNIT_ASSERT(msg->isInvalidate());
  201. }
  202. void BtPieceMessageTest::testCancelSendingPieceEvent_noMatch() {
  203. CPPUNIT_ASSERT(!msg->isInvalidate());
  204. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  205. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  206. msg->handleEvent(new BtCancelSendingPieceEvent(0, 1024, 16*1024));
  207. CPPUNIT_ASSERT(!msg->isInvalidate());
  208. msg->handleEvent(new BtCancelSendingPieceEvent(1, 0, 16*1024));
  209. CPPUNIT_ASSERT(!msg->isInvalidate());
  210. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 0));
  211. CPPUNIT_ASSERT(!msg->isInvalidate());
  212. }
  213. void BtPieceMessageTest::testCancelSendingPieceEvent_allowedFastEnabled() {
  214. peer->setFastExtensionEnabled(true);
  215. CPPUNIT_ASSERT(!msg->isInvalidate());
  216. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  217. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  218. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  219. CPPUNIT_ASSERT(msg->isInvalidate());
  220. CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size());
  221. MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get();
  222. CPPUNIT_ASSERT_EQUAL((int32_t)1, rej->index);
  223. CPPUNIT_ASSERT_EQUAL((int32_t)1024, rej->begin);
  224. CPPUNIT_ASSERT_EQUAL((int32_t)16*1024, rej->length);
  225. }
  226. void BtPieceMessageTest::testCancelSendingPieceEvent_invalidate() {
  227. msg->setInvalidate(true);
  228. peer->setFastExtensionEnabled(true);
  229. CPPUNIT_ASSERT(msg->isInvalidate());
  230. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  231. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  232. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  233. CPPUNIT_ASSERT(msg->isInvalidate());
  234. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  235. }
  236. void BtPieceMessageTest::testCancelSendingPieceEvent_sendingInProgress() {
  237. msg->setSendingInProgress(true);
  238. CPPUNIT_ASSERT(!msg->isInvalidate());
  239. CPPUNIT_ASSERT(msg->isSendingInProgress());
  240. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  241. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  242. CPPUNIT_ASSERT(!msg->isInvalidate());
  243. }
  244. void BtPieceMessageTest::testToString() {
  245. CPPUNIT_ASSERT_EQUAL(std::string("piece index=1, begin=1024, length=16384"),
  246. msg->toString());
  247. }
  248. } // namespace aria2