1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/base64/base64.cpp
3 // Purpose: wxBase64Encode/Decode unit test
4 // Author: Charles Reimers
6 ///////////////////////////////////////////////////////////////////////////////
8 // ----------------------------------------------------------------------------
10 // ----------------------------------------------------------------------------
24 #include "wx/base64.h"
26 static const char encoded0to255
[] =
27 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj"
28 "JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
29 "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr"
30 "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P"
31 "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz"
32 "tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX"
33 "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7"
37 generatePatternedData(void* buff
, size_t len
, unsigned char startVal
,
38 unsigned char addVal
, unsigned char multVal
= 1,
39 unsigned char xorMask
= 0, unsigned char andMask
= 255)
41 unsigned char *cbuff
= (unsigned char *)buff
;
42 unsigned char curval
= startVal
;
46 curval
= (((curval
+ addVal
) * multVal
) ^ xorMask
) & andMask
;
50 static void generateRandomData(void* buff
, size_t len
)
52 unsigned char *cbuff
= (unsigned char *)buff
;
55 *(cbuff
++) = (unsigned char)(((rand() * 255) / RAND_MAX
));
59 static void generateGibberish(void* buff
, size_t len
)
61 static const unsigned char cb64
[] =
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64 unsigned char *cbuff
= (unsigned char *)buff
;
67 *(cbuff
++) = cb64
[((rand() * 64) / RAND_MAX
)];
71 // --------------------------------------------------------------------------
73 // --------------------------------------------------------------------------
75 class Base64TestCase
: public CppUnit::TestCase
81 CPPUNIT_TEST_SUITE( Base64TestCase
);
82 CPPUNIT_TEST( EncodeDecodeEmpty
);
83 CPPUNIT_TEST( EncodeDecodeA
);
84 CPPUNIT_TEST( EncodeDecodeAB
);
85 CPPUNIT_TEST( EncodeDecodeABC
);
86 CPPUNIT_TEST( EncodeDecodeABCD
);
87 CPPUNIT_TEST( EncodeDecode0to255
);
88 CPPUNIT_TEST( EncodeDecodePatternA
);
89 CPPUNIT_TEST( EncodeDecodePatternB
);
90 CPPUNIT_TEST( EncodeDecodePatternC
);
91 CPPUNIT_TEST( EncodeDecodeRandom
);
92 CPPUNIT_TEST( DecodeInvalid
);
93 CPPUNIT_TEST_SUITE_END();
95 void EncodeDecodeEmpty();
97 void EncodeDecodeAB();
98 void EncodeDecodeABC();
99 void EncodeDecodeABCD();
100 void EncodeDecode0to255();
101 void EncodeDecodePatternA();
102 void EncodeDecodePatternB();
103 void EncodeDecodePatternC();
104 void EncodeDecodeRandom();
105 void DecodeInvalid();
107 DECLARE_NO_COPY_CLASS(Base64TestCase
)
110 // register in the unnamed registry so that these tests are run by default
111 CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase
);
113 // also include in its own registry so that these tests can be run alone
114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase
, "Base64TestCase" );
116 void Base64TestCase::EncodeDecodeEmpty()
118 char shouldBeEmpty
[10];
119 shouldBeEmpty
[0] = '\0';
122 CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty
, len
, "", 0) != wxCONV_FAILED
);
123 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty
[0]);
125 CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty
, len
, "") != wxCONV_FAILED
);
126 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty
[0]);
128 wxMemoryBuffer bufmt
;
129 wxString resultEmpty
= wxBase64Encode(bufmt
);
130 CPPUNIT_ASSERT(resultEmpty
.empty());
132 bufmt
= wxBase64Decode(resultEmpty
);
133 CPPUNIT_ASSERT_EQUAL(0, bufmt
.GetDataLen());
136 void Base64TestCase::EncodeDecodeA()
138 const wxString str
= wxBase64Encode("A", 1);
139 CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str
);
141 wxMemoryBuffer buf
= wxBase64Decode(str
);
142 CPPUNIT_ASSERT_EQUAL(1, buf
.GetDataLen());
143 CPPUNIT_ASSERT_EQUAL('A', *(char *)buf
.GetData());
146 memset(cbuf
, (char)-1, sizeof(cbuf
));
147 CPPUNIT_ASSERT_EQUAL( 1, wxBase64Decode(cbuf
, 1, str
) );
148 CPPUNIT_ASSERT_EQUAL( 'A', cbuf
[0] );
149 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[1] );
150 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[2] );
153 void Base64TestCase::EncodeDecodeAB()
155 const wxString str
= wxBase64Encode("AB", 2);
156 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str
);
158 wxMemoryBuffer buf
= wxBase64Decode(str
);
159 CPPUNIT_ASSERT_EQUAL(2, buf
.GetDataLen());
160 CPPUNIT_ASSERT_EQUAL('A', buf
[0]);
161 CPPUNIT_ASSERT_EQUAL('B', buf
[1]);
164 memset(cbuf
, (char)-1, sizeof(cbuf
));
165 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, wxBase64Decode(cbuf
, 1, str
) );
166 CPPUNIT_ASSERT_EQUAL( 2, wxBase64Decode(cbuf
, 2, str
) );
167 CPPUNIT_ASSERT_EQUAL( 'A', cbuf
[0] );
168 CPPUNIT_ASSERT_EQUAL( 'B', cbuf
[1] );
169 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[2] );
170 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[3] );
173 void Base64TestCase::EncodeDecodeABC()
175 const wxString str
= wxBase64Encode("ABC", 3);
176 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str
);
178 wxMemoryBuffer buf
= wxBase64Decode(str
);
179 CPPUNIT_ASSERT_EQUAL(3, buf
.GetDataLen());
180 CPPUNIT_ASSERT_EQUAL('A', buf
[0]);
181 CPPUNIT_ASSERT_EQUAL('B', buf
[1]);
182 CPPUNIT_ASSERT_EQUAL('C', buf
[2]);
185 memset(cbuf
, (char)-1, sizeof(cbuf
));
186 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, wxBase64Decode(cbuf
, 2, str
) );
187 CPPUNIT_ASSERT_EQUAL( 3, wxBase64Decode(cbuf
, 3, str
) );
188 CPPUNIT_ASSERT_EQUAL( 'A', cbuf
[0] );
189 CPPUNIT_ASSERT_EQUAL( 'B', cbuf
[1] );
190 CPPUNIT_ASSERT_EQUAL( 'C', cbuf
[2] );
191 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[3] );
192 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[4] );
195 void Base64TestCase::EncodeDecodeABCD()
197 const wxString str
= wxBase64Encode("ABCD", 4);
198 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str
);
200 wxMemoryBuffer buf
= wxBase64Decode(str
);
201 CPPUNIT_ASSERT_EQUAL(4, buf
.GetDataLen());
202 CPPUNIT_ASSERT_EQUAL('A', buf
[0]);
203 CPPUNIT_ASSERT_EQUAL('B', buf
[1]);
204 CPPUNIT_ASSERT_EQUAL('C', buf
[2]);
205 CPPUNIT_ASSERT_EQUAL('D', buf
[3]);
208 memset(cbuf
, (char)-1, sizeof(cbuf
));
209 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, wxBase64Decode(cbuf
, 3, str
) );
210 CPPUNIT_ASSERT_EQUAL( 4, wxBase64Decode(cbuf
, 4, str
) );
211 CPPUNIT_ASSERT_EQUAL( 'A', cbuf
[0] );
212 CPPUNIT_ASSERT_EQUAL( 'B', cbuf
[1] );
213 CPPUNIT_ASSERT_EQUAL( 'C', cbuf
[2] );
214 CPPUNIT_ASSERT_EQUAL( 'D', cbuf
[3] );
215 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[4] );
216 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf
[5] );
219 void Base64TestCase::EncodeDecode0to255()
221 unsigned char buff
[256];
222 generatePatternedData(buff
, 256, 0, 1);
223 wxString str
= wxBase64Encode(buff
, 256);
224 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
225 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
227 mbuff
= wxBase64Decode(encoded0to255
);
228 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
231 void Base64TestCase::EncodeDecodePatternA()
233 unsigned char buff
[350];
234 generatePatternedData(buff
, 350, 24, 5, 3);
235 wxString str
= wxBase64Encode(buff
, 350);
236 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
237 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
240 void Base64TestCase::EncodeDecodePatternB()
242 unsigned char buff
[350];
243 generatePatternedData(buff
, 350, 0, 1, 1, 0xAA);
244 wxString str
= wxBase64Encode(buff
, 350);
245 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
246 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
249 void Base64TestCase::EncodeDecodePatternC()
251 unsigned char buff
[11];
252 generatePatternedData(buff
, 11, 1, 0, 2);
253 wxString str
= wxBase64Encode(buff
, 11);
254 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
255 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
258 void Base64TestCase::EncodeDecodeRandom()
260 size_t size
= rand() * 3000 / RAND_MAX
+ 11;
261 unsigned char *buff
= new unsigned char[size
];
262 generateRandomData(buff
, size
);
263 wxString str
= wxBase64Encode(buff
, size
);
264 wxMemoryBuffer mbuff
= wxBase64Decode(str
);
265 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
267 generateGibberish(buff
, size
);
268 char *buff2
= new char[size
];
269 size_t realsize
= size
;
270 CPPUNIT_ASSERT(wxBase64Decode(buff2
, realsize
, (char *)buff
, size
));
271 CPPUNIT_ASSERT(wxBase64Encode(buff2
, size
, buff2
, realsize
));
274 void Base64TestCase::DecodeInvalid()
277 rc
= wxBase64Decode(NULL
, 0, "one two!", wxNO_LEN
,
278 wxBase64DecodeMode_Strict
, &posErr
);
279 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, rc
);
280 CPPUNIT_ASSERT_EQUAL( 3, posErr
);
282 rc
= wxBase64Decode(NULL
, 0, "one two!", wxNO_LEN
,
283 wxBase64DecodeMode_SkipWS
, &posErr
);
284 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, rc
);
285 CPPUNIT_ASSERT_EQUAL( 7, posErr
);
287 rc
= wxBase64Decode(NULL
, 0, "? QQ==", wxNO_LEN
,
288 wxBase64DecodeMode_SkipWS
, &posErr
);
289 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED
, rc
);
290 CPPUNIT_ASSERT_EQUAL( 0, posErr
);
293 rc
= wxBase64Decode(NULL
, 0, " QQ==", wxNO_LEN
,
294 wxBase64DecodeMode_SkipWS
, &posErr
);
295 CPPUNIT_ASSERT_EQUAL( 1, rc
);
296 CPPUNIT_ASSERT_EQUAL( -1, posErr
);
298 rc
= wxBase64Decode(NULL
, 0, "? QQ==", wxNO_LEN
,
299 wxBase64DecodeMode_Relaxed
, &posErr
);
300 CPPUNIT_ASSERT_EQUAL( 1, rc
);
301 CPPUNIT_ASSERT_EQUAL( -1, posErr
);
303 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
306 #endif // wxUSE_BASE64