PeerSessionResourceTest.cc 6.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "PeerSessionResource.h"

#include <cppunit/extensions/HelperMacros.h>

#include "MockBtMessageDispatcher.h"
#include "Exception.h"
#include "util.h"

namespace aria2 {

11
class PeerSessionResourceTest : public CppUnit::TestFixture {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

  CPPUNIT_TEST_SUITE(PeerSessionResourceTest);
  CPPUNIT_TEST(testPeerAllowedIndexSetContains);
  CPPUNIT_TEST(testAmAllowedIndexSetContains);
  CPPUNIT_TEST(testHasAllPieces);
  CPPUNIT_TEST(testHasPiece);
  CPPUNIT_TEST(testUpdateUploadLength);
  CPPUNIT_TEST(testUpdateDownloadLength);
  CPPUNIT_TEST(testExtendedMessageEnabled);
  CPPUNIT_TEST(testGetExtensionMessageID);
  CPPUNIT_TEST(testFastExtensionEnabled);
  CPPUNIT_TEST(testSnubbing);
  CPPUNIT_TEST(testAmChoking);
  CPPUNIT_TEST(testAmInterested);
  CPPUNIT_TEST(testPeerChoking);
  CPPUNIT_TEST(testPeerInterested);
  CPPUNIT_TEST(testChokingRequired);
  CPPUNIT_TEST(testOptUnchoking);
  CPPUNIT_TEST(testShouldBeChoking);
  CPPUNIT_TEST(testCountOutstandingRequest);
  CPPUNIT_TEST_SUITE_END();
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
public:
  void setUp() {}

  void tearDown() {}

  void testPeerAllowedIndexSetContains();
  void testAmAllowedIndexSetContains();
  void testHasAllPieces();
  void testHasPiece();
  void testUpdateUploadLength();
  void testUpdateDownloadLength();
  void testExtendedMessageEnabled();
  void testGetExtensionMessageID();
  void testFastExtensionEnabled();
  void testSnubbing();
  void testAmChoking();
  void testAmInterested();
  void testPeerChoking();
  void testPeerInterested();
  void testChokingRequired();
  void testOptUnchoking();
  void testShouldBeChoking();
  void testCountOutstandingRequest();
};

CPPUNIT_TEST_SUITE_REGISTRATION(PeerSessionResourceTest);

void PeerSessionResourceTest::testPeerAllowedIndexSetContains()
{
63
  PeerSessionResource res(1_k, 1_m);
64 65 66 67 68 69 70 71 72 73 74

  res.addPeerAllowedIndex(567);
  res.addPeerAllowedIndex(789);

  CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(567));
  CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(789));
  CPPUNIT_ASSERT(!res.peerAllowedIndexSetContains(123));
}

void PeerSessionResourceTest::testAmAllowedIndexSetContains()
{
75
  PeerSessionResource res(1_k, 1_m);
76

77 78 79 80 81 82 83 84 85 86
  res.addAmAllowedIndex(567);
  res.addAmAllowedIndex(789);

  CPPUNIT_ASSERT(res.amAllowedIndexSetContains(567));
  CPPUNIT_ASSERT(res.amAllowedIndexSetContains(789));
  CPPUNIT_ASSERT(!res.amAllowedIndexSetContains(123));
}

void PeerSessionResourceTest::testHasAllPieces()
{
87
  PeerSessionResource res(1_k, 1_m);
88 89 90 91 92 93 94 95

  CPPUNIT_ASSERT(!res.hasAllPieces());
  res.markSeeder();
  CPPUNIT_ASSERT(res.hasAllPieces());
}

void PeerSessionResourceTest::testHasPiece()
{
96
  PeerSessionResource res(1_k, 1_m);
97 98 99 100 101 102

  CPPUNIT_ASSERT(!res.hasPiece(300));
  res.updateBitfield(300, 1);
  CPPUNIT_ASSERT(res.hasPiece(300));
  res.updateBitfield(300, 0);
  CPPUNIT_ASSERT(!res.hasPiece(300));
103
}
104 105 106

void PeerSessionResourceTest::testUpdateUploadLength()
{
107
  PeerSessionResource res(1_k, 1_m);
108

109
  CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.uploadLength());
110 111
  res.updateUploadLength(100);
  res.updateUploadLength(200);
112
  CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.uploadLength());
113 114 115 116
}

void PeerSessionResourceTest::testUpdateDownloadLength()
{
117
  PeerSessionResource res(1_k, 1_m);
118

119
  CPPUNIT_ASSERT_EQUAL((int64_t)0LL, res.downloadLength());
120 121
  res.updateDownload(100);
  res.updateDownload(200);
122
  CPPUNIT_ASSERT_EQUAL((int64_t)300LL, res.downloadLength());
123 124 125
}

void PeerSessionResourceTest::testExtendedMessageEnabled()
126
{
127
  PeerSessionResource res(1_k, 1_m);
128 129 130 131 132 133 134 135 136 137

  CPPUNIT_ASSERT(!res.extendedMessagingEnabled());
  res.extendedMessagingEnabled(true);
  CPPUNIT_ASSERT(res.extendedMessagingEnabled());
  res.extendedMessagingEnabled(false);
  CPPUNIT_ASSERT(!res.extendedMessagingEnabled());
}

