]> git.saurik.com Git - wxWidgets.git/blob - tests/base64/base64.cpp
Fix comparison operator for functions keys (fixes #10547).
[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 it's 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
147 void Base64TestCase::EncodeDecodeAB()
148 {
149 const wxString str = wxBase64Encode("AB", 2);
150 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);
151
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]);
156 }
157
158 void Base64TestCase::EncodeDecodeABC()
159 {
160 const wxString str = wxBase64Encode("ABC", 3);
161 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str);
162
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]);
168 }
169
170 void Base64TestCase::EncodeDecodeABCD()
171 {
172 const wxString str = wxBase64Encode("ABCD", 4);
173 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);
174
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]);
181 }
182
183 void 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
195 void 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
204 void 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
213 void 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
222 void 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
238 void 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);
244 CPPUNIT_ASSERT_EQUAL( 3, posErr );
245
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 );
250
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 );
255
256 posErr = (size_t)-1;
257 rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
258 wxBase64DecodeMode_SkipWS, &posErr);
259 CPPUNIT_ASSERT_EQUAL( 1, rc );
260 CPPUNIT_ASSERT_EQUAL( -1, posErr );
261
262 rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
263 wxBase64DecodeMode_Relaxed, &posErr);
264 CPPUNIT_ASSERT_EQUAL( 1, rc );
265 CPPUNIT_ASSERT_EQUAL( -1, posErr );
266
267 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
268 }
269
270 #endif // wxUSE_BASE64