IteratableChunkChecksumValidatorTest.cc 3.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "IteratableChunkChecksumValidator.h"

#include <cppunit/extensions/HelperMacros.h>

#include "TestUtil.h"
#include "DownloadContext.h"
#include "DefaultPieceStorage.h"
#include "Option.h"
#include "DiskAdaptor.h"
#include "FileEntry.h"
#include "PieceSelector.h"

namespace aria2 {

15
class IteratableChunkChecksumValidatorTest : public CppUnit::TestFixture {
16 17 18 19 20 21

  CPPUNIT_TEST_SUITE(IteratableChunkChecksumValidatorTest);
  CPPUNIT_TEST(testValidate);
  CPPUNIT_TEST(testValidate_readError);
  CPPUNIT_TEST_SUITE_END();

22
private:
23
  static const std::string csArray[];
24

25
public:
26
  void setUp() {}
27 28 29 30 31

  void testValidate();
  void testValidate_readError();
};

32
CPPUNIT_TEST_SUITE_REGISTRATION(IteratableChunkChecksumValidatorTest);
33

34 35
const std::string IteratableChunkChecksumValidatorTest::csArray[] = {
    fromHex("29b0e7878271645fffb7eec7db4a7473a1c00bc1"),
36
    fromHex("4df75a661cb7eb2733d9cdaa7f772eae3a4e2976"),
37
    fromHex("0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb")};
38

39 40
void IteratableChunkChecksumValidatorTest::testValidate()
{
41
  Option option;
42 43
  std::shared_ptr<DownloadContext> dctx(new DownloadContext(
      100, 250, A2_TEST_DIR "/chunkChecksumTestFile250.txt"));
44
  dctx->setPieceHashes("sha-1", &csArray[0], &csArray[3]);
45 46
  std::shared_ptr<DefaultPieceStorage> ps(
      new DefaultPieceStorage(dctx, &option));
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  ps->initStorage();
  ps->getDiskAdaptor()->enableReadOnly();
  ps->getDiskAdaptor()->openFile();

  IteratableChunkChecksumValidator validator(dctx, ps);
  validator.init();

  validator.validateChunk();
  CPPUNIT_ASSERT(!validator.finished());
  validator.validateChunk();
  CPPUNIT_ASSERT(!validator.finished());
  validator.validateChunk();
  CPPUNIT_ASSERT(validator.finished());
  CPPUNIT_ASSERT(ps->downloadFinished());

  // make the test fail
  std::deque<std::string> badHashes(&csArray[0], &csArray[3]);
  badHashes[1] = fromHex("ffffffffffffffffffffffffffffffffffffffff");
  dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end());

  validator.init();

69
  while (!validator.finished()) {
70 71 72 73 74 75 76
    validator.validateChunk();
  }
  CPPUNIT_ASSERT(ps->hasPiece(0));
  CPPUNIT_ASSERT(!ps->hasPiece(1));
  CPPUNIT_ASSERT(ps->hasPiece(2));
}

77 78
void IteratableChunkChecksumValidatorTest::testValidate_readError()
{
79
  Option option;
80 81
  std::shared_ptr<DownloadContext> dctx(new DownloadContext(
      100, 500, A2_TEST_DIR "/chunkChecksumTestFile250.txt"));
82 83 84 85
  std::deque<std::string> hashes(&csArray[0], &csArray[3]);
  hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
  hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
  dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end());
86 87
  std::shared_ptr<DefaultPieceStorage> ps(
      new DefaultPieceStorage(dctx, &option));
88 89 90 91 92 93 94
  ps->initStorage();
  ps->getDiskAdaptor()->enableReadOnly();
  ps->getDiskAdaptor()->openFile();

  IteratableChunkChecksumValidator validator(dctx, ps);
  validator.init();

95
  while (!validator.finished()) {
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    validator.validateChunk();
  }

  CPPUNIT_ASSERT(ps->hasPiece(0));
  CPPUNIT_ASSERT(ps->hasPiece(1));
  CPPUNIT_ASSERT(!ps->hasPiece(2)); // #2 piece is not valid because
                                    // #program expects its size is
                                    // #100, but it reads only 50
                                    // #bytes and raises error.
  CPPUNIT_ASSERT(!ps->hasPiece(3));
  CPPUNIT_ASSERT(!ps->hasPiece(4));
}

} // namespace aria2