IteratableChecksumValidatorTest.cc 1.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "IteratableChecksumValidator.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 IteratableChecksumValidatorTest : public CppUnit::TestFixture {
16 17 18 19 20 21

  CPPUNIT_TEST_SUITE(IteratableChecksumValidatorTest);
  CPPUNIT_TEST(testValidate);
  CPPUNIT_TEST(testValidate_fail);
  CPPUNIT_TEST_SUITE_END();

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

  void testValidate();
  void testValidate_fail();
};

30
CPPUNIT_TEST_SUITE_REGISTRATION(IteratableChecksumValidatorTest);
31

32 33
void IteratableChecksumValidatorTest::testValidate()
{
34
  Option option;
35 36 37 38 39
  std::shared_ptr<DownloadContext> dctx(new DownloadContext(
      100, 250, A2_TEST_DIR "/chunkChecksumTestFile250.txt"));
  dctx->setDigest("sha-1", fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a"));
  std::shared_ptr<DefaultPieceStorage> ps(
      new DefaultPieceStorage(dctx, &option));
40 41 42 43 44 45
  ps->initStorage();
  ps->getDiskAdaptor()->enableReadOnly();
  ps->getDiskAdaptor()->openFile();

  IteratableChecksumValidator validator(dctx, ps);
  validator.init();
46
  while (!validator.finished()) {
47 48 49 50 51 52
    validator.validateChunk();
  }

  CPPUNIT_ASSERT(ps->downloadFinished());
}

53 54
void IteratableChecksumValidatorTest::testValidate_fail()
{
55
  Option option;
56 57 58 59 60
  std::shared_ptr<DownloadContext> dctx(new DownloadContext(
      100, 250, A2_TEST_DIR "/chunkChecksumTestFile250.txt"));
  dctx->setDigest("sha-1", fromHex(std::string(40, '0'))); // set wrong checksum
  std::shared_ptr<DefaultPieceStorage> ps(
      new DefaultPieceStorage(dctx, &option));
61 62 63 64 65 66
  ps->initStorage();
  ps->getDiskAdaptor()->enableReadOnly();
  ps->getDiskAdaptor()->openFile();

  IteratableChecksumValidator validator(dctx, ps);
  validator.init();
67

68
  while (!validator.finished()) {
69 70 71 72 73 74 75
    validator.validateChunk();
  }

  CPPUNIT_ASSERT(!ps->downloadFinished());
}

} // namespace aria2