PieceTest.cc 1.8 KB

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