BtPieceMessageTest.cc 9.6 KB

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