Base64Test.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "base64.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. namespace aria2 {
  4. class Base64Test:public CppUnit::TestFixture {
  5. CPPUNIT_TEST_SUITE(Base64Test);
  6. CPPUNIT_TEST(testEncode);
  7. CPPUNIT_TEST(testDecode);
  8. CPPUNIT_TEST(testLongString);
  9. CPPUNIT_TEST_SUITE_END();
  10. private:
  11. public:
  12. void setUp() {
  13. }
  14. void testEncode();
  15. void testDecode();
  16. void testLongString();
  17. };
  18. CPPUNIT_TEST_SUITE_REGISTRATION( Base64Test );
  19. void Base64Test::testEncode() {
  20. std::string s = "Hello World!";
  21. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybGQh"),
  22. base64::encode(s.begin(), s.end()));
  23. s = "Hello World";
  24. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybGQ="),
  25. base64::encode(s.begin(), s.end()));
  26. s = "Hello Worl";
  27. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybA=="),
  28. base64::encode(s.begin(), s.end()));
  29. s = "Man";
  30. CPPUNIT_ASSERT_EQUAL(std::string("TWFu"), base64::encode(s.begin(), s.end()));
  31. s = "M";
  32. CPPUNIT_ASSERT_EQUAL(std::string("TQ=="), base64::encode(s.begin(), s.end()));
  33. s = "";
  34. CPPUNIT_ASSERT_EQUAL(std::string(), base64::encode(s.begin(), s.end()));
  35. s.assign(1, (char)-1);
  36. base64::encode(s.begin(), s.end());
  37. CPPUNIT_ASSERT_EQUAL(std::string("/w=="), base64::encode(s.begin(), s.end()));
  38. s.assign(2, (char)-1);
  39. base64::encode(s.begin(), s.end());
  40. CPPUNIT_ASSERT_EQUAL(std::string("//8="), base64::encode(s.begin(), s.end()));
  41. s.assign(3, (char)-1);
  42. base64::encode(s.begin(), s.end());
  43. CPPUNIT_ASSERT_EQUAL(std::string("////"), base64::encode(s.begin(), s.end()));
  44. }
  45. void Base64Test::testDecode()
  46. {
  47. std::string s = "SGVsbG8gV29ybGQh";
  48. CPPUNIT_ASSERT_EQUAL(std::string("Hello World!"),
  49. base64::decode(s.begin(), s.end()));
  50. s = "SGVsbG8gV29ybGQ=";
  51. CPPUNIT_ASSERT_EQUAL(std::string("Hello World"),
  52. base64::decode(s.begin(), s.end()));
  53. s = "SGVsbG8gV29ybA==";
  54. CPPUNIT_ASSERT_EQUAL(std::string("Hello Worl"),
  55. base64::decode(s.begin(), s.end()));
  56. s = "TWFu";
  57. CPPUNIT_ASSERT_EQUAL(std::string("Man"), base64::decode(s.begin(), s.end()));
  58. s = "TWFu\n";
  59. CPPUNIT_ASSERT_EQUAL(std::string("Man"), base64::decode(s.begin(), s.end()));
  60. s = "TQ==";
  61. CPPUNIT_ASSERT_EQUAL(std::string("M"), base64::decode(s.begin(), s.end()));
  62. s = "";
  63. CPPUNIT_ASSERT_EQUAL(std::string(""), base64::decode(s.begin(), s.end()));
  64. s = "SGVsbG8\ngV2*9ybGQ=";
  65. CPPUNIT_ASSERT_EQUAL(std::string("Hello World"),
  66. base64::decode(s.begin(), s.end()));
  67. s = "SGVsbG8\ngV2*9ybGQ";
  68. CPPUNIT_ASSERT_EQUAL(std::string(""), base64::decode(s.begin(), s.end()));
  69. s = "/w==";
  70. CPPUNIT_ASSERT_EQUAL(std::string(1, -1), base64::decode(s.begin(), s.end()));
  71. }
  72. void Base64Test::testLongString()
  73. {
  74. std::string s =
  75. "LyogPCEtLSBjb3B5cmlnaHQgKi8KLyoKICogYXJpYTIgLSBUaGUgaGlnaCBzcGVlZCBkb3dubG9h"
  76. "ZCB1dGlsaXR5CiAqCiAqIENvcHlyaWdodCAoQykgMjAwNiBUYXRzdWhpcm8gVHN1amlrYXdhCiAq"
  77. "CiAqIF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h0IC0tPiAq"
  103. "Lwo=";
  104. std::string d =
  105. "/* <!-- copyright */\n"
  106. "/*\n"
  107. " * aria2 - The high speed download utility\n"
  108. " *\n"
  109. " * Copyright (C) 2006 Tatsuhiro Tsujikawa\n"
  110. " *\n"
  111. " * This program is free software; you can redistribute it and/or modify\n"
  112. " * it under the terms of the GNU General Public License as published by\n"
  113. " * the Free Software Foundation; either version 2 of the License, or\n"
  114. " * (at your option) any later version.\n"
  115. " *\n"
  116. " * This program is distributed in the hope that it will be useful,\n"
  117. " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  118. " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  119. " * GNU General Public License for more details.\n"
  120. " *\n"
  121. " * You should have received a copy of the GNU General Public License\n"
  122. " * along with this program; if not, write to the Free Software\n"
  123. " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
  124. " *\n"
  125. " * In addition, as a special exception, the copyright holders give\n"
  126. " * permission to link the code of portions of this program with the\n"
  127. " * OpenSSL library under certain conditions as described in each\n"
  128. " * individual source file, and distribute linked combinations\n"
  129. " * including the two.\n"
  130. " * You must obey the GNU General Public License in all respects\n"
  131. " * for all of the code used other than OpenSSL. If you modify\n"
  132. " * file(s) with this exception, you may extend this exception to your\n"
  133. " * version of the file(s), but you are not obligated to do so. If you\n"
  134. " * do not wish to do so, delete this exception statement from your\n"
  135. " * version. If you delete this exception statement from all source\n"
  136. " * files in the program, then also delete it here.\n"
  137. " */\n"
  138. "/* copyright --> */\n";
  139. CPPUNIT_ASSERT_EQUAL(d, base64::decode(s.begin(), s.end()));
  140. CPPUNIT_ASSERT_EQUAL(s, base64::encode(d.begin(), d.end()));
  141. }
  142. } // namespace aria2