Skip to content

Commit

Permalink
remove plugin design
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweij committed Nov 1, 2024
1 parent 1ff91a3 commit 587f61b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 78 deletions.
57 changes: 7 additions & 50 deletions test/jdk/sun/security/provider/acvp/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,20 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.json.JSONValue;

import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Provider;
import java.security.Security;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* @test
* @bug 8342442
* @library /test/lib
* @build jdk.test.lib.Asserts jdk.test.lib.json.JSONValue
* @run main Launcher
*/
public class Launcher {

public interface Test {
/// Algorithms (as named in ACVP JSON files) this test supports
List<String> supportedAlgs();
/// Runs the KAT with an optional provider
void run(JSONValue kat, Provider p) throws Exception;
}

private static Map<String, Test> tests = new HashMap<>();

public static void main(String[] args) throws Exception {

// This test runs on "internalProjection.json"-style files generated
Expand All @@ -67,9 +52,7 @@ public static void main(String[] args) throws Exception {
// "acvp.test.provider" system property. The provider must be
// registered.
//
// Tests are included in this directory and each must implement
// the Launcher.Test interface. Tests for each algorithm must be
// compliant to its specification linked from
// Tests for each algorithm must be compliant to its specification linked from
// https://github.com/usnistgov/ACVP?tab=readme-ov-file#supported-algorithms.

var testDataProp = System.getProperty("acvp.test.data");
Expand All @@ -86,8 +69,6 @@ public static void main(String[] args) throws Exception {
System.out.println("Provider: " + provProp);
}

setup();

try (var stream = Files.walk(dataPath)) {
stream.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString()
Expand All @@ -96,31 +77,6 @@ public static void main(String[] args) throws Exception {
}
}

static void setup() throws Exception {
var srcDir = Path.of(System.getProperty("test.src"));
try (var files = Files.newDirectoryStream(srcDir)) {
for (var file : files) {
var name = file.getFileName().toString();
if (!name.equals("Launcher.java") && name.endsWith(".java")) {
CompilerUtils.compile(file,
Path.of(System.getProperty("test.classes")),
"-cp",
System.getProperty("test.class.path"));
var obj = Class.forName(name.substring(0, name.length() - 5))
.getConstructor().newInstance();
if (obj instanceof Test t) {
for (var alg : t.supportedAlgs()) {
tests.put(alg, t);
}
} else {
throw new RuntimeException(
name + " has not implemented Test");
}
}
}
}
}

static void run(Provider provider, Path test) {
System.out.println(">>> Testing " + test + "...");
try {
Expand All @@ -132,11 +88,12 @@ static void run(Provider provider, Path test) {
return;
}
var alg = kat.get("algorithm").asString();
var t = tests.get(alg);
if (t != null) {
t.run(kat, provider);
} else {
System.out.println("Skipped unsupported algorithm: " + alg);
switch (alg) {
case "ML-DSA" -> ML_DSA_Test.run(kat, provider);
case "ML-KEM" -> ML_KEM_Test.run(kat, provider);
case "SHA2-256", "SHA2-224", "SHA3-256", "SHA3-224"
-> SHA_Test.run(kat, provider);
default -> System.out.println("Skipped unsupported algorithm: " + alg);
}
} catch (RuntimeException re) {
throw re;
Expand Down
11 changes: 2 additions & 9 deletions test/jdk/sun/security/provider/acvp/ML_DSA_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@
import java.security.spec.EncodedKeySpec;
import java.security.spec.NamedParameterSpec;
import java.util.HexFormat;
import java.util.List;

// JSON spec at https://pages.nist.gov/ACVP/draft-celi-acvp-ml-dsa.html
public class ML_DSA_Test implements Launcher.Test {
public class ML_DSA_Test {

@Override
public List<String> supportedAlgs() {
return List.of("ML-DSA");
}

@Override
public void run(JSONValue kat, Provider provider) throws Exception {
public static void run(JSONValue kat, Provider provider) throws Exception {
switch (kat.get("mode").asString()) {
case "keyGen" -> mldsaGen(kat, provider);
case "sigGen" -> mldsaSign(kat, provider);
Expand Down
11 changes: 2 additions & 9 deletions test/jdk/sun/security/provider/acvp/ML_KEM_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@
import java.security.spec.EncodedKeySpec;
import java.security.spec.NamedParameterSpec;
import java.util.HexFormat;
import java.util.List;

// JSON spec at https://pages.nist.gov/ACVP/draft-celi-acvp-ml-kem.html
public class ML_KEM_Test implements Launcher.Test {
public class ML_KEM_Test {

@Override
public List<String> supportedAlgs() {
return List.of("ML-KEM");
}

@Override
public void run(JSONValue kat, Provider provider) throws Exception {
public static void run(JSONValue kat, Provider provider) throws Exception {
switch (kat.get("mode").asString()) {
case "keyGen" -> mlkemGen(kat, provider);
case "encapDecap" -> mlkemEnc(kat, provider);
Expand Down
12 changes: 2 additions & 10 deletions test/jdk/sun/security/provider/acvp/SHA_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,12 @@
import java.security.*;
import java.util.Arrays;
import java.util.HexFormat;
import java.util.List;

// JSON spec at https://pages.nist.gov/ACVP/draft-celi-acvp-sha.html
// and https://pages.nist.gov/ACVP/draft-celi-acvp-sha3.html
public class SHA_Test implements Launcher.Test {
public class SHA_Test {

@Override
public List<String> supportedAlgs() {
return List.of("SHA2-256", "SHA2-224",
"SHA3-256", "SHA3-224");
}

@Override
public void run(JSONValue kat, Provider provider) throws Exception {
public static void run(JSONValue kat, Provider provider) throws Exception {
var alg = kat.get("algorithm").asString();
if (alg.startsWith("SHA2-")) alg = "SHA-" + alg.substring(5);
var md = provider == null ? MessageDigest.getInstance(alg)
Expand Down

0 comments on commit 587f61b

Please sign in to comment.