PeerSessionResourceTest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "PeerSessionResource.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "MockBtMessageDispatcher.h"
  4. #include "Exception.h"
  5. #include "util.h"
  6. namespace aria2 {
  7. class PeerSessionResourceTest : public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(PeerSessionResourceTest);
  9. CPPUNIT_TEST(testPeerAllowedIndexSetContains);
  10. CPPUNIT_TEST(testAmAllowedIndexSetContains);
  11. CPPUNIT_TEST(testHasAllPieces);
  12. CPPUNIT_TEST(testHasPiece);
  13. CPPUNIT_TEST(testUpdateUploadLength);
  14. CPPUNIT_TEST(testUpdateDownloadLength);
  15. CPPUNIT_TEST(testExtendedMessageEnabled);
  16. CPPUNIT_TEST(testGetExtensionMessageID);
  17. CPPUNIT_TEST(testFastExtensionEnabled);
  18. CPPUNIT_TEST(testSnubbing);
  19. CPPUNIT_TEST(testAmChoking);
  20. CPPUNIT_TEST(testAmInterested);
  21. CPPUNIT_TEST(testPeerChoking);
  22. CPPUNIT_TEST(testPeerInterested);
  23. CPPUNIT_TEST(testChokingRequired);
  24. CPPUNIT_TEST(testOptUnchoking);
  25. CPPUNIT_TEST(testShouldBeChoking);
  26. CPPUNIT_TEST(testCountOutstandingRequest);
  27. CPPUNIT_TEST_SUITE_END();
  28. public:
  29. void setUp() {}
  30. void tearDown() {}
  31. void testPeerAllowedIndexSetContains();
  32. void testAmAllowedIndexSetContains();
  33. void testHasAllPieces();
  34. void testHasPiece();
  35. void testUpdateUploadLength();
  36. void testUpdateDownloadLength();
  37. void testExtendedMessageEnabled();
  38. void testGetExtensionMessageID();
  39. void testFastExtensionEnabled();
  40. void testSnubbing();
  41. void testAmChoking();
  42. void testAmInterested();
  43. void testPeerChoking();
  44. void testPeerInterested();
  45. void testChokingRequired();
  46. void testOptUnchoking();
  47. void testShouldBeChoking();
  48. void testCountOutstandingRequest();
  49. };
  50. CPPUNIT_TEST_SUITE_REGISTRATION(PeerSessionResourceTest);
  51. void PeerSessionResourceTest::testPeerAllowedIndexSetContains()
  52. {
  53. PeerSessionResource res(1_k, 1_m);
  54. res.addPeerAllowedIndex(567);
  55. res.addPeerAllowedIndex(789);
  56. CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(567));
  57. CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(789));
  58. CPPUNIT_ASSERT(!res.peerAllowedIndexSetContains(123));
  59. }
  60. void PeerSessionResourceTest::testAmAllowedIndexSetContains()
  61. {
  62. PeerSessionResource res(1_k, 1_m);
  63. res.addAmAllowedIndex(567);
  64. res.addAmAllowedIndex(789);
  65. CPPUNIT_ASSERT(res.amAllowedIndexSetContains(567));
  66. CPPUNIT_ASSERT(res.amAllowedIndexSetContains(789));
  67. CPPUNIT_ASSERT(!res.amAllowedIndexSetContains(123));
  68. }
  69. void PeerSessionResourceTest::testHasAllPieces()
  70. {
  71. PeerSessionResource res(1_k, 1_m);
  72. CPPUNIT_ASSERT(!res.hasAllPieces());
  73. res.markSeeder();
  74. CPPUNIT_ASSERT(res.hasAllPieces());
  75. }
  76. void PeerSessionResourceTest::testHasPiece()
  77. {
  78. PeerSessionResource res(1_k, 1_m);
  79. CPPUNIT_ASSERT(!res.hasPiece(300));
  80. res.updateBitfield(300, 1);
  81. CPPUNIT_ASSERT(res.hasPiece(300));
  82. res.updateBitfield(300, 0);
  83. CPPUNIT_ASSERT(!res.hasPiece(300));
  84. }
  85. void PeerSessionResourceTest::testUpdateUploadLength()
  86. {
  87. PeerSessionResource res(1_k, 1_m);
  88. CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.uploadLength());
  89. res.updateUploadLength(100);
  90. res.updateUploadLength(200);
  91. CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.uploadLength());
  92. }
  93. void PeerSessionResourceTest::testUpdateDownloadLength()
  94. {
  95. PeerSessionResource res(1_k, 1_m);
  96. CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.downloadLength());
  97. res.updateDownload(100);
  98. res.updateDownload(200);
  99. CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.downloadLength());
  100. }
  101. void PeerSessionResourceTest::testExtendedMessageEnabled()
  102. {
  103. PeerSessionResource res(1_k, 1_m);
  104. CPPUNIT_ASSERT(!res.extendedMessagingEnabled());
  105. res.extendedMessagingEnabled(true);
  106. CPPUNIT_ASSERT(res.extendedMessagingEnabled());
  107. res.extendedMessagingEnabled(false);
  108. CPPUNIT_ASSERT(!res.extendedMessagingEnabled());
  109. }
  110. void PeerSessionResourceTest::testGetExtensionMessageID()
  111. {
  112. PeerSessionResource res(1_k, 1_m);
  113. res.addExtension(ExtensionMessageRegistry::UT_PEX, 9);
  114. CPPUNIT_ASSERT_EQUAL(
  115. (uint8_t)9, res.getExtensionMessageID(ExtensionMessageRegistry::UT_PEX));
  116. CPPUNIT_ASSERT_EQUAL((uint8_t)0, res.getExtensionMessageID(
  117. ExtensionMessageRegistry::UT_METADATA));
  118. CPPUNIT_ASSERT_EQUAL(std::string("ut_pex"),
  119. std::string(res.getExtensionName(9)));
  120. CPPUNIT_ASSERT(!res.getExtensionName(10));
  121. }
  122. void PeerSessionResourceTest::testFastExtensionEnabled()
  123. {
  124. PeerSessionResource res(1_k, 1_m);
  125. CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  126. res.fastExtensionEnabled(true);
  127. CPPUNIT_ASSERT(res.fastExtensionEnabled());
  128. res.fastExtensionEnabled(false);
  129. CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  130. }
  131. void PeerSessionResourceTest::testSnubbing()
  132. {
  133. PeerSessionResource res(1_k, 1_m);
  134. CPPUNIT_ASSERT(!res.snubbing());
  135. res.snubbing(true);
  136. CPPUNIT_ASSERT(res.snubbing());
  137. res.snubbing(false);
  138. CPPUNIT_ASSERT(!res.snubbing());
  139. }
  140. void PeerSessionResourceTest::testAmChoking()
  141. {
  142. PeerSessionResource res(1_k, 1_m);
  143. CPPUNIT_ASSERT(res.amChoking());
  144. res.amChoking(false);
  145. CPPUNIT_ASSERT(!res.amChoking());
  146. res.amChoking(true);
  147. CPPUNIT_ASSERT(res.amChoking());
  148. }
  149. void PeerSessionResourceTest::testAmInterested()
  150. {
  151. PeerSessionResource res(1_k, 1_m);
  152. CPPUNIT_ASSERT(!res.amInterested());
  153. res.amInterested(true);
  154. CPPUNIT_ASSERT(res.amInterested());
  155. res.amInterested(false);
  156. CPPUNIT_ASSERT(!res.amInterested());
  157. }
  158. void PeerSessionResourceTest::testPeerChoking()
  159. {
  160. PeerSessionResource res(1_k, 1_m);
  161. CPPUNIT_ASSERT(res.peerChoking());
  162. res.peerChoking(false);
  163. CPPUNIT_ASSERT(!res.peerChoking());
  164. res.peerChoking(true);
  165. CPPUNIT_ASSERT(res.peerChoking());
  166. }
  167. void PeerSessionResourceTest::testPeerInterested()
  168. {
  169. PeerSessionResource res(1_k, 1_m);
  170. CPPUNIT_ASSERT(!res.peerInterested());
  171. res.peerInterested(true);
  172. CPPUNIT_ASSERT(res.peerInterested());
  173. res.peerInterested(false);
  174. CPPUNIT_ASSERT(!res.peerInterested());
  175. }
  176. void PeerSessionResourceTest::testChokingRequired()
  177. {
  178. PeerSessionResource res(1_k, 1_m);
  179. CPPUNIT_ASSERT(res.chokingRequired());
  180. res.chokingRequired(false);
  181. CPPUNIT_ASSERT(!res.chokingRequired());
  182. res.chokingRequired(true);
  183. CPPUNIT_ASSERT(res.chokingRequired());
  184. }
  185. void PeerSessionResourceTest::testOptUnchoking()
  186. {
  187. PeerSessionResource res(1_k, 1_m);
  188. CPPUNIT_ASSERT(!res.optUnchoking());
  189. res.optUnchoking(true);
  190. CPPUNIT_ASSERT(res.optUnchoking());
  191. res.optUnchoking(false);
  192. CPPUNIT_ASSERT(!res.optUnchoking());
  193. }
  194. void PeerSessionResourceTest::testShouldBeChoking()
  195. {
  196. PeerSessionResource res(1_k, 1_m);
  197. CPPUNIT_ASSERT(res.shouldBeChoking());
  198. res.chokingRequired(false);
  199. CPPUNIT_ASSERT(!res.shouldBeChoking());
  200. res.chokingRequired(true);
  201. res.optUnchoking(true);
  202. CPPUNIT_ASSERT(!res.shouldBeChoking());
  203. }
  204. void PeerSessionResourceTest::testCountOutstandingRequest()
  205. {
  206. PeerSessionResource res(1_k, 1_m);
  207. std::shared_ptr<MockBtMessageDispatcher> dispatcher(
  208. new MockBtMessageDispatcher());
  209. res.setBtMessageDispatcher(dispatcher.get());
  210. CPPUNIT_ASSERT_EQUAL((size_t)0, res.countOutstandingUpload());
  211. }
  212. } // namespace aria2