8349664: HEX dump should always use ASCII or ISO_8859_1

Reviewed-by: weijun
This commit is contained in:
Mikhail Yankelevich 2025-02-19 18:56:26 +00:00 committed by Weijun Wang
parent 7631984525
commit 7734f8ed13
2 changed files with 104 additions and 3 deletions

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 { protected void encodeBufferPrefix(OutputStream o) throws IOException {
offset = 0; offset = 0;
pStream = new PrintStream(o); pStream = new PrintStream(o, false, ISO_8859_1);
} }
protected void encodeLinePrefix(OutputStream o, int len) { 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 * A 'streamless' version of encode that simply takes a buffer of
* bytes and returns a string containing the encoded buffer. * bytes and returns a string containing the encoded buffer.
* <P>
* Returned string is encoded with the ISO-8859-1, also known as ISO-LATIN-1.
*/ */
public String encodeBuffer(byte[] aBuffer) { public String encodeBuffer(byte[] aBuffer) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
@ -313,7 +315,7 @@ public class HexDumpEncoder {
// This should never happen. // This should never happen.
throw new Error("CharacterEncoder.encodeBuffer internal error"); throw new Error("CharacterEncoder.encodeBuffer internal error");
} }
return (outStream.toString()); return (outStream.toString(ISO_8859_1));
} }
/** /**

View file

@ -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
* <p>
* 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]));
}
}
}