Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk17 Public archive

Commit

Permalink
8269276: Additional tests for MessageDigest with different providers
Browse files Browse the repository at this point in the history
Reviewed-by: valeriep, wetmore
  • Loading branch information
Sibabrata Sahoo committed Jul 15, 2021
1 parent 7b4d84c commit a32d2ee
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions test/jdk/sun/security/pkcs11/MessageDigest/ReinitDigest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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 @@ -21,9 +21,9 @@
* questions.
*/

/*
/*
* @test
* @bug 4856966 8242332
* @bug 4856966 8242332 8269276
* @summary
* @author Andreas Sterbenz
* @library /test/lib ..
Expand All @@ -32,7 +32,6 @@
* @run main/othervm ReinitDigest
* @run main/othervm -Djava.security.manager=allow ReinitDigest sm
*/

import java.security.MessageDigest;
import java.security.Provider;
import java.util.Arrays;
Expand All @@ -47,8 +46,7 @@ public static void main(String[] args) throws Exception {

@Override
public void main(Provider p) throws Exception {
List<String> ALGS = getSupportedAlgorithms("MessageDigest",
"SHA", p);
List<String> ALGS = getSupportedAlgorithms("MessageDigest", "SHA", p);
Random r = new Random();
byte[] data1 = new byte[10 * 1024];
byte[] data2 = new byte[10 * 1024];
Expand All @@ -75,17 +73,18 @@ public void main(Provider p) throws Exception {
private void doTest(String alg, Provider p, byte[] data1, byte[] data2)
throws Exception {
System.out.println("Testing " + alg);
MessageDigest md = MessageDigest.getInstance(alg, "SUN");
byte[] d1 = md.digest(data1);
md = MessageDigest.getInstance(alg, p);
byte[] d2 = md.digest(data1);
MessageDigest md1 = MessageDigest.getInstance(alg, "SUN");
byte[] d1 = md1.digest(data1);
MessageDigest md2 = MessageDigest.getInstance(alg, p);
checkInstances(md1, md2);
byte[] d2 = md2.digest(data1);
check(d1, d2);
byte[] d3 = md.digest(data1);
byte[] d3 = md2.digest(data1);
check(d1, d3);
md.update(data2);
md.update((byte)0);
md.reset();
byte[] d4 = md.digest(data1);
md2.update(data2);
md2.update((byte) 0);
md2.reset();
byte[] d4 = md2.digest(data1);
check(d1, d4);
}

Expand All @@ -94,4 +93,17 @@ private static void check(byte[] d1, byte[] d2) throws Exception {
throw new RuntimeException("Digest mismatch");
}
}

private static void checkInstances(MessageDigest md1, MessageDigest md2)
throws Exception {
if (md1.equals(md2)) {
throw new RuntimeException("MD instances should be different");
}
if (!md1.getAlgorithm().equals(md2.getAlgorithm())) {
throw new RuntimeException("Algorithm name should equal");
}
if (md1.getProvider().getName().equals(md2.getProvider().getName())) {
throw new RuntimeException("Provider name should be different");
}
}
}

1 comment on commit a32d2ee

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.