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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include "ChunkedDecoder.h"
#include "DlAbortEx.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class ChunkedDecoderTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ChunkedDecoderTest);
CPPUNIT_TEST(testDecode);
CPPUNIT_TEST(testDecode_withoutTrailer);
CPPUNIT_TEST(testDecode_tooLargeChunkSize);
CPPUNIT_TEST(testDecode_chunkSizeMismatch);
CPPUNIT_TEST(testGetName);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void testDecode();
void testDecode_withoutTrailer();
void testDecode_tooLargeChunkSize();
void testDecode_chunkSizeMismatch();
void testGetName();
};
CPPUNIT_TEST_SUITE_REGISTRATION( ChunkedDecoderTest );
void ChunkedDecoderTest::testDecode()
{
ChunkedDecoder decoder;
decoder.init();
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("a\r\n1234567890\r\n");
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"),
decoder.decode(msg.c_str(), msg.size()));
}
// Feed extension; see it is ignored.
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>
("3;extensionIgnored\r\n123\r\n");
CPPUNIT_ASSERT_EQUAL(std::string("123"),
decoder.decode(msg.c_str(), msg.size()));
}
// Not all chunk size is available
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("1");
CPPUNIT_ASSERT_EQUAL(std::string(),
decoder.decode(msg.c_str(), msg.size()));
}
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("0\r\n1234567890123456\r\n");
CPPUNIT_ASSERT_EQUAL(std::string("1234567890123456"),
decoder.decode(msg.c_str(), msg.size()));
}
// Not all chunk data is available
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("10\r\n1234567890");
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"),
decoder.decode(msg.c_str(), msg.size()));
}
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("123456\r\n");
CPPUNIT_ASSERT_EQUAL(std::string("123456"),
decoder.decode(msg.c_str(), msg.size()));
}
// no trailing CR LF.
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>
("10\r\n1234567890123456");
CPPUNIT_ASSERT_EQUAL(std::string("1234567890123456"),
decoder.decode(msg.c_str(), msg.size()));
}
// feed only CR
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>
("\r");
CPPUNIT_ASSERT_EQUAL(std::string(),
decoder.decode(msg.c_str(), msg.size()));
}
// feed next LF
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>
("\n");
CPPUNIT_ASSERT_EQUAL(std::string(),
decoder.decode(msg.c_str(), msg.size()));
}
// feed 0 CR LF.
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>
("0\r\n");
CPPUNIT_ASSERT_EQUAL(std::string(),
decoder.decode(msg.c_str(), msg.size()));
}
// feed trailer
{
CPPUNIT_ASSERT_EQUAL
(std::string(),
decoder.decode
(reinterpret_cast<const unsigned char*>("trailer\r\n"), 9));
}
// feed final CRLF
{
CPPUNIT_ASSERT_EQUAL
(std::string(),
decoder.decode(reinterpret_cast<const unsigned char*>("\r\n"), 2));
}
// input is over
CPPUNIT_ASSERT(decoder.finished());
decoder.release();
}
void ChunkedDecoderTest::testDecode_withoutTrailer()
{
ChunkedDecoder decoder;
decoder.init();
CPPUNIT_ASSERT_EQUAL
(std::string(),
decoder.decode(reinterpret_cast<const unsigned char*>("0\r\n\r\n"), 5));
CPPUNIT_ASSERT(decoder.finished());
}
void ChunkedDecoderTest::testDecode_tooLargeChunkSize()
{
// chunkSize should be under 2^64-1
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("ffffffffffffffff\r\n");
ChunkedDecoder decoder;
decoder.decode(msg.c_str(), msg.size());
}
// chunkSize 2^64 causes error
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("10000000000000000\r\n");
ChunkedDecoder decoder;
try {
decoder.decode(msg.c_str(), msg.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(DlAbortEx& e) {
// success
}
}
}
void ChunkedDecoderTest::testDecode_chunkSizeMismatch()
{
std::basic_string<unsigned char> msg =
reinterpret_cast<const unsigned char*>("3\r\n1234\r\n");
ChunkedDecoder decoder;
try {
decoder.decode(msg.c_str(), msg.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(DlAbortEx& e) {
// success
}
}
void ChunkedDecoderTest::testGetName()
{
ChunkedDecoder decoder;
CPPUNIT_ASSERT_EQUAL(std::string("ChunkedDecoder"), decoder.getName());
}
} // namespace aria2
|