forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GZipDecodingStreamFilterTest.cc
79 lines (63 loc) · 2.02 KB
/
GZipDecodingStreamFilterTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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"
#include "MessageDigest.h"
namespace aria2 {
class GZipDecodingStreamFilterTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(GZipDecodingStreamFilterTest);
CPPUNIT_TEST(testTransform);
CPPUNIT_TEST_SUITE_END();
class MockSegment2 : public MockSegment {
private:
int64_t positionToWrite_;
public:
MockSegment2() : positionToWrite_(0) {}
virtual void updateWrittenLength(int64_t bytes) CXX11_OVERRIDE
{
positionToWrite_ += bytes;
}
virtual int64_t getPositionToWrite() const CXX11_OVERRIDE
{
return positionToWrite_;
}
};
std::unique_ptr<GZipDecodingStreamFilter> filter_;
std::shared_ptr<ByteArrayDiskWriter> writer_;
std::shared_ptr<MockSegment2> segment_;
public:
void setUp()
{
writer_ = std::make_shared<ByteArrayDiskWriter>();
auto sinkFilter = make_unique<SinkStreamFilter>();
sinkFilter->init();
filter_ = make_unique<GZipDecodingStreamFilter>(std::move(sinkFilter));
filter_->init();
segment_ = std::make_shared<MockSegment2>();
}
void testTransform();
};
CPPUNIT_TEST_SUITE_REGISTRATION(GZipDecodingStreamFilterTest);
void GZipDecodingStreamFilterTest::testTransform()
{
unsigned char buf[4_k];
std::ifstream in(A2_TEST_DIR "/gzip_decode_test.gz", std::ios::binary);
while (in) {
in.read(reinterpret_cast<char*>(buf), sizeof(buf));
filter_->transform(writer_, segment_, buf, in.gcount());
}
CPPUNIT_ASSERT(filter_->finished());
std::string data = writer_->getString();
std::shared_ptr<MessageDigest> sha1(MessageDigest::sha1());
sha1->update(data.data(), data.size());
CPPUNIT_ASSERT_EQUAL(std::string("8b577b33c0411b2be9d4fa74c7402d54a8d21f96"),
util::toHex(sha1->digest()));
}
} // namespace aria2