]> git.saurik.com Git - wxWidgets.git/blame_incremental - tests/base64/base64.cpp
wxRTC: save and load the 'shown' status in case there's a situation where layout...
[wxWidgets.git] / tests / base64 / base64.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/base64/base64.cpp
3// Purpose: wxBase64Encode/Decode unit test
4// Author: Charles Reimers
5// Created: 2007-06-22
6///////////////////////////////////////////////////////////////////////////////
7
8// ----------------------------------------------------------------------------
9// headers
10// ----------------------------------------------------------------------------
11
12#include "testprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#ifndef WX_PRECOMP
19 #include "wx/wx.h"
20#endif // WX_PRECOMP
21
22#if wxUSE_BASE64
23
24#include "wx/base64.h"
25
26static 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"
34 "/P3+/w==";
35
36static void
37generatePatternedData(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)
40{
41 unsigned char *cbuff = (unsigned char *)buff;
42 unsigned char curval = startVal;
43 while(len--)
44 {
45 *(cbuff++) = curval;
46 curval = (((curval + addVal) * multVal) ^ xorMask) & andMask;
47 }
48}
49
50static void generateRandomData(void* buff, size_t len)
51{
52 unsigned char *cbuff = (unsigned char *)buff;
53 while(len--)
54 {
55 *(cbuff++) = (unsigned char)(((rand() * 255) / RAND_MAX));
56 }
57}
58
59static void generateGibberish(void* buff, size_t len)
60{
61 static const unsigned char cb64[] =
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
63
64 unsigned char *cbuff = (unsigned char *)buff;
65 while(len--)
66 {
67 *(cbuff++) = cb64[((rand() * 64) / RAND_MAX)];
68 }
69}
70
71// --------------------------------------------------------------------------
72// test class
73// --------------------------------------------------------------------------
74
75class Base64TestCase : public CppUnit::TestCase
76{
77public:
78 Base64TestCase() { }
79
80private:
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();
94
95 void EncodeDecodeEmpty();
96 void EncodeDecodeA();
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();
106
107 DECLARE_NO_COPY_CLASS(Base64TestCase)
108};
109
110// register in the unnamed registry so that these tests are run by default
111CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase );
112
113// also include in its own registry so that these tests can be run alone
114CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase, "Base64TestCase" );
115
116void Base64TestCase::EncodeDecodeEmpty()
117{
118 char shouldBeEmpty[10];
119 shouldBeEmpty[0] = '\0';
120 size_t len = 10;
121
122 CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty, len, "", 0) != wxCONV_FAILED);
123 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
124
125 CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty, len, "") != wxCONV_FAILED);
126 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
127
128 wxMemoryBuffer bufmt;
129 wxString resultEmpty = wxBase64Encode(bufmt);
130 CPPUNIT_ASSERT(resultEmpty.empty());
131
132 bufmt = wxBase64Decode(resultEmpty);
133 CPPUNIT_ASSERT_EQUAL(0, bufmt.GetDataLen());
134}
135
136void Base64TestCase::EncodeDecodeA()
137{
138 const wxString str = wxBase64Encode("A", 1);
139 CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str);
140
141 wxMemoryBuffer buf = wxBase64Decode(str);
142 CPPUNIT_ASSERT_EQUAL(1, buf.GetDataLen());
143 CPPUNIT_ASSERT_EQUAL('A', *(char *)buf.GetData());
144
145 char cbuf[10];
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] );
151}
152
153void Base64TestCase::EncodeDecodeAB()
154{
155 const wxString str = wxBase64Encode("AB", 2);
156 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);
157
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]);
162
163 char cbuf[10];
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] );
171}
172
173void Base64TestCase::EncodeDecodeABC()
174{
175 const wxString str = wxBase64Encode("ABC", 3);
176 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str);
177
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]);
183
184 char cbuf[10];
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] );
193}
194
195void Base64TestCase::EncodeDecodeABCD()
196{
197 const wxString str = wxBase64Encode("ABCD", 4);
198 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);
199
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]);
206
207 char cbuf[10];
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] );
217}
218
219void Base64TestCase::EncodeDecode0to255()
220{
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);
226
227 mbuff = wxBase64Decode(encoded0to255);
228 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
229}
230
231void Base64TestCase::EncodeDecodePatternA()
232{
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);
238}
239
240void Base64TestCase::EncodeDecodePatternB()
241{
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);
247}
248
249void Base64TestCase::EncodeDecodePatternC()
250{
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);
256}
257
258void Base64TestCase::EncodeDecodeRandom()
259{
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);
266
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));
272}
273
274void Base64TestCase::DecodeInvalid()
275{
276 size_t rc, posErr;
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 );
281
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 );
286
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 );
291
292 posErr = (size_t)-1;
293 rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
294 wxBase64DecodeMode_SkipWS, &posErr);
295 CPPUNIT_ASSERT_EQUAL( 1, rc );
296 CPPUNIT_ASSERT_EQUAL( -1, posErr );
297
298 rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
299 wxBase64DecodeMode_Relaxed, &posErr);
300 CPPUNIT_ASSERT_EQUAL( 1, rc );
301 CPPUNIT_ASSERT_EQUAL( -1, posErr );
302
303 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
304}
305
306#endif // wxUSE_BASE64