Base64Test.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. void testEncode();
  14. void testDecode();
  15. void testLongString();
  16. };
  17. CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test);
  18. void Base64Test::testEncode()
  19. {
  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 = "LyogPCEtLSBjb3B5cmlnaHQgKi8KLyoKICogYXJpYTIgLSBUaGUgaGlnaCBz"
  75. "cGVlZCBkb3dubG9h"
  76. "ZCB1dGlsaXR5CiAqCiAqIENvcHlyaWdodCAoQykgMjAwNiBUYXRzdWhp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sIDUxIEZyYW5rbGluIFN0"
  101. "cmVldCwgRmlmdGgg"
  102. "Rmxvb3IsIEJvc3RvbiwgTUEgIDAyMTEwLTEzMDEgIFVTQQogKgogKiBJbiBh"
  103. "ZGRpdGlvbiwgYXMg"
  104. "YSBzcGVjaWFsIGV4Y2VwdGlvbiwgdGhlIGNvcHlyaWdodCBob2xkZXJzIGdp"
  105. "dmUKICogcGVybWlz"
  106. "c2lvbiB0byBsaW5rIHRoZSBjb2RlIG9mIHBvcnRpb25zIG9mIHRoaXMgcHJv"
  107. "Z3JhbSB3aXRoIHRo"
  108. "ZQogKiBPcGVuU1NMIGxpYnJhcnkgdW5kZXIgY2VydGFpbiBjb25kaXRpb25z"
  109. "IGFzIGRlc2NyaWJl"
  110. "ZCBpbiBlYWNoCiAqIGluZGl2aWR1YWwgc291cmNlIGZpbGUsIGFuZCBkaXN0"
  111. "cmlidXRlIGxpbmtl"
  112. "ZCBjb21iaW5hdGlvbnMKICogaW5jbHVkaW5nIHRoZSB0d28uCiAqIFlvdSBt"
  113. "dXN0IG9iZXkgdGhl"
  114. "IEdOVSBHZW5lcmFsIFB1YmxpYyBMaWNlbnNlIGluIGFsbCByZXNwZWN0cwog"
  115. "KiBmb3IgYWxsIG9m"
  116. "IHRoZSBjb2RlIHVzZWQgb3RoZXIgdGhhbiBPcGVuU1NMLiAgSWYgeW91IG1v"
  117. "ZGlmeQogKiBmaWxl"
  118. "KHMpIHdpdGggdGhpcyBleGNlcHRpb24sIHlvdSBtYXkgZXh0ZW5kIHRoaXMg"
  119. "ZXhjZXB0aW9uIHRv"
  120. "IHlvdXIKICogdmVyc2lvbiBvZiB0aGUgZmlsZShzKSwgYnV0IHlvdSBhcmUg"
  121. "bm90IG9ibGlnYXRl"
  122. "ZCB0byBkbyBzby4gIElmIHlvdQogKiBkbyBub3Qgd2lzaCB0byBkbyBzbywg"
  123. "ZGVsZXRlIHRoaXMg"
  124. "ZXhjZXB0aW9uIHN0YXRlbWVudCBmcm9tIHlvdXIKICogdmVyc2lvbi4gIElm"
  125. "IHlvdSBkZWxldGUg"
  126. "dGhpcyBleGNlcHRpb24gc3RhdGVtZW50IGZyb20gYWxsIHNvdXJjZQogKiBm"
  127. "aWxlcyBpbiB0aGUg"
  128. "cHJvZ3JhbSwgdGhlbiBhbHNvIGRlbGV0ZSBpdCBoZXJlLgogKi8KLyogY29w"
  129. "eXJpZ2h0IC0tPiAq"
  130. "Lwo=";
  131. std::string d =
  132. "/* <!-- copyright */\n"
  133. "/*\n"
  134. " * aria2 - The high speed download utility\n"
  135. " *\n"
  136. " * Copyright (C) 2006 Tatsuhiro Tsujikawa\n"
  137. " *\n"
  138. " * This program is free software; you can redistribute it and/or "
  139. "modify\n"
  140. " * it under the terms of the GNU General Public License as published "
  141. "by\n"
  142. " * the Free Software Foundation; either version 2 of the License, or\n"
  143. " * (at your option) any later version.\n"
  144. " *\n"
  145. " * This program is distributed in the hope that it will be useful,\n"
  146. " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  147. " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  148. " * GNU General Public License for more details.\n"
  149. " *\n"
  150. " * You should have received a copy of the GNU General Public License\n"
  151. " * along with this program; if not, write to the Free Software\n"
  152. " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA "
  153. "02110-1301 USA\n"
  154. " *\n"
  155. " * In addition, as a special exception, the copyright holders give\n"
  156. " * permission to link the code of portions of this program with the\n"
  157. " * OpenSSL library under certain conditions as described in each\n"
  158. " * individual source file, and distribute linked combinations\n"
  159. " * including the two.\n"
  160. " * You must obey the GNU General Public License in all respects\n"
  161. " * for all of the code used other than OpenSSL. If you modify\n"
  162. " * file(s) with this exception, you may extend this exception to your\n"
  163. " * version of the file(s), but you are not obligated to do so. If you\n"
  164. " * do not wish to do so, delete this exception statement from your\n"
  165. " * version. If you delete this exception statement from all source\n"
  166. " * files in the program, then also delete it here.\n"
  167. " */\n"
  168. "/* copyright --> */\n";
  169. CPPUNIT_ASSERT_EQUAL(d, base64::decode(s.begin(), s.end()));
  170. CPPUNIT_ASSERT_EQUAL(s, base64::encode(d.begin(), d.end()));
  171. }
  172. } // namespace aria2