BtUnchokeMessageTest.cc 2.16 KB
Newer Older
1
#include "BtUnchokeMessage.h"
2

3 4 5
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>

6 7 8 9
#include "bittorrent_helper.h"
#include "Peer.h"
#include "SocketBuffer.h"

10 11
namespace aria2 {

12
class BtUnchokeMessageTest : public CppUnit::TestFixture {
13 14 15 16 17 18 19 20

  CPPUNIT_TEST_SUITE(BtUnchokeMessageTest);
  CPPUNIT_TEST(testCreate);
  CPPUNIT_TEST(testCreateMessage);
  CPPUNIT_TEST(testDoReceivedAction);
  CPPUNIT_TEST(testToString);
  CPPUNIT_TEST_SUITE_END();

21
private:
22
public:
23
  void setUp() {}
24 25 26 27 28 29 30 31 32

  void testCreate();
  void testCreateMessage();
  void testDoReceivedAction();
  void testToString();
};

CPPUNIT_TEST_SUITE_REGISTRATION(BtUnchokeMessageTest);

33 34
void BtUnchokeMessageTest::testCreate()
{
35 36
  unsigned char msg[5];
  bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 1);
37
  std::shared_ptr<BtUnchokeMessage> pm(BtUnchokeMessage::create(&msg[4], 1));
38 39 40 41 42 43 44 45
  CPPUNIT_ASSERT_EQUAL((uint8_t)1, pm->getId());

  // case: payload size is wrong
  try {
    unsigned char msg[6];
    bittorrent::createPeerMessageString(msg, sizeof(msg), 2, 1);
    BtUnchokeMessage::create(&msg[4], 2);
    CPPUNIT_FAIL("exception must be thrown.");
46 47
  }
  catch (...) {
48 49 50 51 52 53 54
  }
  // case: id is wrong
  try {
    unsigned char msg[5];
    bittorrent::createPeerMessageString(msg, sizeof(msg), 1, 2);
    BtUnchokeMessage::create(&msg[4], 1);
    CPPUNIT_FAIL("exception must be thrown.");
55 56
  }
  catch (...) {
57 58 59
  }
}

60 61
void BtUnchokeMessageTest::testCreateMessage()
{
62 63 64
  BtUnchokeMessage msg;
  unsigned char data[5];
  bittorrent::createPeerMessageString(data, sizeof(data), 1, 1);
65 66 67
  auto rawmsg = msg.createMessage();
  CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
  CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
68 69
}

70 71
void BtUnchokeMessageTest::testDoReceivedAction()
{
72
  std::shared_ptr<Peer> peer(new Peer("host", 6969));
73
  peer->allocateSessionResource(1_k, 1_m);
74 75 76 77 78 79 80 81 82
  peer->peerChoking(true);
  BtUnchokeMessage msg;
  msg.setPeer(peer);

  CPPUNIT_ASSERT(peer->peerChoking());
  msg.doReceivedAction();
  CPPUNIT_ASSERT(!peer->peerChoking());
}

83 84
void BtUnchokeMessageTest::testToString()
{
85 86 87 88 89
  BtUnchokeMessage msg;
  CPPUNIT_ASSERT_EQUAL(std::string("unchoke"), msg.toString());
}

} // namespace aria2