Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestBaseCodec.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package jau.test.util;
26
27import java.nio.ByteBuffer;
28import java.nio.charset.StandardCharsets;
29
30import org.jau.util.BaseCodec;
31import org.jau.util.BasicTypes;
32import org.jau.util.BaseCodec.Alphabet;
33import org.junit.Assert;
34import org.junit.FixMethodOrder;
35import org.junit.Test;
36import org.junit.runners.MethodSorters;
37
38@FixMethodOrder(MethodSorters.NAME_ASCENDING)
39public class TestBaseCodec {
40
41 static final Alphabet natural86_alphabet = new BaseCodec.Natural86Alphabet();
42
43 private static void testRadix_3digits_int32(final Alphabet aspec) {
44 final int base = aspec.base();
45 Assert.assertTrue( 1 < base );
46
47 final char min_cp = aspec.charAt(0); // minimum code-point
48 final char max_cp = aspec.charAt(base-1); // maximum code-point
49
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);
53
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 );
58
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);
63
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);
68
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);
71
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));
76
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));
80
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));
85
86 {
87 final int v0_d = (int)BaseCodec.decode(r1_max, aspec);
88 final String v1_s = BaseCodec.encode(base-1, aspec, 3);
89 // System.err.printf("r1_max '%s' ('%s'), base-1 %d (%d)\n", r1_max, v1_s, base-1, v0_d);
90 Assert.assertEquals(r1_max, v1_s);
91 Assert.assertEquals(base-1, v0_d);
92 }
93 {
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);
98 }
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);
103 }
104 if( natural86_alphabet.equals( aspec ) ) {
105 // Test 0-9 ..
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);
112 }
113 System.err.println();
114 }
115 }
116
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; // 9223372036854775807 == '7__________' (base 64, natural)
119 final int base = aspec.base();
120 Assert.assertTrue( 1 < base );
121
122 final char min_cp = aspec.charAt(0); // minimum code-point
123 final char max_cp = aspec.charAt(base-1); // maximum code-point
124
125 final String max_radix = BaseCodec.encode(Long.MAX_VALUE, aspec, int64_max_enc_width);
126
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);
130
131 Assert.assertEquals(0, min);
132 Assert.assertEquals(base-1, max_s);
133 Assert.assertEquals(Long.MAX_VALUE, max);
134
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);
139
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);
144
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));
149
150 System.err.printf("- range: [%d .. %d] <-> ['%s' .. '%s']\n",
151 test_min, test_max,
152 BaseCodec.encode(test_min, aspec), BaseCodec.encode(test_max, aspec));
153
154 Assert.assertEquals(0, BaseCodec.decode(""+min_cp+min_cp+min_cp, aspec));
155 Assert.assertEquals(""+min_cp, BaseCodec.encode(0, aspec));
156
157 {
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);
162 }
163 for(long iter=Math.max(0L, test_min-1); iter<test_max; ) {
164 ++iter;
165 final String rad = BaseCodec.encode(iter, aspec, int64_max_enc_width);
166 final long dec = BaseCodec.decode(rad, aspec);
167 // if( false ) {
168 // System.err.printf("test base %d: iter %d, rad '%s', dec %d\n", base, iter, rad, dec);
169 // }
170 Assert.assertEquals(iter, dec);
171 }
172 }
173
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);
179 }
180
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);
186 }
187
188 @Test
189 public void test01IntegerBase38() {
190 testRadix_3digits_int32(new BaseCodec.Ascii38Alphabet());
191 }
192
193 @Test
194 public void test02IntegerBase64() {
195 testIntegerBase64(new BaseCodec.Base64Alphabet());
196 testIntegerBase64(new BaseCodec.Base64urlAlphabet());
197 testIntegerBase64(new BaseCodec.Ascii64Alphabet());
198 }
199
200 @Test
201 public void test03IntegerBase86() {
202 testIntegerBase86(new BaseCodec.Natural86Alphabet());
203 testIntegerBase86(new BaseCodec.Ascii86Alphabet());
204 }
205
206 public static class Base64AlphabetNopadding extends Alphabet {
207 private static final String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
208
209 @Override
210 public int code_point(final char c) {
211 if ('A' <= c && c <= 'Z') {
212 return c - 'A';
213 } else if ('a' <= c && c <= 'z') {
214 return c - 'a' + 26;
215 } else if ('0' <= c && c <= '9') {
216 return c - '0' + 52;
217 } else if ('+' == c) {
218 return 62;
219 } else if ('/' == c) {
220 return 63;
221 } else {
222 return -1;
223 }
224 }
225
227 super("base64", 64, data, (char)0);
228 }
229 }
230
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);
235 bb.position(pos);
236 return res;
237 }
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();
242
243 // Test Vectors taken from `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html)
244 {
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 );
251 }
252 {
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 );
259 }
260 {
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 ) );
266 }
267 {
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 ) );
273 }
274 {
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 ) );
280 }
281 {
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 ) );
287 }
288
289 // Further encoding tests
290 {
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 ) );
296 }
297 {
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 ) );
303 }
304 {
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 ) );
310 }
311 {
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 ) );
317 }
318 {
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 ) );
324 }
325 {
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 ) );
331 }
332 {
333 // Test no-padding accept and error, double padding dropped '=='
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);
337 {
338 // accept no padding
339 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
340 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
341 }
342 {
343 // not accepting lack of padding
344 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
345 Assert.assertEquals( 0, dec_octets.remaining() );
346 }
347 }
348 {
349 // Test no-padding accept and error, double padding dropped '=='
350 final byte[] octets = { 'a' };
351 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
352 Assert.assertEquals( "YQ", encstr);
353 {
354 // accept no padding
355 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
356 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
357 }
358 {
359 // not accepting lack of padding
360 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
361 Assert.assertEquals( 0, dec_octets.remaining() );
362 }
363 }
364 {
365 // Test no-padding accept and error, single padding dropped '='
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);
369 {
370 // accept no padding
371 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
372 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
373 }
374 {
375 // not accepting lack of padding
376 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
377 Assert.assertEquals( 0, dec_octets.remaining() );
378 }
379 }
380 {
381 // Test no-padding accept and error, single padding dropped '='
382 final byte[] octets = { 'a', 'b' };
383 final String encstr = BaseCodec.encode64(octets, 0, octets.length, aspec_nopadding).toString();
384 Assert.assertEquals( "YWI", encstr);
385 {
386 // accept no padding
387 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
388 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
389 }
390 {
391 // not accepting lack of padding
392 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
393 Assert.assertEquals( 0, dec_octets.remaining() );
394 }
395 }
396 {
397 // Test no-padding accept and error, zero padding dropped
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);
401 {
402 // accept no padding
403 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec_nopadding);
404 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
405 }
406 {
407 // accept no padding
408 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
409 Assert.assertArrayEquals( octets, to_array( dec_octets ) );
410 }
411 }
412 {
413 final String in_str = "aaaaaaaaaaaaaaaaa"; // a17
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 ) );
419 }
420
421 {
422 // Test code-points 63 and 64 of base64
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 ) );
428 }
429 {
430 // Test code-points 63 and 64 of base64url
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 ) );
436 }
437
438 {
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 ) );
448 }
449 {
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 ) );
459 }
460 {
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 ) );
470 }
471
472 // Erroneous coded string in decoding
473 {
474 final String encstr = "!@#$%^&*()"; // non-alphebet error
475 final ByteBuffer dec_octets = BaseCodec.decode64(encstr, aspec);
476 Assert.assertEquals( 0, dec_octets.remaining() );
477 }
478
479 }
480
481 @Test
482 public void test11BinaryBase86() {
483 testBinaryBase64();
484 }
485
486 public static void main(final String args[]) {
487 org.junit.runner.JUnitCore.main(TestBaseCodec.class.getName());
488 }
489
490}
static void main(final String args[])