1 | use MIME::Base64;
|
---|
2 |
|
---|
3 | print "1..282\n";
|
---|
4 |
|
---|
5 | print "Testing MIME::Base64-", $MIME::Base64::VERSION, "\n";
|
---|
6 |
|
---|
7 | $testno = 1;
|
---|
8 |
|
---|
9 | encodeTest();
|
---|
10 | decodeTest();
|
---|
11 |
|
---|
12 | # This used to generate a warning
|
---|
13 | print "not " unless decode_base64(encode_base64("foo")) eq "foo";
|
---|
14 | print "ok ", $testno++, "\n";
|
---|
15 |
|
---|
16 | sub encodeTest
|
---|
17 | {
|
---|
18 | print "encode test\n";
|
---|
19 |
|
---|
20 | my @encode_tests = (
|
---|
21 | ['' => ''],
|
---|
22 | ['a' => 'YQ=='],
|
---|
23 | ['aa' => 'YWE='],
|
---|
24 | ['aaa' => 'YWFh'],
|
---|
25 |
|
---|
26 | ['aaa' => 'YWFh'],
|
---|
27 | ['aaa' => 'YWFh'],
|
---|
28 | ['aaa' => 'YWFh'],
|
---|
29 |
|
---|
30 | ["\000\377" => "AP8="],
|
---|
31 | ["\377\000" => "/wA="],
|
---|
32 | ["\000\000\000" => "AAAA"],
|
---|
33 |
|
---|
34 | # from HTTP spec
|
---|
35 | ['Aladdin:open sesame' => 'QWxhZGRpbjpvcGVuIHNlc2FtZQ=='],
|
---|
36 |
|
---|
37 | ['a' x 100 => 'YWFh' x 33 . 'YQ=='],
|
---|
38 |
|
---|
39 | ['Multipurpose Internet Mail Extensions: The Base64 Content-Transfer-Encoding is designed to represent sequences of octets in a form that is not humanly readable. '
|
---|
40 | => "TXVsdGlwdXJwb3NlIEludGVybmV0IE1haWwgRXh0ZW5zaW9uczogVGhlIEJhc2U2NCBDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nIGlzIGRlc2lnbmVkIHRvIHJlcHJlc2VudCBzZXF1ZW5jZXMgb2Ygb2N0ZXRzIGluIGEgZm9ybSB0aGF0IGlzIG5vdCBodW1hbmx5IHJlYWRhYmxlLiA="],
|
---|
41 |
|
---|
42 | # All values
|
---|
43 | ["\000" => "AA=="],
|
---|
44 | ["\001" => "AQ=="],
|
---|
45 | ["\002" => "Ag=="],
|
---|
46 | ["\003" => "Aw=="],
|
---|
47 | ["\004" => "BA=="],
|
---|
48 | ["\005" => "BQ=="],
|
---|
49 | ["\006" => "Bg=="],
|
---|
50 | ["\007" => "Bw=="],
|
---|
51 | ["\010" => "CA=="],
|
---|
52 | ["\011" => "CQ=="],
|
---|
53 | ["\012" => "Cg=="],
|
---|
54 | ["\013" => "Cw=="],
|
---|
55 | ["\014" => "DA=="],
|
---|
56 | ["\015" => "DQ=="],
|
---|
57 | ["\016" => "Dg=="],
|
---|
58 | ["\017" => "Dw=="],
|
---|
59 | ["\020" => "EA=="],
|
---|
60 | ["\021" => "EQ=="],
|
---|
61 | ["\022" => "Eg=="],
|
---|
62 | ["\023" => "Ew=="],
|
---|
63 | ["\024" => "FA=="],
|
---|
64 | ["\025" => "FQ=="],
|
---|
65 | ["\026" => "Fg=="],
|
---|
66 | ["\027" => "Fw=="],
|
---|
67 | ["\030" => "GA=="],
|
---|
68 | ["\031" => "GQ=="],
|
---|
69 | ["\032" => "Gg=="],
|
---|
70 | ["\033" => "Gw=="],
|
---|
71 | ["\034" => "HA=="],
|
---|
72 | ["\035" => "HQ=="],
|
---|
73 | ["\036" => "Hg=="],
|
---|
74 | ["\037" => "Hw=="],
|
---|
75 | ["\040" => "IA=="],
|
---|
76 | ["\041" => "IQ=="],
|
---|
77 | ["\042" => "Ig=="],
|
---|
78 | ["\043" => "Iw=="],
|
---|
79 | ["\044" => "JA=="],
|
---|
80 | ["\045" => "JQ=="],
|
---|
81 | ["\046" => "Jg=="],
|
---|
82 | ["\047" => "Jw=="],
|
---|
83 | ["\050" => "KA=="],
|
---|
84 | ["\051" => "KQ=="],
|
---|
85 | ["\052" => "Kg=="],
|
---|
86 | ["\053" => "Kw=="],
|
---|
87 | ["\054" => "LA=="],
|
---|
88 | ["\055" => "LQ=="],
|
---|
89 | ["\056" => "Lg=="],
|
---|
90 | ["\057" => "Lw=="],
|
---|
91 | ["\060" => "MA=="],
|
---|
92 | ["\061" => "MQ=="],
|
---|
93 | ["\062" => "Mg=="],
|
---|
94 | ["\063" => "Mw=="],
|
---|
95 | ["\064" => "NA=="],
|
---|
96 | ["\065" => "NQ=="],
|
---|
97 | ["\066" => "Ng=="],
|
---|
98 | ["\067" => "Nw=="],
|
---|
99 | ["\070" => "OA=="],
|
---|
100 | ["\071" => "OQ=="],
|
---|
101 | ["\072" => "Og=="],
|
---|
102 | ["\073" => "Ow=="],
|
---|
103 | ["\074" => "PA=="],
|
---|
104 | ["\075" => "PQ=="],
|
---|
105 | ["\076" => "Pg=="],
|
---|
106 | ["\077" => "Pw=="],
|
---|
107 | ["\100" => "QA=="],
|
---|
108 | ["\101" => "QQ=="],
|
---|
109 | ["\102" => "Qg=="],
|
---|
110 | ["\103" => "Qw=="],
|
---|
111 | ["\104" => "RA=="],
|
---|
112 | ["\105" => "RQ=="],
|
---|
113 | ["\106" => "Rg=="],
|
---|
114 | ["\107" => "Rw=="],
|
---|
115 | ["\110" => "SA=="],
|
---|
116 | ["\111" => "SQ=="],
|
---|
117 | ["\112" => "Sg=="],
|
---|
118 | ["\113" => "Sw=="],
|
---|
119 | ["\114" => "TA=="],
|
---|
120 | ["\115" => "TQ=="],
|
---|
121 | ["\116" => "Tg=="],
|
---|
122 | ["\117" => "Tw=="],
|
---|
123 | ["\120" => "UA=="],
|
---|
124 | ["\121" => "UQ=="],
|
---|
125 | ["\122" => "Ug=="],
|
---|
126 | ["\123" => "Uw=="],
|
---|
127 | ["\124" => "VA=="],
|
---|
128 | ["\125" => "VQ=="],
|
---|
129 | ["\126" => "Vg=="],
|
---|
130 | ["\127" => "Vw=="],
|
---|
131 | ["\130" => "WA=="],
|
---|
132 | ["\131" => "WQ=="],
|
---|
133 | ["\132" => "Wg=="],
|
---|
134 | ["\133" => "Ww=="],
|
---|
135 | ["\134" => "XA=="],
|
---|
136 | ["\135" => "XQ=="],
|
---|
137 | ["\136" => "Xg=="],
|
---|
138 | ["\137" => "Xw=="],
|
---|
139 | ["\140" => "YA=="],
|
---|
140 | ["\141" => "YQ=="],
|
---|
141 | ["\142" => "Yg=="],
|
---|
142 | ["\143" => "Yw=="],
|
---|
143 | ["\144" => "ZA=="],
|
---|
144 | ["\145" => "ZQ=="],
|
---|
145 | ["\146" => "Zg=="],
|
---|
146 | ["\147" => "Zw=="],
|
---|
147 | ["\150" => "aA=="],
|
---|
148 | ["\151" => "aQ=="],
|
---|
149 | ["\152" => "ag=="],
|
---|
150 | ["\153" => "aw=="],
|
---|
151 | ["\154" => "bA=="],
|
---|
152 | ["\155" => "bQ=="],
|
---|
153 | ["\156" => "bg=="],
|
---|
154 | ["\157" => "bw=="],
|
---|
155 | ["\160" => "cA=="],
|
---|
156 | ["\161" => "cQ=="],
|
---|
157 | ["\162" => "cg=="],
|
---|
158 | ["\163" => "cw=="],
|
---|
159 | ["\164" => "dA=="],
|
---|
160 | ["\165" => "dQ=="],
|
---|
161 | ["\166" => "dg=="],
|
---|
162 | ["\167" => "dw=="],
|
---|
163 | ["\170" => "eA=="],
|
---|
164 | ["\171" => "eQ=="],
|
---|
165 | ["\172" => "eg=="],
|
---|
166 | ["\173" => "ew=="],
|
---|
167 | ["\174" => "fA=="],
|
---|
168 | ["\175" => "fQ=="],
|
---|
169 | ["\176" => "fg=="],
|
---|
170 | ["\177" => "fw=="],
|
---|
171 | ["\200" => "gA=="],
|
---|
172 | ["\201" => "gQ=="],
|
---|
173 | ["\202" => "gg=="],
|
---|
174 | ["\203" => "gw=="],
|
---|
175 | ["\204" => "hA=="],
|
---|
176 | ["\205" => "hQ=="],
|
---|
177 | ["\206" => "hg=="],
|
---|
178 | ["\207" => "hw=="],
|
---|
179 | ["\210" => "iA=="],
|
---|
180 | ["\211" => "iQ=="],
|
---|
181 | ["\212" => "ig=="],
|
---|
182 | ["\213" => "iw=="],
|
---|
183 | ["\214" => "jA=="],
|
---|
184 | ["\215" => "jQ=="],
|
---|
185 | ["\216" => "jg=="],
|
---|
186 | ["\217" => "jw=="],
|
---|
187 | ["\220" => "kA=="],
|
---|
188 | ["\221" => "kQ=="],
|
---|
189 | ["\222" => "kg=="],
|
---|
190 | ["\223" => "kw=="],
|
---|
191 | ["\224" => "lA=="],
|
---|
192 | ["\225" => "lQ=="],
|
---|
193 | ["\226" => "lg=="],
|
---|
194 | ["\227" => "lw=="],
|
---|
195 | ["\230" => "mA=="],
|
---|
196 | ["\231" => "mQ=="],
|
---|
197 | ["\232" => "mg=="],
|
---|
198 | ["\233" => "mw=="],
|
---|
199 | ["\234" => "nA=="],
|
---|
200 | ["\235" => "nQ=="],
|
---|
201 | ["\236" => "ng=="],
|
---|
202 | ["\237" => "nw=="],
|
---|
203 | ["\240" => "oA=="],
|
---|
204 | ["\241" => "oQ=="],
|
---|
205 | ["\242" => "og=="],
|
---|
206 | ["\243" => "ow=="],
|
---|
207 | ["\244" => "pA=="],
|
---|
208 | ["\245" => "pQ=="],
|
---|
209 | ["\246" => "pg=="],
|
---|
210 | ["\247" => "pw=="],
|
---|
211 | ["\250" => "qA=="],
|
---|
212 | ["\251" => "qQ=="],
|
---|
213 | ["\252" => "qg=="],
|
---|
214 | ["\253" => "qw=="],
|
---|
215 | ["\254" => "rA=="],
|
---|
216 | ["\255" => "rQ=="],
|
---|
217 | ["\256" => "rg=="],
|
---|
218 | ["\257" => "rw=="],
|
---|
219 | ["\260" => "sA=="],
|
---|
220 | ["\261" => "sQ=="],
|
---|
221 | ["\262" => "sg=="],
|
---|
222 | ["\263" => "sw=="],
|
---|
223 | ["\264" => "tA=="],
|
---|
224 | ["\265" => "tQ=="],
|
---|
225 | ["\266" => "tg=="],
|
---|
226 | ["\267" => "tw=="],
|
---|
227 | ["\270" => "uA=="],
|
---|
228 | ["\271" => "uQ=="],
|
---|
229 | ["\272" => "ug=="],
|
---|
230 | ["\273" => "uw=="],
|
---|
231 | ["\274" => "vA=="],
|
---|
232 | ["\275" => "vQ=="],
|
---|
233 | ["\276" => "vg=="],
|
---|
234 | ["\277" => "vw=="],
|
---|
235 | ["\300" => "wA=="],
|
---|
236 | ["\301" => "wQ=="],
|
---|
237 | ["\302" => "wg=="],
|
---|
238 | ["\303" => "ww=="],
|
---|
239 | ["\304" => "xA=="],
|
---|
240 | ["\305" => "xQ=="],
|
---|
241 | ["\306" => "xg=="],
|
---|
242 | ["\307" => "xw=="],
|
---|
243 | ["\310" => "yA=="],
|
---|
244 | ["\311" => "yQ=="],
|
---|
245 | ["\312" => "yg=="],
|
---|
246 | ["\313" => "yw=="],
|
---|
247 | ["\314" => "zA=="],
|
---|
248 | ["\315" => "zQ=="],
|
---|
249 | ["\316" => "zg=="],
|
---|
250 | ["\317" => "zw=="],
|
---|
251 | ["\320" => "0A=="],
|
---|
252 | ["\321" => "0Q=="],
|
---|
253 | ["\322" => "0g=="],
|
---|
254 | ["\323" => "0w=="],
|
---|
255 | ["\324" => "1A=="],
|
---|
256 | ["\325" => "1Q=="],
|
---|
257 | ["\326" => "1g=="],
|
---|
258 | ["\327" => "1w=="],
|
---|
259 | ["\330" => "2A=="],
|
---|
260 | ["\331" => "2Q=="],
|
---|
261 | ["\332" => "2g=="],
|
---|
262 | ["\333" => "2w=="],
|
---|
263 | ["\334" => "3A=="],
|
---|
264 | ["\335" => "3Q=="],
|
---|
265 | ["\336" => "3g=="],
|
---|
266 | ["\337" => "3w=="],
|
---|
267 | ["\340" => "4A=="],
|
---|
268 | ["\341" => "4Q=="],
|
---|
269 | ["\342" => "4g=="],
|
---|
270 | ["\343" => "4w=="],
|
---|
271 | ["\344" => "5A=="],
|
---|
272 | ["\345" => "5Q=="],
|
---|
273 | ["\346" => "5g=="],
|
---|
274 | ["\347" => "5w=="],
|
---|
275 | ["\350" => "6A=="],
|
---|
276 | ["\351" => "6Q=="],
|
---|
277 | ["\352" => "6g=="],
|
---|
278 | ["\353" => "6w=="],
|
---|
279 | ["\354" => "7A=="],
|
---|
280 | ["\355" => "7Q=="],
|
---|
281 | ["\356" => "7g=="],
|
---|
282 | ["\357" => "7w=="],
|
---|
283 | ["\360" => "8A=="],
|
---|
284 | ["\361" => "8Q=="],
|
---|
285 | ["\362" => "8g=="],
|
---|
286 | ["\363" => "8w=="],
|
---|
287 | ["\364" => "9A=="],
|
---|
288 | ["\365" => "9Q=="],
|
---|
289 | ["\366" => "9g=="],
|
---|
290 | ["\367" => "9w=="],
|
---|
291 | ["\370" => "+A=="],
|
---|
292 | ["\371" => "+Q=="],
|
---|
293 | ["\372" => "+g=="],
|
---|
294 | ["\373" => "+w=="],
|
---|
295 | ["\374" => "/A=="],
|
---|
296 | ["\375" => "/Q=="],
|
---|
297 | ["\376" => "/g=="],
|
---|
298 | ["\377" => "/w=="],
|
---|
299 | );
|
---|
300 |
|
---|
301 | for $test (@encode_tests) {
|
---|
302 | my($plain, $expected) = ($$test[0], $$test[1]);
|
---|
303 |
|
---|
304 | my $encoded = encode_base64($plain, '');
|
---|
305 | if ($encoded ne $expected) {
|
---|
306 | print "test $testno ($plain): expected $expected, got $encoded\n";
|
---|
307 | print "not ";
|
---|
308 | }
|
---|
309 | my $decoded = decode_base64($encoded);
|
---|
310 | if ($decoded ne $plain) {
|
---|
311 | print "test $testno ($plain): expected $expected, got $encoded\n";
|
---|
312 | print "not ";
|
---|
313 | }
|
---|
314 |
|
---|
315 | # Try the old C versions too
|
---|
316 | if ($encoded ne MIME::Base64::old_encode_base64($plain, '')) {
|
---|
317 | print "old_encode_base64 give different result.\n";
|
---|
318 | print "not ";
|
---|
319 | }
|
---|
320 | if ($plain ne MIME::Base64::old_decode_base64($encoded)) {
|
---|
321 | print "old_decode_base64 give different result.\n";
|
---|
322 | print "not ";
|
---|
323 | }
|
---|
324 |
|
---|
325 | print "ok $testno\n";
|
---|
326 | $testno++;
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | sub decodeTest
|
---|
331 | {
|
---|
332 | print "decode test:\n";
|
---|
333 | local $SIG{__WARN__} = sub { print $_[0] }; # avoid warnings on stderr
|
---|
334 |
|
---|
335 | my @decode_tests = (
|
---|
336 | ['YWE=' => 'aa'],
|
---|
337 | [' YWE=' => 'aa'],
|
---|
338 | ['Y WE=' => 'aa'],
|
---|
339 | ['YWE= ' => 'aa'],
|
---|
340 | ["Y\nW\r\nE=" => 'aa'],
|
---|
341 |
|
---|
342 | # These will generate some warnings
|
---|
343 | ['YWE=====' => 'aa'], # extra padding
|
---|
344 | ['YWE' => 'aa'], # missing padding
|
---|
345 | ['YWFh====' => 'aaa'],
|
---|
346 | ['YQ' => 'a'],
|
---|
347 | ['Y' => ''],
|
---|
348 | ['' => ''],
|
---|
349 | [undef() => ''],
|
---|
350 | );
|
---|
351 |
|
---|
352 | for $test (@decode_tests) {
|
---|
353 | my($encoded, $expected) = ($$test[0], $$test[1]);
|
---|
354 |
|
---|
355 | my $decoded = decode_base64($encoded);
|
---|
356 | if ($decoded ne $expected) {
|
---|
357 | die "test $testno ($encoded): expected $expected, got $decoded\n";
|
---|
358 | }
|
---|
359 | print "ok $testno\n";
|
---|
360 | $testno++;
|
---|
361 | }
|
---|
362 | }
|
---|