test/java/util/Base64/TestBase64.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
  26  *       8014217 8025003
  27  * @summary tests java.util.Base64
  28  */
  29 
  30 import java.io.ByteArrayInputStream;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.InputStream;
  33 import java.io.IOException;
  34 import java.io.OutputStream;
  35 import java.nio.ByteBuffer;
  36 import java.util.Arrays;
  37 import java.util.Base64;
  38 import java.util.Random;
  39 
  40 public class TestBase64 {
  41 
  42     public static void main(String args[]) throws Throwable {
  43         int numRuns  = 10;
  44         int numBytes = 200;
  45         if (args.length > 1) {
  46             numRuns  = Integer.parseInt(args[0]);
  47             numBytes = Integer.parseInt(args[1]);
  48         }
  49 
  50         test(Base64.getEncoder(),     Base64.getDecoder(),
  51              numRuns, numBytes);
  52         test(Base64.getUrlEncoder(),  Base64.getUrlDecoder(),
  53              numRuns, numBytes);
  54         test(Base64.getMimeEncoder(), Base64.getMimeDecoder(),
  55              numRuns, numBytes);
  56 
  57         Random rnd = new java.util.Random();
  58         byte[] nl_1 = new byte[] {'\n'};
  59         byte[] nl_2 = new byte[] {'\n', '\r'};
  60         byte[] nl_3 = new byte[] {'\n', '\r', '\n'};
  61         for (int i = 0; i < 10; i++) {
  62             int len = rnd.nextInt(200) + 4;
  63             test(Base64.getMimeEncoder(len, nl_1),
  64                  Base64.getMimeDecoder(),
  65                  numRuns, numBytes);
  66             test(Base64.getMimeEncoder(len, nl_2),
  67                  Base64.getMimeDecoder(),
  68                  numRuns, numBytes);
  69             test(Base64.getMimeEncoder(len, nl_3),
  70                  Base64.getMimeDecoder(),
  71                  numRuns, numBytes);
  72         }
  73 
  74         testNull(Base64.getEncoder());
  75         testNull(Base64.getUrlEncoder());


 125 
 126         // test decoding of unpadded data
 127         testDecodeUnpadded();
 128         // test mime decoding with ignored character after padding
 129         testDecodeIgnoredAfterPadding();
 130 
 131         // lenient mode for ending unit
 132         testLenientPadding();
 133 
 134     }
 135 
 136     private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
 137 
 138     private static void test(Base64.Encoder enc, Base64.Decoder dec,
 139                              int numRuns, int numBytes) throws Throwable {
 140         Random rnd = new java.util.Random();
 141 
 142         enc.encode(new byte[0]);
 143         dec.decode(new byte[0]);
 144 




 145         for (int i=0; i<numRuns; i++) {
 146             for (int j=1; j<numBytes; j++) {
 147                 byte[] orig = new byte[j];
 148                 rnd.nextBytes(orig);
 149 
 150                 // --------testing encode/decode(byte[])--------
 151                 byte[] encoded = enc.encode(orig);
 152                 byte[] decoded = dec.decode(encoded);
 153 
 154                 checkEqual(orig, decoded,
 155                            "Base64 array encoding/decoding failed!");
 156 




 157                 // compare to sun.misc.BASE64Encoder

 158                 byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII");
 159                 checkEqual(normalize(encoded),
 160                            normalize(encoded2),
 161                            "Base64 enc.encode() does not match sun.misc.base64!");
 162 
 163                 // remove padding '=' to test non-padding decoding case
 164                 if (encoded[encoded.length -2] == '=')
 165                     encoded2 = Arrays.copyOf(encoded,  encoded.length -2);
 166                 else if (encoded[encoded.length -1] == '=')
 167                     encoded2 = Arrays.copyOf(encoded, encoded.length -1);
 168                 else
 169                     encoded2 = null;
 170 
 171                 // --------testing encodetoString(byte[])/decode(String)--------
 172                 String str = enc.encodeToString(orig);
 173                 if (!Arrays.equals(str.getBytes("ASCII"), encoded)) {
 174                     throw new RuntimeException(
 175                         "Base64 encodingToString() failed!");
 176                 }
 177                 byte[] buf = dec.decode(new String(encoded, "ASCII"));
 178                 checkEqual(buf, orig, "Base64 decoding(String) failed!");
 179 
 180                 if (encoded2 != null) {
 181                     buf = dec.decode(new String(encoded2, "ASCII"));
 182                     checkEqual(buf, orig, "Base64 decoding(String) failed!");


 287                 buf = new byte[encoded.length + rnd.nextInt(100)];
 288                 int ret = enc.encode(orig, buf);
 289                 checkEqual(ret, encoded.length,
 290                            "Base64 enc.encode(src, null) returns wrong size!");
 291                 buf = Arrays.copyOf(buf, ret);
 292                 checkEqual(buf, encoded,
 293                            "Base64 enc.encode(src, dst) failed!");
 294 
 295                 // --------testing decode(in, out); -> bigger buf--------
 296                 buf = new byte[orig.length + rnd.nextInt(100)];
 297                 ret = dec.decode(encoded, buf);
 298                 checkEqual(ret, orig.length,
 299                           "Base64 enc.encode(src, null) returns wrong size!");
 300                 buf = Arrays.copyOf(buf, ret);
 301                 checkEqual(buf, orig,
 302                            "Base64 dec.decode(src, dst) failed!");
 303 
 304             }
 305         }
 306     }

 307 
 308     private static final byte[] ba_null = null;
 309     private static final String str_null = null;
 310     private static final ByteBuffer bb_null = null;
 311 
 312     private static void testNull(final Base64.Encoder enc) {
 313         checkNull(new Runnable() { public void run() { enc.encode(ba_null); }});
 314         checkNull(new Runnable() { public void run() { enc.encodeToString(ba_null); }});
 315         checkNull(new Runnable() { public void run() { enc.encode(ba_null, new byte[10]); }});
 316         checkNull(new Runnable() { public void run() { enc.encode(new byte[10], ba_null); }});
 317         checkNull(new Runnable() { public void run() { enc.encode(bb_null); }});
 318         checkNull(new Runnable() { public void run() { enc.encode(bb_null, ByteBuffer.allocate(10), 0); }});
 319         checkNull(new Runnable() { public void run() { enc.encode(ByteBuffer.allocate(10), bb_null, 0); }});
 320         checkNull(new Runnable() { public void run() { enc.wrap(null); }});
 321     }
 322 
 323     private static void testNull(final Base64.Decoder dec) {
 324         checkNull(new Runnable() { public void run() { dec.decode(ba_null); }});
 325         checkNull(new Runnable() { public void run() { dec.decode(str_null); }});
 326         checkNull(new Runnable() { public void run() { dec.decode(ba_null, new byte[10]); }});




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
  26  *       8014217 8025003 8026330
  27  * @summary tests java.util.Base64
  28  */
  29 
  30 import java.io.ByteArrayInputStream;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.InputStream;
  33 import java.io.IOException;
  34 import java.io.OutputStream;
  35 import java.nio.ByteBuffer;
  36 import java.util.Arrays;
  37 import java.util.Base64;
  38 import java.util.Random;
  39 
  40 public class TestBase64 {
  41 
  42     public static void main(String args[]) throws Throwable {
  43         int numRuns  = 10;
  44         int numBytes = 200;
  45         if (args.length > 1) {
  46             numRuns  = Integer.parseInt(args[0]);
  47             numBytes = Integer.parseInt(args[1]);
  48         }
  49 
  50         test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes);
  51         test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes);
  52         test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes);



  53 
  54         Random rnd = new java.util.Random();
  55         byte[] nl_1 = new byte[] {'\n'};
  56         byte[] nl_2 = new byte[] {'\n', '\r'};
  57         byte[] nl_3 = new byte[] {'\n', '\r', '\n'};
  58         for (int i = 0; i < 10; i++) {
  59             int len = rnd.nextInt(200) + 4;
  60             test(Base64.getMimeEncoder(len, nl_1),
  61                  Base64.getMimeDecoder(),
  62                  numRuns, numBytes);
  63             test(Base64.getMimeEncoder(len, nl_2),
  64                  Base64.getMimeDecoder(),
  65                  numRuns, numBytes);
  66             test(Base64.getMimeEncoder(len, nl_3),
  67                  Base64.getMimeDecoder(),
  68                  numRuns, numBytes);
  69         }
  70 
  71         testNull(Base64.getEncoder());
  72         testNull(Base64.getUrlEncoder());


 122 
 123         // test decoding of unpadded data
 124         testDecodeUnpadded();
 125         // test mime decoding with ignored character after padding
 126         testDecodeIgnoredAfterPadding();
 127 
 128         // lenient mode for ending unit
 129         testLenientPadding();
 130 
 131     }
 132 
 133     private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
 134 
 135     private static void test(Base64.Encoder enc, Base64.Decoder dec,
 136                              int numRuns, int numBytes) throws Throwable {
 137         Random rnd = new java.util.Random();
 138 
 139         enc.encode(new byte[0]);
 140         dec.decode(new byte[0]);
 141 
 142         for (boolean withoutPadding : new boolean[] { false, true}) {
 143             if (withoutPadding) {
 144                  enc = enc.withoutPadding();
 145             }
 146             for (int i=0; i<numRuns; i++) {
 147                 for (int j=1; j<numBytes; j++) {
 148                     byte[] orig = new byte[j];
 149                     rnd.nextBytes(orig);
 150     
 151                     // --------testing encode/decode(byte[])--------
 152                     byte[] encoded = enc.encode(orig);
 153                     byte[] decoded = dec.decode(encoded);
 154     
 155                     checkEqual(orig, decoded,
 156                                "Base64 array encoding/decoding failed!");
 157                     if (withoutPadding) {
 158                         if (encoded[encoded.length - 1] == '=')
 159                             throw new RuntimeException(
 160                                "Base64 enc.encode().withoutPadding() has padding!");
 161                     }
 162                     // compare to sun.misc.BASE64Encoder
 163 
 164                     byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII");
 165                     if (!withoutPadding) {    // don't test for withoutPadding()
 166                         checkEqual(normalize(encoded), normalize(encoded2),
 167                                    "Base64 enc.encode() does not match sun.misc.base64!");
 168                     }
 169                     // remove padding '=' to test non-padding decoding case
 170                     if (encoded[encoded.length -2] == '=')
 171                         encoded2 = Arrays.copyOf(encoded,  encoded.length -2);
 172                     else if (encoded[encoded.length -1] == '=')
 173                         encoded2 = Arrays.copyOf(encoded, encoded.length -1);
 174                     else
 175                         encoded2 = null;
 176     
 177                     // --------testing encodetoString(byte[])/decode(String)--------
 178                     String str = enc.encodeToString(orig);
 179                     if (!Arrays.equals(str.getBytes("ASCII"), encoded)) {
 180                         throw new RuntimeException(
 181                             "Base64 encodingToString() failed!");
 182                     }
 183                     byte[] buf = dec.decode(new String(encoded, "ASCII"));
 184                     checkEqual(buf, orig, "Base64 decoding(String) failed!");
 185     
 186                     if (encoded2 != null) {
 187                         buf = dec.decode(new String(encoded2, "ASCII"));
 188                         checkEqual(buf, orig, "Base64 decoding(String) failed!");


 293                     buf = new byte[encoded.length + rnd.nextInt(100)];
 294                     int ret = enc.encode(orig, buf);
 295                     checkEqual(ret, encoded.length,
 296                                "Base64 enc.encode(src, null) returns wrong size!");
 297                     buf = Arrays.copyOf(buf, ret);
 298                     checkEqual(buf, encoded,
 299                                "Base64 enc.encode(src, dst) failed!");
 300     
 301                     // --------testing decode(in, out); -> bigger buf--------
 302                     buf = new byte[orig.length + rnd.nextInt(100)];
 303                     ret = dec.decode(encoded, buf);
 304                     checkEqual(ret, orig.length,
 305                               "Base64 enc.encode(src, null) returns wrong size!");
 306                     buf = Arrays.copyOf(buf, ret);
 307                     checkEqual(buf, orig,
 308                                "Base64 dec.decode(src, dst) failed!");
 309     
 310                 }
 311             }
 312         }
 313     }
 314 
 315     private static final byte[] ba_null = null;
 316     private static final String str_null = null;
 317     private static final ByteBuffer bb_null = null;
 318 
 319     private static void testNull(final Base64.Encoder enc) {
 320         checkNull(new Runnable() { public void run() { enc.encode(ba_null); }});
 321         checkNull(new Runnable() { public void run() { enc.encodeToString(ba_null); }});
 322         checkNull(new Runnable() { public void run() { enc.encode(ba_null, new byte[10]); }});
 323         checkNull(new Runnable() { public void run() { enc.encode(new byte[10], ba_null); }});
 324         checkNull(new Runnable() { public void run() { enc.encode(bb_null); }});
 325         checkNull(new Runnable() { public void run() { enc.encode(bb_null, ByteBuffer.allocate(10), 0); }});
 326         checkNull(new Runnable() { public void run() { enc.encode(ByteBuffer.allocate(10), bb_null, 0); }});
 327         checkNull(new Runnable() { public void run() { enc.wrap(null); }});
 328     }
 329 
 330     private static void testNull(final Base64.Decoder dec) {
 331         checkNull(new Runnable() { public void run() { dec.decode(ba_null); }});
 332         checkNull(new Runnable() { public void run() { dec.decode(str_null); }});
 333         checkNull(new Runnable() { public void run() { dec.decode(ba_null, new byte[10]); }});