BencodeTest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "bencode.h"
  2. #include <cstdlib>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "RecoverableException.h"
  5. #include "TimeA2.h"
  6. namespace aria2 {
  7. class BencodeTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(BencodeTest);
  9. CPPUNIT_TEST(testDecode);
  10. CPPUNIT_TEST(testDecode_overflow);
  11. CPPUNIT_TEST(testEncode);
  12. CPPUNIT_TEST_SUITE_END();
  13. private:
  14. public:
  15. void testDecode();
  16. void testDecode_overflow();
  17. void testEncode();
  18. };
  19. CPPUNIT_TEST_SUITE_REGISTRATION( BencodeTest );
  20. void BencodeTest::testDecode()
  21. {
  22. {
  23. // string, integer and list in dict
  24. BDE dict =
  25. bencode::decode("d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee");
  26. CPPUNIT_ASSERT(dict.isDict());
  27. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), dict["name"].s());
  28. CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(12345678900LL),
  29. dict["size"].i());
  30. BDE list = dict["files"];
  31. CPPUNIT_ASSERT(list.isList());
  32. CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list.size());
  33. CPPUNIT_ASSERT_EQUAL(std::string("bin"), list[0].s());
  34. CPPUNIT_ASSERT_EQUAL(std::string("doc"), list[1].s());
  35. }
  36. {
  37. // dict in list
  38. BDE list = bencode::decode("ld1:ki123eee");
  39. CPPUNIT_ASSERT(list.isList());
  40. CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list.size());
  41. BDE dict = list[0];
  42. CPPUNIT_ASSERT(dict.isDict());
  43. CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(123),
  44. dict["k"].i());
  45. }
  46. {
  47. // empty key is allowed
  48. BDE s = bencode::decode("d0:1:ve");
  49. }
  50. {
  51. // empty string
  52. BDE s = bencode::decode("0:");
  53. CPPUNIT_ASSERT_EQUAL(std::string(""), s.s());
  54. }
  55. {
  56. // empty dict
  57. BDE d = bencode::decode("de");
  58. CPPUNIT_ASSERT(d.empty());
  59. }
  60. {
  61. // empty list
  62. BDE l = bencode::decode("le");
  63. CPPUNIT_ASSERT(l.empty());
  64. }
  65. {
  66. // integer, without ending 'e'
  67. try {
  68. bencode::decode("i3");
  69. CPPUNIT_FAIL("exception must be thrown.");
  70. } catch(RecoverableException& e) {
  71. CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
  72. " Delimiter 'e' not found."),
  73. std::string(e.what()));
  74. }
  75. }
  76. {
  77. // dict, without ending 'e'
  78. try {
  79. bencode::decode("d");
  80. CPPUNIT_FAIL("exception must be thrown.");
  81. } catch(RecoverableException& e) {
  82. CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
  83. " Unexpected EOF in dict context."
  84. " 'e' expected."),
  85. std::string(e.what()));
  86. }
  87. }
  88. {
  89. // list, without ending 'e'
  90. try {
  91. bencode::decode("l");
  92. CPPUNIT_FAIL("exception must be thrown.");
  93. } catch(RecoverableException& e) {
  94. CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
  95. " Unexpected EOF in list context."
  96. " 'e' expected."),
  97. std::string(e.what()));
  98. }
  99. }
  100. {
  101. // string, less than the specified length.
  102. try {
  103. bencode::decode("3:ab");
  104. CPPUNIT_FAIL("exception must be thrown.");
  105. } catch(RecoverableException& e) {
  106. CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
  107. " Expected 3 bytes of data,"
  108. " but only 2 read."),
  109. std::string(e.what()));
  110. }
  111. }
  112. {
  113. // string, but length is invalid
  114. try {
  115. bencode::decode("x:abc");
  116. CPPUNIT_FAIL("exception must be thrown.");
  117. } catch(RecoverableException& e) {
  118. CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
  119. " A positive integer expected"
  120. " but none found."),
  121. std::string(e.what()));
  122. }
  123. }
  124. {
  125. // string with minus length
  126. try {
  127. bencode::decode("-1:a");
  128. CPPUNIT_FAIL("exception must be thrown.");
  129. } catch(RecoverableException& e) {
  130. CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
  131. " A positive integer expected"
  132. " but none found."),
  133. std::string(e.what()));
  134. }
  135. }
  136. {
  137. // empty encoded data
  138. CPPUNIT_ASSERT(bencode::decode("").isNone());
  139. }
  140. {
  141. // ignore trailing garbage at the end of the input.
  142. BDE s = bencode::decode("5:aria2trail");
  143. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
  144. }
  145. {
  146. // Get trailing garbage position
  147. size_t end;
  148. BDE s = bencode::decode("5:aria2trail", end);
  149. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
  150. CPPUNIT_ASSERT_EQUAL((size_t)7, end);
  151. }
  152. }
  153. void BencodeTest::testDecode_overflow()
  154. {
  155. std::string s;
  156. size_t depth = bencode::MAX_STRUCTURE_DEPTH+1;
  157. for(size_t i = 0; i < depth; ++i) {
  158. s += "l";
  159. }
  160. for(size_t i = 0; i < depth; ++i) {
  161. s += "e";
  162. }
  163. try {
  164. bencode::decode(s);
  165. CPPUNIT_FAIL("exception must be thrown.");
  166. } catch(RecoverableException& e) {
  167. // success
  168. }
  169. }
  170. void BencodeTest::testEncode()
  171. {
  172. {
  173. BDE dict = BDE::dict();
  174. dict["name"] = std::string("aria2");
  175. dict["loc"] = 80000;
  176. dict["files"] = BDE::list();
  177. dict["files"] << std::string("aria2c");
  178. dict["attrs"] = BDE::dict();
  179. dict["attrs"]["license"] = std::string("GPL");
  180. CPPUNIT_ASSERT_EQUAL(std::string("d"
  181. "5:attrsd7:license3:GPLe"
  182. "5:filesl6:aria2ce"
  183. "3:loci80000e"
  184. "4:name5:aria2"
  185. "e"),
  186. bencode::encode(dict));
  187. }
  188. }
  189. } // namespace aria2