BencodeTest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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("Delimiter 'e' not found."),
  68. std::string(e.what()));
  69. }
  70. }
  71. {
  72. // dict, without ending 'e'
  73. try {
  74. bencode::decode("d");
  75. CPPUNIT_FAIL("exception must be thrown.");
  76. } catch(RecoverableException& e) {
  77. CPPUNIT_ASSERT_EQUAL(std::string("Unexpected EOF in dict context."
  78. " 'e' expected."),
  79. std::string(e.what()));
  80. }
  81. }
  82. {
  83. // list, without ending 'e'
  84. try {
  85. bencode::decode("l");
  86. CPPUNIT_FAIL("exception must be thrown.");
  87. } catch(RecoverableException& e) {
  88. CPPUNIT_ASSERT_EQUAL(std::string("Unexpected EOF in list context."
  89. " 'e' expected."),
  90. std::string(e.what()));
  91. }
  92. }
  93. {
  94. // string, less than the specified length.
  95. try {
  96. bencode::decode("3:ab");
  97. CPPUNIT_FAIL("exception must be thrown.");
  98. } catch(RecoverableException& e) {
  99. CPPUNIT_ASSERT_EQUAL(std::string("Expected 3 bytes of data,"
  100. " but only 2 read."),
  101. std::string(e.what()));
  102. }
  103. }
  104. {
  105. // string, but length is invalid
  106. try {
  107. bencode::decode("x:abc");
  108. CPPUNIT_FAIL("exception must be thrown.");
  109. } catch(RecoverableException& e) {
  110. CPPUNIT_ASSERT_EQUAL(std::string("A positive integer expected"
  111. " but none found."),
  112. std::string(e.what()));
  113. }
  114. }
  115. {
  116. // string with minus length
  117. try {
  118. bencode::decode("-1:a");
  119. CPPUNIT_FAIL("exception must be thrown.");
  120. } catch(RecoverableException& e) {
  121. CPPUNIT_ASSERT_EQUAL(std::string("A positive integer expected"
  122. " but none found."),
  123. std::string(e.what()));
  124. }
  125. }
  126. {
  127. // empty encoded data
  128. CPPUNIT_ASSERT(bencode::decode("").isNone());
  129. }
  130. {
  131. // ignore trailing garbage at the end of the input.
  132. BDE s = bencode::decode("5:aria2trail");
  133. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
  134. }
  135. }
  136. void BencodeTest::testEncode()
  137. {
  138. {
  139. BDE dict = BDE::dict();
  140. dict["name"] = std::string("aria2");
  141. dict["loc"] = 80000;
  142. dict["files"] = BDE::list();
  143. dict["files"] << std::string("aria2c");
  144. dict["attrs"] = BDE::dict();
  145. dict["attrs"]["license"] = std::string("GPL");
  146. CPPUNIT_ASSERT_EQUAL(std::string("d"
  147. "5:attrsd7:license3:GPLe"
  148. "5:filesl6:aria2ce"
  149. "3:loci80000e"
  150. "4:name5:aria2"
  151. "e"),
  152. bencode::encode(dict));
  153. }
  154. }
  155. } // namespace aria2