PieceTest.cc 1.8 KB

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