PeerSessionResourceTest.cc 7.1 KB

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