BencodeTest.cc 4.4 KB

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