BtPieceMessageTest.cc 9.7 KB

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