1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/base64/base64.cpp
3 // Purpose: wxBase64Encode/Decode unit test
4 // Author: Charles Reimers
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
25 #include "wx/base64.h"
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"
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)
42 unsigned char *cbuff
= (unsigned char *)buff
;
43 unsigned char curval
= startVal
;
47 curval
= (((curval
+ addVal
) * multVal
) ^ xorMask
) & andMask
;
51 static void generateRandomData(void* buff
, size_t len
)
53 unsigned char *cbuff
= (unsigned char *)buff
;
56 *(cbuff
++) = (unsigned char)(((rand() * 255) / RAND_MAX
));
60 static void generateGibberish(void* buff
, size_t len
)
62 static const unsigned char cb64
[] =
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
65 unsigned char *cbuff
= (unsigned char *)buff
;
68 *(cbuff
++) = cb64
[((rand() * 64) / RAND_MAX
)];
72 // --------------------------------------------------------------------------
74 // --------------------------------------------------------------------------
76 class Base64TestCase
: public CppUnit::TestCase
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();
96 void EncodeDecodeEmpty();
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();
108 DECLARE_NO_COPY_CLASS(Base64TestCase
)
111 // register in the unnamed registry so that these tests are run by default
112 CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase
);
114 // also include in its own registry so that these tests can be run alone
115 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase
, "Base64TestCase" );
117 void Base64TestCase::EncodeDecodeEmpty()
119 char shouldBeEmpty
[10];
120 shouldBeEmpty
[0] = '\0';
123 CPPUNIT_ASSERT(wxBase64Encode(shouldBeEmpty
, len
, "", 0) != wxCONV_FAILED
);
124 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty
[0]);
126 CPPUNIT_ASSERT(wxBase64Decode(shouldBeEmpty
, len
, "") != wxCONV_FAILED
);
127 CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty
[0]);
129 wxMemoryBuffer bufmt
;
130 wxString resultEmpty
= wxBase64Encode(bufmt
);
131 CPPUNIT_ASSERT(resultEmpty
.empty());
133 bufmt
= wxBase64Decode(resultEmpty
);
134 CPPUNIT_ASSERT_EQUAL(0, bufmt
.GetDataLen());
137 void Base64TestCase::EncodeDecodeA()
139 const wxString str
= wxBase64Encode("A", 1);
140 CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str
);
142 wxMemoryBuffer buf
= wxBase64Decode(str
);
143 CPPUNIT_ASSERT_EQUAL(1, buf
.GetDataLen());
144 CPPUNIT_ASSERT_EQUAL('A', *(char *)buf
.GetData());
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] );
154 void Base64TestCase::EncodeDecodeAB()
156 const wxString str
= wxBase64Encode("AB", 2);
157 CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str
);
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]);
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] );
174 void Base64TestCase::EncodeDecodeABC()
176 const wxString str
= wxBase64Encode("ABC", 3);
177 CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str
);
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]);
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] );
196 void Base64TestCase::EncodeDecodeABCD()
198 const wxString str
= wxBase64Encode("ABCD", 4);
199 CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str
);
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]);
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] );
220 void Base64TestCase::EncodeDecode0to255()
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);
228 mbuff
= wxBase64Decode(encoded0to255
);
229 CPPUNIT_ASSERT(memcmp(mbuff
.GetData(), buff
, mbuff
.GetDataLen()) == 0);
232 void Base64TestCase::EncodeDecodePatternA()
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);
241 void Base64TestCase::EncodeDecodePatternB()
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);
250 void Base64TestCase::EncodeDecodePatternC()
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);
259 void Base64TestCase::EncodeDecodeRandom()
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);
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
));
275 void Base64TestCase::DecodeInvalid()
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
);
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
);
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
);
294 rc
= wxBase64Decode(NULL
, 0, " QQ==", wxNO_LEN
,
295 wxBase64DecodeMode_SkipWS
, &posErr
);
296 CPPUNIT_ASSERT_EQUAL( 1, rc
);
297 CPPUNIT_ASSERT_EQUAL( -1, posErr
);
299 rc
= wxBase64Decode(NULL
, 0, "? QQ==", wxNO_LEN
,
300 wxBase64DecodeMode_Relaxed
, &posErr
);
301 CPPUNIT_ASSERT_EQUAL( 1, rc
);
302 CPPUNIT_ASSERT_EQUAL( -1, posErr
);
304 CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
307 #endif // wxUSE_BASE64