2010-09-06 14:29:36 +00:00
|
|
|
#include "GZipDecodingStreamFilter.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include "Exception.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "Segment.h"
|
|
|
|
#include "ByteArrayDiskWriter.h"
|
|
|
|
#include "SinkStreamFilter.h"
|
|
|
|
#include "MockSegment.h"
|
2014-04-16 23:36:19 +00:00
|
|
|
#include "MessageDigest.h"
|
2010-09-06 14:29:36 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class GZipDecodingStreamFilterTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(GZipDecodingStreamFilterTest);
|
|
|
|
CPPUNIT_TEST(testTransform);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
class MockSegment2:public MockSegment {
|
|
|
|
private:
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t positionToWrite_;
|
2010-09-06 14:29:36 +00:00
|
|
|
public:
|
|
|
|
MockSegment2():positionToWrite_(0) {}
|
|
|
|
|
2014-05-24 20:42:23 +00:00
|
|
|
virtual void updateWrittenLength(int64_t bytes) CXX11_OVERRIDE
|
2010-09-06 14:29:36 +00:00
|
|
|
{
|
|
|
|
positionToWrite_ += bytes;
|
|
|
|
}
|
|
|
|
|
2013-07-06 06:54:03 +00:00
|
|
|
virtual int64_t getPositionToWrite() const CXX11_OVERRIDE
|
2010-09-06 14:29:36 +00:00
|
|
|
{
|
|
|
|
return positionToWrite_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-03 15:39:11 +00:00
|
|
|
std::unique_ptr<GZipDecodingStreamFilter> filter_;
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ByteArrayDiskWriter> writer_;
|
|
|
|
std::shared_ptr<MockSegment2> segment_;
|
2010-09-06 14:29:36 +00:00
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2013-07-03 15:39:11 +00:00
|
|
|
writer_ = std::make_shared<ByteArrayDiskWriter>();
|
|
|
|
auto sinkFilter = make_unique<SinkStreamFilter>();
|
|
|
|
sinkFilter->init();
|
|
|
|
filter_ = make_unique<GZipDecodingStreamFilter>(std::move(sinkFilter));
|
2010-09-06 14:29:36 +00:00
|
|
|
filter_->init();
|
2013-07-03 15:39:11 +00:00
|
|
|
segment_ = std::make_shared<MockSegment2>();
|
2010-09-06 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void testTransform();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(GZipDecodingStreamFilterTest);
|
|
|
|
|
|
|
|
void GZipDecodingStreamFilterTest::testTransform()
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
unsigned char buf[4_k];
|
2010-12-02 13:38:36 +00:00
|
|
|
std::ifstream in(A2_TEST_DIR"/gzip_decode_test.gz", std::ios::binary);
|
2010-09-06 14:29:36 +00:00
|
|
|
while(in) {
|
|
|
|
in.read(reinterpret_cast<char*>(buf), sizeof(buf));
|
|
|
|
filter_->transform(writer_, segment_, buf, in.gcount());
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT(filter_->finished());
|
2010-11-11 02:56:24 +00:00
|
|
|
std::string data = writer_->getString();
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<MessageDigest> sha1(MessageDigest::sha1());
|
2010-11-11 02:56:24 +00:00
|
|
|
sha1->update(data.data(), data.size());
|
2010-09-06 14:29:36 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("8b577b33c0411b2be9d4fa74c7402d54a8d21f96"),
|
2011-10-13 12:40:07 +00:00
|
|
|
util::toHex(sha1->digest()));
|
2010-09-06 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|