Skip to content

Commit

Permalink
8243010: Test support: Customizable Hex Printer
Browse files Browse the repository at this point in the history
Reviewed-by: lancea, dfuchs, weijun
  • Loading branch information
Roger Riggs committed Apr 16, 2020
1 parent d2e0d0e commit bdf6726
Show file tree
Hide file tree
Showing 16 changed files with 1,657 additions and 60 deletions.
3 changes: 0 additions & 3 deletions test/jdk/com/sun/jndi/ldap/Base64Test.java
Expand Up @@ -163,9 +163,6 @@ public static void main(String[] args) throws Exception {
*/
private static void deserialize(byte[] bytes) throws Exception {

//System.out.println("\nSerialized RefAddr object: ");
//System.out.println(new sun.security.util.HexDumpEncoder().encode(bytes));

ObjectInputStream objectStream =
new ObjectInputStream(new ByteArrayInputStream(bytes));
Object object = objectStream.readObject();
Expand Down
11 changes: 6 additions & 5 deletions test/jdk/com/sun/security/sasl/ntlm/NTLMTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2020, 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
Expand All @@ -24,6 +24,7 @@
/*
* @test
* @bug 6911951 7150092
* @library /test/lib
* @summary NTLM should be a supported Java SASL mechanism
* @modules java.base/sun.security.util
* java.security.sasl
Expand All @@ -32,7 +33,7 @@
import javax.security.sasl.*;
import javax.security.auth.callback.*;
import java.util.*;
import sun.security.util.HexDumpEncoder;
import jdk.test.lib.hexdump.HexPrinter;

public class NTLMTest {

Expand Down Expand Up @@ -312,20 +313,20 @@ private static void handshake(SaslClient clnt, SaslServer srv)
byte[] response = (clnt.hasInitialResponse()
? clnt.evaluateChallenge(EMPTY) : EMPTY);
System.out.println("Initial:");
new HexDumpEncoder().encodeBuffer(response, System.out);
HexPrinter.simple().format(response);
byte[] challenge;

while (!clnt.isComplete() || !srv.isComplete()) {
challenge = srv.evaluateResponse(response);
response = null;
if (challenge != null) {
System.out.println("Challenge:");
new HexDumpEncoder().encodeBuffer(challenge, System.out);
HexPrinter.simple().format(challenge);
response = clnt.evaluateChallenge(challenge);
}
if (response != null) {
System.out.println("Response:");
new HexDumpEncoder().encodeBuffer(response, System.out);
HexPrinter.simple().format(response);
}
}

Expand Down
12 changes: 4 additions & 8 deletions test/jdk/javax/net/ssl/DTLS/DTLSOverDatagram.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, 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
Expand Down Expand Up @@ -43,7 +43,7 @@

import java.util.concurrent.*;

import sun.security.util.HexDumpEncoder;
import jdk.test.lib.hexdump.HexPrinter;

/**
* An example to show the way to use SSLEngine in datagram connections.
Expand Down Expand Up @@ -688,12 +688,11 @@ public String call() throws Exception {
}

final static void printHex(String prefix, ByteBuffer bb) {
HexDumpEncoder dump = new HexDumpEncoder();

synchronized (System.out) {
System.out.println(prefix);
try {
dump.encodeBuffer(bb.slice(), System.out);
HexPrinter.simple().format(bb.slice());
} catch (Exception e) {
// ignore
}
Expand All @@ -704,13 +703,10 @@ final static void printHex(String prefix, ByteBuffer bb) {
final static void printHex(String prefix,
byte[] bytes, int offset, int length) {

HexDumpEncoder dump = new HexDumpEncoder();

synchronized (System.out) {
System.out.println(prefix);
try {
ByteBuffer bb = ByteBuffer.wrap(bytes, offset, length);
dump.encodeBuffer(bb, System.out);
HexPrinter.simple().format(bytes, offset, length);
} catch (Exception e) {
// ignore
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, 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
Expand Down Expand Up @@ -30,13 +30,15 @@
* @test
* @bug 8215790 8219389
* @summary Verify exception
* @library /test/lib
* @modules java.base/sun.security.util
* @run main/othervm ClientHelloBufferUnderflowException
*/

import sun.security.util.HexDumpEncoder;
import javax.net.ssl.SSLHandshakeException;

import jdk.test.lib.hexdump.HexPrinter;

public class ClientHelloBufferUnderflowException extends ClientHelloInterOp {
/*
* Main entry point for this test.
Expand Down Expand Up @@ -75,7 +77,7 @@ protected byte[] createClientHelloMessage() {

System.out.println("The ClientHello message used");
try {
(new HexDumpEncoder()).encodeBuffer(bytes, System.out);
HexPrinter.simple().format(bytes);
} catch (Exception e) {
// ignore
}
Expand Down
9 changes: 5 additions & 4 deletions test/jdk/javax/net/ssl/interop/ClientHelloChromeInterOp.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020, 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
Expand Down Expand Up @@ -30,13 +30,15 @@
* @test
* @bug 8169362
* @summary Interop automated testing with Chrome
* @library /test/lib
* @modules jdk.crypto.ec
* java.base/sun.security.util
* @run main/othervm ClientHelloChromeInterOp
*/

import java.util.Base64;
import sun.security.util.HexDumpEncoder;
import jdk.test.lib.hexdump.HexPrinter;


public class ClientHelloChromeInterOp extends ClientHelloInterOp {
// The ClientHello message.
Expand All @@ -63,10 +65,9 @@ protected byte[] createClientHelloMessage() {

// Dump the hex codes of the ClientHello message so that developers
// can easily check whether the message is captured correct or not.
HexDumpEncoder dump = new HexDumpEncoder();
System.out.println("The ClientHello message used");
try {
dump.encodeBuffer(bytes, System.out);
HexPrinter.simple().format(bytes);
} catch (Exception e) {
// ignore
}
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/sun/security/krb5/auto/MSOID2.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, 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
Expand Down Expand Up @@ -32,7 +32,7 @@
*/

import sun.security.jgss.GSSUtil;
import sun.security.util.HexDumpEncoder;
import jdk.test.lib.hexdump.HexPrinter;

// The basic krb5 test skeleton you can copy from
public class MSOID2 {
Expand Down Expand Up @@ -72,7 +72,7 @@ public static void main(String[] args) throws Exception {
nt[pos] = (byte)newLen;
}
t = nt;
new HexDumpEncoder().encodeBuffer(t, System.out);
HexPrinter.simple().format(t);
}
if (t != null || !s.x().isEstablished()) t = s.take(t);
if (c.x().isEstablished() && s.x().isEstablished()) break;
Expand Down
7 changes: 4 additions & 3 deletions test/jdk/sun/security/krb5/etype/KerberosAesSha2.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020, 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
Expand All @@ -23,6 +23,7 @@
/*
* @test
* @bug 8014628
* @library /test/lib
* @modules java.base/sun.security.util
* java.security.jgss/sun.security.krb5.internal.crypto.dk:+open
* @summary https://tools.ietf.org/html/rfc8009 Test Vectors
Expand All @@ -33,7 +34,7 @@
import java.util.Arrays;

import sun.security.krb5.internal.crypto.dk.AesSha2DkCrypto;
import sun.security.util.HexDumpEncoder;
import jdk.test.lib.hexdump.HexPrinter;

public class KerberosAesSha2 {

Expand Down Expand Up @@ -204,6 +205,6 @@ private static void check(byte[] b1, byte[] b2) throws Exception {
}

private static void dump(byte[] data) throws Exception {
new HexDumpEncoder().encodeBuffer(data, System.err);
HexPrinter.simple().dest(System.err).format(data);
}
}
14 changes: 7 additions & 7 deletions test/jdk/sun/security/mscapi/PublicKeyInterop.java
Expand Up @@ -35,7 +35,7 @@
import javax.crypto.*;

import jdk.test.lib.SecurityTools;
import sun.security.util.HexDumpEncoder;
import jdk.test.lib.hexdump.HexPrinter;

/*
* Confirm interoperability of RSA public keys between SunMSCAPI and SunJCE
Expand Down Expand Up @@ -84,29 +84,29 @@ static void run() throws Exception {
System.out.println();

byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
HexDumpEncoder hde = new HexDumpEncoder();
System.out.println("Plaintext:\n" + hde.encode(plain) + "\n");
HexPrinter hp = HexPrinter.simple();
System.out.println("Plaintext:\n" + hp.toString(plain) + "\n");

Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsa.init(Cipher.ENCRYPT_MODE, myPuKey);
byte[] encrypted = rsa.doFinal(plain);
System.out.println("Encrypted plaintext using RSA Cipher from " +
rsa.getProvider().getName() + " JCE provider\n");
System.out.println(hde.encode(encrypted) + "\n");
System.out.println(hp.toString(encrypted) + "\n");

Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);
byte[] encrypted2 = rsa2.doFinal(plain);
System.out.println("Encrypted plaintext using RSA Cipher from " +
rsa2.getProvider().getName() + " JCE provider\n");
System.out.println(hde.encode(encrypted2) + "\n");
System.out.println(hp.toString(encrypted2) + "\n");

Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
rsa3.init(Cipher.DECRYPT_MODE, myPrKey);
byte[] decrypted = rsa3.doFinal(encrypted);
System.out.println("Decrypted first ciphertext using RSA Cipher from " +
rsa3.getProvider().getName() + " JCE provider\n");
System.out.println(hde.encode(decrypted) + "\n");
System.out.println(hp.toString(decrypted) + "\n");
if (! Arrays.equals(plain, decrypted)) {
throw new Exception("First decrypted ciphertext does not match " +
"original plaintext");
Expand All @@ -115,7 +115,7 @@ static void run() throws Exception {
decrypted = rsa3.doFinal(encrypted2);
System.out.println("Decrypted second ciphertext using RSA Cipher from "
+ rsa3.getProvider().getName() + " JCE provider\n");
System.out.println(hde.encode(decrypted) + "\n");
System.out.println(hp.toString(decrypted) + "\n");
if (! Arrays.equals(plain, decrypted)) {
throw new Exception("Second decrypted ciphertext does not match " +
"original plaintext");
Expand Down
11 changes: 5 additions & 6 deletions test/jdk/sun/security/pkcs/pkcs7/SignerOrder.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, 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
Expand All @@ -25,6 +25,7 @@
* @test
* @bug 8048357
* @summary test PKCS7 data signing, encoding and verification
* @library /test/lib
* @modules java.base/sun.security.pkcs
* java.base/sun.security.util
* java.base/sun.security.x509
Expand All @@ -40,7 +41,6 @@
import java.security.SignatureException;
import java.security.cert.X509Certificate;
import java.util.Date;
import sun.security.util.HexDumpEncoder;
import sun.security.pkcs.ContentInfo;
import sun.security.pkcs.PKCS7;
import sun.security.pkcs.SignerInfo;
Expand All @@ -55,11 +55,10 @@
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
import sun.security.x509.X509Key;
import jdk.test.lib.hexdump.HexPrinter;

public class SignerOrder {

static final HexDumpEncoder hexDump = new HexDumpEncoder();

//signer infos
static final byte[] data1 = "12345".getBytes();
static final byte[] data2 = "abcde".getBytes();
Expand Down Expand Up @@ -120,7 +119,7 @@ static void printSignerInfos(SignerInfo signerInfo) throws IOException {
signerInfo.derEncode(strm);
System.out.println("SignerInfo, length: "
+ strm.toByteArray().length);
System.out.println(hexDump.encode(strm.toByteArray()));
HexPrinter.simple().format(strm.toByteArray());
System.out.println("\n");
strm.reset();
}
Expand All @@ -131,7 +130,7 @@ static void printSignerInfos(SignerInfo[] signerInfos) throws IOException {
signerInfos[i].derEncode(strm);
System.out.println("SignerInfo[" + i + "], length: "
+ strm.toByteArray().length);
System.out.println(hexDump.encode(strm.toByteArray()));
HexPrinter.simple().format(strm.toByteArray());
System.out.println("\n");
strm.reset();
}
Expand Down
9 changes: 4 additions & 5 deletions test/jdk/sun/security/pkcs/pkcs8/PKCS8Test.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, 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
Expand All @@ -25,6 +25,7 @@
* @test
* @bug 8048357
* @summary PKCS8 Standards Conformance Tests
* @library /test/lib
* @requires (os.family != "solaris")
* @modules java.base/sun.security.pkcs
* java.base/sun.security.util
Expand All @@ -42,18 +43,16 @@
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.util.Arrays;
import sun.security.util.HexDumpEncoder;
import sun.security.pkcs.PKCS8Key;
import sun.security.provider.DSAPrivateKey;
import sun.security.util.DerOutputStream;
import sun.security.util.DerValue;
import sun.security.x509.AlgorithmId;
import jdk.test.lib.hexdump.HexPrinter;
import static java.lang.System.out;

public class PKCS8Test {

static final HexDumpEncoder hexDump = new HexDumpEncoder();

static final DerOutputStream derOutput = new DerOutputStream();

static final String FORMAT = "PKCS#8";
Expand Down Expand Up @@ -281,6 +280,6 @@ public static void main(String[] args)

static void dumpByteArray(String nm, byte[] bytes) throws IOException {
out.println(nm + " length: " + bytes.length);
hexDump.encodeBuffer(bytes, out);
HexPrinter.simple().dest(out).format(bytes);
}
}

0 comments on commit bdf6726

Please sign in to comment.