PeerSessionResourceTest.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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(1024, 1024*1024);
  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(1024, 1024*1024);
  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(1024, 1024*1024);
  72. CPPUNIT_ASSERT(!res.hasAllPieces());
  73. res.markSeeder();
  74. CPPUNIT_ASSERT(res.hasAllPieces());
  75. }
  76. void PeerSessionResourceTest::testHasPiece()
  77. {
  78. PeerSessionResource res(1024, 1024*1024);
  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(1024, 1024*1024);
  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(1024, 1024*1024);
  96. CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.downloadLength());
  97. res.updateDownloadLength(100);
  98. res.updateDownloadLength(200);
  99. CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.downloadLength());
  100. }
  101. void PeerSessionResourceTest::testExtendedMessageEnabled()
  102. {
  103. PeerSessionResource res(1024, 1024*1024);
  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(1024, 1024*1024);
  113. res.addExtension(ExtensionMessageRegistry::UT_PEX, 9);
  114. CPPUNIT_ASSERT_EQUAL((uint8_t)9,
  115. res.getExtensionMessageID
  116. (ExtensionMessageRegistry::UT_PEX));
  117. CPPUNIT_ASSERT_EQUAL((uint8_t)0,
  118. res.getExtensionMessageID
  119. (ExtensionMessageRegistry::UT_METADATA));
  120. CPPUNIT_ASSERT_EQUAL(std::string("ut_pex"),
  121. std::string(res.getExtensionName(9)));
  122. CPPUNIT_ASSERT(!res.getExtensionName(10));
  123. }
  124. void PeerSessionResourceTest::testFastExtensionEnabled()
  125. {
  126. PeerSessionResource res(1024, 1024*1024);
  127. CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  128. res.fastExtensionEnabled(true);
  129. CPPUNIT_ASSERT(res.fastExtensionEnabled());
  130. res.fastExtensionEnabled(false);
  131. CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  132. }
  133. void PeerSessionResourceTest::testSnubbing()
  134. {
  135. PeerSessionResource res(1024, 1024*1024);
  136. CPPUNIT_ASSERT(!res.snubbing());
  137. res.snubbing(true);
  138. CPPUNIT_ASSERT(res.snubbing());
  139. res.snubbing(false);
  140. CPPUNIT_ASSERT(!res.snubbing());
  141. }
  142. void PeerSessionResourceTest::testAmChoking()
  143. {
  144. PeerSessionResource res(1024, 1024*1024);
  145. CPPUNIT_ASSERT(res.amChoking());
  146. res.amChoking(false);
  147. CPPUNIT_ASSERT(!res.amChoking());
  148. res.amChoking(true);
  149. CPPUNIT_ASSERT(res.amChoking());
  150. }
  151. void PeerSessionResourceTest::testAmInterested()
  152. {
  153. PeerSessionResource res(1024, 1024*1024);
  154. CPPUNIT_ASSERT(!res.amInterested());
  155. res.amInterested(true);
  156. CPPUNIT_ASSERT(res.amInterested());
  157. res.amInterested(false);
  158. CPPUNIT_ASSERT(!res.amInterested());
  159. }
  160. void PeerSessionResourceTest::testPeerChoking()
  161. {
  162. PeerSessionResource res(1024, 1024*1024);
  163. CPPUNIT_ASSERT(res.peerChoking());
  164. res.peerChoking(false);
  165. CPPUNIT_ASSERT(!res.peerChoking());
  166. res.peerChoking(true);
  167. CPPUNIT_ASSERT(res.peerChoking());
  168. }
  169. void PeerSessionResourceTest::testPeerInterested()
  170. {
  171. PeerSessionResource res(1024, 1024*1024);
  172. CPPUNIT_ASSERT(!res.peerInterested());
  173. res.peerInterested(true);
  174. CPPUNIT_ASSERT(res.peerInterested());
  175. res.peerInterested(false);
  176. CPPUNIT_ASSERT(!res.peerInterested());
  177. }
  178. void PeerSessionResourceTest::testChokingRequired()
  179. {
  180. PeerSessionResource res(1024, 1024*1024);
  181. CPPUNIT_ASSERT(res.chokingRequired());
  182. res.chokingRequired(false);
  183. CPPUNIT_ASSERT(!res.chokingRequired());
  184. res.chokingRequired(true);
  185. CPPUNIT_ASSERT(res.chokingRequired());
  186. }
  187. void PeerSessionResourceTest::testOptUnchoking()
  188. {
  189. PeerSessionResource res(1024, 1024*1024);
  190. CPPUNIT_ASSERT(!res.optUnchoking());
  191. res.optUnchoking(true);
  192. CPPUNIT_ASSERT(res.optUnchoking());
  193. res.optUnchoking(false);
  194. CPPUNIT_ASSERT(!res.optUnchoking());
  195. }
  196. void PeerSessionResourceTest::testShouldBeChoking()
  197. {
  198. PeerSessionResource res(1024, 1024*1024);
  199. CPPUNIT_ASSERT(res.shouldBeChoking());
  200. res.chokingRequired(false);
  201. CPPUNIT_ASSERT(!res.shouldBeChoking());
  202. res.chokingRequired(true);
  203. res.optUnchoking(true);
  204. CPPUNIT_ASSERT(!res.shouldBeChoking());
  205. }
  206. void PeerSessionResourceTest::testCountOutstandingRequest()
  207. {
  208. PeerSessionResource res(1024, 1024*1024);
  209. std::shared_ptr<MockBtMessageDispatcher> dispatcher
  210. (new MockBtMessageDispatcher());
  211. res.setBtMessageDispatcher(dispatcher.get());
  212. CPPUNIT_ASSERT_EQUAL((size_t)0, res.countOutstandingUpload());
  213. }
  214. } // namespace aria2