BtPieceMessageTest.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(),
  81. new PeerObjectCluster());
  82. PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject());
  83. btMessageDispatcher = new MockBtMessageDispatcher();
  84. PEER_OBJECT(btContext, peer)->btMessageDispatcher = btMessageDispatcher;
  85. PEER_OBJECT(btContext, peer)->btMessageFactory = new MockBtMessageFactory2();
  86. msg = new BtPieceMessage();
  87. msg->setIndex(1);
  88. msg->setBegin(1024);
  89. msg->setBlockLength(16*1024);
  90. msg->setBtContext(btContext);
  91. msg->setPeer(peer);
  92. msg->setBtMessageDispatcher(btMessageDispatcher);
  93. msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer));
  94. }
  95. };
  96. CPPUNIT_TEST_SUITE_REGISTRATION(BtPieceMessageTest);
  97. void BtPieceMessageTest::testCreate() {
  98. unsigned char msg[13+2];
  99. unsigned char data[2];
  100. memset(data, 0xff, sizeof(data));
  101. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 11, 7);
  102. PeerMessageUtil::setIntParam(&msg[5], 12345);
  103. PeerMessageUtil::setIntParam(&msg[9], 256);
  104. memcpy(&msg[13], data, sizeof(data));
  105. SharedHandle<BtPieceMessage> pm = BtPieceMessage::create(&msg[4], 11);
  106. CPPUNIT_ASSERT_EQUAL((int8_t)7, pm->getId());
  107. CPPUNIT_ASSERT_EQUAL((int32_t)12345, pm->getIndex());
  108. CPPUNIT_ASSERT_EQUAL((int32_t)256, pm->getBegin());
  109. CPPUNIT_ASSERT(memcmp(data, pm->getBlock(), sizeof(data)) == 0);
  110. CPPUNIT_ASSERT_EQUAL((int32_t)2, pm->getBlockLength());
  111. // case: payload size is wrong
  112. try {
  113. unsigned char msg[13];
  114. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 9, 7);
  115. BtPieceMessage::create(&msg[4], 9);
  116. CPPUNIT_FAIL("exception must be thrown.");
  117. } catch(...) {
  118. }
  119. // case: id is wrong
  120. try {
  121. unsigned char msg[13+2];
  122. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 11, 8);
  123. BtPieceMessage::create(&msg[4], 11);
  124. CPPUNIT_FAIL("exception must be thrown.");
  125. } catch(...) {
  126. }
  127. }
  128. void BtPieceMessageTest::testGetMessageHeader() {
  129. BtPieceMessage msg;
  130. msg.setIndex(12345);
  131. msg.setBegin(256);
  132. msg.setBlockLength(1024);
  133. unsigned char data[13];
  134. PeerMessageUtil::createPeerMessageString(data, sizeof(data), 9+1024, 7);
  135. PeerMessageUtil::setIntParam(&data[5], 12345);
  136. PeerMessageUtil::setIntParam(&data[9], 256);
  137. CPPUNIT_ASSERT(memcmp(msg.getMessageHeader(), data, 13) == 0);
  138. }
  139. void BtPieceMessageTest::testChokingEvent() {
  140. CPPUNIT_ASSERT(!msg->isInvalidate());
  141. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  142. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  143. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  144. msg->handleEvent(new BtChokingEvent());
  145. CPPUNIT_ASSERT(msg->isInvalidate());
  146. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  147. }
  148. void BtPieceMessageTest::testChokingEvent_allowedFastEnabled() {
  149. peer->setFastExtensionEnabled(true);
  150. CPPUNIT_ASSERT(!msg->isInvalidate());
  151. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  152. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  153. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  154. msg->handleEvent(new BtChokingEvent());
  155. CPPUNIT_ASSERT(msg->isInvalidate());
  156. CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size());
  157. MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get();
  158. CPPUNIT_ASSERT_EQUAL((int32_t)1, rej->index);
  159. CPPUNIT_ASSERT_EQUAL((int32_t)1024, rej->begin);
  160. CPPUNIT_ASSERT_EQUAL((int32_t)16*1024, rej->length);
  161. }
  162. void BtPieceMessageTest::testChokingEvent_inAmAllowedIndexSet() {
  163. peer->setFastExtensionEnabled(true);
  164. peer->addAmAllowedIndex(1);
  165. CPPUNIT_ASSERT(!msg->isInvalidate());
  166. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  167. CPPUNIT_ASSERT(peer->isInAmAllowedIndexSet(1));
  168. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  169. msg->handleEvent(new BtChokingEvent());
  170. CPPUNIT_ASSERT(!msg->isInvalidate());
  171. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  172. }
  173. void BtPieceMessageTest::testChokingEvent_invalidate() {
  174. msg->setInvalidate(true);
  175. CPPUNIT_ASSERT(msg->isInvalidate());
  176. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  177. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  178. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  179. msg->handleEvent(new BtChokingEvent());
  180. CPPUNIT_ASSERT(msg->isInvalidate());
  181. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  182. }
  183. void BtPieceMessageTest::testChokingEvent_sendingInProgress() {
  184. msg->setSendingInProgress(true);
  185. CPPUNIT_ASSERT(!msg->isInvalidate());
  186. CPPUNIT_ASSERT(msg->isSendingInProgress());
  187. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  188. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  189. msg->handleEvent(new BtChokingEvent());
  190. CPPUNIT_ASSERT(!msg->isInvalidate());
  191. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  192. }
  193. void BtPieceMessageTest::testCancelSendingPieceEvent() {
  194. CPPUNIT_ASSERT(!msg->isInvalidate());
  195. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  196. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  197. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  198. CPPUNIT_ASSERT(msg->isInvalidate());
  199. }
  200. void BtPieceMessageTest::testCancelSendingPieceEvent_noMatch() {
  201. CPPUNIT_ASSERT(!msg->isInvalidate());
  202. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  203. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  204. msg->handleEvent(new BtCancelSendingPieceEvent(0, 1024, 16*1024));
  205. CPPUNIT_ASSERT(!msg->isInvalidate());
  206. msg->handleEvent(new BtCancelSendingPieceEvent(1, 0, 16*1024));
  207. CPPUNIT_ASSERT(!msg->isInvalidate());
  208. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 0));
  209. CPPUNIT_ASSERT(!msg->isInvalidate());
  210. }
  211. void BtPieceMessageTest::testCancelSendingPieceEvent_allowedFastEnabled() {
  212. peer->setFastExtensionEnabled(true);
  213. CPPUNIT_ASSERT(!msg->isInvalidate());
  214. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  215. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  216. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  217. CPPUNIT_ASSERT(msg->isInvalidate());
  218. CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size());
  219. MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get();
  220. CPPUNIT_ASSERT_EQUAL((int32_t)1, rej->index);
  221. CPPUNIT_ASSERT_EQUAL((int32_t)1024, rej->begin);
  222. CPPUNIT_ASSERT_EQUAL((int32_t)16*1024, rej->length);
  223. }
  224. void BtPieceMessageTest::testCancelSendingPieceEvent_invalidate() {
  225. msg->setInvalidate(true);
  226. peer->setFastExtensionEnabled(true);
  227. CPPUNIT_ASSERT(msg->isInvalidate());
  228. CPPUNIT_ASSERT(!msg->isSendingInProgress());
  229. CPPUNIT_ASSERT(peer->isFastExtensionEnabled());
  230. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  231. CPPUNIT_ASSERT(msg->isInvalidate());
  232. CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->messageQueue.size());
  233. }
  234. void BtPieceMessageTest::testCancelSendingPieceEvent_sendingInProgress() {
  235. msg->setSendingInProgress(true);
  236. CPPUNIT_ASSERT(!msg->isInvalidate());
  237. CPPUNIT_ASSERT(msg->isSendingInProgress());
  238. CPPUNIT_ASSERT(!peer->isFastExtensionEnabled());
  239. msg->handleEvent(new BtCancelSendingPieceEvent(1, 1024, 16*1024));
  240. CPPUNIT_ASSERT(!msg->isInvalidate());
  241. }
  242. void BtPieceMessageTest::testToString() {
  243. CPPUNIT_ASSERT_EQUAL(std::string("piece index=1, begin=1024, length=16384"),
  244. msg->toString());
  245. }
  246. } // namespace aria2