/
mime_base64u.t
287 lines (249 loc) · 7.18 KB
/
mime_base64u.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!./parrot
# Copyright (C) 2006-2012, Parrot Foundation.
=head1 NAME
t/library/mime_base64u.t - test unicode [ 'MIME'; 'Base64' ]
=head1 SYNOPSIS
% prove t/library/mime_base64u.t
=head1 DESCRIPTION
Test non-ascii encoded MIME::Base64, without JSON.
perl -MMIME::Base64 -e'for ([qq(a2)],[qq(c2a2)],[qw(203e)],[qw(3e 20)],[qw(1000)],[qw(00c7)],[qw(00ff 0000)])
{ $s=pack "H*",@{$_}; printf "0x%s\t=> %s",join("",@{$_}),encode_base64($s) }'
=cut
.sub test :main
load_bytecode 'Test/More.pbc'
load_bytecode 'MIME/Base64.pir'
.local pmc plan, is, ok
plan = get_hll_global [ 'Test'; 'More' ], 'plan'
is = get_hll_global [ 'Test'; 'More' ], 'is'
ok = get_hll_global [ 'Test'; 'More' ], 'ok'
plan(20)
.local pmc encode_decode_tests, decode_tests
.local int i, size
i = 0
size = 9
encode_decode_tests = new 'FixedPMCArray', size
$P0 = new 'FixedStringArray', 2
$P0[0] = binary:"\x3e\x20" # same as 0x203e bswapped
$P0[1] = "PiA="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = ucs2:"\x{203e}" # OVERLINE ‾ 0x203e
# TODO on big endian we'll get ID4=
$P0[1] = "PiA="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf8:"\u203e" # OVERLINE ‾ 0xe280be in utf8
$P0[1] = "4oC+"
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = iso-8859-1:"\x{a2}" # ¢
$P0[1] = "og=="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf8:"\x{a2}" # ¢ 0xc2a2 in utf8
$P0[1] = "wqI="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = ucs2:"\x{0100}" # Ā 0xc480 in utf8
$P0[1] = "AAE="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf8:"\u0100" # Ā
$P0[1] = "xIA="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf16:"\x{00c7}" # Ç
$P0[1] = "xwA="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = ucs2:"\x{00ff}\x{0000}" # "ÿ\0"
$P0[1] = "/wAAAA=="
encode_decode_tests[i] = $P0
inc i
decode_tests = new 'FixedPMCArray', 2
$P0 = new 'FixedStringArray', 2
$P0[0] = "YWE="
$P0[1] = "aa"
decode_tests[0] = $P0
$P0 = new 'FixedStringArray', 2
$P0[0] = "YQ"
$P0[1] = "a"
decode_tests[1] = $P0
.local int count, enc_num, hex_cnt, bufused, len
count = 0
.local pmc test_iterator, test_case
.local string plain, base64, comment, comment_count, enc, esc_plain, hex_str
hex_str = ''
.include "iglobals.pasm"
.local pmc interp
interp = getinterp
.local pmc config
config = interp[.IGLOBALS_CONFIG_HASH]
.local int bigendian
bigendian = config['bigendian']
unless bigendian goto BE_INIT
.local pmc be_result
be_result = new 'FixedStringArray', size
test_iterator = iter encode_decode_tests
be_loop:
unless test_iterator goto be_loop_end
test_case = shift test_iterator
base64 = test_case[1]
be_result[count] = base64
inc count
goto be_loop
be_loop_end:
be_result[1] = 'ID4='
be_result[5] = 'AQA='
be_result[7] = 'AMc='
be_result[8] = 'AP8AAA=='
BE_INIT:
count = 0
test_iterator = iter encode_decode_tests
enc_dec_loop:
unless test_iterator goto enc_dec_loop_end
test_case = shift test_iterator
plain = test_case[0]
base64 = test_case[1]
enc_num = encoding plain
enc = encodingname enc_num
esc_plain = escape plain
bufused = bytelength plain
len = length plain
hex_str = hex_chars( plain, enc )
$P0 = new 'FixedPMCArray', 6
$P0[0] = count
$P0[1] = enc
$P0[2] = esc_plain
$P0[3] = hex_str
$P0[4] = bufused
$P0[5] = len
comment = sprintf "encode%d %s:\"%s\" %s(size=%d,len=%d)", $P0
unless bigendian goto BE_SWAP
base64 = be_result[count]
BE_SWAP:
test_encode( plain, base64, comment )
comment = 'decode'
comment_count = count
comment = concat comment, comment_count
test_decode( plain, base64, comment )
inc count
goto enc_dec_loop
enc_dec_loop_end:
test_iterator = iter decode_tests
dec_loop:
unless test_iterator goto dec_loop_end
test_case = shift test_iterator
base64 = test_case[0]
plain = test_case[1]
$I0 = encoding plain
enc = encodingname $I0
esc_plain = escape plain
$P0 = new 'ResizablePMCArray'
push $P0, comment_count
push $P0, enc
push $P0, esc_plain
push $P0, $I0
comment = sprintf "decode%d %s:\"%s\"(enc=%d)", $P0
test_decode( plain, base64, comment )
inc count
goto dec_loop
dec_loop_end:
.end
.sub hex_chars
.param string chars
.param string enc
.local string s
.local pmc bb
.local int len
bb = new 'ByteBuffer'
s = clone chars
bb = s
s = bb.'get_string'(enc)
len = length s
$P0 = new 'FixedPMCArray', 2
$I1 = 0
$S0 = ''
loop_1:
$I0 = ord s, $I1
$P0[0] = $S0
$P0[1] = $I0
$S0 = sprintf '%s\x%x', $P0
inc $I1
if $I1 < len goto loop_1
## $S0 = chopn $S0, 2
.return ($S0)
.end
.sub test_encode
.param string plain
.param string base64
.param string comment
.local pmc enc_sub
enc_sub = get_global [ "MIME"; "Base64" ], 'encode_base64'
.local pmc is
is = get_hll_global [ 'Test'; 'More' ], 'is'
.local string result_encode
result_encode = enc_sub( plain )
is( result_encode, base64, comment )
.end
.sub test_decode
.param string plain
.param string base64
.param string comment
.include "iglobals.pasm"
.local pmc interp
interp = getinterp
.local pmc config
config = interp[.IGLOBALS_CONFIG_HASH]
.local int has_icu
has_icu = config['has_icu']
.local int bigendian
bigendian = config['bigendian']
.local pmc dec_sub
dec_sub = get_global [ "MIME"; "Base64" ], 'decode_base64'
.local pmc is
is = get_hll_global [ 'Test'; 'More' ], 'is'
.local string decode, result_norm, plain_norm
.local string enc, enc1
$I0 = encoding plain
enc = encodingname $I0
decode = dec_sub( base64, enc )
$I1 = encoding decode
enc1 = encodingname $I1
$P0 = new 'ResizablePMCArray'
push $P0, comment
push $P0, enc1
push $P0, enc
push $P0, $I0
comment = sprintf "%s %s<-%s(enc=%d)", $P0
if enc == 'binary' goto HEX_C
if has_icu goto HAS_ICU
is( decode, plain, comment )
goto END
HAS_ICU:
result_norm = compose decode
plain_norm = compose plain
is( result_norm, plain_norm, comment )
goto END
HEX_C:
$S0 = hex_chars(decode, enc1)
$S1 = hex_chars(plain, enc)
is( $S0, $S1, comment )
END:
.end
=head1 AUTHOR
Reini Urban, Bernhard Schmalhofer <Bernhard Schmalhofer@gmx.de> and others.
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: