BtNotInterestedMessageTest.cc 3.0 KB

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