ChunkedDecoderTest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "ChunkedDecoder.h"
  2. #include "DlAbortEx.h"
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. namespace aria2 {
  6. class ChunkedDecoderTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(ChunkedDecoderTest);
  8. CPPUNIT_TEST(testDecode);
  9. CPPUNIT_TEST(testDecode_withoutTrailer);
  10. CPPUNIT_TEST(testDecode_tooLargeChunkSize);
  11. CPPUNIT_TEST(testDecode_chunkSizeMismatch);
  12. CPPUNIT_TEST(testGetName);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void setUp() {}
  16. void testDecode();
  17. void testDecode_withoutTrailer();
  18. void testDecode_tooLargeChunkSize();
  19. void testDecode_chunkSizeMismatch();
  20. void testGetName();
  21. };
  22. CPPUNIT_TEST_SUITE_REGISTRATION( ChunkedDecoderTest );
  23. void ChunkedDecoderTest::testDecode()
  24. {
  25. ChunkedDecoder decoder;
  26. decoder.init();
  27. {
  28. std::basic_string<unsigned char> msg =
  29. reinterpret_cast<const unsigned char*>("a\r\n1234567890\r\n");
  30. CPPUNIT_ASSERT_EQUAL(std::string("1234567890"),
  31. decoder.decode(msg.c_str(), msg.size()));
  32. }
  33. // Feed extension; see it is ignored.
  34. {
  35. std::basic_string<unsigned char> msg =
  36. reinterpret_cast<const unsigned char*>
  37. ("3;extensionIgnored\r\n123\r\n");
  38. CPPUNIT_ASSERT_EQUAL(std::string("123"),
  39. decoder.decode(msg.c_str(), msg.size()));
  40. }
  41. // Not all chunk size is available
  42. {
  43. std::basic_string<unsigned char> msg =
  44. reinterpret_cast<const unsigned char*>("1");
  45. CPPUNIT_ASSERT_EQUAL(std::string(),
  46. decoder.decode(msg.c_str(), msg.size()));
  47. }
  48. {
  49. std::basic_string<unsigned char> msg =
  50. reinterpret_cast<const unsigned char*>("0\r\n1234567890123456\r\n");
  51. CPPUNIT_ASSERT_EQUAL(std::string("1234567890123456"),
  52. decoder.decode(msg.c_str(), msg.size()));
  53. }
  54. // Not all chunk data is available
  55. {
  56. std::basic_string<unsigned char> msg =
  57. reinterpret_cast<const unsigned char*>("10\r\n1234567890");
  58. CPPUNIT_ASSERT_EQUAL(std::string("1234567890"),
  59. decoder.decode(msg.c_str(), msg.size()));
  60. }
  61. {
  62. std::basic_string<unsigned char> msg =
  63. reinterpret_cast<const unsigned char*>("123456\r\n");
  64. CPPUNIT_ASSERT_EQUAL(std::string("123456"),
  65. decoder.decode(msg.c_str(), msg.size()));
  66. }
  67. // no trailing CR LF.
  68. {
  69. std::basic_string<unsigned char> msg =
  70. reinterpret_cast<const unsigned char*>
  71. ("10\r\n1234567890123456");
  72. CPPUNIT_ASSERT_EQUAL(std::string("1234567890123456"),
  73. decoder.decode(msg.c_str(), msg.size()));
  74. }
  75. // feed only CR
  76. {
  77. std::basic_string<unsigned char> msg =
  78. reinterpret_cast<const unsigned char*>
  79. ("\r");
  80. CPPUNIT_ASSERT_EQUAL(std::string(),
  81. decoder.decode(msg.c_str(), msg.size()));
  82. }
  83. // feed next LF
  84. {
  85. std::basic_string<unsigned char> msg =
  86. reinterpret_cast<const unsigned char*>
  87. ("\n");
  88. CPPUNIT_ASSERT_EQUAL(std::string(),
  89. decoder.decode(msg.c_str(), msg.size()));
  90. }
  91. // feed 0 CR LF.
  92. {
  93. std::basic_string<unsigned char> msg =
  94. reinterpret_cast<const unsigned char*>
  95. ("0\r\n");
  96. CPPUNIT_ASSERT_EQUAL(std::string(),
  97. decoder.decode(msg.c_str(), msg.size()));
  98. }
  99. // feed trailer
  100. {
  101. CPPUNIT_ASSERT_EQUAL
  102. (std::string(),
  103. decoder.decode
  104. (reinterpret_cast<const unsigned char*>("trailer\r\n"), 9));
  105. }
  106. // feed final CRLF
  107. {
  108. CPPUNIT_ASSERT_EQUAL
  109. (std::string(),
  110. decoder.decode(reinterpret_cast<const unsigned char*>("\r\n"), 2));
  111. }
  112. // input is over
  113. CPPUNIT_ASSERT(decoder.finished());
  114. decoder.release();
  115. }
  116. void ChunkedDecoderTest::testDecode_withoutTrailer()
  117. {
  118. ChunkedDecoder decoder;
  119. decoder.init();
  120. CPPUNIT_ASSERT_EQUAL
  121. (std::string(),
  122. decoder.decode(reinterpret_cast<const unsigned char*>("0\r\n\r\n"), 5));
  123. CPPUNIT_ASSERT(decoder.finished());
  124. }
  125. void ChunkedDecoderTest::testDecode_tooLargeChunkSize()
  126. {
  127. // chunkSize should be under 2^64-1
  128. {
  129. std::basic_string<unsigned char> msg =
  130. reinterpret_cast<const unsigned char*>("ffffffffffffffff\r\n");
  131. ChunkedDecoder decoder;
  132. decoder.decode(msg.c_str(), msg.size());
  133. }
  134. // chunkSize 2^64 causes error
  135. {
  136. std::basic_string<unsigned char> msg =
  137. reinterpret_cast<const unsigned char*>("10000000000000000\r\n");
  138. ChunkedDecoder decoder;
  139. try {
  140. decoder.decode(msg.c_str(), msg.size());
  141. CPPUNIT_FAIL("exception must be thrown.");
  142. } catch(DlAbortEx& e) {
  143. // success
  144. }
  145. }
  146. }
  147. void ChunkedDecoderTest::testDecode_chunkSizeMismatch()
  148. {
  149. std::basic_string<unsigned char> msg =
  150. reinterpret_cast<const unsigned char*>("3\r\n1234\r\n");
  151. ChunkedDecoder decoder;
  152. try {
  153. decoder.decode(msg.c_str(), msg.size());
  154. CPPUNIT_FAIL("exception must be thrown.");
  155. } catch(DlAbortEx& e) {
  156. // success
  157. }
  158. }
  159. void ChunkedDecoderTest::testGetName()
  160. {
  161. ChunkedDecoder decoder;
  162. CPPUNIT_ASSERT_EQUAL(std::string("ChunkedDecoder"), decoder.getName());
  163. }
  164. } // namespace aria2