test/java/util/Base64/TestBase64Golden.java

Print this page




  38 import java.util.Base64.Decoder;
  39 import java.util.Base64.Encoder;
  40 import java.util.Objects;
  41 import java.util.Random;
  42 
  43 import sun.misc.BASE64Decoder;
  44 import sun.misc.BASE64Encoder;
  45 
  46 public class TestBase64Golden {
  47 
  48     public static void main(String[] args) throws Exception {
  49         test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),
  50               "plain.txt", "baseEncode.txt");
  51 
  52         test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),
  53               "plain.txt", "urlEncode.txt");
  54 
  55         test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),
  56               "plain.txt", "mimeEncode.txt");
  57         test1();
  58         test2();
  59     }
  60 
  61     public static void test0(Base64Type type, Encoder encoder, Decoder decoder,
  62                              String srcFile, String encodedFile) throws Exception {
  63 
  64         String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)
  65                                .toArray(new String[0]);
  66         String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile),
  67                                                  DEF_CHARSET)
  68                                    .toArray(new String[0]);
  69         int lns = 0;
  70         for (String srcStr : srcLns) {
  71             String encodedStr = null;
  72             if (type != Base64Type.MIME) {
  73                 encodedStr = encodedLns[lns++];
  74             } else {
  75                 while (lns < encodedLns.length) {
  76                     String s = encodedLns[lns++];
  77                     if (s.length() == 0)
  78                         break;


  96             ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);
  97             byte[] resArr = new byte[encodedArr.length];
  98 
  99             // test int encode(byte[], byte[])
 100             int len = encoder.encode(srcArr, resArr);
 101             assertEqual(len, encodedArr.length);
 102             assertEqual(resArr, encodedArr);
 103 
 104             // test byte[] encode(byte[])
 105             resArr = encoder.encode(srcArr);
 106             assertEqual(resArr, encodedArr);
 107 
 108             // test ByteBuffer encode(ByteBuffer)
 109             int limit = srcBuf.limit();
 110             ByteBuffer resBuf = encoder.encode(srcBuf);
 111             assertEqual(srcBuf.position(), limit);
 112             assertEqual(srcBuf.limit(), limit);
 113             assertEqual(resBuf, encodedBuf);
 114             srcBuf.rewind(); // reset for next test
 115 
 116             // test encode(ByteBuffer, ByteBuffer, bytesOut)
 117             resBuf.clear();
 118             len = encoder.encode(srcBuf, resBuf, 0);
 119             assertEqual(len, encodedArr.length);
 120             assertEqual(srcBuf.position(), limit);
 121             assertEqual(srcBuf.limit(), limit);
 122             assertEqual(resBuf.position(), len);
 123             resBuf.flip();
 124             assertEqual(resBuf, encodedBuf);
 125             srcBuf.rewind();
 126 
 127             // test encode(ByteBuffer, ByteBuffer, bytesOut)[direct]
 128             ByteBuffer resBuf_d = ByteBuffer.allocateDirect(encodedArr.length);
 129             len = encoder.encode(srcBuf, resBuf_d, 0);
 130             assertEqual(len, encodedArr.length);
 131             assertEqual(srcBuf.position(), limit);
 132             assertEqual(srcBuf.limit(), limit);
 133             assertEqual(resBuf_d.position(), len);
 134             resBuf_d.flip();
 135             assertEqual(resBuf_d, encodedBuf);
 136             srcBuf.rewind();
 137 
 138             // test String encodeToString(byte[])
 139             String resEncodeStr = encoder.encodeToString(srcArr);
 140             assertEqual(resEncodeStr, encodedStr);
 141 
 142             // test int decode(byte[], byte[])
 143             resArr = new byte[srcArr.length];
 144             len = decoder.decode(encodedArr, resArr);
 145             assertEqual(len, srcArr.length);
 146             assertEqual(resArr, srcArr);
 147 
 148             // test byte[] decode(byte[])
 149             resArr = decoder.decode(encodedArr);
 150             assertEqual(resArr, srcArr);
 151 
 152             // test ByteBuffer decode(ByteBuffer)
 153             limit = encodedBuf.limit();
 154             resBuf = decoder.decode(encodedBuf);
 155             assertEqual(encodedBuf.position(), limit);
 156             assertEqual(encodedBuf.limit(), limit);
 157             assertEqual(resBuf, srcBuf);
 158             encodedBuf.rewind(); // reset for next test
 159 
 160             // test int decode(ByteBuffer, ByteBuffer)
 161             resBuf.clear();
 162             len = decoder.decode(encodedBuf, resBuf);
 163             assertEqual(len, srcArr.length);
 164             assertEqual(encodedBuf.position(), limit);
 165             assertEqual(encodedBuf.limit(), limit);
 166             assertEqual(resBuf.position(), len);
 167             resBuf.flip();
 168             assertEqual(resBuf, srcBuf);
 169             encodedBuf.rewind(); // reset for next test
 170 
 171             // test int decode(ByteBuffer, ByteBuffer)[direct]
 172             resBuf_d = ByteBuffer.allocateDirect(srcArr.length);
 173             len = decoder.decode(encodedBuf, resBuf_d);
 174             assertEqual(len, srcArr.length);
 175             assertEqual(encodedBuf.position(), limit);
 176             assertEqual(encodedBuf.limit(), limit);
 177             assertEqual(resBuf_d.position(), len);
 178             resBuf_d.flip();
 179             assertEqual(resBuf_d, srcBuf);
 180             encodedBuf.rewind(); // reset for next test
 181 
 182             // test byte[] decode(String)
 183             resArr = decoder.decode(encodedStr);
 184             assertEqual(resArr, srcArr);
 185 
 186             // test compatible with sun.misc.Base64Encoder
 187             if (type == Base64Type.MIME) {
 188                 sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder();
 189                 sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder();
 190                 resArr = decoder.decode(miscEncoder.encode(srcArr));
 191                 assertEqual(resArr, srcArr);
 192 
 193                 resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr));
 194                 assertEqual(new String(resArr, DEF_CHARSET), encodedStr);
 195             }
 196         }
 197     }
 198 
 199     private static void test1() throws Exception {
 200         byte[] src = new byte[6];
 201         new Random().nextBytes(src);
 202 
 203         ByteBuffer srcBuf = ByteBuffer.allocate(10);
 204         srcBuf.position(2);
 205         srcBuf.mark();
 206         srcBuf.limit(8);
 207         srcBuf.put(src);
 208         srcBuf.reset();
 209 
 210         ByteBuffer dstBuf = ByteBuffer.allocate((src.length + 2) / 3 * 4);
 211         Base64.getEncoder().encode(srcBuf, dstBuf, 0);
 212         dstBuf.rewind();
 213         byte[] dst = new byte[dstBuf.limit()];
 214         dstBuf.get(dst);
 215         System.out.printf("%n    src[%d]: %s%n", src.length, new String(src));
 216         System.out.printf("encoded[%d]: %s%n",   dst.length, new String(dst));
 217         assertEqual(src, Base64.getDecoder().decode(dst));
 218 
 219         dstBuf = ByteBuffer.allocateDirect((src.length + 2) / 3 * 4);
 220         srcBuf.reset();
 221         Base64.getEncoder().encode(srcBuf, dstBuf, 0);
 222         dstBuf.rewind();
 223         dst = new byte[dstBuf.limit()];
 224         dstBuf.get(dst);
 225         assertEqual(src, Base64.getDecoder().decode(dst));
 226     }
 227 
 228     private static void test2() throws Exception {
 229         byte[] src = new byte[] {
 230             46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
 231             40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
 232             -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
 233         Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
 234         byte[] encoded = encoder.encode(src);
 235         Decoder decoder = Base64.getMimeDecoder();
 236         byte[] decoded = decoder.decode(encoded);
 237         if (!Objects.deepEquals(src, decoded)) {
 238             throw new RuntimeException();
 239         }
 240     }
 241 
 242     // helper
 243     enum Base64Type {
 244         BASIC, URLSAFE, MIME
 245     }
 246 
 247     private static final String SRCDIR = System.getProperty("test.src", ".");
 248     private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;


  38 import java.util.Base64.Decoder;
  39 import java.util.Base64.Encoder;
  40 import java.util.Objects;
  41 import java.util.Random;
  42 
  43 import sun.misc.BASE64Decoder;
  44 import sun.misc.BASE64Encoder;
  45 
  46 public class TestBase64Golden {
  47 
  48     public static void main(String[] args) throws Exception {
  49         test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),
  50               "plain.txt", "baseEncode.txt");
  51 
  52         test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),
  53               "plain.txt", "urlEncode.txt");
  54 
  55         test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),
  56               "plain.txt", "mimeEncode.txt");
  57         test1();

  58     }
  59 
  60     public static void test0(Base64Type type, Encoder encoder, Decoder decoder,
  61                              String srcFile, String encodedFile) throws Exception {
  62 
  63         String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)
  64                                .toArray(new String[0]);
  65         String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile),
  66                                                  DEF_CHARSET)
  67                                    .toArray(new String[0]);
  68         int lns = 0;
  69         for (String srcStr : srcLns) {
  70             String encodedStr = null;
  71             if (type != Base64Type.MIME) {
  72                 encodedStr = encodedLns[lns++];
  73             } else {
  74                 while (lns < encodedLns.length) {
  75                     String s = encodedLns[lns++];
  76                     if (s.length() == 0)
  77                         break;


  95             ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);
  96             byte[] resArr = new byte[encodedArr.length];
  97 
  98             // test int encode(byte[], byte[])
  99             int len = encoder.encode(srcArr, resArr);
 100             assertEqual(len, encodedArr.length);
 101             assertEqual(resArr, encodedArr);
 102 
 103             // test byte[] encode(byte[])
 104             resArr = encoder.encode(srcArr);
 105             assertEqual(resArr, encodedArr);
 106 
 107             // test ByteBuffer encode(ByteBuffer)
 108             int limit = srcBuf.limit();
 109             ByteBuffer resBuf = encoder.encode(srcBuf);
 110             assertEqual(srcBuf.position(), limit);
 111             assertEqual(srcBuf.limit(), limit);
 112             assertEqual(resBuf, encodedBuf);
 113             srcBuf.rewind(); // reset for next test
 114 






















 115             // test String encodeToString(byte[])
 116             String resEncodeStr = encoder.encodeToString(srcArr);
 117             assertEqual(resEncodeStr, encodedStr);
 118 
 119             // test int decode(byte[], byte[])
 120             resArr = new byte[srcArr.length];
 121             len = decoder.decode(encodedArr, resArr);
 122             assertEqual(len, srcArr.length);
 123             assertEqual(resArr, srcArr);
 124 
 125             // test byte[] decode(byte[])
 126             resArr = decoder.decode(encodedArr);
 127             assertEqual(resArr, srcArr);
 128 
 129             // test ByteBuffer decode(ByteBuffer)
 130             limit = encodedBuf.limit();
 131             resBuf = decoder.decode(encodedBuf);
 132             assertEqual(encodedBuf.position(), limit);
 133             assertEqual(encodedBuf.limit(), limit);
 134             assertEqual(resBuf, srcBuf);
 135             encodedBuf.rewind(); // reset for next test
 136 






















 137             // test byte[] decode(String)
 138             resArr = decoder.decode(encodedStr);
 139             assertEqual(resArr, srcArr);
 140 
 141             // test compatible with sun.misc.Base64Encoder
 142             if (type == Base64Type.MIME) {
 143                 sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder();
 144                 sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder();
 145                 resArr = decoder.decode(miscEncoder.encode(srcArr));
 146                 assertEqual(resArr, srcArr);
 147 
 148                 resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr));
 149                 assertEqual(new String(resArr, DEF_CHARSET), encodedStr);
 150             }
 151         }
 152     }
 153 
 154     private static void test1() throws Exception {





























 155         byte[] src = new byte[] {
 156             46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
 157             40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
 158             -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
 159         Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
 160         byte[] encoded = encoder.encode(src);
 161         Decoder decoder = Base64.getMimeDecoder();
 162         byte[] decoded = decoder.decode(encoded);
 163         if (!Objects.deepEquals(src, decoded)) {
 164             throw new RuntimeException();
 165         }
 166     }
 167 
 168     // helper
 169     enum Base64Type {
 170         BASIC, URLSAFE, MIME
 171     }
 172 
 173     private static final String SRCDIR = System.getProperty("test.src", ".");
 174     private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;