2006-02-17 13:35:04 +00:00
|
|
|
#include "ChunkedEncoding.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
class ChunkedEncodingTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(ChunkedEncodingTest);
|
|
|
|
CPPUNIT_TEST(testInflate1);
|
|
|
|
CPPUNIT_TEST(testInflateLargeChunk);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
ChunkedEncoding* enc;
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
enc = new ChunkedEncoding();
|
|
|
|
enc->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInflate1();
|
|
|
|
void testInflateLargeChunk();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( ChunkedEncodingTest );
|
|
|
|
|
|
|
|
void ChunkedEncodingTest::testInflate1() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string msg = "a\r\n1234567890\r\n";
|
2006-02-17 13:35:04 +00:00
|
|
|
char buf[100];
|
2007-08-15 15:11:01 +00:00
|
|
|
int32_t len = sizeof(buf);
|
2006-02-17 13:35:04 +00:00
|
|
|
enc->inflate(buf, len, msg.c_str(), msg.size());
|
|
|
|
buf[len] = '\0';
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), std::string(buf));
|
2006-02-17 13:35:04 +00:00
|
|
|
// second pass
|
|
|
|
len = sizeof(buf);
|
|
|
|
msg = "3;extensionIgnored\r\n123\r\n0\r\n";
|
|
|
|
enc->inflate(buf, len, msg.c_str(), msg.size());
|
|
|
|
buf[len] = '\0';
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("123"), std::string(buf));
|
2006-02-17 13:35:04 +00:00
|
|
|
// input is over
|
|
|
|
CPPUNIT_ASSERT(enc->finished());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChunkedEncodingTest::testInflateLargeChunk() {
|
|
|
|
// give over 4096 character chunk
|
2008-02-08 15:53:45 +00:00
|
|
|
std::fstream is("4096chunk.txt", std::ios::in);
|
2006-02-17 13:35:04 +00:00
|
|
|
if(is.fail()) {
|
|
|
|
CPPUNIT_FAIL("cannot open file 4096chunk.txt");
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string body;
|
2006-02-17 13:35:04 +00:00
|
|
|
is >> body;
|
|
|
|
char buf[4097];
|
2007-08-15 15:11:01 +00:00
|
|
|
int32_t len = sizeof(buf);
|
2006-02-17 13:35:04 +00:00
|
|
|
for(int i = 0; i < 2; i++) {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string msg = "1000\r\n"+body+"\r\n";
|
2006-02-17 13:35:04 +00:00
|
|
|
len = sizeof(buf);
|
|
|
|
enc->inflate(buf, len, msg.c_str(), msg.size());
|
|
|
|
buf[len] = '\0';
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(body, std::string(buf));
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
enc->inflate(buf, len, "0\r\n", 3);
|
2007-08-15 15:11:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)0, len);
|
2006-02-17 13:35:04 +00:00
|
|
|
CPPUNIT_ASSERT(enc->finished());
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|