PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wxWidgets/tests/base64/base64.cpp

https://bitbucket.org/kcwu/xchm-base
C++ | 270 lines | 210 code | 45 blank | 15 comment | 8 complexity | b2bb18343f8b20b8852956b94c4d800c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, AGPL-3.0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/base64/base64.cpp
  3. // Purpose: wxBase64Encode/Decode unit test
  4. // Author: Charles Reimers
  5. // Created: 2007-06-22
  6. // RCS-ID: $Id: base64.cpp 54696 2008-07-18 23:07:23Z VZ $
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #if wxUSE_BASE64
  19. #include "wx/base64.h"
  20. static const char encoded0to255[] =
  21. "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj"
  22. "JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
  23. "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr"
  24. "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P"
  25. "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz"
  26. "tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX"
  27. "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7"
  28. "/P3+/w==";
  29. static void
  30. generatePatternedData(void* buff, size_t len, unsigned char startVal,
  31. unsigned char addVal, unsigned char multVal = 1,
  32. unsigned char xorMask = 0, unsigned char andMask = 255)
  33. {
  34. unsigned char *cbuff = (unsigned char *)buff;
  35. unsigned char curval = startVal;
  36. while(len--)
  37. {
  38. *(cbuff++) = curval;
  39. curval = (((curval + addVal) * multVal) ^ xorMask) & andMask;
  40. }
  41. }
  42. static void generateRandomData(void* buff, size_t len)
  43. {
  44. unsigned char *cbuff = (unsigned char *)buff;
  45. while(len--)
  46. {
  47. *(cbuff++) = (unsigned char)(((rand() * 255) / RAND_MAX));
  48. }
  49. }
  50. static void generateGibberish(void* buff, size_t len)
  51. {
  52. static const unsigned char cb64[] =
  53. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  54. unsigned char *cbuff = (unsigned char *)buff;
  55. while(len--)
  56. {
  57. *(cbuff++) = cb64[((rand() * 64) / RAND_MAX)];
  58. }
  59. }
  60. // --------------------------------------------------------------------------
  61. // test class
  62. // --------------------------------------------------------------------------
  63. class Base64TestCase : public CppUnit::TestCase
  64. {
  65. public:
  66. Base64TestCase() { }
  67. private:
  68. CPPUNIT_TEST_SUITE( Base64TestCase );
  69. CPPUNIT_TEST( EncodeDecodeEmpty );
  70. CPPUNIT_TEST( EncodeDecodeA );
  71. CPPUNIT_TEST( EncodeDecodeAB );
  72. CPPUNIT_TEST( EncodeDecodeABC );
  73. CPPUNIT_TEST( EncodeDecodeABCD );
  74. CPPUNIT_TEST( EncodeDecode0to255 );
  75. CPPUNIT_TEST( EncodeDecodePatternA );
  76. CPPUNIT_TEST( EncodeDecodePatternB );
  77. CPPUNIT_TEST( EncodeDecodePatternC );
  78. CPPUNIT_TEST( EncodeDecodeRandom );
  79. CPPUNIT_TEST( DecodeInvalid );
  80. CPPUNIT_TEST_SUITE_END();
  81. void EncodeDecodeEmpty();
  82. void EncodeDecodeA();
  83. void EncodeDecodeAB();
  84. void EncodeDecodeABC();
  85. void EncodeDecodeABCD();
  86. void EncodeDecode0to255();
  87. void EncodeDecodePatternA();
  88. void EncodeDecodePatternB();
  89. void EncodeDecodePatternC();
  90. void EncodeDecodeRandom();
  91. void DecodeInvalid();
  92. DECLARE_NO_COPY_CLASS(Base64TestCase)
  93. };
  94. // register in the unnamed registry so that these tests are run by default
  95. CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase );
  96. // also include in it's own registry so that these tests can be run alone
  97. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase, "Base64TestCase" );
  98. void Base64TestCase::EncodeDecodeEmpty()
  99. {
  100. char shouldBeEmpty[10];
  101. shouldBeEmpty[0] = '\0';
  102. size_t len = 10;
  103. CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty, len, "", 0) != wxCONV_FAILED);
  104. CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
  105. CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty, len, "") != wxCONV_FAILED);
  106. CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
  107. wxMemoryBuffer bufmt;
  108. wxString resultEmpty = wxBase64Encode(bufmt);
  109. CPPUNIT_ASSERT(resultEmpty.empty());
  110. bufmt = wxBase64Decode(resultEmpty);
  111. CPPUNIT_ASSERT_EQUAL(0, bufmt.GetDataLen());
  112. }
  113. void Base64TestCase::EncodeDecodeA()
  114. {
  115. const wxString str = wxBase64Encode("A", 1);
  116. CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str);
  117. wxMemoryBuffer buf = wxBase64Decode(str);
  118. CPPUNIT_ASSERT_EQUAL(1, buf.GetDataLen());
  119. CPPUNIT_ASSERT_EQUAL('A', *(char *)buf.GetData());
  120. }
  121. void Base64TestCase::EncodeDecodeAB()
  122. {
  123. const wxString str = wxBase64Encode("AB", 2);
  124. CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);
  125. wxMemoryBuffer buf = wxBase64Decode(str);
  126. CPPUNIT_ASSERT_EQUAL(2, buf.GetDataLen());
  127. CPPUNIT_ASSERT_EQUAL('A', buf[0]);
  128. CPPUNIT_ASSERT_EQUAL('B', buf[1]);
  129. }
  130. void Base64TestCase::EncodeDecodeABC()
  131. {
  132. const wxString str = wxBase64Encode("ABC", 3);
  133. CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str);
  134. wxMemoryBuffer buf = wxBase64Decode(str);
  135. CPPUNIT_ASSERT_EQUAL(3, buf.GetDataLen());
  136. CPPUNIT_ASSERT_EQUAL('A', buf[0]);
  137. CPPUNIT_ASSERT_EQUAL('B', buf[1]);
  138. CPPUNIT_ASSERT_EQUAL('C', buf[2]);
  139. }
  140. void Base64TestCase::EncodeDecodeABCD()
  141. {
  142. const wxString str = wxBase64Encode("ABCD", 4);
  143. CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);
  144. wxMemoryBuffer buf = wxBase64Decode(str);
  145. CPPUNIT_ASSERT_EQUAL(4, buf.GetDataLen());
  146. CPPUNIT_ASSERT_EQUAL('A', buf[0]);
  147. CPPUNIT_ASSERT_EQUAL('B', buf[1]);
  148. CPPUNIT_ASSERT_EQUAL('C', buf[2]);
  149. CPPUNIT_ASSERT_EQUAL('D', buf[3]);
  150. }
  151. void Base64TestCase::EncodeDecode0to255()
  152. {
  153. unsigned char buff[256];
  154. generatePatternedData(buff, 256, 0, 1);
  155. wxString str = wxBase64Encode(buff, 256);
  156. wxMemoryBuffer mbuff = wxBase64Decode(str);
  157. CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
  158. mbuff = wxBase64Decode(encoded0to255);
  159. CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
  160. }
  161. void Base64TestCase::EncodeDecodePatternA()
  162. {
  163. unsigned char buff[350];
  164. generatePatternedData(buff, 350, 24, 5, 3);
  165. wxString str = wxBase64Encode(buff, 350);
  166. wxMemoryBuffer mbuff = wxBase64Decode(str);
  167. CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
  168. }
  169. void Base64TestCase::EncodeDecodePatternB()
  170. {
  171. unsigned char buff[350];
  172. generatePatternedData(buff, 350, 0, 1, 1, 0xAA);
  173. wxString str = wxBase64Encode(buff, 350);
  174. wxMemoryBuffer mbuff = wxBase64Decode(str);
  175. CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
  176. }
  177. void Base64TestCase::EncodeDecodePatternC()
  178. {
  179. unsigned char buff[11];
  180. generatePatternedData(buff, 11, 1, 0, 2);
  181. wxString str = wxBase64Encode(buff, 11);
  182. wxMemoryBuffer mbuff = wxBase64Decode(str);
  183. CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
  184. }
  185. void Base64TestCase::EncodeDecodeRandom()
  186. {
  187. size_t size = rand() * 3000 / RAND_MAX + 11;
  188. unsigned char *buff = new unsigned char[size];
  189. generateRandomData(buff, size);
  190. wxString str = wxBase64Encode(buff, size);
  191. wxMemoryBuffer mbuff = wxBase64Decode(str);
  192. CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
  193. generateGibberish(buff, size);
  194. char *buff2 = new char[size];
  195. size_t realsize = size;
  196. CPPUNIT_ASSERT(wxBase64Decode(buff2, realsize, (char *)buff, size));
  197. CPPUNIT_ASSERT(wxBase64Encode(buff2, size, buff2, realsize));
  198. }
  199. void Base64TestCase::DecodeInvalid()
  200. {
  201. size_t rc, posErr;
  202. rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
  203. wxBase64DecodeMode_Strict, &posErr);
  204. CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
  205. CPPUNIT_ASSERT_EQUAL( 3, posErr );
  206. rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
  207. wxBase64DecodeMode_SkipWS, &posErr);
  208. CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
  209. CPPUNIT_ASSERT_EQUAL( 7, posErr );
  210. rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
  211. wxBase64DecodeMode_SkipWS, &posErr);
  212. CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
  213. CPPUNIT_ASSERT_EQUAL( 0, posErr );
  214. posErr = (size_t)-1;
  215. rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
  216. wxBase64DecodeMode_SkipWS, &posErr);
  217. CPPUNIT_ASSERT_EQUAL( 1, rc );
  218. CPPUNIT_ASSERT_EQUAL( -1, posErr );
  219. rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
  220. wxBase64DecodeMode_Relaxed, &posErr);
  221. CPPUNIT_ASSERT_EQUAL( 1, rc );
  222. CPPUNIT_ASSERT_EQUAL( -1, posErr );
  223. CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
  224. }
  225. #endif // wxUSE_BASE64