Bencode2Test.cc 6.2 KB

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