BtAllowedFastMessageTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  21. void testCreate();
  22. void testCreateMessage();
  23. void testDoReceivedAction();
  24. void testOnSendComplete();
  25. void testToString();
  26. };
  27. CPPUNIT_TEST_SUITE_REGISTRATION(BtAllowedFastMessageTest);
  28. void BtAllowedFastMessageTest::testCreate() {
  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. } catch(...) {
  43. }
  44. // case: id is wrong
  45. try {
  46. unsigned char msg[9];
  47. bittorrent::createPeerMessageString(msg, sizeof(msg), 5, 18);
  48. BtAllowedFastMessage::create(&msg[4], 5);
  49. CPPUNIT_FAIL("exception must be thrown.");
  50. } catch(...) {
  51. }
  52. }
  53. void BtAllowedFastMessageTest::testCreateMessage() {
  54. BtAllowedFastMessage msg;
  55. msg.setIndex(12345);
  56. unsigned char data[9];
  57. bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
  58. bittorrent::setIntParam(&data[5], 12345);
  59. unsigned char* rawmsg = msg.createMessage();
  60. CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
  61. delete [] rawmsg;
  62. }
  63. void BtAllowedFastMessageTest::testDoReceivedAction() {
  64. BtAllowedFastMessage msg;
  65. msg.setIndex(1);
  66. std::shared_ptr<Peer> peer(new Peer("localhost", 6969));
  67. peer->allocateSessionResource(1024, 1024*1024);
  68. peer->setFastExtensionEnabled(true);
  69. msg.setPeer(peer);
  70. CPPUNIT_ASSERT(!peer->isInPeerAllowedIndexSet(1));
  71. msg.doReceivedAction();
  72. CPPUNIT_ASSERT(peer->isInPeerAllowedIndexSet(1));
  73. peer->setFastExtensionEnabled(false);
  74. try {
  75. msg.doReceivedAction();
  76. CPPUNIT_FAIL("exception must be thrown.");
  77. } catch(...) {}
  78. }
  79. void BtAllowedFastMessageTest::testOnSendComplete() {
  80. BtAllowedFastMessage msg;
  81. msg.setIndex(1);
  82. std::shared_ptr<Peer> peer(new Peer("localhost", 6969));
  83. peer->allocateSessionResource(1024, 1024*1024);
  84. peer->setFastExtensionEnabled(true);
  85. msg.setPeer(peer);
  86. CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1));
  87. std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
  88. pu->update(0, true);
  89. CPPUNIT_ASSERT(peer->isInAmAllowedIndexSet(1));
  90. }
  91. void BtAllowedFastMessageTest::testToString() {
  92. BtAllowedFastMessage msg;
  93. msg.setIndex(1);
  94. CPPUNIT_ASSERT_EQUAL(std::string("allowed fast index=1"), msg.toString());
  95. }
  96. } // namespace aria2