1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/base64/base64.cpp
3 // Purpose: wxBase64Encode/Decode unit test
4 // Author: Charles Reimers
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
25 #include "wx/base64.h"
27 static const char encoded0to255
[] =
28 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj"
29 "JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
30 "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr"
31 "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P"
32 "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz"
33 "tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX"
34 "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7"
38 generatePatternedData(void* buff
, size_t len
, unsigned char startVal
,
39 unsigned char addVal
, unsigned char multVal
= 1,
40 unsigned char xorMask
= 0, unsigned char andMask
= 255)
42 unsigned char *cbuff
= (unsigned char *)buff
;
43 unsigned char curval
= startVal
;
47 curval
= (((curval
+ addVal
) * multVal
) ^ xorMask
) & andMask
;
51 static void generateRandomData(void* buff
, size_t len
)
53 unsigned char *cbuff
= (unsigned char *)buff
;
56 *(cbuff
++) = (unsigned char)(((rand() * 255) / RAND_MAX
));
60 static void generateGibberish(void* buff
, size_t len
)
62 static const unsigned char cb64
[] =
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
65 unsigned char *cbuff
= (unsigned char *)buff
;
68 *(cbuff
++) = cb64
[((rand() * 64) / RAND_MAX
)];
72 // --------------------------------------------------------------------------
74 // --------------------------------------------------------------------------
76 class Base64TestCase
: public CppUnit::TestCase
82 CPPUNIT_TEST_SUITE( Base64TestCase
);
83 CPPUNIT_TEST( EncodeDecodeEmpty
);
84 CPPUNIT_TEST( EncodeDecodeA
);
85 CPPUNIT_TEST( EncodeDecodeAB
);
86 CPPUNIT_TEST( EncodeDecodeABC
);
87 CPPUNIT_TEST( EncodeDecodeABCD
);
88 CPPUNIT_TEST( EncodeDecode0to255
);
89 CPPUNIT_TEST( EncodeDecodePatternA
);
90 CPPUNIT_TEST( EncodeDecodePatternB
);
91 CPPUNIT_TEST( EncodeDecodePatternC
);
92 CPPUNIT_TEST( EncodeDecodeRandom
);
93 CPPUNIT_TEST( DecodeInvalid
);
94 CPPUNIT_TEST_SUITE_END();
96 void EncodeDecodeEmpty();
98 void EncodeDecodeAB();
99 void EncodeDecodeABC();
100 void EncodeDecodeABCD();
101 void EncodeDecode0to255();
102 void EncodeDecodePatternA();
103 void EncodeDecodePatternB();
104 void EncodeDecodePatternC();
105 void EncodeDecodeRandom();
106 void DecodeInvalid();
108 DECLARE_NO_COPY_CLASS(Base64TestCase
)
111 // register in the unnamed registry so that these tests are run by default
112 CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase
);
114 // also include in it's own registry so that these tests can be run alone
115 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase
, "Base64TestCase" );
117 void Base64TestCase::EncodeDecodeEmpty()
119 char shouldBeEmpty
[10];
120 shouldBeEmpty
[0] = '\0';
123 CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty
, len
, "", 0) != wxCONV_FAILED
);
124 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty
[0]);
126 CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty
, len
, "") != wxCONV_FAILED
);
127 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty
[0]);
129 wxMemoryBuffer bufmt
;
130 wxString resultEmpty
= wxBase64Encode(bufmt
);
131 CPPUNIT_ASSERT(resultEmpty
.empty());
133 bufmt
= wxBase64Decode(resultEmpty
);
134 CPPUNIT_ASSERT_EQUAL(0, bufmt
.GetDataLen());
137 void Base64TestCase::EncodeDecodeA()
139 const wxString str
= wxBase64Encode("A", 1);
140 CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str
);
142 wxMemoryBuffer buf
= wxBase64Decode(str
);
143 CPPUNIT_ASSERT_EQUAL(1, buf
.GetDataLen());
144 CPPUNIT_ASSERT_EQUAL('A', *(char *)buf
.GetData());
147 void Base64TestCase::EncodeDecodeAB()
149 const wxString str
= wxBase64Encode("AB", 2);
150 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str
);
152 wxMemoryBuffer buf
= wxBase64Decode(str
);
153 CPPUNIT_ASSERT_EQUAL(2, buf
.GetDataLen());
154 CPPUNIT_ASSERT_EQUAL('A', buf
[0]);
155 CPPUNIT_ASSERT_EQUAL('B', buf
[1]);
158 void Base64TestCase::EncodeDecodeABC()
160 const wxString str
= wxBase64Encode("ABC", 3);
161 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str
);
163 wxMemoryBuffer buf
= wxBase64Decode(str
);
164 CPPUNIT_ASSERT_EQUAL(3, buf
.GetDataLen());
165 CPPUNIT_ASSERT_EQUAL('A', buf
[0]);
166 CPPUNIT_ASSERT_EQUAL('B', buf
[1]);
167 CPPUNIT_ASSERT_EQUAL('C', buf
[2]);
170 void Base64TestCase::EncodeDecodeABCD()
172 const wxString str
= wxBase64Encode("ABCD", 4);
173 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str
);
175 wxMemoryBuffer buf
= wxBase64Decode(str
);
176 CPPUNIT_ASSERT_EQUAL(4, buf
.GetDataLen());
177 CPPUNIT_ASSERT_EQUAL('A', buf
[0]);
178 CPPUNIT_ASSERT_EQUAL('B', buf
[1]);
179 CPPUNIT_ASSERT_EQUAL('C', buf
[2]);
180 CPPUNIT_ASSERT_EQUAL('D', buf
[3]);
183 void Base64TestCase::EncodeDecode0to255()
185 unsigned char buff
[256];
186 generatePatternedData(buff
, 256, 0, 1);
187 wxString str
= wxBase64Encode(buff
, 256);
188 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
189 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
191 mbuff
= wxBase64Decode(encoded0to255
);
192 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
195 void Base64TestCase::EncodeDecodePatternA()
197 unsigned char buff
[350];
198 generatePatternedData(buff
, 350, 24, 5, 3);
199 wxString str
= wxBase64Encode(buff
, 350);
200 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
201 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
204 void Base64TestCase::EncodeDecodePatternB()
206 unsigned char buff
[350];
207 generatePatternedData(buff
, 350, 0, 1, 1, 0xAA);
208 wxString str
= wxBase64Encode(buff
, 350);
209 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
210 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
213 void Base64TestCase::EncodeDecodePatternC()
215 unsigned char buff
[11];
216 generatePatternedData(buff
, 11, 1, 0, 2);
217 wxString str
= wxBase64Encode(buff
, 11);
218 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
219 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
222 void Base64TestCase::EncodeDecodeRandom()
224 size_t size
= rand() * 3000 / RAND_MAX
+ 11;
225 unsigned char *buff
= new unsigned char[size
];
226 generateRandomData(buff
, size
);
227 wxString str
= wxBase64Encode(buff
, size
);
228 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
229 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
231 generateGibberish(buff
, size
);
232 char *buff2
= new char[size
];
233 size_t realsize
= size
;
234 CPPUNIT_ASSERT(wxBase64Decode(buff2
, realsize
, (char *)buff
, size
));
235 CPPUNIT_ASSERT(wxBase64Encode(buff2
, size
, buff2
, realsize
));
238 void Base64TestCase::DecodeInvalid()
241 rc
= wxBase64Decode(NULL
, 0, "one two!", wxNO_LEN
,
242 wxBase64DecodeMode_Strict
, &posErr
);
243 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, rc
);
244 CPPUNIT_ASSERT_EQUAL( 3, posErr
);
246 rc
= wxBase64Decode(NULL
, 0, "one two!", wxNO_LEN
,
247 wxBase64DecodeMode_SkipWS
, &posErr
);
248 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, rc
);
249 CPPUNIT_ASSERT_EQUAL( 7, posErr
);
251 rc
= wxBase64Decode(NULL
, 0, "? QQ==", wxNO_LEN
,
252 wxBase64DecodeMode_SkipWS
, &posErr
);
253 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, rc
);
254 CPPUNIT_ASSERT_EQUAL( 0, posErr
);
257 rc
= wxBase64Decode(NULL
, 0, " QQ==", wxNO_LEN
,
258 wxBase64DecodeMode_SkipWS
, &posErr
);
259 CPPUNIT_ASSERT_EQUAL( 1, rc
);
260 CPPUNIT_ASSERT_EQUAL( -1, posErr
);
262 rc
= wxBase64Decode(NULL
, 0, "? QQ==", wxNO_LEN
,
263 wxBase64DecodeMode_Relaxed
, &posErr
);
264 CPPUNIT_ASSERT_EQUAL( 1, rc
);
265 CPPUNIT_ASSERT_EQUAL( -1, posErr
);
267 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
270 #endif // wxUSE_BASE64