27import java.nio.ByteBuffer;
28import java.nio.charset.StandardCharsets;
30import org.jau.util.BaseCodec;
31import org.jau.util.BasicTypes;
32import org.jau.util.BaseCodec.Alphabet;
33import org.junit.Assert;
34import org.junit.FixMethodOrder;
36import org.junit.runners.MethodSorters;
38@FixMethodOrder(MethodSorters.NAME_ASCENDING)
41 static final Alphabet natural86_alphabet =
new BaseCodec.Natural86Alphabet();
43 private static void testRadix_3digits_int32(
final Alphabet aspec) {
44 final int base = aspec.base();
45 Assert.assertTrue( 1 < base );
47 final char min_cp = aspec.charAt(0);
48 final char max_cp = aspec.charAt(base-1);
50 final int min = (int)BaseCodec.decode(
""+min_cp, aspec);
51 final int max = (int)BaseCodec.decode(
""+max_cp+max_cp+max_cp, aspec);
52 final int max_s = (int)BaseCodec.decode(
""+max_cp, aspec);
54 final double machine_epsilon = BasicTypes.machineEpsilonDouble();
55 Assert.assertEquals(0, min);
56 Assert.assertEquals(base-1, max_s);
57 Assert.assertTrue( Math.abs( Math.pow(base, 3)-1 - max ) <= machine_epsilon );
59 final String r1_min = BaseCodec.encode(0, aspec, 3);
60 final String r1_min_s = BaseCodec.encode(0, aspec);
61 Assert.assertEquals(
""+min_cp+min_cp+min_cp, r1_min);
62 Assert.assertEquals(
""+min_cp, r1_min_s);
64 final String r1_max = BaseCodec.encode(base-1, aspec, 3);
65 final String r1_max_s = BaseCodec.encode(base-1, aspec);
66 Assert.assertEquals(
""+min_cp+min_cp+max_cp, r1_max);
67 Assert.assertEquals(
""+max_cp, r1_max_s);
69 final String r3_max = BaseCodec.encode((
int)Math.pow(base, 3)-1, aspec, 3);
70 Assert.assertEquals(
""+max_cp+max_cp+max_cp, r3_max);
72 System.err.printf(
"Test base %d, %s: [%d .. %d] <-> ['%s' .. '%s'], %d years (max/365d)\n",
73 base, aspec, min, max,
74 BaseCodec.encode(min, aspec),
75 BaseCodec.encode(max, aspec), (max/365));
77 Assert.assertEquals(0, BaseCodec.decode(
""+min_cp+min_cp+min_cp, aspec));
78 Assert.assertEquals(
""+min_cp, BaseCodec.encode(0, aspec));
79 Assert.assertEquals(
""+min_cp+min_cp+min_cp, BaseCodec.encode(0, aspec, 3));
81 Assert.assertEquals(max, BaseCodec.decode(
""+max_cp+max_cp+max_cp, aspec));
82 Assert.assertEquals(
""+max_cp+max_cp+max_cp, BaseCodec.encode(max, aspec, 3));
83 Assert.assertEquals(max_s, BaseCodec.decode(
""+max_cp, aspec));
84 Assert.assertEquals(
""+min_cp+min_cp+max_cp, BaseCodec.encode(max_s, aspec, 3));
87 final int v0_d = (int)BaseCodec.decode(r1_max, aspec);
88 final String v1_s = BaseCodec.encode(base-1, aspec, 3);
90 Assert.assertEquals(r1_max, v1_s);
91 Assert.assertEquals(base-1, v0_d);
94 final int v0_d = (int)BaseCodec.decode(r3_max, aspec);
95 final String v1_s = BaseCodec.encode(max, aspec, 3);
96 Assert.assertEquals(r3_max, v1_s);
97 Assert.assertEquals(max, v0_d);
99 for(
int iter=min; iter<=max; ++iter) {
100 final String rad = BaseCodec.encode(iter, aspec, 3);
101 final int dec = (int)BaseCodec.decode(rad, aspec);
102 Assert.assertEquals(iter, dec);
104 if( natural86_alphabet.equals( aspec ) ) {
106 System.err.printf(
"Natural 0-9: ");
107 for(
int iter=0; iter<=9; ++iter) {
108 final String rad = BaseCodec.encode(iter, aspec);
109 System.err.printf(
"%s, ", rad);
110 final char c = (char)(
'0'+iter);
111 Assert.assertEquals(
""+c, rad);
113 System.err.println();
117 private static void testRadix_int64(
final Alphabet aspec,
final long test_min,
final long test_max) {
118 final int int64_max_enc_width = 11;
119 final int base = aspec.base();
120 Assert.assertTrue( 1 < base );
122 final char min_cp = aspec.charAt(0);
123 final char max_cp = aspec.charAt(base-1);
125 final String max_radix = BaseCodec.encode(Long.MAX_VALUE, aspec, int64_max_enc_width);
127 final long min = BaseCodec.decode(
""+min_cp, aspec);
128 final long max = BaseCodec.decode(max_radix, aspec);
129 final long max_s = BaseCodec.decode(
""+max_cp, aspec);
131 Assert.assertEquals(0, min);
132 Assert.assertEquals(base-1, max_s);
133 Assert.assertEquals(Long.MAX_VALUE, max);
135 final String r1_min = BaseCodec.encode(0, aspec, int64_max_enc_width);
136 final String r1_min_s = BaseCodec.encode(0, aspec);
137 Assert.assertEquals(
""+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp, r1_min);
138 Assert.assertEquals(
""+min_cp, r1_min_s);
140 final String r1_max = BaseCodec.encode(base-1, aspec, int64_max_enc_width);
141 final String r1_max_s = BaseCodec.encode(base-1, aspec);
142 Assert.assertEquals(
""+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+max_cp, r1_max);
143 Assert.assertEquals(
""+max_cp, r1_max_s);
145 System.err.printf(
"Test base %d, %s: [%d .. %d] <-> ['%s' .. '%s'], %d years (max/365d)\n",
146 base, aspec, min, max,
147 BaseCodec.encode(min, aspec),
148 BaseCodec.encode(max, aspec), (max/365));
150 System.err.printf(
"- range: [%d .. %d] <-> ['%s' .. '%s']\n",
152 BaseCodec.encode(test_min, aspec), BaseCodec.encode(test_max, aspec));
154 Assert.assertEquals(0, BaseCodec.decode(
""+min_cp+min_cp+min_cp, aspec));
155 Assert.assertEquals(
""+min_cp, BaseCodec.encode(0, aspec));
158 final long v0_d = BaseCodec.decode(r1_max, aspec);
159 final String v1_s = BaseCodec.encode(base-1, aspec, int64_max_enc_width);
160 Assert.assertEquals(r1_max, v1_s);
161 Assert.assertEquals(base-1, v0_d);
163 for(
long iter=Math.max(0L, test_min-1); iter<test_max; ) {
165 final String rad = BaseCodec.encode(iter, aspec, int64_max_enc_width);
166 final long dec = BaseCodec.decode(rad, aspec);
170 Assert.assertEquals(iter, dec);
174 private static void testIntegerBase64(
final Alphabet alphabet) {
175 testRadix_3digits_int32(alphabet);
176 testRadix_int64(alphabet, 0x7fffff00L, 0x80000100L);
177 testRadix_int64(alphabet, 0xFFFFFFF0L, 0x100000010L);
178 testRadix_int64(alphabet, 0x7FFFFFFFFFFFFFF0L, 0x7FFFFFFFFFFFFFFFL);
181 private static void testIntegerBase86(
final Alphabet alphabet) {
182 testRadix_3digits_int32(alphabet);
183 testRadix_int64(alphabet, 0x7fffff00L, 0x80000100L);
184 testRadix_int64(alphabet, 0xFFFFFFF0L, 0x100000010L);
185 testRadix_int64(alphabet, 0x7FFFFFFFFFFFFFF0L, 0x7FFFFFFFFFFFFFFFL);
190 testRadix_3digits_int32(
new BaseCodec.Ascii38Alphabet());
195 testIntegerBase64(
new BaseCodec.Base64Alphabet());
196 testIntegerBase64(
new BaseCodec.Base64urlAlphabet());
197 testIntegerBase64(
new BaseCodec.Ascii64Alphabet());
202 testIntegerBase86(
new BaseCodec.Natural86Alphabet());
203 testIntegerBase86(
new BaseCodec.Ascii86Alphabet());
207 private static final String data =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
211 if (
'A' <= c && c <=
'Z') {
213 }
else if (
'a' <= c && c <=
'z') {
215 }
else if (
'0' <= c && c <=
'9') {
217 }
else if (
'+' == c) {
219 }
else if (
'/' == c) {
227 super(
"base64", 64, data, (
char)0);
231 private static byte[] to_array(
final ByteBuffer bb) {
232 final byte[] res =
new byte[bb.remaining()];
233 final int pos = bb.position();
234 bb.get(res, 0, res.length);
238 private static void testBinaryBase64() {
239 final BaseCodec.Base64Alphabet aspec =
new BaseCodec.Base64Alphabet();
240 final BaseCodec.Base64urlAlphabet aspec_url =
new BaseCodec.Base64urlAlphabet();
241 final Base64AlphabetNopadding aspec_nopadding =
new Base64AlphabetNopadding();
245 final byte[] octets = { };
246 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
247 Assert.assertEquals(
"", encstr);
248 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
249 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
250 Assert.assertEquals( ByteBuffer.wrap(octets), dec_octets );
253 final byte[] octets = {
'f' };
254 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
255 Assert.assertEquals(
"Zg==", encstr);
256 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
257 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
258 Assert.assertEquals( ByteBuffer.wrap(octets), dec_octets );
261 final byte[] octets = {
'f',
'o' };
262 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
263 Assert.assertEquals(
"Zm8=", encstr);
264 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
265 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
268 final byte[] octets = {
'f',
'o',
'o' };
269 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
270 Assert.assertEquals(
"Zm9v", encstr);
271 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
272 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
275 final byte[] octets = {
'f',
'o',
'o',
'b' };
276 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
277 Assert.assertEquals(
"Zm9vYg==", encstr);
278 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
279 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
282 final byte[] octets = {
'f',
'o',
'o',
'b',
'a' };
283 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
284 Assert.assertEquals(
"Zm9vYmE=", encstr);
285 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
286 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
291 final byte[] octets = {
'a' };
292 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
293 Assert.assertEquals(
"YQ==", encstr);
294 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
295 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
298 final byte[] octets = {
'a',
'b' };
299 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
300 Assert.assertEquals(
"YWI=", encstr);
301 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
302 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
305 final byte[] octets = {
'a',
'b',
'c' };
306 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
307 Assert.assertEquals(
"YWJj", encstr);
308 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
309 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
312 final byte[] octets = {
'a',
'b',
'c',
'd' };
313 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
314 Assert.assertEquals(
"YWJjZA==", encstr);
315 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
316 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
319 final byte[] octets = {
'a',
'b',
'c',
'd',
'e' };
320 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
321 Assert.assertEquals(
"YWJjZGU=", encstr);
322 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
323 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
326 final byte[] octets = {
'a',
'b',
'c',
'd',
'e',
'f',
'g' };
327 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
328 Assert.assertEquals(
"YWJjZGVmZw==", encstr);
329 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
330 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
334 final byte[] octets = {
'a',
'b',
'c',
'd',
'e',
'f',
'g' };
335 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
336 Assert.assertEquals(
"YWJjZGVmZw", encstr);
339 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
340 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
344 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
345 Assert.assertEquals( 0, dec_octets.remaining() );
350 final byte[] octets = {
'a' };
351 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
352 Assert.assertEquals(
"YQ", encstr);
355 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
356 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
360 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
361 Assert.assertEquals( 0, dec_octets.remaining() );
366 final byte[] octets = {
'a',
'b',
'c',
'd',
'e' };
367 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
368 Assert.assertEquals(
"YWJjZGU", encstr);
371 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
372 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
376 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
377 Assert.assertEquals( 0, dec_octets.remaining() );
382 final byte[] octets = {
'a',
'b' };
383 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
384 Assert.assertEquals(
"YWI", encstr);
387 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
388 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
392 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
393 Assert.assertEquals( 0, dec_octets.remaining() );
398 final byte[] octets = {
'a',
'b',
'c' };
399 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
400 Assert.assertEquals(
"YWJj", encstr);
403 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
404 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
408 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
409 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
413 final String in_str =
"aaaaaaaaaaaaaaaaa";
414 final byte[] octets = in_str.getBytes(StandardCharsets.UTF_8);
415 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
416 Assert.assertEquals(
"YWFhYWFhYWFhYWFhYWFhYWE=", encstr);
417 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
418 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
423 final byte[] octets = { (byte)0x03, (
byte)0xef, (byte)0xff, (
byte)0xf9 };
424 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
425 Assert.assertEquals(
"A+//+Q==", encstr);
426 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
427 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
431 final byte[] octets = { (byte)0x03, (
byte)0xef, (byte)0xff, (
byte)0xf9 };
432 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_url).toString();
433 Assert.assertEquals(
"A-__-Q==", encstr);
434 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_url);
435 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
439 final String in_str =
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
440 final String exp_encstr =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0"+
441 "d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl"+
442 "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
443 final byte[] octets = in_str.getBytes(StandardCharsets.UTF_8);
444 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec).toString();
445 Assert.assertEquals( exp_encstr, encstr);
446 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
447 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
450 final String in_str =
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
451 final String exp_encstr =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0\n"+
452 "d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl\n"+
453 "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
454 final byte[] octets = in_str.getBytes(StandardCharsets.UTF_8);
455 final String encstr = BaseCodec.encode64_mime(octets, 0, octets.length, aspec).toString();
456 Assert.assertEquals( exp_encstr, encstr);
457 final ByteBuffer dec_octets = BaseCodec.decode64_lf(encstr, aspec);
458 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
461 final String in_str =
"one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
462 final String exp_encstr =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVu\n"+
463 "IGVsZXZlbiB0d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVu\n"+
464 "IHNldmVudGVlbiBlaWdodGVlbiBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
465 final byte[] octets = in_str.getBytes(StandardCharsets.UTF_8);
466 final String encstr = BaseCodec.encode64_pem(octets, 0, octets.length, aspec).toString();
467 Assert.assertEquals( exp_encstr, encstr);
468 final ByteBuffer dec_octets = BaseCodec.decode64_lf(encstr, aspec);
469 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
474 final String encstr =
"!@#$%^&*()";
475 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
476 Assert.assertEquals( 0, dec_octets.remaining() );
486 public static void main(
final String args[]) {
487 org.junit.runner.JUnitCore.main(
TestBaseCodec.class.getName());
Base64AlphabetNopadding()
int code_point(final char c)
void test01IntegerBase38()
static void main(final String args[])
void test03IntegerBase86()
void test11BinaryBase86()
void test02IntegerBase64()