void PeerSessionResourceTest::testGetExtensionMessageID()
{
138
  PeerSessionResource res(1_k, 1_m);
139

140
  res.addExtension(ExtensionMessageRegistry::UT_PEX, 9);
141 142 143 144
  CPPUNIT_ASSERT_EQUAL(
      (uint8_t)9, res.getExtensionMessageID(ExtensionMessageRegistry::UT_PEX));
  CPPUNIT_ASSERT_EQUAL((uint8_t)0, res.getExtensionMessageID(
                                       ExtensionMessageRegistry::UT_METADATA));
145 146 147 148

  CPPUNIT_ASSERT_EQUAL(std::string("ut_pex"),
                       std::string(res.getExtensionName(9)));
  CPPUNIT_ASSERT(!res.getExtensionName(10));
149 150 151 152
}

void PeerSessionResourceTest::testFastExtensionEnabled()
{
153
  PeerSessionResource res(1_k, 1_m);
154 155 156 157 158 159 160 161 162 163

  CPPUNIT_ASSERT(!res.fastExtensionEnabled());
  res.fastExtensionEnabled(true);
  CPPUNIT_ASSERT(res.fastExtensionEnabled());
  res.fastExtensionEnabled(false);
  CPPUNIT_ASSERT(!res.fastExtensionEnabled());
}

void PeerSessionResourceTest::testSnubbing()
{
164
  PeerSessionResource res(1_k, 1_m);
165 166 167 168 169 170 171 172 173 174

  CPPUNIT_ASSERT(!res.snubbing());
  res.snubbing(true);
  CPPUNIT_ASSERT(res.snubbing());
  res.snubbing(false);
  CPPUNIT_ASSERT(!res.snubbing());
}

void PeerSessionResourceTest::testAmChoking()
{
175
  PeerSessionResource res(1_k, 1_m);
176 177 178 179 180 181 182 183 184 185

  CPPUNIT_ASSERT(res.amChoking());
  res.amChoking(false);
  CPPUNIT_ASSERT(!res.amChoking());
  res.amChoking(true);
  CPPUNIT_ASSERT(res.amChoking());
}

void PeerSessionResourceTest::testAmInterested()
{
186
  PeerSessionResource res(1_k, 1_m);
187 188 189 190 191 192 193 194 195 196

  CPPUNIT_ASSERT(!res.amInterested());
  res.amInterested(true);
  CPPUNIT_ASSERT(res.amInterested());
  res.amInterested(false);
  CPPUNIT_ASSERT(!res.amInterested());
}

void PeerSessionResourceTest::testPeerChoking()
{
197
  PeerSessionResource res(1_k, 1_m);
198 199 200 201 202 203 204 205 206 207

  CPPUNIT_ASSERT(res.peerChoking());
  res.peerChoking(false);
  CPPUNIT_ASSERT(!res.peerChoking());
  res.peerChoking(true);
  CPPUNIT_ASSERT(res.peerChoking());
}

void PeerSessionResourceTest::testPeerInterested()
{
208
  PeerSessionResource res(1_k, 1_m);
209 210 211 212 213 214 215 216 217 218

  CPPUNIT_ASSERT(!res.peerInterested());
  res.peerInterested(true);
  CPPUNIT_ASSERT(res.peerInterested());
  res.peerInterested(false);
  CPPUNIT_ASSERT(!res.peerInterested());
}

void PeerSessionResourceTest::testChokingRequired()
{
219
  PeerSessionResource res(1_k, 1_m);
220 221 222 223 224 225 226 227 228 229

  CPPUNIT_ASSERT(res.chokingRequired());
  res.chokingRequired(false);
  CPPUNIT_ASSERT(!res.chokingRequired());
  res.chokingRequired(true);
  CPPUNIT_ASSERT(res.chokingRequired());
}

void PeerSessionResourceTest::testOptUnchoking()
{
230
  PeerSessionResource res(1_k, 1_m);
231 232 233 234 235 236 237 238 239 240

  CPPUNIT_ASSERT(!res.optUnchoking());
  res.optUnchoking(true);
  CPPUNIT_ASSERT(res.optUnchoking());
  res.optUnchoking(false);
  CPPUNIT_ASSERT(!res.optUnchoking());
}

void PeerSessionResourceTest::testShouldBeChoking()
{
241
  PeerSessionResource res(1_k, 1_m);
242

243 244 245 246 247 248 249 250 251 252
  CPPUNIT_ASSERT(res.shouldBeChoking());
  res.chokingRequired(false);
  CPPUNIT_ASSERT(!res.shouldBeChoking());
  res.chokingRequired(true);
  res.optUnchoking(true);
  CPPUNIT_ASSERT(!res.shouldBeChoking());
}

void PeerSessionResourceTest::testCountOutstandingRequest()
{
253 254 255
  PeerSessionResource res(1_k, 1_m);
  std::shared_ptr<MockBtMessageDispatcher> dispatcher(
      new MockBtMessageDispatcher());
256 257 258 259 260 261
  res.setBtMessageDispatcher(dispatcher.get());

  CPPUNIT_ASSERT_EQUAL((size_t)0, res.countOutstandingUpload());
}

} // namespace aria2