mirror of
https://github.com/openjdk/jdk.git
synced 2026-03-14 09:53:18 +00:00
8340493: Fix some Asserts failure messages
Reviewed-by: valeriep, djelinski
This commit is contained in:
parent
4d77dbad4e
commit
b2811a0ccd
@ -52,14 +52,14 @@ public class HKDFBasicFunctionsTest {
|
||||
var extractAndExpand = HKDFParameterSpec.ofExtract().addIKM(ikm).addSalt(salt).thenExpand(info, len);
|
||||
var okm2 = kdf.deriveKey("OKM", extractAndExpand);
|
||||
|
||||
Asserts.assertEqualsByteArray(prk.getEncoded(), expectedPrk,
|
||||
Asserts.assertEqualsByteArray(expectedPrk, prk.getEncoded(),
|
||||
"the PRK must match the expected value");
|
||||
|
||||
Asserts.assertEqualsByteArray(okm1.getEncoded(), expectedOkm,
|
||||
Asserts.assertEqualsByteArray(expectedOkm, okm1.getEncoded(),
|
||||
"the OKM must match the expected value "
|
||||
+ "(expand)");
|
||||
|
||||
Asserts.assertEqualsByteArray(okm2.getEncoded(), expectedOkm,
|
||||
Asserts.assertEqualsByteArray(expectedOkm, okm2.getEncoded(),
|
||||
"the OKM must match the expected value "
|
||||
+ "(extract expand)");
|
||||
|
||||
|
||||
@ -118,7 +118,7 @@ public class ECDHPrimitive {
|
||||
byte[] secret = ka.generateSecret();
|
||||
|
||||
byte[] expectedSecret = values.get("ZIUT");
|
||||
Asserts.assertEqualsByteArray(secret, expectedSecret, "Incorrect secret value");
|
||||
Asserts.assertEqualsByteArray(expectedSecret, secret, "Incorrect secret value");
|
||||
int testIndex = values.get("COUNT")[0];
|
||||
System.out.println("Test " + testIndex + " passed.");
|
||||
}
|
||||
@ -141,4 +141,4 @@ public class ECDHPrimitive {
|
||||
private static String lookupName(String name) {
|
||||
return NAME_MAP.get(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,8 +76,8 @@ public class ML_DSA_Test {
|
||||
var kp = g.generateKeyPair();
|
||||
var pk = f.getKeySpec(kp.getPublic(), EncodedKeySpec.class).getEncoded();
|
||||
var sk = f.getKeySpec(kp.getPrivate(), EncodedKeySpec.class).getEncoded();
|
||||
Asserts.assertEqualsByteArray(pk, toByteArray(c.get("pk").asString()));
|
||||
Asserts.assertEqualsByteArray(sk, toByteArray(c.get("sk").asString()));
|
||||
Asserts.assertEqualsByteArray(toByteArray(c.get("pk").asString()), pk);
|
||||
Asserts.assertEqualsByteArray(toByteArray(c.get("sk").asString()), sk);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
@ -104,7 +104,7 @@ public class ML_DSA_Test {
|
||||
s.update(toByteArray(c.get("message").asString()));
|
||||
var sig = s.sign();
|
||||
Asserts.assertEqualsByteArray(
|
||||
sig, toByteArray(c.get("signature").asString()));
|
||||
toByteArray(c.get("signature").asString()), sig);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@ -70,8 +70,8 @@ public class ML_KEM_Test {
|
||||
var kp = g.generateKeyPair();
|
||||
var pk = f.getKeySpec(kp.getPublic(), EncodedKeySpec.class).getEncoded();
|
||||
var sk = f.getKeySpec(kp.getPrivate(), EncodedKeySpec.class).getEncoded();
|
||||
Asserts.assertEqualsByteArray(pk, toByteArray(c.get("ek").asString()));
|
||||
Asserts.assertEqualsByteArray(sk, toByteArray(c.get("dk").asString()));
|
||||
Asserts.assertEqualsByteArray(toByteArray(c.get("ek").asString()), pk);
|
||||
Asserts.assertEqualsByteArray(toByteArray(c.get("dk").asString()), sk);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
@ -97,9 +97,9 @@ public class ML_KEM_Test {
|
||||
ek, new FixedSecureRandom(toByteArray(c.get("m").asString())));
|
||||
var enc = e.encapsulate();
|
||||
Asserts.assertEqualsByteArray(
|
||||
enc.encapsulation(), toByteArray(c.get("c").asString()));
|
||||
toByteArray(c.get("c").asString()), enc.encapsulation());
|
||||
Asserts.assertEqualsByteArray(
|
||||
enc.key().getEncoded(), toByteArray(c.get("k").asString()));
|
||||
toByteArray(c.get("k").asString()), enc.key().getEncoded());
|
||||
}
|
||||
System.out.println();
|
||||
} else if (function.equals("decapsulation")) {
|
||||
@ -112,7 +112,7 @@ public class ML_KEM_Test {
|
||||
System.out.print(c.get("tcId").asString() + " ");
|
||||
var d = g.newDecapsulator(dk);
|
||||
var k = d.decapsulate(toByteArray(c.get("c").asString()));
|
||||
Asserts.assertEqualsByteArray(k.getEncoded(), toByteArray(c.get("k").asString()));
|
||||
Asserts.assertEqualsByteArray(toByteArray(c.get("k").asString()), k.getEncoded());
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@ -46,8 +46,8 @@ public class SHA_Test {
|
||||
var msg = toByteArray(c.get("msg").asString());
|
||||
var len = Integer.parseInt(c.get("len").asString());
|
||||
if (msg.length * 8 == len) {
|
||||
Asserts.assertEqualsByteArray(md.digest(msg),
|
||||
toByteArray(c.get("md").asString()));
|
||||
Asserts.assertEqualsByteArray(
|
||||
toByteArray(c.get("md").asString()), md.digest(msg));
|
||||
} else {
|
||||
System.out.print("bits ");
|
||||
}
|
||||
@ -70,8 +70,8 @@ public class SHA_Test {
|
||||
}
|
||||
MD = md.digest(MD);
|
||||
}
|
||||
Asserts.assertEqualsByteArray(MD,
|
||||
toByteArray(r.get("md").asString()));
|
||||
Asserts.assertEqualsByteArray(
|
||||
toByteArray(r.get("md").asString()), MD);
|
||||
SEED = MD;
|
||||
} else {
|
||||
var A = SEED;
|
||||
@ -88,8 +88,8 @@ public class SHA_Test {
|
||||
B = C;
|
||||
C = MD;
|
||||
}
|
||||
Asserts.assertEqualsByteArray(MD,
|
||||
toByteArray(r.get("md").asString()));
|
||||
Asserts.assertEqualsByteArray(
|
||||
toByteArray(r.get("md").asString()), MD);
|
||||
SEED = MD;
|
||||
}
|
||||
}
|
||||
@ -110,8 +110,8 @@ public class SHA_Test {
|
||||
md.update(ct);
|
||||
cc += clen;
|
||||
}
|
||||
Asserts.assertEqualsByteArray(md.digest(),
|
||||
toByteArray(c.get("md").asString()));
|
||||
Asserts.assertEqualsByteArray(
|
||||
toByteArray(c.get("md").asString()), md.digest());
|
||||
}
|
||||
}
|
||||
default -> throw new UnsupportedOperationException(
|
||||
|
||||
@ -25,12 +25,12 @@ import jdk.test.lib.Asserts;
|
||||
|
||||
import java.lang.SuppressWarnings;
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
|
||||
import static jdk.test.lib.Asserts.*;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8340493
|
||||
* @library /test/lib
|
||||
* @summary Tests the different assertions in the Assert class
|
||||
*/
|
||||
@ -49,6 +49,29 @@ public class AssertsTest {
|
||||
}
|
||||
}
|
||||
|
||||
// equals() always returns true
|
||||
public static class Bar {
|
||||
private final int i;
|
||||
public Bar(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
testLessThan();
|
||||
testLessThanOrEqual();
|
||||
@ -62,6 +85,19 @@ public class AssertsTest {
|
||||
testTrue();
|
||||
testFalse();
|
||||
testFail();
|
||||
|
||||
testErrorMessages();
|
||||
}
|
||||
|
||||
public static void testErrorMessages() throws Exception {
|
||||
try {
|
||||
Asserts.assertNotEquals(new Bar(1), new Bar(2));
|
||||
throw new Exception("Should fail");
|
||||
} catch (RuntimeException e) {
|
||||
if (!e.getMessage().contains("was 2")) {
|
||||
throw new Exception("msg is " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void testLessThan() throws Exception {
|
||||
@ -216,8 +252,7 @@ public class AssertsTest {
|
||||
" to throw a RuntimeException");
|
||||
}
|
||||
|
||||
private static void expectPass(Assertion assertion, byte[] b1, byte[] b2)
|
||||
throws Exception {
|
||||
private static void expectPass(Assertion assertion, byte[] b1, byte[] b2) {
|
||||
if (assertion == Assertion.EQBA) {
|
||||
String msg = "Expected " + Assertion.asString("assertEqualsByteArray",
|
||||
Arrays.toString(b1), Arrays.toString(b2)) + " to pass";
|
||||
|
||||
@ -35,13 +35,13 @@ public class FixedSecureRandomTest {
|
||||
new byte[] {4, 5, 6});
|
||||
var b1 = new byte[2];
|
||||
fsr.nextBytes(b1);
|
||||
Asserts.assertEqualsByteArray(b1, new byte[] {1, 2});
|
||||
Asserts.assertEqualsByteArray(new byte[] {1, 2}, b1);
|
||||
Asserts.assertTrue(fsr.hasRemaining());
|
||||
fsr.nextBytes(b1);
|
||||
Asserts.assertEqualsByteArray(b1, new byte[] {3, 4});
|
||||
Asserts.assertEqualsByteArray(new byte[] {3, 4}, b1);
|
||||
Asserts.assertTrue(fsr.hasRemaining());
|
||||
fsr.nextBytes(b1);
|
||||
Asserts.assertEqualsByteArray(b1, new byte[] {5, 6});
|
||||
Asserts.assertEqualsByteArray(new byte[] {5, 6}, b1);
|
||||
Asserts.assertFalse(fsr.hasRemaining());
|
||||
Utils.runAndCheckException(() -> fsr.nextBytes(b1),
|
||||
IllegalStateException.class);
|
||||
|
||||
@ -234,59 +234,58 @@ public class Asserts {
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that {@code lhs} is the same byte array as {@code rhs}.
|
||||
* Asserts that {@code actual} has the same content as {@code expected}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param expected The expected value
|
||||
* @param actual The actual value
|
||||
* @throws RuntimeException if the assertion is not true.
|
||||
* @see #assertEqualsByteArray(byte[], byte[], String)
|
||||
*/
|
||||
public static void assertEqualsByteArray(byte[] lhs, byte[] rhs) {
|
||||
assertEqualsByteArray(lhs, rhs, null);
|
||||
public static void assertEqualsByteArray(byte[] expected, byte[] actual) {
|
||||
assertEqualsByteArray(expected, actual, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that {@code lhs} is not the same byte array as {@code rhs}.
|
||||
* Asserts that {@code actual} does not have the same content as {@code unexpected}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param unexpected The unexpected value
|
||||
* @param actual The actual value
|
||||
* @throws RuntimeException if the assertion is not true.
|
||||
* @see #assertNotEqualsByteArray(byte[], byte[], String)
|
||||
*/
|
||||
public static void assertNotEqualsByteArray(byte[] lhs, byte[] rhs) {
|
||||
assertNotEqualsByteArray(lhs, rhs, null);
|
||||
public static void assertNotEqualsByteArray(byte[] unexpected, byte[] actual) {
|
||||
assertNotEqualsByteArray(unexpected, actual, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that {@code lhs} is the same byte array as {@code rhs}.
|
||||
* Asserts that {@code actual} is the same byte array as {@code expected}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param expected The expected value
|
||||
* @param actual The actual value
|
||||
* @param msg A description of the assumption; {@code null} for a default message.
|
||||
* @throws RuntimeException if the assertion is not true.
|
||||
*/
|
||||
public static void assertEqualsByteArray(byte[] lhs, byte[] rhs, String msg) {
|
||||
if (!Arrays.equals(lhs, rhs)) {
|
||||
public static void assertEqualsByteArray(byte[] expected, byte[] actual, String msg) {
|
||||
if (!Arrays.equals(expected, actual)) {
|
||||
msg = Objects.toString(msg, "assertEqualsByteArray")
|
||||
+ ": expected " + HexFormat.of().formatHex(lhs)
|
||||
+ " to equal " + HexFormat.of().formatHex(rhs);
|
||||
+ ": expected " + HexFormat.of().formatHex(expected)
|
||||
+ " but was " + HexFormat.of().formatHex(actual);
|
||||
fail(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that {@code lhs} is not the same byte array as {@code rhs}.
|
||||
* Asserts that {@code actual} is not the same byte array as {@code unexpected}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param unexpected The unexpected value
|
||||
* @param actual The actual value
|
||||
* @param msg A description of the assumption; {@code null} for a default message.
|
||||
* @throws RuntimeException if the assertion is not true.
|
||||
*/
|
||||
public static void assertNotEqualsByteArray(byte[] lhs, byte[] rhs, String msg) {
|
||||
if (Arrays.equals(lhs, rhs)) {
|
||||
public static void assertNotEqualsByteArray(byte[] unexpected, byte[] actual, String msg) {
|
||||
if (Arrays.equals(unexpected, actual)) {
|
||||
msg = Objects.toString(msg, "assertNotEqualsByteArray")
|
||||
+ ": expected " + HexFormat.of().formatHex(lhs)
|
||||
+ " to not equal " + HexFormat.of().formatHex(rhs);
|
||||
+ ": expected not equals but was " + HexFormat.of().formatHex(actual);
|
||||
fail(msg);
|
||||
}
|
||||
}
|
||||
@ -404,50 +403,49 @@ public class Asserts {
|
||||
/**
|
||||
* Shorthand for {@link #assertNotEquals(Object, Object)}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param unexpected The unexpected value
|
||||
* @param actual The actual value
|
||||
* @see #assertNotEquals(Object, Object)
|
||||
*/
|
||||
public static void assertNE(Object lhs, Object rhs) {
|
||||
assertNotEquals(lhs, rhs);
|
||||
public static void assertNE(Object unexpected, Object actual) {
|
||||
assertNotEquals(unexpected, actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand for {@link #assertNotEquals(Object, Object, String)}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param unexpected The unexpected value
|
||||
* @param actual The actual value
|
||||
* @param msg A description of the assumption; {@code null} for a default message.
|
||||
* @see #assertNotEquals(Object, Object, String)
|
||||
*/
|
||||
public static void assertNE(Object lhs, Object rhs, String msg) {
|
||||
assertNotEquals(lhs, rhs, msg);
|
||||
public static void assertNE(Object unexpected, Object actual, String msg) {
|
||||
assertNotEquals(unexpected, actual, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link #assertNotEquals(Object, Object, String)} with a default message.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param unexpected The unexpected value
|
||||
* @param actual The actual value
|
||||
* @see #assertNotEquals(Object, Object, String)
|
||||
*/
|
||||
public static void assertNotEquals(Object lhs, Object rhs) {
|
||||
assertNotEquals(lhs, rhs, null);
|
||||
public static void assertNotEquals(Object unexpected, Object actual) {
|
||||
assertNotEquals(unexpected, actual, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that {@code lhs} is not equal to {@code rhs}.
|
||||
* Asserts that {@code actual} is not equal to {@code unexpected}.
|
||||
*
|
||||
* @param lhs The left hand side of the comparison.
|
||||
* @param rhs The right hand side of the comparison.
|
||||
* @param unexpected The unexpected value
|
||||
* @param actual The actual value
|
||||
* @param msg A description of the assumption; {@code null} for a default message.
|
||||
* @throws RuntimeException if the assertion is not true.
|
||||
*/
|
||||
public static void assertNotEquals(Object lhs, Object rhs, String msg) {
|
||||
if ((lhs == rhs) || (lhs != null && lhs.equals(rhs))) {
|
||||
public static void assertNotEquals(Object unexpected, Object actual, String msg) {
|
||||
if ((unexpected == actual) || (unexpected != null && unexpected.equals(actual))) {
|
||||
msg = Objects.toString(msg, "assertNotEquals")
|
||||
+ ": expected " + Objects.toString(lhs)
|
||||
+ " to not equal " + Objects.toString(rhs);
|
||||
+ ": expected not equals but was " + Objects.toString(actual);
|
||||
fail(msg);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user