BtCancelMessageTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "BtCancelMessage.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "bittorrent_helper.h"
  5. #include "MockBtMessageDispatcher.h"
  6. #include "Peer.h"
  7. #include "FileEntry.h"
  8. #include "Piece.h"
  9. namespace aria2 {
  10. class BtCancelMessageTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(BtCancelMessageTest);
  12. CPPUNIT_TEST(testCreate);
  13. CPPUNIT_TEST(testCreateMessage);
  14. CPPUNIT_TEST(testDoReceivedAction);
  15. CPPUNIT_TEST_SUITE_END();
  16. private:
  17. std::shared_ptr<Peer> peer;
  18. public:
  19. void setUp() {
  20. peer.reset(new Peer("host", 6969));
  21. }
  22. void testCreate();
  23. void testCreateMessage();
  24. void testDoReceivedAction();
  25. class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
  26. public:
  27. size_t index;
  28. int32_t begin;
  29. int32_t length;
  30. public:
  31. MockBtMessageDispatcher2():index(0),
  32. begin(0),
  33. length(0) {}
  34. virtual void doCancelSendingPieceAction
  35. (size_t index, int32_t begin, int32_t length) CXX11_OVERRIDE {
  36. this->index = index;
  37. this->begin = begin;
  38. this->length = length;
  39. }
  40. };
  41. };
  42. CPPUNIT_TEST_SUITE_REGISTRATION(BtCancelMessageTest);
  43. void BtCancelMessageTest::testCreate() {
  44. unsigned char msg[17];
  45. bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 8);
  46. bittorrent::setIntParam(&msg[5], 12345);
  47. bittorrent::setIntParam(&msg[9], 256);
  48. bittorrent::setIntParam(&msg[13], 1024);
  49. auto pm = BtCancelMessage::create(&msg[4], 13);
  50. CPPUNIT_ASSERT_EQUAL((uint8_t)8, pm->getId());
  51. CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
  52. CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
  53. CPPUNIT_ASSERT_EQUAL(1024, pm->getLength());
  54. // case: payload size is wrong
  55. try {
  56. unsigned char msg[18];
  57. bittorrent::createPeerMessageString(msg, sizeof(msg), 14, 8);
  58. BtCancelMessage::create(&msg[4], 14);
  59. CPPUNIT_FAIL("exception must be thrown.");
  60. } catch(...) {
  61. }
  62. // case: id is wrong
  63. try {
  64. unsigned char msg[17];
  65. bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 9);
  66. BtCancelMessage::create(&msg[4], 13);
  67. CPPUNIT_FAIL("exception must be thrown.");
  68. } catch(...) {
  69. }
  70. }
  71. void BtCancelMessageTest::testCreateMessage() {
  72. BtCancelMessage msg;
  73. msg.setIndex(12345);
  74. msg.setBegin(256);
  75. msg.setLength(1024);
  76. unsigned char data[17];
  77. bittorrent::createPeerMessageString(data, sizeof(data), 13, 8);
  78. bittorrent::setIntParam(&data[5], 12345);
  79. bittorrent::setIntParam(&data[9], 256);
  80. bittorrent::setIntParam(&data[13], 1024);
  81. unsigned char* rawmsg = msg.createMessage();
  82. CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
  83. delete [] rawmsg;
  84. }
  85. void BtCancelMessageTest::testDoReceivedAction() {
  86. BtCancelMessage msg;
  87. msg.setIndex(1);
  88. msg.setBegin(2*16*1024);
  89. msg.setLength(16*1024);
  90. msg.setPeer(peer);
  91. auto dispatcher = make_unique<MockBtMessageDispatcher2>();
  92. msg.setBtMessageDispatcher(dispatcher.get());
  93. msg.doReceivedAction();
  94. CPPUNIT_ASSERT_EQUAL(msg.getIndex(), dispatcher->index);
  95. CPPUNIT_ASSERT_EQUAL(msg.getBegin(), dispatcher->begin);
  96. CPPUNIT_ASSERT_EQUAL(msg.getLength(), dispatcher->length);
  97. }
  98. } // namespace aria2