ChunkedDecoderTest.cc 4.3 KB

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