PieceTest.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "Piece.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "util.h"
  5. namespace aria2 {
  6. class PieceTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(PieceTest);
  8. CPPUNIT_TEST(testCompleteBlock);
  9. CPPUNIT_TEST(testGetCompletedLength);
  10. #ifdef ENABLE_MESSAGE_DIGEST
  11. CPPUNIT_TEST(testUpdateHash);
  12. #endif // ENABLE_MESSAGE_DIGEST
  13. CPPUNIT_TEST_SUITE_END();
  14. private:
  15. public:
  16. void setUp() {}
  17. void testCompleteBlock();
  18. void testGetCompletedLength();
  19. #ifdef ENABLE_MESSAGE_DIGEST
  20. void testUpdateHash();
  21. #endif // ENABLE_MESSAGE_DIGEST
  22. };
  23. CPPUNIT_TEST_SUITE_REGISTRATION( PieceTest );
  24. void PieceTest::testCompleteBlock()
  25. {
  26. size_t blockLength = 32*1024;
  27. Piece p(0, blockLength*10, blockLength);
  28. p.completeBlock(5);
  29. CPPUNIT_ASSERT(p.hasBlock(5));
  30. }
  31. void PieceTest::testGetCompletedLength()
  32. {
  33. size_t blockLength = 16*1024;
  34. Piece p(0, blockLength*10+100, blockLength);
  35. p.completeBlock(1);
  36. p.completeBlock(2);
  37. p.completeBlock(9);
  38. p.completeBlock(10); // <-- 100 bytes
  39. CPPUNIT_ASSERT_EQUAL(blockLength*3+100, p.getCompletedLength());
  40. }
  41. #ifdef ENABLE_MESSAGE_DIGEST
  42. void PieceTest::testUpdateHash()
  43. {
  44. Piece p(0, 16, 2*1024*1024);
  45. p.setHashType("sha-1");
  46. std::string spam("SPAM!");
  47. CPPUNIT_ASSERT(p.updateHash
  48. (0, reinterpret_cast<const unsigned char*>(spam.c_str()),
  49. spam.size()));
  50. CPPUNIT_ASSERT(!p.isHashCalculated());
  51. std::string spamspam("SPAM!SPAM!!");
  52. CPPUNIT_ASSERT(p.updateHash
  53. (spam.size(),
  54. reinterpret_cast<const unsigned char*>(spamspam.c_str()),
  55. spamspam.size()));
  56. CPPUNIT_ASSERT(p.isHashCalculated());
  57. CPPUNIT_ASSERT_EQUAL(std::string("d9189aff79e075a2e60271b9556a710dc1bc7de7"),
  58. util::toHex(p.getDigest()));
  59. }
  60. #endif // ENABLE_MESSAGE_DIGEST
  61. } // namespace aria2