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. SharedHandle<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. uint32_t begin;
  29. size_t length;
  30. public:
  31. MockBtMessageDispatcher2():index(0),
  32. begin(0),
  33. length(0) {}
  34. virtual void doCancelSendingPieceAction(size_t index, uint32_t begin, size_t length) {
  35. this->index = index;
  36. this->begin = begin;
  37. this->length = length;
  38. }
  39. };
  40. };
  41. CPPUNIT_TEST_SUITE_REGISTRATION(BtCancelMessageTest);
  42. void BtCancelMessageTest::testCreate() {
  43. unsigned char msg[17];
  44. bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 8);
  45. bittorrent::setIntParam(&msg[5], 12345);
  46. bittorrent::setIntParam(&msg[9], 256);
  47. bittorrent::setIntParam(&msg[13], 1024);
  48. SharedHandle<BtCancelMessage> pm = BtCancelMessage::create(&msg[4], 13);
  49. CPPUNIT_ASSERT_EQUAL((uint8_t)8, pm->getId());
  50. CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
  51. CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
  52. CPPUNIT_ASSERT_EQUAL((size_t)1024, pm->getLength());
  53. // case: payload size is wrong
  54. try {
  55. unsigned char msg[18];
  56. bittorrent::createPeerMessageString(msg, sizeof(msg), 14, 8);
  57. BtCancelMessage::create(&msg[4], 14);
  58. CPPUNIT_FAIL("exception must be thrown.");
  59. } catch(...) {
  60. }
  61. // case: id is wrong
  62. try {
  63. unsigned char msg[17];
  64. bittorrent::createPeerMessageString(msg, sizeof(msg), 13, 9);
  65. BtCancelMessage::create(&msg[4], 13);
  66. CPPUNIT_FAIL("exception must be thrown.");
  67. } catch(...) {
  68. }
  69. }
  70. void BtCancelMessageTest::testCreateMessage() {
  71. BtCancelMessage msg;
  72. msg.setIndex(12345);
  73. msg.setBegin(256);
  74. msg.setLength(1024);
  75. unsigned char data[17];
  76. bittorrent::createPeerMessageString(data, sizeof(data), 13, 8);
  77. bittorrent::setIntParam(&data[5], 12345);
  78. bittorrent::setIntParam(&data[9], 256);
  79. bittorrent::setIntParam(&data[13], 1024);
  80. unsigned char* rawmsg = msg.createMessage();
  81. CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
  82. delete [] rawmsg;
  83. }
  84. void BtCancelMessageTest::testDoReceivedAction() {
  85. BtCancelMessage msg;
  86. msg.setIndex(1);
  87. msg.setBegin(2*16*1024);
  88. msg.setLength(16*1024);
  89. msg.setPeer(peer);
  90. SharedHandle<MockBtMessageDispatcher2> dispatcher
  91. (new 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