Base64Test.cc 8.2 KB

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