diff --git a/src/java.base/share/classes/sun/security/util/HexDumpEncoder.java b/src/java.base/share/classes/sun/security/util/HexDumpEncoder.java index 0d1b4ae8984..2431b95562f 100644 --- a/src/java.base/share/classes/sun/security/util/HexDumpEncoder.java +++ b/src/java.base/share/classes/sun/security/util/HexDumpEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -84,7 +84,7 @@ public class HexDumpEncoder { protected void encodeBufferPrefix(OutputStream o) throws IOException { offset = 0; - pStream = new PrintStream(o); + pStream = new PrintStream(o, false, ISO_8859_1); } protected void encodeLinePrefix(OutputStream o, int len) { @@ -303,6 +303,8 @@ public class HexDumpEncoder { /** * A 'streamless' version of encode that simply takes a buffer of * bytes and returns a string containing the encoded buffer. + *
+ * Returned string is encoded with the ISO-8859-1, also known as ISO-LATIN-1. */ public String encodeBuffer(byte[] aBuffer) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); @@ -313,7 +315,7 @@ public class HexDumpEncoder { // This should never happen. throw new Error("CharacterEncoder.encodeBuffer internal error"); } - return (outStream.toString()); + return (outStream.toString(ISO_8859_1)); } /** diff --git a/test/jdk/sun/security/util/HexDumpEncoderTests.java b/test/jdk/sun/security/util/HexDumpEncoderTests.java new file mode 100644 index 00000000000..9ff08cdb192 --- /dev/null +++ b/test/jdk/sun/security/util/HexDumpEncoderTests.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import jdk.test.lib.Asserts; +import jdk.test.lib.process.ProcessTools; +import sun.security.util.HexDumpEncoder; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +/* + * @test + * @bug 8349664 + * @summary HEX dump should always use ASCII or ISO_8859_1 + * @modules java.base/sun.security.util + * @library /test/lib + */ +public class HexDumpEncoderTests { + + + private static String[] getTestCommand(final String encoding) { + return new String[]{ + "--add-modules", "java.base", + "--add-exports", "java.base/sun.security.util=ALL-UNNAMED", + "-Dfile.encoding=" + encoding, + HexDumpEncoderTests.HexDumpEncoderTest.class.getName() + }; + } + + public static void main(String[] args) throws Exception { + + final var testCommandIso = getTestCommand("ISO-8859-1"); + + final var resultIso = ProcessTools.executeTestJava(testCommandIso); + resultIso.shouldHaveExitValue(0); + + // This will take all available StandardCharsets and test them all comparing to the ISO_8859_1 + // Dome im parallel, as this is significantly faster + Arrays.stream(StandardCharsets.class.getDeclaredFields()) + .parallel() + .forEach(field -> { + if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { + try { + final var charset = (Charset) field.get(StandardCharsets.ISO_8859_1); // getting the charset to test + + final var testCommand = getTestCommand(charset.name()); + + final var result = ProcessTools.executeTestJava(testCommand); + result.shouldHaveExitValue(0); + + // The outputs of the ISO encoding must be identical to the one tested + Asserts.assertEquals(resultIso.getStdout(), + result.getStdout(), + "Encoding " + charset.name()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + } + + public static class HexDumpEncoderTest { + + /** + * This will test the encode and encode buffer functions at once, + * as they are both representing the string in LATIN_1 + *
+ * The output is put as a system.out + */ + public static void main(String[] args) throws Exception { + + final var encoder = new HexDumpEncoder(); + + System.out.printf("\nCert Encoded With Encode Buffer: %s\n", encoder.encodeBuffer(new byte[100])); + System.out.printf("\nCert Encoded With Encode: %s\n", encoder.encode(new byte[100])); + } + } +}