Base64Test.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(testEncode_string);
  9. CPPUNIT_TEST(testDecode_string);
  10. CPPUNIT_TEST(testLongString);
  11. CPPUNIT_TEST_SUITE_END();
  12. private:
  13. public:
  14. void setUp() {
  15. }
  16. void testEncode();
  17. void testDecode();
  18. void testEncode_string();
  19. void testDecode_string();
  20. void testLongString();
  21. };
  22. CPPUNIT_TEST_SUITE_REGISTRATION( Base64Test );
  23. void Base64Test::testEncode() {
  24. unsigned char* buf = 0;
  25. size_t len;
  26. std::string s1 = "Hello World!";
  27. Base64::encode(buf, len, s1.c_str(), s1.size());
  28. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybGQh"), std::string(&buf[0], &buf[len]));
  29. delete [] buf;
  30. std::string s2 = "Hello World";
  31. Base64::encode(buf, len, s2.c_str(), s2.size());
  32. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybGQ="), std::string(&buf[0], &buf[len]));
  33. delete [] buf;
  34. std::string s3 = "Hello Worl";
  35. Base64::encode(buf, len, s3.c_str(), s3.size());
  36. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybA=="), std::string(&buf[0], &buf[len]));
  37. delete [] buf;
  38. std::string s4 = "Man";
  39. Base64::encode(buf, len, s4.c_str(), s4.size());
  40. CPPUNIT_ASSERT_EQUAL(std::string("TWFu"), std::string(&buf[0], &buf[len]));
  41. delete [] buf;
  42. std::string s5 = "M";
  43. Base64::encode(buf, len, s5.c_str(), s5.size());
  44. CPPUNIT_ASSERT_EQUAL(std::string("TQ=="), std::string(&buf[0], &buf[len]));
  45. delete [] buf;
  46. buf = 0;
  47. std::string s6 = "";
  48. Base64::encode(buf, len, s6.c_str(), s6.size());
  49. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(&buf[0], &buf[len]));
  50. CPPUNIT_ASSERT_EQUAL((size_t)0, len);
  51. CPPUNIT_ASSERT(0 == buf);
  52. {
  53. const char temp[] = { -1 };
  54. Base64::encode(buf, len, temp, 1);
  55. CPPUNIT_ASSERT_EQUAL(std::string("/w=="), std::string(&buf[0], &buf[len]));
  56. delete [] buf;
  57. }
  58. }
  59. void Base64Test::testEncode_string()
  60. {
  61. std::string s1 = "Hello World!";
  62. CPPUNIT_ASSERT_EQUAL(std::string("SGVsbG8gV29ybGQh"), Base64::encode(s1));
  63. std::string s2 = "";
  64. CPPUNIT_ASSERT_EQUAL(std::string(""), Base64::encode(s2));
  65. }
  66. void Base64Test::testDecode()
  67. {
  68. unsigned char* buf;
  69. size_t len;
  70. std::string s1 = "SGVsbG8gV29ybGQh";
  71. Base64::decode(buf, len, s1.c_str(), s1.size());
  72. CPPUNIT_ASSERT_EQUAL(std::string("Hello World!"), std::string(&buf[0], &buf[len]));
  73. delete [] buf;
  74. std::string s2 = "SGVsbG8gV29ybGQ=";
  75. Base64::decode(buf, len, s2.c_str(), s2.size());
  76. CPPUNIT_ASSERT_EQUAL(std::string("Hello World"), std::string(&buf[0], &buf[len]));
  77. delete [] buf;
  78. std::string s3 = "SGVsbG8gV29ybA==";
  79. Base64::decode(buf, len, s3.c_str(), s3.size());
  80. CPPUNIT_ASSERT_EQUAL(std::string("Hello Worl"), std::string(&buf[0], &buf[len]));
  81. delete [] buf;
  82. std::string s4 = "TWFu";
  83. Base64::decode(buf, len, s4.c_str(), s4.size());
  84. CPPUNIT_ASSERT_EQUAL(std::string("Man"), std::string(&buf[0], &buf[len]));
  85. delete [] buf;
  86. std::string s5 = "TQ==";
  87. Base64::decode(buf, len, s5.c_str(), s5.size());
  88. CPPUNIT_ASSERT_EQUAL(std::string("M"), std::string(&buf[0], &buf[len]));
  89. delete [] buf;
  90. buf = 0;
  91. std::string s6 = "";
  92. Base64::decode(buf, len, s6.c_str(), s6.size());
  93. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(&buf[0], &buf[len]));
  94. CPPUNIT_ASSERT_EQUAL((size_t)0, len);
  95. CPPUNIT_ASSERT(!buf);
  96. std::string s7 = "SGVsbG8\ngV2*9ybGQ=";
  97. Base64::decode(buf, len, s7.c_str(), s7.size());
  98. CPPUNIT_ASSERT_EQUAL(std::string("Hello World"), std::string(&buf[0], &buf[len]));
  99. delete [] buf;
  100. buf = 0;
  101. std::string s8 = "SGVsbG8\ngV2*9ybGQ";
  102. Base64::decode(buf, len, s8.c_str(), s8.size());
  103. CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(&buf[0], &buf[len]));
  104. CPPUNIT_ASSERT_EQUAL((size_t)0, len);
  105. CPPUNIT_ASSERT(!buf);
  106. {
  107. std::string s = "/w==";
  108. Base64::decode(buf, len, s.c_str(), s.size());
  109. CPPUNIT_ASSERT_EQUAL((unsigned char)-1, buf[0]);
  110. delete [] buf;
  111. }
  112. }
  113. void Base64Test::testDecode_string()
  114. {
  115. std::string s1 = "SGVsbG8gV29ybGQh";
  116. CPPUNIT_ASSERT_EQUAL(std::string("Hello World!"), Base64::decode(s1));
  117. std::string s2 = "";
  118. CPPUNIT_ASSERT_EQUAL(std::string(""), Base64::decode(s2));
  119. }
  120. void Base64Test::testLongString()
  121. {
  122. std::string s =
  123. "LyogPCEtLSBjb3B5cmlnaHQgKi8KLyoKICogYXJpYTIgLSBUaGUgaGlnaCBzcGVlZCBkb3dubG9h"
  124. "ZCB1dGlsaXR5CiAqCiAqIENvcHlyaWdodCAoQykgMjAwN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NvIGRlbGV0ZSBpdCBoZXJlLgogKi8KLyogY29weXJpZ2h0IC0tPiAq"
  151. "Lwo=";
  152. std::string d =
  153. "/* <!-- copyright */\n"
  154. "/*\n"
  155. " * aria2 - The high speed download utility\n"
  156. " *\n"
  157. " * Copyright (C) 2006 Tatsuhiro Tsujikawa\n"
  158. " *\n"
  159. " * This program is free software; you can redistribute it and/or modify\n"
  160. " * it under the terms of the GNU General Public License as published by\n"
  161. " * the Free Software Foundation; either version 2 of the License, or\n"
  162. " * (at your option) any later version.\n"
  163. " *\n"
  164. " * This program is distributed in the hope that it will be useful,\n"
  165. " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  166. " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  167. " * GNU General Public License for more details.\n"
  168. " *\n"
  169. " * You should have received a copy of the GNU General Public License\n"
  170. " * along with this program; if not, write to the Free Software\n"
  171. " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
  172. " *\n"
  173. " * In addition, as a special exception, the copyright holders give\n"
  174. " * permission to link the code of portions of this program with the\n"
  175. " * OpenSSL library under certain conditions as described in each\n"
  176. " * individual source file, and distribute linked combinations\n"
  177. " * including the two.\n"
  178. " * You must obey the GNU General Public License in all respects\n"
  179. " * for all of the code used other than OpenSSL. If you modify\n"
  180. " * file(s) with this exception, you may extend this exception to your\n"
  181. " * version of the file(s), but you are not obligated to do so. If you\n"
  182. " * do not wish to do so, delete this exception statement from your\n"
  183. " * version. If you delete this exception statement from all source\n"
  184. " * files in the program, then also delete it here.\n"
  185. " */\n"
  186. "/* copyright --> */\n";
  187. CPPUNIT_ASSERT_EQUAL(d, Base64::decode(s));
  188. CPPUNIT_ASSERT_EQUAL(s, Base64::encode(d));
  189. }
  190. } // namespace aria2