1 /*
2  * Copyright (c) 2008, 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 6378295
26  * @summary Roundtrip Encoding/Decoding of ASCII chars from 0x00-0x7f
27  */
28 
29 import java.util.*;
30 import java.nio.*;
31 import java.nio.charset.*;
32 
33 public class FindASCIIRangeCodingBugs {
34     private static int failures = 0;
35     private static byte[] asciiBytes = new byte[0x80];
36     private static char[] asciiChars = new char[0x80];
37     private static String asciiString;
38 
check(String csn)39     private static void check(String csn) throws Exception {
40         System.out.println(csn);
41         if (! Arrays.equals(asciiString.getBytes(csn), asciiBytes)) {
42             System.out.printf("%s -> bytes%n", csn);
43             failures++;
44         }
45         if (! new String(asciiBytes, csn).equals(asciiString)) {
46             System.out.printf("%s -> chars%n", csn);
47             failures++;
48         }
49     }
50 
main(String[] args)51     public static void main(String[] args) throws Exception {
52         for (int i = 0; i < 0x80; i++) {
53             asciiBytes[i] = (byte) i;
54             asciiChars[i] = (char) i;
55         }
56         asciiString = new String(asciiChars);
57         Charset ascii = Charset.forName("ASCII");
58         for (Map.Entry<String,Charset> e
59                  : Charset.availableCharsets().entrySet()) {
60             String csn = e.getKey();
61             Charset cs = e.getValue();
62             if (!cs.contains(ascii) ||
63                 csn.matches(".*2022.*") ||             //iso2022 family
64                 csn.matches("x-windows-5022[0|1]") ||  //windows 2022jp
65                 csn.matches(".*UTF-[16|32].*"))        //multi-bytes
66                 continue;
67             if (! cs.canEncode()) continue;
68             try {
69                 check(csn);
70             } catch (Throwable t) {
71                 t.printStackTrace();
72                 failures++;
73             }
74         }
75         if (failures > 0)
76             throw new Exception(failures + "tests failed");
77     }
78 }
79