BtAllowedFastMessageTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "BtAllowedFastMessage.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "bittorrent_helper.h"
  5. #include "util.h"
  6. #include "Peer.h"
  7. #include "SocketBuffer.h"
  8. namespace aria2 {
  9. class BtAllowedFastMessageTest : public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(BtAllowedFastMessageTest);
  11. CPPUNIT_TEST(testCreate);
  12. CPPUNIT_TEST(testCreateMessage);
  13. CPPUNIT_TEST(testDoReceivedAction);
  14. CPPUNIT_TEST(testOnSendComplete);
  15. CPPUNIT_TEST(testToString);
  16. CPPUNIT_TEST_SUITE_END();
  17. private:
  18. public:
  19. void setUp() {}
  20. void testCreate();
  21. void testCreateMessage();
  22. void testDoReceivedAction();
  23. void testOnSendComplete();
  24. void testToString();
  25. };
  26. CPPUNIT_TEST_SUITE_REGISTRATION(BtAllowedFastMessageTest);
  27. void BtAllowedFastMessageTest::testCreate()
  28. {
  29. unsigned char msg[9];
  30. bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 17);
  31. bittorrent::setIntParam(&msg[5], 12345);
  32. std::shared_ptr<BtAllowedFastMessage> pm(
  33. BtAllowedFastMessage::create(&msg[4], 5));
  34. CPPUNIT_ASSERT_EQUAL((uint8_t)17, pm->getId());
  35. CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
  36. // case: payload size is wrong
  37. try {
  38. unsigned char msg[10];
  39. bittorrent::createPeerMessageString(msg, sizeof(msg), 6, 17);
  40. BtAllowedFastMessage::create(&msg[4], 6);
  41. CPPUNIT_FAIL("exception must be thrown.");
  42. }
  43. catch (...) {
  44. }
  45. // case: id is wrong
  46. try {
  47. unsigned char msg[9];
  48. bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 18);
  49. BtAllowedFastMessage::create(&msg[4], 5);
  50. CPPUNIT_FAIL("exception must be thrown.");
  51. }
  52. catch (...) {
  53. }
  54. }
  55. void BtAllowedFastMessageTest::testCreateMessage()
  56. {
  57. BtAllowedFastMessage msg;
  58. msg.setIndex(12345);
  59. unsigned char data[9];
  60. bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
  61. bittorrent::setIntParam(&data[5], 12345);
  62. auto rawmsg = msg.createMessage();
  63. CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
  64. CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
  65. }
  66. void BtAllowedFastMessageTest::testDoReceivedAction()
  67. {
  68. BtAllowedFastMessage msg;
  69. msg.setIndex(1);
  70. std::shared_ptr<Peer> peer(new Peer("localhost", 6969));
  71. peer->allocateSessionResource(1_k, 1_m);
  72. peer->setFastExtensionEnabled(true);
  73. msg.setPeer(peer);
  74. CPPUNIT_ASSERT(!peer->isInPeerAllowedIndexSet(1));
  75. msg.doReceivedAction();
  76. CPPUNIT_ASSERT(peer->isInPeerAllowedIndexSet(1));
  77. peer->setFastExtensionEnabled(false);
  78. try {
  79. msg.doReceivedAction();
  80. CPPUNIT_FAIL("exception must be thrown.");
  81. }
  82. catch (...) {
  83. }
  84. }
  85. void BtAllowedFastMessageTest::testOnSendComplete()
  86. {
  87. BtAllowedFastMessage msg;
  88. msg.setIndex(1);
  89. std::shared_ptr<Peer> peer(new Peer("localhost", 6969));
  90. peer->allocateSessionResource(1_k, 1_m);
  91. peer->setFastExtensionEnabled(true);
  92. msg.setPeer(peer);
  93. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  94. std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
  95. pu->update(0, true);
  96. CPPUNIT_ASSERT(peer->isInAmAllowedIndexSet(1));
  97. }
  98. void BtAllowedFastMessageTest::testToString()
  99. {
  100. BtAllowedFastMessage msg;
  101. msg.setIndex(1);
  102. CPPUNIT_ASSERT_EQUAL(std::string("allowed fast index=1"), msg.toString());
  103. }
  104. } // namespace aria2