BencodeTest.cc 5.7 KB

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