test/java/util/Base64/TestBase64.java

Print this page

        

@@ -21,11 +21,11 @@
  * questions.
  */
 
 /**
  * @test 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
- *       8014217 8025003
+ *       8014217 8025003 8026330
  * @summary tests java.util.Base64
  */
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;

@@ -45,16 +45,13 @@
         if (args.length > 1) {
             numRuns  = Integer.parseInt(args[0]);
             numBytes = Integer.parseInt(args[1]);
         }
 
-        test(Base64.getEncoder(),     Base64.getDecoder(),
-             numRuns, numBytes);
-        test(Base64.getUrlEncoder(),  Base64.getUrlDecoder(),
-             numRuns, numBytes);
-        test(Base64.getMimeEncoder(), Base64.getMimeDecoder(),
-             numRuns, numBytes);
+        test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes);
+        test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes);
+        test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes);
 
         Random rnd = new java.util.Random();
         byte[] nl_1 = new byte[] {'\n'};
         byte[] nl_2 = new byte[] {'\n', '\r'};
         byte[] nl_3 = new byte[] {'\n', '\r', '\n'};

@@ -140,10 +137,14 @@
         Random rnd = new java.util.Random();
 
         enc.encode(new byte[0]);
         dec.decode(new byte[0]);
 
+        for (boolean withoutPadding : new boolean[] { false, true}) {
+            if (withoutPadding) {
+                 enc = enc.withoutPadding();
+            }
         for (int i=0; i<numRuns; i++) {
             for (int j=1; j<numBytes; j++) {
                 byte[] orig = new byte[j];
                 rnd.nextBytes(orig);
 

@@ -151,17 +152,22 @@
                 byte[] encoded = enc.encode(orig);
                 byte[] decoded = dec.decode(encoded);
 
                 checkEqual(orig, decoded,
                            "Base64 array encoding/decoding failed!");
-
+                    if (withoutPadding) {
+                        if (encoded[encoded.length - 1] == '=')
+                            throw new RuntimeException(
+                               "Base64 enc.encode().withoutPadding() has padding!");
+                    }
                 // compare to sun.misc.BASE64Encoder
+
                 byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII");
-                checkEqual(normalize(encoded),
-                           normalize(encoded2),
+                    if (!withoutPadding) {    // don't test for withoutPadding()
+                        checkEqual(normalize(encoded), normalize(encoded2),
                            "Base64 enc.encode() does not match sun.misc.base64!");
-
+                    }
                 // remove padding '=' to test non-padding decoding case
                 if (encoded[encoded.length -2] == '=')
                     encoded2 = Arrays.copyOf(encoded,  encoded.length -2);
                 else if (encoded[encoded.length -1] == '=')
                     encoded2 = Arrays.copyOf(encoded, encoded.length -1);

@@ -302,10 +308,11 @@
                            "Base64 dec.decode(src, dst) failed!");
 
             }
         }
     }
+    }
 
     private static final byte[] ba_null = null;
     private static final String str_null = null;
     private static final ByteBuffer bb_null = null;