]> git.saurik.com Git - wxWidgets.git/blob - tests/base64/base64.cpp
Move wxBell() from base to core library.
[wxWidgets.git] / tests / base64 / base64.cpp
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$
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif // WX_PRECOMP
22
23 #if wxUSE_BASE64
24
25 #include "wx/base64.h"
26
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"
35 "/P3+/w==";
36
37 static void
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)
41 {
42 unsigned char *cbuff = (unsigned char *)buff;
43 unsigned char curval = startVal;
44 while(len--)
45 {
46 *(cbuff++) = curval;
47 curval = (((curval + addVal) * multVal) ^ xorMask) & andMask;
48 }
49 }
50
51 static void generateRandomData(void* buff, size_t len)
52 {
53 unsigned char *cbuff = (unsigned char *)buff;
54 while(len--)
55 {
56 *(cbuff++) = (unsigned char)(((rand() * 255) / RAND_MAX));
57 }
58 }
59
60 static void generateGibberish(void* buff, size_t len)
61 {
62 static const unsigned char cb64[] =
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64
65 unsigned char *cbuff = (unsigned char *)buff;
66 while(len--)
67 {
68 *(cbuff++) = cb64[((rand() * 64) / RAND_MAX)];
69 }
70 }
71
72 // --------------------------------------------------------------------------
73 // test class
74 // --------------------------------------------------------------------------
75
76 class Base64TestCase : public CppUnit::TestCase
77 {
78 public:
79 Base64TestCase() { }
80
81 private:
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();
95
96 void EncodeDecodeEmpty();
97 void EncodeDecodeA();
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();
107
108 DECLARE_NO_COPY_CLASS(Base64TestCase)
109 };
110
111 // register in the unnamed registry so that these tests are run by default
112 CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase );
113
114 // also include in its own registry so that these tests can be run alone
115 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase, "Base64TestCase" );
116
117 void Base64TestCase::EncodeDecodeEmpty()
118 {
119 char shouldBeEmpty[10];
120 shouldBeEmpty[0] = '\0';
121 size_t len = 10;
122
123 CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty, len, "", 0) != wxCONV_FAILED);
124 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
125
126 CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty, len, "") != wxCONV_FAILED);
127 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
128
129 wxMemoryBuffer bufmt;
130 wxString resultEmpty = wxBase64Encode(bufmt);
131 CPPUNIT_ASSERT(resultEmpty.empty());
132
133 bufmt = wxBase64Decode(resultEmpty);
134 CPPUNIT_ASSERT_EQUAL(0, bufmt.GetDataLen());
135 }
136
137 void Base64TestCase::EncodeDecodeA()
138 {
139 const wxString str = wxBase64Encode("A", 1);
140 CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str);
141
142 wxMemoryBuffer buf = wxBase64Decode(str);
143 CPPUNIT_ASSERT_EQUAL(1, buf.GetDataLen());
144 CPPUNIT_ASSERT_EQUAL('A', *(char *)buf.GetData());
145
146 char cbuf[10];
147 memset(cbuf, (char)-1, sizeof(cbuf));
148 CPPUNIT_ASSERT_EQUAL( 1, wxBase64Decode(cbuf, 1, str) );
149 CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
150 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[1] );
151 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[2] );
152 }
153
154 void Base64TestCase::EncodeDecodeAB()
155 {
156 const wxString str = wxBase64Encode("AB", 2);
157 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);
158
159 wxMemoryBuffer buf = wxBase64Decode(str);
160 CPPUNIT_ASSERT_EQUAL(2, buf.GetDataLen());
161 CPPUNIT_ASSERT_EQUAL('A', buf[0]);
162 CPPUNIT_ASSERT_EQUAL('B', buf[1]);
163
164 char cbuf[10];
165 memset(cbuf, (char)-1, sizeof(cbuf));
166 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 1, str) );
167 CPPUNIT_ASSERT_EQUAL( 2, wxBase64Decode(cbuf, 2, str) );
168 CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
169 CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
170 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[2] );
171 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[3] );
172 }
173
174 void Base64TestCase::EncodeDecodeABC()
175 {
176 const wxString str = wxBase64Encode("ABC", 3);
177 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str);
178
179 wxMemoryBuffer buf = wxBase64Decode(str);
180 CPPUNIT_ASSERT_EQUAL(3, buf.GetDataLen());
181 CPPUNIT_ASSERT_EQUAL('A', buf[0]);
182 CPPUNIT_ASSERT_EQUAL('B', buf[1]);
183 CPPUNIT_ASSERT_EQUAL('C', buf[2]);
184
185 char cbuf[10];
186 memset(cbuf, (char)-1, sizeof(cbuf));
187 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 2, str) );
188 CPPUNIT_ASSERT_EQUAL( 3, wxBase64Decode(cbuf, 3, str) );
189 CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
190 CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
191 CPPUNIT_ASSERT_EQUAL( 'C', cbuf[2] );
192 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[3] );
193 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[4] );
194 }
195
196 void Base64TestCase::EncodeDecodeABCD()
197 {
198 const wxString str = wxBase64Encode("ABCD", 4);
199 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);
200
201 wxMemoryBuffer buf = wxBase64Decode(str);
202 CPPUNIT_ASSERT_EQUAL(4, buf.GetDataLen());
203 CPPUNIT_ASSERT_EQUAL('A', buf[0]);
204 CPPUNIT_ASSERT_EQUAL('B', buf[1]);
205 CPPUNIT_ASSERT_EQUAL('C', buf[2]);
206 CPPUNIT_ASSERT_EQUAL('D', buf[3]);
207
208 char cbuf[10];
209 memset(cbuf, (char)-1, sizeof(cbuf));
210 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 3, str) );
211 CPPUNIT_ASSERT_EQUAL( 4, wxBase64Decode(cbuf, 4, str) );
212 CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
213 CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
214 CPPUNIT_ASSERT_EQUAL( 'C', cbuf[2] );
215 CPPUNIT_ASSERT_EQUAL( 'D', cbuf[3] );
216 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[4] );
217 CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[5] );
218 }
219
220 void Base64TestCase::EncodeDecode0to255()
221 {
222 unsigned char buff[256];
223 generatePatternedData(buff, 256, 0, 1);
224 wxString str = wxBase64Encode(buff, 256);
225 wxMemoryBuffer mbuff = wxBase64Decode(str);
226 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
227
228 mbuff = wxBase64Decode(encoded0to255);
229 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
230 }
231
232 void Base64TestCase::EncodeDecodePatternA()
233 {
234 unsigned char buff[350];
235 generatePatternedData(buff, 350, 24, 5, 3);
236 wxString str = wxBase64Encode(buff, 350);
237 wxMemoryBuffer mbuff = wxBase64Decode(str);
238 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
239 }
240
241 void Base64TestCase::EncodeDecodePatternB()
242 {
243 unsigned char buff[350];
244 generatePatternedData(buff, 350, 0, 1, 1, 0xAA);
245 wxString str = wxBase64Encode(buff, 350);
246 wxMemoryBuffer mbuff = wxBase64Decode(str);
247 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
248 }
249
250 void Base64TestCase::EncodeDecodePatternC()
251 {
252 unsigned char buff[11];
253 generatePatternedData(buff, 11, 1, 0, 2);
254 wxString str = wxBase64Encode(buff, 11);
255 wxMemoryBuffer mbuff = wxBase64Decode(str);
256 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
257 }
258
259 void Base64TestCase::EncodeDecodeRandom()
260 {
261 size_t size = rand() * 3000 / RAND_MAX + 11;
262 unsigned char *buff = new unsigned char[size];
263 generateRandomData(buff, size);
264 wxString str = wxBase64Encode(buff, size);
265 wxMemoryBuffer mbuff = wxBase64Decode(str);
266 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
267
268 generateGibberish(buff, size);
269 char *buff2 = new char[size];
270 size_t realsize = size;
271 CPPUNIT_ASSERT(wxBase64Decode(buff2, realsize, (char *)buff, size));
272 CPPUNIT_ASSERT(wxBase64Encode(buff2, size, buff2, realsize));
273 }
274
275 void Base64TestCase::DecodeInvalid()
276 {
277 size_t rc, posErr;
278 rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
279 wxBase64DecodeMode_Strict, &posErr);
280 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
281 CPPUNIT_ASSERT_EQUAL( 3, posErr );
282
283 rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
284 wxBase64DecodeMode_SkipWS, &posErr);
285 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
286 CPPUNIT_ASSERT_EQUAL( 7, posErr );
287
288 rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
289 wxBase64DecodeMode_SkipWS, &posErr);
290 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
291 CPPUNIT_ASSERT_EQUAL( 0, posErr );
292
293 posErr = (size_t)-1;
294 rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
295 wxBase64DecodeMode_SkipWS, &posErr);
296 CPPUNIT_ASSERT_EQUAL( 1, rc );
297 CPPUNIT_ASSERT_EQUAL( -1, posErr );
298
299 rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
300 wxBase64DecodeMode_Relaxed, &posErr);
301 CPPUNIT_ASSERT_EQUAL( 1, rc );
302 CPPUNIT_ASSERT_EQUAL( -1, posErr );
303
304 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
305 }
306
307 #endif // wxUSE_BASE64