BtNotInterestedMessageTest.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(testToString);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void testCreate();
  18. void testCreateMessage();
  19. void testDoReceivedAction();
  20. void testToString();
  21. };
  22. CPPUNIT_TEST_SUITE_REGISTRATION(BtNotInterestedMessageTest);
  23. void BtNotInterestedMessageTest::testCreate()
  24. {
  25. unsigned char msg[5];
  26. bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 3);
  27. std::shared_ptr<BtNotInterestedMessage> pm(
  28. BtNotInterestedMessage::create(&msg[4], 1));
  29. CPPUNIT_ASSERT_EQUAL((uint8_t)3, pm->getId());
  30. // case: payload size is wrong
  31. try {
  32. unsigned char msg[6];
  33. bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 3);
  34. BtNotInterestedMessage::create(&msg[4], 2);
  35. CPPUNIT_FAIL("exception must be thrown.");
  36. }
  37. catch (...) {
  38. }
  39. // case: id is wrong
  40. try {
  41. unsigned char msg[5];
  42. bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 4);
  43. BtNotInterestedMessage::create(&msg[4], 1);
  44. CPPUNIT_FAIL("exception must be thrown.");
  45. }
  46. catch (...) {
  47. }
  48. }
  49. void BtNotInterestedMessageTest::testCreateMessage()
  50. {
  51. BtNotInterestedMessage msg;
  52. unsigned char data[5];
  53. bittorrent::createPeerMessageString(data, sizeof(data), 1, 3);
  54. auto rawmsg = msg.createMessage();
  55. CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
  56. CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
  57. }
  58. void BtNotInterestedMessageTest::testDoReceivedAction()
  59. {
  60. std::shared_ptr<Peer> peer(new Peer("host", 6969));
  61. peer->allocateSessionResource(1_k, 1_m);
  62. peer->peerInterested(true);
  63. auto peerStorage = make_unique<MockPeerStorage>();
  64. BtNotInterestedMessage msg;
  65. msg.setPeer(peer);
  66. msg.setPeerStorage(peerStorage.get());
  67. CPPUNIT_ASSERT(peer->peerInterested());
  68. msg.doReceivedAction();
  69. CPPUNIT_ASSERT(!peer->peerInterested());
  70. CPPUNIT_ASSERT_EQUAL(0, peerStorage->getNumChokeExecuted());
  71. peer->amChoking(false);
  72. msg.doReceivedAction();
  73. CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
  74. }
  75. void BtNotInterestedMessageTest::testToString()
  76. {
  77. BtNotInterestedMessage msg;
  78. CPPUNIT_ASSERT_EQUAL(std::string("not interested"), msg.toString());
  79. }
  80. } // namespace aria2