PeerSessionResourceTest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "PeerSessionResource.h"
  2. #include "Exception.h"
  3. #include "Util.h"
  4. #include <cppunit/extensions/HelperMacros.h>
  5. namespace aria2 {
  6. class PeerSessionResourceTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(PeerSessionResourceTest);
  8. CPPUNIT_TEST(testPeerAllowedIndexSetContains);
  9. CPPUNIT_TEST(testAmAllowedIndexSetContains);
  10. CPPUNIT_TEST(testHasAllPieces);
  11. CPPUNIT_TEST(testHasPiece);
  12. CPPUNIT_TEST(testUpdateUploadLength);
  13. CPPUNIT_TEST(testUpdateDownloadLength);
  14. CPPUNIT_TEST(testUpdateLatency);
  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_SUITE_END();
  27. public:
  28. void setUp() {}
  29. void tearDown() {}
  30. void testPeerAllowedIndexSetContains();
  31. void testAmAllowedIndexSetContains();
  32. void testHasAllPieces();
  33. void testHasPiece();
  34. void testUpdateUploadLength();
  35. void testUpdateDownloadLength();
  36. void testUpdateLatency();
  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. };
  49. CPPUNIT_TEST_SUITE_REGISTRATION(PeerSessionResourceTest);
  50. void PeerSessionResourceTest::testPeerAllowedIndexSetContains()
  51. {
  52. PeerSessionResource res(1024, 1024*1024);
  53. res.addPeerAllowedIndex(567);
  54. res.addPeerAllowedIndex(789);
  55. CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(567));
  56. CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(789));
  57. CPPUNIT_ASSERT(!res.peerAllowedIndexSetContains(123));
  58. }
  59. void PeerSessionResourceTest::testAmAllowedIndexSetContains()
  60. {
  61. PeerSessionResource res(1024, 1024*1024);
  62. res.addAmAllowedIndex(567);
  63. res.addAmAllowedIndex(789);
  64. CPPUNIT_ASSERT(res.amAllowedIndexSetContains(567));
  65. CPPUNIT_ASSERT(res.amAllowedIndexSetContains(789));
  66. CPPUNIT_ASSERT(!res.amAllowedIndexSetContains(123));
  67. }
  68. void PeerSessionResourceTest::testHasAllPieces()
  69. {
  70. PeerSessionResource res(1024, 1024*1024);
  71. CPPUNIT_ASSERT(!res.hasAllPieces());
  72. res.markSeeder();
  73. CPPUNIT_ASSERT(res.hasAllPieces());
  74. }
  75. void PeerSessionResourceTest::testHasPiece()
  76. {
  77. PeerSessionResource res(1024, 1024*1024);
  78. CPPUNIT_ASSERT(!res.hasPiece(300));
  79. res.updateBitfield(300, 1);
  80. CPPUNIT_ASSERT(res.hasPiece(300));
  81. res.updateBitfield(300, 0);
  82. CPPUNIT_ASSERT(!res.hasPiece(300));
  83. }
  84. void PeerSessionResourceTest::testUpdateUploadLength()
  85. {
  86. PeerSessionResource res(1024, 1024*1024);
  87. CPPUNIT_ASSERT_EQUAL(0ULL, res.uploadLength());
  88. res.updateUploadLength(100);
  89. res.updateUploadLength(200);
  90. CPPUNIT_ASSERT_EQUAL(300ULL, res.uploadLength());
  91. }
  92. void PeerSessionResourceTest::testUpdateDownloadLength()
  93. {
  94. PeerSessionResource res(1024, 1024*1024);
  95. CPPUNIT_ASSERT_EQUAL(0ULL, res.downloadLength());
  96. res.updateDownloadLength(100);
  97. res.updateDownloadLength(200);
  98. CPPUNIT_ASSERT_EQUAL(300ULL, res.downloadLength());
  99. }
  100. void PeerSessionResourceTest::testUpdateLatency()
  101. {
  102. PeerSessionResource res(1024, 1024*1024);
  103. CPPUNIT_ASSERT_EQUAL((unsigned int)1500, res.latency());
  104. res.updateLatency(1000);
  105. CPPUNIT_ASSERT_EQUAL((unsigned int)1100, res.latency());
  106. }
  107. void PeerSessionResourceTest::testExtendedMessageEnabled()
  108. {
  109. PeerSessionResource res(1024, 1024*1024);
  110. CPPUNIT_ASSERT(!res.extendedMessagingEnabled());
  111. res.extendedMessagingEnabled(true);
  112. CPPUNIT_ASSERT(res.extendedMessagingEnabled());
  113. res.extendedMessagingEnabled(false);
  114. CPPUNIT_ASSERT(!res.extendedMessagingEnabled());
  115. }
  116. void PeerSessionResourceTest::testGetExtensionMessageID()
  117. {
  118. PeerSessionResource res(1024, 1024*1024);
  119. res.addExtension("a2", 9);
  120. CPPUNIT_ASSERT_EQUAL((uint8_t)9, res.getExtensionMessageID("a2"));
  121. CPPUNIT_ASSERT_EQUAL((uint8_t)0, res.getExtensionMessageID("non"));
  122. CPPUNIT_ASSERT_EQUAL(std::string("a2"), res.getExtensionName(9));
  123. CPPUNIT_ASSERT_EQUAL(std::string(""), res.getExtensionName(10));
  124. }
  125. void PeerSessionResourceTest::testFastExtensionEnabled()
  126. {
  127. PeerSessionResource res(1024, 1024*1024);
  128. CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  129. res.fastExtensionEnabled(true);
  130. CPPUNIT_ASSERT(res.fastExtensionEnabled());
  131. res.fastExtensionEnabled(false);
  132. CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  133. }
  134. void PeerSessionResourceTest::testSnubbing()
  135. {
  136. PeerSessionResource res(1024, 1024*1024);
  137. CPPUNIT_ASSERT(!res.snubbing());
  138. res.snubbing(true);
  139. CPPUNIT_ASSERT(res.snubbing());
  140. res.snubbing(false);
  141. CPPUNIT_ASSERT(!res.snubbing());
  142. }
  143. void PeerSessionResourceTest::testAmChoking()
  144. {
  145. PeerSessionResource res(1024, 1024*1024);
  146. CPPUNIT_ASSERT(res.amChoking());
  147. res.amChoking(false);
  148. CPPUNIT_ASSERT(!res.amChoking());
  149. res.amChoking(true);
  150. CPPUNIT_ASSERT(res.amChoking());
  151. }
  152. void PeerSessionResourceTest::testAmInterested()
  153. {
  154. PeerSessionResource res(1024, 1024*1024);
  155. CPPUNIT_ASSERT(!res.amInterested());
  156. res.amInterested(true);
  157. CPPUNIT_ASSERT(res.amInterested());
  158. res.amInterested(false);
  159. CPPUNIT_ASSERT(!res.amInterested());
  160. }
  161. void PeerSessionResourceTest::testPeerChoking()
  162. {
  163. PeerSessionResource res(1024, 1024*1024);
  164. CPPUNIT_ASSERT(res.peerChoking());
  165. res.peerChoking(false);
  166. CPPUNIT_ASSERT(!res.peerChoking());
  167. res.peerChoking(true);
  168. CPPUNIT_ASSERT(res.peerChoking());
  169. }
  170. void PeerSessionResourceTest::testPeerInterested()
  171. {
  172. PeerSessionResource res(1024, 1024*1024);
  173. CPPUNIT_ASSERT(!res.peerInterested());
  174. res.peerInterested(true);
  175. CPPUNIT_ASSERT(res.peerInterested());
  176. res.peerInterested(false);
  177. CPPUNIT_ASSERT(!res.peerInterested());
  178. }
  179. void PeerSessionResourceTest::testChokingRequired()
  180. {
  181. PeerSessionResource res(1024, 1024*1024);
  182. CPPUNIT_ASSERT(res.chokingRequired());
  183. res.chokingRequired(false);
  184. CPPUNIT_ASSERT(!res.chokingRequired());
  185. res.chokingRequired(true);
  186. CPPUNIT_ASSERT(res.chokingRequired());
  187. }
  188. void PeerSessionResourceTest::testOptUnchoking()
  189. {
  190. PeerSessionResource res(1024, 1024*1024);
  191. CPPUNIT_ASSERT(!res.optUnchoking());
  192. res.optUnchoking(true);
  193. CPPUNIT_ASSERT(res.optUnchoking());
  194. res.optUnchoking(false);
  195. CPPUNIT_ASSERT(!res.optUnchoking());
  196. }
  197. void PeerSessionResourceTest::testShouldBeChoking()
  198. {
  199. PeerSessionResource res(1024, 1024*1024);
  200. CPPUNIT_ASSERT(res.shouldBeChoking());
  201. res.chokingRequired(false);
  202. CPPUNIT_ASSERT(!res.shouldBeChoking());
  203. res.chokingRequired(true);
  204. res.optUnchoking(true);
  205. CPPUNIT_ASSERT(!res.shouldBeChoking());
  206. }
  207. } // namespace aria2