1 /*
   2  * Copyright (c) 1999, 2020, 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 4179153 4652234 6529796
  26    @summary Read code mapping table and check code conversion
  27    @modules jdk.charsets
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.charset.Charset;
  32 import java.util.HashSet;
  33 
  34 public class TestConv {
  35     static int errorNum = 0;
  36     private static final int maxBytesPerChar = 10;
  37 
  38     public static void main(String args[])
  39         throws Exception
  40     {
  41         File d = new File(System.getProperty("test.src", "."));
  42         if (args.length == 0) {
  43             String[] files = d.list();
  44             String encoding;
  45             for (int i = 0; i < files.length; i++) {
  46                 if (files[i].endsWith(".b2c")) {
  47                     encoding = files[i].substring(0, files[i].length() - 4 );
  48                     check(d, encoding);
  49                 }
  50             }
  51         } else {
  52             for (int i = 0; i < args.length; i++)
  53                 check(d, args[i]);
  54         }
  55     }
  56 
  57     static class Parser2 extends CoderTest.Parser {
  58         int warnOff;
  59         String regwarnCP;
  60         Parser2 (InputStream is) throws IOException {
  61             super(is);
  62         }
  63         protected boolean isDirective(String line) {
  64             if ((warnOff = line.indexOf("REGWARN")) != -1)
  65                 regwarnCP = line.substring(warnOff+7);
  66             else
  67                 regwarnCP = null;
  68             return false;
  69         }
  70     }
  71 
  72     private static void check(File dir, String encoding) throws Exception
  73     {
  74         byte[] inByte;
  75         byte[] outByte;
  76         char[] inChar;
  77         String inStr;
  78         String outStr;
  79 
  80         System.out.println("\nChecking " + encoding + "...");
  81         errorNum = 0;
  82 
  83         if (!Charset.isSupported(encoding)) {
  84             System.out.println("Not supported: " + encoding);
  85             return;
  86         }
  87 
  88         Parser2 p = null;
  89         try {
  90             p = new Parser2(new FileInputStream(new File(dir, encoding + ".b2c")));
  91         } catch (Exception e) {
  92             throw new Exception("Can't open file " + encoding + ".b2c");
  93         }
  94         CoderTest.Entry e = new CoderTest.Entry();
  95 
  96         while ((e = (CoderTest.Entry)p.next(e)) != null) {
  97             if (e.cp2 != 0)
  98                 continue;  // skip composite (base+cc) for now
  99             inByte = e.bb;
 100             inChar = Character.toChars(e.cp);
 101             inStr = new String(inChar);
 102             outStr = new String(inByte, encoding);
 103             outByte = inStr.getBytes(encoding);
 104             int r = compareInOut(inStr, outStr, inByte, outByte);
 105             if (r == 1) {
 106                 if (p.warnOff == -1)
 107                     errorNum++;
 108                 else {
 109                    System.out.println ("Regression Warning code point " +
 110                                        p.regwarnCP);
 111                 }
 112                 System.out.println("Warning " + errorNum
 113                                    + ": " + byteString(inByte)
 114                                    + " -> \\u" + toHex(outStr)
 115                                    + "  multi-mapping? \\u" + toHex(inStr));
 116             } else if (r == 2) {
 117                 if (p.warnOff == -1)
 118                     errorNum++;
 119             }
 120         }
 121 
 122         if (errorNum == 0) {
 123             System.out.println("OK.");
 124         } else {
 125             throw new RuntimeException(errorNum + " Warning(s).");
 126         }
 127     }
 128 
 129     private static int compareInOut(String inStr, String outStr,
 130                                 byte[] inByte, byte[] outByte)
 131     {
 132         if (inStr.compareTo(outStr) != 0)
 133             return 1;
 134 
 135         if (inByte.length != outByte.length) {
 136             return 2;
 137         }
 138 
 139         for (int i = 0; i < inByte.length; i++) {
 140             if (inByte[i] != outByte[i])
 141                 return 2;
 142         }
 143 
 144         return 0;
 145     }
 146 
 147     private static String toHex(String str)
 148     {
 149         if (str.length() == 0)
 150             return "";
 151         String s = Integer.toHexString(str.charAt(0)).toUpperCase();
 152         if (s.length() == 1 || s.length() == 3)
 153             return "0" + s;
 154         return s;
 155     }
 156 
 157     private static String byteString(byte[] b)
 158     {
 159         String s = "0x";
 160         for (int i = 0; i < b.length; i++)
 161             s += Integer.toHexString(b[i] & 0xff).toUpperCase();
 162         return s;
 163     }
 164 
 165 }