1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 /* @test
  25    @bug 7040220 8054307
  26    @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8
  27  * @run main/othervm/timeout=2000 TestStringCodingUTF8
  28  * @key randomness
  29  */
  30 
  31 import java.util.*;
  32 import java.nio.*;
  33 import java.nio.charset.*;
  34 
  35 public class TestStringCodingUTF8 {
  36     public static void main(String[] args) throws Throwable {
  37         test("UTF-8");
  38         test("CESU-8");
  39         // security manager on
  40         System.setSecurityManager(new PermissiveSecurityManger());
  41         test("UTF-8");
  42         test("CESU-8");
  43     }
  44 
  45     static void test(String csn) throws Throwable {
  46         Charset cs = Charset.forName(csn);
  47         char[] bmp = new char[0x10000];
  48         for (int i = 0; i < 0x10000; i++) {
  49             bmp[i] = (char)i;
  50         }
  51         test(cs, bmp, 0, bmp.length);
  52 
  53         char[] ascii = new char[0x80];
  54         for (int i = 0; i < 0x80; i++) {
  55             ascii[i] = (char)i;
  56         }
  57         test(cs, ascii, 0, ascii.length);
  58 
  59         char[] latin1 = new char[0x100];
  60         for (int i = 0; i < 0x100; i++) {
  61             latin1[i] = (char)i;
  62         }
  63         test(cs, latin1, 0, latin1.length);
  64 
  65         ArrayList<Integer> list = new ArrayList<>(0x20000);
  66         for (int i = 0; i < 0x20000; i++) {
  67             list.add(i, i);
  68         }
  69         Collections.shuffle(list);
  70         int j = 0;
  71         char[] bmpsupp = new char[0x30000];
  72         for (int i = 0; i < 0x20000; i++) {
  73             j += Character.toChars(list.get(i), bmpsupp, j);
  74         }
  75         assert (j == bmpsupp.length);
  76         test(cs, bmpsupp, 0, bmpsupp.length);
  77 
  78         // randomed "off" and "len" on shuffled data
  79         Random rnd = new Random();
  80         int maxlen = 1000;
  81         int itr = 5000;
  82         for (int i = 0; i < itr; i++) {
  83             int off = rnd.nextInt(bmpsupp.length - maxlen);
  84             int len = rnd.nextInt(maxlen);
  85             test(cs, bmpsupp, off, len);
  86         }
  87 
  88         // random length of bytes, test the edge corner case
  89         for (int i = 0; i < itr; i++) {
  90             byte[] ba = new byte[rnd.nextInt(maxlen)];
  91             rnd.nextBytes(ba);
  92             //new String(csn);
  93             if (!new String(ba, cs.name()).equals(
  94                  new String(decode(cs, ba, 0, ba.length))))
  95                 throw new RuntimeException("new String(csn) failed");
  96             //new String(cs);
  97             if (!new String(ba, cs).equals(
  98                  new String(decode(cs, ba, 0, ba.length))))
  99                 throw new RuntimeException("new String(cs) failed");
 100         }
 101         System.out.println("done!");
 102     }
 103 
 104     static void test(Charset cs, char[] ca, int off, int len) throws Throwable {
 105         String str = new String(ca, off, len);
 106         byte[] ba = encode(cs, ca, off, len);
 107 
 108         //getBytes(csn);
 109         byte[] baStr = str.getBytes(cs.name());
 110         if (!Arrays.equals(ba, baStr))
 111             throw new RuntimeException("getBytes(csn) failed");
 112 
 113         //getBytes(cs);
 114         baStr = str.getBytes(cs);
 115         if (!Arrays.equals(ba, baStr))
 116             throw new RuntimeException("getBytes(cs) failed");
 117 
 118         //new String(csn);
 119         if (!new String(ba, cs.name()).equals(new String(decode(cs, ba, 0, ba.length))))
 120             throw new RuntimeException("new String(csn) failed");
 121 
 122         //new String(cs);
 123         if (!new String(ba, cs).equals(new String(decode(cs, ba, 0, ba.length))))
 124             throw new RuntimeException("new String(cs) failed");
 125     }
 126 
 127     // copy/paste of the StringCoding.decode()
 128     static char[] decode(Charset cs, byte[] ba, int off, int len) {
 129         CharsetDecoder cd = cs.newDecoder();
 130         int en = (int)(len * cd.maxCharsPerByte());
 131         char[] ca = new char[en];
 132         if (len == 0)
 133             return ca;
 134         cd.onMalformedInput(CodingErrorAction.REPLACE)
 135           .onUnmappableCharacter(CodingErrorAction.REPLACE)
 136           .reset();
 137 
 138         ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
 139         CharBuffer cb = CharBuffer.wrap(ca);
 140         try {
 141             CoderResult cr = cd.decode(bb, cb, true);
 142             if (!cr.isUnderflow())
 143                 cr.throwException();
 144             cr = cd.flush(cb);
 145             if (!cr.isUnderflow())
 146                 cr.throwException();
 147         } catch (CharacterCodingException x) {
 148             throw new Error(x);
 149         }
 150         return Arrays.copyOf(ca, cb.position());
 151     }
 152 
 153     // copy/paste of the StringCoding.encode()
 154     static byte[] encode(Charset cs, char[] ca, int off, int len) {
 155         CharsetEncoder ce = cs.newEncoder();
 156         int en = (int)(len * ce.maxBytesPerChar());
 157         byte[] ba = new byte[en];
 158         if (len == 0)
 159             return ba;
 160         ce.onMalformedInput(CodingErrorAction.REPLACE)
 161           .onUnmappableCharacter(CodingErrorAction.REPLACE)
 162           .reset();
 163         ByteBuffer bb = ByteBuffer.wrap(ba);
 164         CharBuffer cb = CharBuffer.wrap(ca, off, len);
 165         try {
 166             CoderResult cr = ce.encode(cb, bb, true);
 167             if (!cr.isUnderflow())
 168                 cr.throwException();
 169             cr = ce.flush(bb);
 170             if (!cr.isUnderflow())
 171                 cr.throwException();
 172         } catch (CharacterCodingException x) {
 173             throw new Error(x);
 174         }
 175         return Arrays.copyOf(ba, bb.position());
 176     }
 177 
 178     static class PermissiveSecurityManger extends SecurityManager {
 179         @Override public void checkPermission(java.security.Permission p) {}
 180     }
 181 }