]> git.saurik.com Git - wxWidgets.git/blame - tests/base64/base64.cpp
respect wxBU_NOTEXT style in wxButton
[wxWidgets.git] / tests / base64 / base64.cpp
CommitLineData
4db03d26
VZ
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
27static 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
37static void
38generatePatternedData(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
51static 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
60static void generateGibberish(void* buff, size_t len)
61{
d2001a56 62 static const unsigned char cb64[] =
4db03d26
VZ
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
76class Base64TestCase : public CppUnit::TestCase
77{
78public:
79 Base64TestCase() { }
80
81private:
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 );
7bac0684 93 CPPUNIT_TEST( DecodeInvalid );
4db03d26
VZ
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();
7bac0684 106 void DecodeInvalid();
4db03d26
VZ
107
108 DECLARE_NO_COPY_CLASS(Base64TestCase)
109};
110
111// register in the unnamed registry so that these tests are run by default
112CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase );
113
114// also include in it's own registry so that these tests can be run alone
115CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase, "Base64TestCase" );
116
117void 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;
ee8c79c1
VZ
130 wxString resultEmpty = wxBase64Encode(bufmt);
131 CPPUNIT_ASSERT(resultEmpty.empty());
4db03d26 132
ee8c79c1 133 bufmt = wxBase64Decode(resultEmpty);
1de532f5 134 CPPUNIT_ASSERT_EQUAL(0, bufmt.GetDataLen());
4db03d26
VZ
135}
136
137void Base64TestCase::EncodeDecodeA()
138{
139 const wxString str = wxBase64Encode("A", 1);
140 CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str);
141
142 wxMemoryBuffer buf = wxBase64Decode(str);
1de532f5 143 CPPUNIT_ASSERT_EQUAL(1, buf.GetDataLen());
4db03d26
VZ
144 CPPUNIT_ASSERT_EQUAL('A', *(char *)buf.GetData());
145}
146
147void Base64TestCase::EncodeDecodeAB()
148{
149 const wxString str = wxBase64Encode("AB", 2);
150 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);
151
152 wxMemoryBuffer buf = wxBase64Decode(str);
1de532f5 153 CPPUNIT_ASSERT_EQUAL(2, buf.GetDataLen());
4db03d26
VZ
154 CPPUNIT_ASSERT_EQUAL('A', buf[0]);
155 CPPUNIT_ASSERT_EQUAL('B', buf[1]);
156}
157
158void Base64TestCase::EncodeDecodeABC()
159{
160 const wxString str = wxBase64Encode("ABC", 3);
161 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str);
162
163 wxMemoryBuffer buf = wxBase64Decode(str);
1de532f5 164 CPPUNIT_ASSERT_EQUAL(3, buf.GetDataLen());
4db03d26
VZ
165 CPPUNIT_ASSERT_EQUAL('A', buf[0]);
166 CPPUNIT_ASSERT_EQUAL('B', buf[1]);
167 CPPUNIT_ASSERT_EQUAL('C', buf[2]);
168}
169
170void Base64TestCase::EncodeDecodeABCD()
171{
172 const wxString str = wxBase64Encode("ABCD", 4);
173 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);
174
175 wxMemoryBuffer buf = wxBase64Decode(str);
1de532f5 176 CPPUNIT_ASSERT_EQUAL(4, buf.GetDataLen());
4db03d26
VZ
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]);
181}
182
183void Base64TestCase::EncodeDecode0to255()
184{
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);
190
191 mbuff = wxBase64Decode(encoded0to255);
192 CPPUNIT_ASSERT(memcmp(mbuff.GetData(), buff, mbuff.GetDataLen()) == 0);
193}
194
195void Base64TestCase::EncodeDecodePatternA()
196{
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);
202}
203
204void Base64TestCase::EncodeDecodePatternB()
205{
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);
211}
212
213void Base64TestCase::EncodeDecodePatternC()
214{
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);
220}
221
222void Base64TestCase::EncodeDecodeRandom()
223{
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);
230
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));
236}
237
7bac0684
VZ
238void Base64TestCase::DecodeInvalid()
239{
240 size_t rc, posErr;
241 rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
242 wxBase64DecodeMode_Strict, &posErr);
243 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
1de532f5 244 CPPUNIT_ASSERT_EQUAL( 3, posErr );
7bac0684
VZ
245
246 rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
247 wxBase64DecodeMode_SkipWS, &posErr);
248 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
1de532f5 249 CPPUNIT_ASSERT_EQUAL( 7, posErr );
7bac0684
VZ
250
251 rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
252 wxBase64DecodeMode_SkipWS, &posErr);
253 CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
1de532f5 254 CPPUNIT_ASSERT_EQUAL( 0, posErr );
7bac0684
VZ
255
256 posErr = (size_t)-1;
257 rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
258 wxBase64DecodeMode_SkipWS, &posErr);
1de532f5
VZ
259 CPPUNIT_ASSERT_EQUAL( 1, rc );
260 CPPUNIT_ASSERT_EQUAL( -1, posErr );
7bac0684
VZ
261
262 rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
263 wxBase64DecodeMode_Relaxed, &posErr);
1de532f5
VZ
264 CPPUNIT_ASSERT_EQUAL( 1, rc );
265 CPPUNIT_ASSERT_EQUAL( -1, posErr );
7bac0684
VZ
266
267 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
268}
269
4db03d26 270#endif // wxUSE_BASE64