Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 6 additions & 32 deletions test/jdk/sun/security/pkcs11/PKCS11Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
Expand Down Expand Up @@ -60,7 +59,6 @@
import jdk.test.lib.Utils;
import jdk.test.lib.artifacts.Artifact;
import jdk.test.lib.artifacts.ArtifactResolver;
import jdk.test.lib.artifacts.ArtifactResolverException;
import jtreg.SkippedException;

public abstract class PKCS11Test {
Expand Down Expand Up @@ -240,10 +238,6 @@ public static String getNSSLibDir() throws Exception {

static String getNSSLibDir(String library) throws Exception {
Path libPath = getNSSLibPath(library);
if (libPath == null) {
return null;
}

String libDir = String.valueOf(libPath.getParent()) + File.separatorChar;
System.out.println("nssLibDir: " + libDir);
System.setProperty("pkcs11test.nss.libdir", libDir);
Expand All @@ -257,12 +251,7 @@ private static Path getNSSLibPath() throws Exception {
static Path getNSSLibPath(String library) throws Exception {
String osid = getOsId();
Path libraryName = Path.of(System.mapLibraryName(library));
Path nssLibPath = fetchNssLib(osid, libraryName);
if (nssLibPath == null) {
throw new SkippedException("Warning: unsupported OS: " + osid
+ ", please initialize NSS library location, skipping test");
}
return nssLibPath;
return fetchNssLib(osid, libraryName);
}

private static String getOsId() {
Expand Down Expand Up @@ -667,7 +656,7 @@ static byte[] generateData(int length) {
return data;
}

private static Path fetchNssLib(String osId, Path libraryName) {
private static Path fetchNssLib(String osId, Path libraryName) throws IOException {
switch (osId) {
case "Windows-amd64-64":
return fetchNssLib(WINDOWS_X64.class, libraryName);
Expand All @@ -692,28 +681,13 @@ private static Path fetchNssLib(String osId, Path libraryName) {
return fetchNssLib(LINUX_AARCH64.class, libraryName);
}
default:
return null;
throw new SkippedException("Unsupported OS: " + osId);
}
}

private static Path fetchNssLib(Class<?> clazz, Path libraryName) {
Path path = null;
try {
Path p = ArtifactResolver.resolve(clazz).entrySet().stream()
.findAny().get().getValue();
path = findNSSLibrary(p, libraryName);
} catch (ArtifactResolverException | IOException e) {
Throwable cause = e.getCause();
if (cause == null) {
System.out.println("Cannot resolve artifact, "
+ "please check if JIB jar is present in classpath.");
} else {
throw new RuntimeException("Fetch artifact failed: " + clazz
+ "\nPlease make sure the artifact is available.", e);
}
}
Policy.setPolicy(null); // Clear the policy created by JIB if any
return path;
private static Path fetchNssLib(Class<?> clazz, Path libraryName) throws IOException {
Path p = ArtifactResolver.fetchOne(clazz);
return findNSSLibrary(p, libraryName);
}

private static Path findNSSLibrary(Path path, Path libraryName) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/sun/security/pkcs11/SecmodTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
Expand Down Expand Up @@ -46,7 +46,7 @@ static boolean initSecmod() throws Exception {
useNSS();
LIBPATH = getNSSLibDir();
// load all the libraries except libnss3 into memory
if ((LIBPATH == null) || (!loadNSPR(LIBPATH))) {
if (!loadNSPR(LIBPATH)) {
throw new SkippedException("Failed to load NSS libraries");
}

Expand Down
17 changes: 3 additions & 14 deletions test/jdk/sun/security/pkcs12/KeytoolOpensslInteropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.security.OpensslArtifactFetcher;
import jtreg.SkippedException;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -82,19 +81,9 @@ public static void main(String[] args) throws Throwable {
boolean generatePKCS12 = Boolean.parseBoolean(args[0]);
if (generatePKCS12) {
String opensslPath = OpensslArtifactFetcher.getOpensslPath();
if (opensslPath != null) {
// if the current version of openssl is available, perform all
// keytool <-> openssl interop tests
generateInitialKeystores(opensslPath);
testWithJavaCommands();
testWithOpensslCommands(opensslPath);
} else {
String exMsg = "Can't find the version: "
+ OpensslArtifactFetcher.getTestOpensslBundleVersion()
+ " of openssl binary on this machine, please install"
+ " and set openssl path with property 'test.openssl.path'";
throw new SkippedException(exMsg);
}
generateInitialKeystores(opensslPath);
testWithJavaCommands();
testWithOpensslCommands(opensslPath);
} else {
// since this scenario is using preexisting PKCS12, skip all
// openssl command dependent tests
Expand Down
34 changes: 34 additions & 0 deletions test/lib/jdk/test/lib/artifacts/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

package jdk.test.lib.artifacts;

import jtreg.SkippedException;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -59,6 +61,38 @@ public static Map<String, Path> resolve(Class<?> klass) throws ArtifactResolverE
return locations;
}

/**
* Retrieve an artifact/library/file from a repository or local file system.
* <p>
* Artifacts are defined with the {@link jdk.test.lib.artifacts.Artifact}
* annotation.
* <p>
* If you have a local version of a dependency that you want to use, you can
* specify that by setting the system property:
* <code>jdk.test.lib.artifacts.ARTIFACT_NAME</code>. Where ARTIFACT_NAME
* is the name field of the Artifact annotation.
* <p>
* Generally, tests that use this method should be run with <code>make test</code>.
* However, tests can also be run with <code>jtreg</code> but you must have a
* local copy of the artifact and the system property must be set as specified
* above.
*
* @param klass a class annotated with {@link jdk.test.lib.artifacts.Artifact}
* @return the local path to the artifact. If the artifact is a compressed
* file that gets unpacked, this path will point to the root
* directory of the uncompressed file(s).
* @throws SkippedException thrown if the artifact cannot be found
*/
public static Path fetchOne(Class<?> klass) {
try {
return ArtifactResolver.resolve(klass).entrySet().stream()
.findAny().get().getValue();
} catch (ArtifactResolverException e) {
Artifact artifact = klass.getAnnotation(Artifact.class);
throw new SkippedException("Cannot find the artifact " + artifact.name(), e);
}
}

private static String artifactName(Artifact artifact) {
// Format of the artifact name is <organization>.<name>-<revision>(-<classifier>)
String name = String.format("%s.%s-%s", artifact.organization(), artifact.name(), artifact.revision());
Expand Down
36 changes: 15 additions & 21 deletions test/lib/jdk/test/lib/security/OpensslArtifactFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@

package jdk.test.lib.security;

import java.io.File;

import java.nio.file.Path;
import jdk.test.lib.Platform;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.artifacts.Artifact;
import jdk.test.lib.artifacts.ArtifactResolver;
import jdk.test.lib.artifacts.ArtifactResolverException;
import jtreg.SkippedException;

public class OpensslArtifactFetcher {

Expand All @@ -50,6 +48,7 @@ public class OpensslArtifactFetcher {
and return that path, if download fails then return null.
*
* @return openssl binary path of the current version
* @throws SkippedException if a valid version of OpenSSL cannot be found
*/
public static String getOpensslPath() {
String path = getOpensslFromSystemProp(OPENSSL_BUNDLE_VERSION);
Expand All @@ -76,7 +75,16 @@ public static String getOpensslPath() {
path = fetchOpenssl(MACOSX_AARCH64.class);
}
}
return verifyOpensslVersion(path, OPENSSL_BUNDLE_VERSION) ? path : null;

if (!verifyOpensslVersion(path, OPENSSL_BUNDLE_VERSION)) {
String exMsg = "Can't find the version: "
+ OpensslArtifactFetcher.getTestOpensslBundleVersion()
+ " of openssl binary on this machine, please install"
+ " and set openssl path with property 'test.openssl.path'";
throw new SkippedException(exMsg);
} else {
return path;
}
}

private static String getOpensslFromSystemProp(String version) {
Expand Down Expand Up @@ -112,23 +120,9 @@ private static boolean verifyOpensslVersion(String path, String version) {
}

private static String fetchOpenssl(Class<?> clazz) {
String path = null;
try {
path = ArtifactResolver.resolve(clazz).entrySet().stream()
.findAny().get().getValue() + File.separator + "openssl"
+ File.separator + "bin" + File.separator + "openssl";
System.out.println("path: " + path);
} catch (ArtifactResolverException e) {
Throwable cause = e.getCause();
if (cause == null) {
System.out.println("Cannot resolve artifact, "
+ "please check if JIB jar is present in classpath.");
} else {
throw new RuntimeException("Fetch artifact failed: " + clazz
+ "\nPlease make sure the artifact is available.", e);
}
}
return path;
return ArtifactResolver.fetchOne(clazz)
.resolve("openssl").resolve("bin").resolve("openssl")
.toString();
}

// retrieve the provider directory path from <OPENSSL_HOME>/bin/openssl
Expand Down