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