BtInterestedMessageTest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "BtInterestedMessage.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "bittorrent_helper.h"
  5. #include "Peer.h"
  6. #include "MockPeerStorage.h"
  7. #include "SocketBuffer.h"
  8. namespace aria2 {
  9. class BtInterestedMessageTest : public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(BtInterestedMessageTest);
  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. public:
  18. void testCreate();
  19. void testCreateMessage();
  20. void testDoReceivedAction();
  21. void testOnSendComplete();
  22. void testToString();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION(BtInterestedMessageTest);
  25. void BtInterestedMessageTest::testCreate()
  26. {
  27. unsigned char msg[5];
  28. bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
  29. std::shared_ptr<BtInterestedMessage> pm(
  30. BtInterestedMessage::create(&msg[4], 1));
  31. CPPUNIT_ASSERT_EQUAL((uint8_t)2, pm->getId());
  32. // case: payload size is wrong
  33. try {
  34. unsigned char msg[6];
  35. bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 2);
  36. BtInterestedMessage::create(&msg[4], 2);
  37. CPPUNIT_FAIL("exception must be thrown.");
  38. }
  39. catch (...) {
  40. }
  41. // case: id is wrong
  42. try {
  43. unsigned char msg[5];
  44. bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 3);
  45. BtInterestedMessage::create(&msg[4], 1);
  46. CPPUNIT_FAIL("exception must be thrown.");
  47. }
  48. catch (...) {
  49. }
  50. }
  51. void BtInterestedMessageTest::testCreateMessage()
  52. {
  53. BtInterestedMessage msg;
  54. unsigned char data[5];
  55. bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
  56. auto rawmsg = msg.createMessage();
  57. CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
  58. CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
  59. }
  60. void BtInterestedMessageTest::testDoReceivedAction()
  61. {
  62. BtInterestedMessage msg;
  63. std::shared_ptr<Peer> peer(new Peer("host", 6969));
  64. peer->allocateSessionResource(1_k, 1_m);
  65. msg.setPeer(peer);
  66. auto peerStorage = make_unique<MockPeerStorage>();
  67. msg.setPeerStorage(peerStorage.get());
  68. CPPUNIT_ASSERT(!peer->peerInterested());
  69. msg.doReceivedAction();
  70. CPPUNIT_ASSERT(peer->peerInterested());
  71. CPPUNIT_ASSERT_EQUAL(0, peerStorage->getNumChokeExecuted());
  72. peer->amChoking(false);
  73. msg.doReceivedAction();
  74. CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
  75. }
  76. void BtInterestedMessageTest::testOnSendComplete()
  77. {
  78. BtInterestedMessage msg;
  79. std::shared_ptr<Peer> peer(new Peer("host", 6969));
  80. peer->allocateSessionResource(1_k, 1_m);
  81. msg.setPeer(peer);
  82. CPPUNIT_ASSERT(!peer->amInterested());
  83. std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
  84. pu->update(0, true);
  85. CPPUNIT_ASSERT(peer->amInterested());
  86. }
  87. void BtInterestedMessageTest::testToString()
  88. {
  89. BtInterestedMessage msg;
  90. CPPUNIT_ASSERT_EQUAL(std::string("interested"), msg.toString());
  91. }
  92. } // namespace aria2