BtNotInterestedMessageTest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "BtNotInterestedMessage.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 BtNotInterestedMessageTest : public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(BtNotInterestedMessageTest);
  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(BtNotInterestedMessageTest);
  25. void BtNotInterestedMessageTest::testCreate()
  26. {
  27. unsigned char msg[5];
  28. bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 3);
  29. std::shared_ptr<BtNotInterestedMessage> pm(
  30. BtNotInterestedMessage::create(&msg[4], 1));
  31. CPPUNIT_ASSERT_EQUAL((uint8_t)3, pm->getId());
  32. // case: payload size is wrong
  33. try {
  34. unsigned char msg[6];
  35. bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 3);
  36. BtNotInterestedMessage::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, 4);
  45. BtNotInterestedMessage::create(&msg[4], 1);
  46. CPPUNIT_FAIL("exception must be thrown.");
  47. }
  48. catch (...) {
  49. }
  50. }
  51. void BtNotInterestedMessageTest::testCreateMessage()
  52. {
  53. BtNotInterestedMessage msg;
  54. unsigned char data[5];
  55. bittorrent::createPeerMessageString(data, sizeof(data), 1, 3);
  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 BtNotInterestedMessageTest::testDoReceivedAction()
  61. {
  62. std::shared_ptr<Peer> peer(new Peer("host", 6969));
  63. peer->allocateSessionResource(1_k, 1_m);
  64. peer->peerInterested(true);
  65. auto peerStorage = make_unique<MockPeerStorage>();
  66. BtNotInterestedMessage msg;
  67. msg.setPeer(peer);
  68. msg.setPeerStorage(peerStorage.get());
  69. CPPUNIT_ASSERT(peer->peerInterested());
  70. msg.doReceivedAction();
  71. CPPUNIT_ASSERT(!peer->peerInterested());
  72. CPPUNIT_ASSERT_EQUAL(0, peerStorage->getNumChokeExecuted());
  73. peer->amChoking(false);
  74. msg.doReceivedAction();
  75. CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
  76. }
  77. void BtNotInterestedMessageTest::testOnSendComplete()
  78. {
  79. std::shared_ptr<Peer> peer(new Peer("host", 6969));
  80. peer->allocateSessionResource(1_k, 1_m);
  81. peer->amInterested(true);
  82. BtNotInterestedMessage msg;
  83. msg.setPeer(peer);
  84. CPPUNIT_ASSERT(peer->amInterested());
  85. std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
  86. pu->update(0, true);
  87. CPPUNIT_ASSERT(!peer->amInterested());
  88. }
  89. void BtNotInterestedMessageTest::testToString()
  90. {
  91. BtNotInterestedMessage msg;
  92. CPPUNIT_ASSERT_EQUAL(std::string("not interested"), msg.toString());
  93. }
  94. } // namespace aria2