PeerSessionResourceTest.cc 6.8 KB

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