Bencode2Test.cc 7.0 KB

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