2007-10-23 16:29:37 +00:00
|
|
|
#include "Piece.h"
|
2010-11-11 02:56:24 +00:00
|
|
|
|
2007-10-23 16:29:37 +00:00
|
|
|
#include <string>
|
2010-11-11 02:56:24 +00:00
|
|
|
|
2007-10-23 16:29:37 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2007-10-23 16:29:37 +00:00
|
|
|
|
|
|
|
class PieceTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(PieceTest);
|
|
|
|
CPPUNIT_TEST(testCompleteBlock);
|
|
|
|
CPPUNIT_TEST(testGetCompletedLength);
|
2008-06-04 16:28:16 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
|
|
|
|
CPPUNIT_TEST(testUpdateHash);
|
|
|
|
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2007-10-23 16:29:37 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {}
|
|
|
|
|
|
|
|
void testCompleteBlock();
|
|
|
|
void testGetCompletedLength();
|
2008-06-04 16:28:16 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
|
|
|
|
void testUpdateHash();
|
|
|
|
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2007-10-23 16:29:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( PieceTest );
|
|
|
|
|
|
|
|
void PieceTest::testCompleteBlock()
|
|
|
|
{
|
2008-03-11 12:31:14 +00:00
|
|
|
size_t blockLength = 32*1024;
|
2007-10-23 16:29:37 +00:00
|
|
|
Piece p(0, blockLength*10, blockLength);
|
|
|
|
|
|
|
|
p.completeBlock(5);
|
|
|
|
|
2008-03-07 12:44:20 +00:00
|
|
|
CPPUNIT_ASSERT(p.hasBlock(5));
|
2007-10-23 16:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PieceTest::testGetCompletedLength()
|
|
|
|
{
|
2008-03-11 12:31:14 +00:00
|
|
|
size_t blockLength = 16*1024;
|
2008-03-07 12:44:20 +00:00
|
|
|
Piece p(0, blockLength*10+100, blockLength);
|
2007-10-23 16:29:37 +00:00
|
|
|
|
|
|
|
p.completeBlock(1);
|
|
|
|
p.completeBlock(2);
|
|
|
|
p.completeBlock(9);
|
2008-03-07 12:44:20 +00:00
|
|
|
p.completeBlock(10); // <-- 100 bytes
|
2007-10-23 16:29:37 +00:00
|
|
|
|
2008-03-11 12:31:14 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(blockLength*3+100, p.getCompletedLength());
|
2007-10-23 16:29:37 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
|
|
|
|
void PieceTest::testUpdateHash()
|
|
|
|
{
|
|
|
|
Piece p(0, 16, 2*1024*1024);
|
2011-07-27 13:50:10 +00:00
|
|
|
p.setHashType("sha-1");
|
2008-06-04 16:28:16 +00:00
|
|
|
|
|
|
|
std::string spam("SPAM!");
|
|
|
|
CPPUNIT_ASSERT(p.updateHash
|
2010-01-05 16:01:46 +00:00
|
|
|
(0, reinterpret_cast<const unsigned char*>(spam.c_str()),
|
|
|
|
spam.size()));
|
2008-06-04 16:28:16 +00:00
|
|
|
CPPUNIT_ASSERT(!p.isHashCalculated());
|
|
|
|
|
|
|
|
std::string spamspam("SPAM!SPAM!!");
|
|
|
|
CPPUNIT_ASSERT(p.updateHash
|
2010-01-05 16:01:46 +00:00
|
|
|
(spam.size(),
|
|
|
|
reinterpret_cast<const unsigned char*>(spamspam.c_str()),
|
|
|
|
spamspam.size()));
|
2008-06-04 16:28:16 +00:00
|
|
|
CPPUNIT_ASSERT(p.isHashCalculated());
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("d9189aff79e075a2e60271b9556a710dc1bc7de7"),
|
2010-01-05 16:01:46 +00:00
|
|
|
p.getHashString());
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|