97 98 // illegal base64 character 99 decoded[2] = (byte)0xe0; 100 checkIAE(new Runnable() { 101 public void run() { Base64.getDecoder().decode(decoded); }}); 102 checkIAE(new Runnable() { 103 public void run() { Base64.getDecoder().decode(decoded, new byte[1024]); }}); 104 checkIAE(new Runnable() { public void run() { 105 Base64.getDecoder().decode(ByteBuffer.wrap(decoded)); }}); 106 107 // test single-non-base64 character for mime decoding 108 testSingleNonBase64MimeDec(); 109 110 // test decoding of unpadded data 111 testDecodeUnpadded(); 112 113 // test mime decoding with ignored character after padding 114 testDecodeIgnoredAfterPadding(); 115 } 116 117 private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder(); 118 119 private static void test(Base64.Encoder enc, Base64.Decoder dec, 120 int numRuns, int numBytes) throws Throwable { 121 Random rnd = new java.util.Random(); 122 123 enc.encode(new byte[0]); 124 dec.decode(new byte[0]); 125 126 for (boolean withoutPadding : new boolean[] { false, true}) { 127 if (withoutPadding) { 128 enc = enc.withoutPadding(); 129 } 130 for (int i=0; i<numRuns; i++) { 131 for (int j=1; j<numBytes; j++) { 132 byte[] orig = new byte[j]; 133 rnd.nextBytes(orig); 134 135 // --------testing encode/decode(byte[])-------- 136 byte[] encoded = enc.encode(orig); 137 byte[] decoded = dec.decode(encoded); 138 139 checkEqual(orig, decoded, 140 "Base64 array encoding/decoding failed!"); 141 if (withoutPadding) { 142 if (encoded[encoded.length - 1] == '=') 143 throw new RuntimeException( 144 "Base64 enc.encode().withoutPadding() has padding!"); 145 } 146 // compare to sun.misc.BASE64Encoder 147 148 byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII"); 149 if (!withoutPadding) { // don't test for withoutPadding() 150 checkEqual(normalize(encoded), normalize(encoded2), 151 "Base64 enc.encode() does not match sun.misc.base64!"); 152 } 153 // remove padding '=' to test non-padding decoding case 154 if (encoded[encoded.length -2] == '=') 155 encoded2 = Arrays.copyOf(encoded, encoded.length -2); 156 else if (encoded[encoded.length -1] == '=') 157 encoded2 = Arrays.copyOf(encoded, encoded.length -1); 158 else 159 encoded2 = null; 160 161 // --------testing encodetoString(byte[])/decode(String)-------- 162 String str = enc.encodeToString(orig); 163 if (!Arrays.equals(str.getBytes("ASCII"), encoded)) { 164 throw new RuntimeException( 165 "Base64 encodingToString() failed!"); 166 } 167 byte[] buf = dec.decode(new String(encoded, "ASCII")); 168 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 169 170 if (encoded2 != null) { 171 buf = dec.decode(new String(encoded2, "ASCII")); 172 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 173 } 174 175 //-------- testing encode/decode(Buffer)-------- 176 testEncode(enc, ByteBuffer.wrap(orig), encoded); 177 ByteBuffer bin = ByteBuffer.allocateDirect(orig.length); 178 bin.put(orig).flip(); 179 testEncode(enc, bin, encoded); 180 181 testDecode(dec, ByteBuffer.wrap(encoded), orig); 182 bin = ByteBuffer.allocateDirect(encoded.length); 183 bin.put(encoded).flip(); 184 testDecode(dec, bin, orig); 185 186 if (encoded2 != null) 187 testDecode(dec, ByteBuffer.wrap(encoded2), orig); 188 189 // --------testing decode.wrap(input stream)-------- 190 // 1) random buf length 191 ByteArrayInputStream bais = new ByteArrayInputStream(encoded); 192 InputStream is = dec.wrap(bais); 193 buf = new byte[orig.length + 10]; 194 int len = orig.length; 195 int off = 0; 196 while (true) { 197 int n = rnd.nextInt(len); 198 if (n == 0) 199 n = 1; 200 n = is.read(buf, off, n); 201 if (n == -1) { 202 checkEqual(off, orig.length, 203 "Base64 stream decoding failed"); 204 break; 205 } 206 off += n; 207 len -= n; 208 if (len == 0) | 97 98 // illegal base64 character 99 decoded[2] = (byte)0xe0; 100 checkIAE(new Runnable() { 101 public void run() { Base64.getDecoder().decode(decoded); }}); 102 checkIAE(new Runnable() { 103 public void run() { Base64.getDecoder().decode(decoded, new byte[1024]); }}); 104 checkIAE(new Runnable() { public void run() { 105 Base64.getDecoder().decode(ByteBuffer.wrap(decoded)); }}); 106 107 // test single-non-base64 character for mime decoding 108 testSingleNonBase64MimeDec(); 109 110 // test decoding of unpadded data 111 testDecodeUnpadded(); 112 113 // test mime decoding with ignored character after padding 114 testDecodeIgnoredAfterPadding(); 115 } 116 117 private static void test(Base64.Encoder enc, Base64.Decoder dec, 118 int numRuns, int numBytes) throws Throwable { 119 Random rnd = new java.util.Random(); 120 121 enc.encode(new byte[0]); 122 dec.decode(new byte[0]); 123 124 for (boolean withoutPadding : new boolean[] { false, true}) { 125 if (withoutPadding) { 126 enc = enc.withoutPadding(); 127 } 128 for (int i=0; i<numRuns; i++) { 129 for (int j=1; j<numBytes; j++) { 130 byte[] orig = new byte[j]; 131 rnd.nextBytes(orig); 132 133 // --------testing encode/decode(byte[])-------- 134 byte[] encoded = enc.encode(orig); 135 byte[] decoded = dec.decode(encoded); 136 137 checkEqual(orig, decoded, 138 "Base64 array encoding/decoding failed!"); 139 if (withoutPadding) { 140 if (encoded[encoded.length - 1] == '=') 141 throw new RuntimeException( 142 "Base64 enc.encode().withoutPadding() has padding!"); 143 } 144 // --------testing encodetoString(byte[])/decode(String)-------- 145 String str = enc.encodeToString(orig); 146 if (!Arrays.equals(str.getBytes("ASCII"), encoded)) { 147 throw new RuntimeException( 148 "Base64 encodingToString() failed!"); 149 } 150 byte[] buf = dec.decode(new String(encoded, "ASCII")); 151 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 152 153 //-------- testing encode/decode(Buffer)-------- 154 testEncode(enc, ByteBuffer.wrap(orig), encoded); 155 ByteBuffer bin = ByteBuffer.allocateDirect(orig.length); 156 bin.put(orig).flip(); 157 testEncode(enc, bin, encoded); 158 159 testDecode(dec, ByteBuffer.wrap(encoded), orig); 160 bin = ByteBuffer.allocateDirect(encoded.length); 161 bin.put(encoded).flip(); 162 testDecode(dec, bin, orig); 163 164 // --------testing decode.wrap(input stream)-------- 165 // 1) random buf length 166 ByteArrayInputStream bais = new ByteArrayInputStream(encoded); 167 InputStream is = dec.wrap(bais); 168 buf = new byte[orig.length + 10]; 169 int len = orig.length; 170 int off = 0; 171 while (true) { 172 int n = rnd.nextInt(len); 173 if (n == 0) 174 n = 1; 175 n = is.read(buf, off, n); 176 if (n == -1) { 177 checkEqual(off, orig.length, 178 "Base64 stream decoding failed"); 179 break; 180 } 181 off += n; 182 len -= n; 183 if (len == 0) |