test/java/util/Base64/TestBase64Golden.java

Print this page




  23 
  24 /*
  25  * @test 4235519
  26  * @author Eric Wang <yiming.wang@oracle.com>
  27  * @summary tests java.util.Base64
  28  */
  29 
  30 import java.io.BufferedReader;
  31 import java.io.FileReader;
  32 import java.nio.ByteBuffer;
  33 import java.nio.charset.Charset;
  34 import java.nio.charset.StandardCharsets;
  35 import java.nio.file.Files;
  36 import java.nio.file.Paths;
  37 import java.util.Base64;
  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),


 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




  23 
  24 /*
  25  * @test 4235519
  26  * @author Eric Wang <yiming.wang@oracle.com>
  27  * @summary tests java.util.Base64
  28  */
  29 
  30 import java.io.BufferedReader;
  31 import java.io.FileReader;
  32 import java.nio.ByteBuffer;
  33 import java.nio.charset.Charset;
  34 import java.nio.charset.StandardCharsets;
  35 import java.nio.file.Files;
  36 import java.nio.file.Paths;
  37 import java.util.Base64;
  38 import java.util.Base64.Decoder;
  39 import java.util.Base64.Encoder;
  40 import java.util.Objects;
  41 import java.util.Random;
  42 



  43 public class TestBase64Golden {
  44 
  45     public static void main(String[] args) throws Exception {
  46         test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),
  47               "plain.txt", "baseEncode.txt");
  48 
  49         test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),
  50               "plain.txt", "urlEncode.txt");
  51 
  52         test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),
  53               "plain.txt", "mimeEncode.txt");
  54         test1();
  55     }
  56 
  57     public static void test0(Base64Type type, Encoder encoder, Decoder decoder,
  58                              String srcFile, String encodedFile) throws Exception {
  59 
  60         String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)
  61                                .toArray(new String[0]);
  62         String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile),


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










 138         }
 139     }
 140 
 141     private static void test1() throws Exception {
 142         byte[] src = new byte[] {
 143             46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
 144             40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
 145             -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
 146         Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
 147         byte[] encoded = encoder.encode(src);
 148         Decoder decoder = Base64.getMimeDecoder();
 149         byte[] decoded = decoder.decode(encoded);
 150         if (!Objects.deepEquals(src, decoded)) {
 151             throw new RuntimeException();
 152         }
 153     }
 154 
 155     // helper
 156     enum Base64Type {
 157         BASIC, URLSAFE, MIME