BtInterestedMessageTest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(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(BtInterestedMessageTest);
  23. void BtInterestedMessageTest::testCreate()
  24. {
  25. unsigned char msg[5];
  26. bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
  27. std::shared_ptr<BtInterestedMessage> pm(
  28. BtInterestedMessage::create(&msg[4], 1));
  29. CPPUNIT_ASSERT_EQUAL((uint8_t)2, pm->getId());
  30. // case: payload size is wrong
  31. try {
  32. unsigned char msg[6];
  33. bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 2);
  34. BtInterestedMessage::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, 3);
  43. BtInterestedMessage::create(&msg[4], 1);
  44. CPPUNIT_FAIL("exception must be thrown.");
  45. }
  46. catch (...) {
  47. }
  48. }
  49. void BtInterestedMessageTest::testCreateMessage()
  50. {
  51. BtInterestedMessage msg;
  52. unsigned char data[5];
  53. bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
  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 BtInterestedMessageTest::testDoReceivedAction()
  59. {
  60. BtInterestedMessage msg;
  61. std::shared_ptr<Peer> peer(new Peer("host", 6969));
  62. peer->allocateSessionResource(1_k, 1_m);
  63. msg.setPeer(peer);
  64. auto peerStorage = make_unique<MockPeerStorage>();
  65. msg.setPeerStorage(peerStorage.get());
  66. CPPUNIT_ASSERT(!peer->peerInterested());
  67. msg.doReceivedAction();
  68. CPPUNIT_ASSERT(peer->peerInterested());
  69. CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
  70. peer->amChoking(false);
  71. msg.doReceivedAction();
  72. CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
  73. }
  74. void BtInterestedMessageTest::testToString()
  75. {
  76. BtInterestedMessage msg;
  77. CPPUNIT_ASSERT_EQUAL(std::string("interested"), msg.toString());
  78. }
  79. } // namespace aria2