PeerSessionResourceTest.cc 6.8 KB

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