Skip to content

Commit 490cea3

Browse files
committed
8350964: Add an ArtifactResolver.fetch(clazz) method
Backport-of: e60304ab52f36bcc85703bf8bc369f9e3ba04a80
1 parent 511e855 commit 490cea3

File tree

5 files changed

+60
-69
lines changed

5 files changed

+60
-69
lines changed

test/jdk/sun/security/pkcs11/PKCS11Test.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.nio.charset.StandardCharsets;
3232
import java.nio.file.Files;
3333
import java.nio.file.Path;
34-
import java.nio.file.Paths;
3534
import java.nio.file.StandardCopyOption;
3635
import java.security.AlgorithmParameters;
3736
import java.security.InvalidAlgorithmParameterException;
@@ -60,7 +59,6 @@
6059
import jdk.test.lib.Utils;
6160
import jdk.test.lib.artifacts.Artifact;
6261
import jdk.test.lib.artifacts.ArtifactResolver;
63-
import jdk.test.lib.artifacts.ArtifactResolverException;
6462
import jtreg.SkippedException;
6563

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

241239
static String getNSSLibDir(String library) throws Exception {
242240
Path libPath = getNSSLibPath(library);
243-
if (libPath == null) {
244-
return null;
245-
}
246-
247241
String libDir = String.valueOf(libPath.getParent()) + File.separatorChar;
248242
System.out.println("nssLibDir: " + libDir);
249243
System.setProperty("pkcs11test.nss.libdir", libDir);
@@ -257,12 +251,7 @@ private static Path getNSSLibPath() throws Exception {
257251
static Path getNSSLibPath(String library) throws Exception {
258252
String osid = getOsId();
259253
Path libraryName = Path.of(System.mapLibraryName(library));
260-
Path nssLibPath = fetchNssLib(osid, libraryName);
261-
if (nssLibPath == null) {
262-
throw new SkippedException("Warning: unsupported OS: " + osid
263-
+ ", please initialize NSS library location, skipping test");
264-
}
265-
return nssLibPath;
254+
return fetchNssLib(osid, libraryName);
266255
}
267256

268257
private static String getOsId() {
@@ -667,7 +656,7 @@ static byte[] generateData(int length) {
667656
return data;
668657
}
669658

670-
private static Path fetchNssLib(String osId, Path libraryName) {
659+
private static Path fetchNssLib(String osId, Path libraryName) throws IOException {
671660
switch (osId) {
672661
case "Windows-amd64-64":
673662
return fetchNssLib(WINDOWS_X64.class, libraryName);
@@ -692,28 +681,13 @@ private static Path fetchNssLib(String osId, Path libraryName) {
692681
return fetchNssLib(LINUX_AARCH64.class, libraryName);
693682
}
694683
default:
695-
return null;
684+
throw new SkippedException("Unsupported OS: " + osId);
696685
}
697686
}
698687

699-
private static Path fetchNssLib(Class<?> clazz, Path libraryName) {
700-
Path path = null;
701-
try {
702-
Path p = ArtifactResolver.resolve(clazz).entrySet().stream()
703-
.findAny().get().getValue();
704-
path = findNSSLibrary(p, libraryName);
705-
} catch (ArtifactResolverException | IOException e) {
706-
Throwable cause = e.getCause();
707-
if (cause == null) {
708-
System.out.println("Cannot resolve artifact, "
709-
+ "please check if JIB jar is present in classpath.");
710-
} else {
711-
throw new RuntimeException("Fetch artifact failed: " + clazz
712-
+ "\nPlease make sure the artifact is available.", e);
713-
}
714-
}
715-
Policy.setPolicy(null); // Clear the policy created by JIB if any
716-
return path;
688+
private static Path fetchNssLib(Class<?> clazz, Path libraryName) throws IOException {
689+
Path p = ArtifactResolver.fetchOne(clazz);
690+
return findNSSLibrary(p, libraryName);
717691
}
718692

719693
private static Path findNSSLibrary(Path path, Path libraryName) throws IOException {

test/jdk/sun/security/pkcs11/SecmodTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@ static boolean initSecmod() throws Exception {
4646
useNSS();
4747
LIBPATH = getNSSLibDir();
4848
// load all the libraries except libnss3 into memory
49-
if ((LIBPATH == null) || (!loadNSPR(LIBPATH))) {
49+
if (!loadNSPR(LIBPATH)) {
5050
throw new SkippedException("Failed to load NSS libraries");
5151
}
5252

test/jdk/sun/security/pkcs12/KeytoolOpensslInteropTest.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import jdk.test.lib.process.ProcessTools;
5757
import jdk.test.lib.process.OutputAnalyzer;
5858
import jdk.test.lib.security.OpensslArtifactFetcher;
59-
import jtreg.SkippedException;
6059

6160
import java.io.File;
6261
import java.io.FileInputStream;
@@ -82,19 +81,9 @@ public static void main(String[] args) throws Throwable {
8281
boolean generatePKCS12 = Boolean.parseBoolean(args[0]);
8382
if (generatePKCS12) {
8483
String opensslPath = OpensslArtifactFetcher.getOpensslPath();
85-
if (opensslPath != null) {
86-
// if the current version of openssl is available, perform all
87-
// keytool <-> openssl interop tests
88-
generateInitialKeystores(opensslPath);
89-
testWithJavaCommands();
90-
testWithOpensslCommands(opensslPath);
91-
} else {
92-
String exMsg = "Can't find the version: "
93-
+ OpensslArtifactFetcher.getTestOpensslBundleVersion()
94-
+ " of openssl binary on this machine, please install"
95-
+ " and set openssl path with property 'test.openssl.path'";
96-
throw new SkippedException(exMsg);
97-
}
84+
generateInitialKeystores(opensslPath);
85+
testWithJavaCommands();
86+
testWithOpensslCommands(opensslPath);
9887
} else {
9988
// since this scenario is using preexisting PKCS12, skip all
10089
// openssl command dependent tests

test/lib/jdk/test/lib/artifacts/ArtifactResolver.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
package jdk.test.lib.artifacts;
2525

26+
import jtreg.SkippedException;
27+
2628
import java.nio.file.Path;
2729
import java.util.HashMap;
2830
import java.util.Map;
@@ -59,6 +61,38 @@ public static Map<String, Path> resolve(Class<?> klass) throws ArtifactResolverE
5961
return locations;
6062
}
6163

64+
/**
65+
* Retrieve an artifact/library/file from a repository or local file system.
66+
* <p>
67+
* Artifacts are defined with the {@link jdk.test.lib.artifacts.Artifact}
68+
* annotation.
69+
* <p>
70+
* If you have a local version of a dependency that you want to use, you can
71+
* specify that by setting the system property:
72+
* <code>jdk.test.lib.artifacts.ARTIFACT_NAME</code>. Where ARTIFACT_NAME
73+
* is the name field of the Artifact annotation.
74+
* <p>
75+
* Generally, tests that use this method should be run with <code>make test</code>.
76+
* However, tests can also be run with <code>jtreg</code> but you must have a
77+
* local copy of the artifact and the system property must be set as specified
78+
* above.
79+
*
80+
* @param klass a class annotated with {@link jdk.test.lib.artifacts.Artifact}
81+
* @return the local path to the artifact. If the artifact is a compressed
82+
* file that gets unpacked, this path will point to the root
83+
* directory of the uncompressed file(s).
84+
* @throws SkippedException thrown if the artifact cannot be found
85+
*/
86+
public static Path fetchOne(Class<?> klass) {
87+
try {
88+
return ArtifactResolver.resolve(klass).entrySet().stream()
89+
.findAny().get().getValue();
90+
} catch (ArtifactResolverException e) {
91+
Artifact artifact = klass.getAnnotation(Artifact.class);
92+
throw new SkippedException("Cannot find the artifact " + artifact.name(), e);
93+
}
94+
}
95+
6296
private static String artifactName(Artifact artifact) {
6397
// Format of the artifact name is <organization>.<name>-<revision>(-<classifier>)
6498
String name = String.format("%s.%s-%s", artifact.organization(), artifact.name(), artifact.revision());

test/lib/jdk/test/lib/security/OpensslArtifactFetcher.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323

2424
package jdk.test.lib.security;
2525

26-
import java.io.File;
27-
2826
import java.nio.file.Path;
2927
import jdk.test.lib.Platform;
3028
import jdk.test.lib.process.ProcessTools;
3129
import jdk.test.lib.artifacts.Artifact;
3230
import jdk.test.lib.artifacts.ArtifactResolver;
33-
import jdk.test.lib.artifacts.ArtifactResolverException;
31+
import jtreg.SkippedException;
3432

3533
public class OpensslArtifactFetcher {
3634

@@ -50,6 +48,7 @@ public class OpensslArtifactFetcher {
5048
and return that path, if download fails then return null.
5149
*
5250
* @return openssl binary path of the current version
51+
* @throws SkippedException if a valid version of OpenSSL cannot be found
5352
*/
5453
public static String getOpensslPath() {
5554
String path = getOpensslFromSystemProp(OPENSSL_BUNDLE_VERSION);
@@ -76,7 +75,16 @@ public static String getOpensslPath() {
7675
path = fetchOpenssl(MACOSX_AARCH64.class);
7776
}
7877
}
79-
return verifyOpensslVersion(path, OPENSSL_BUNDLE_VERSION) ? path : null;
78+
79+
if (!verifyOpensslVersion(path, OPENSSL_BUNDLE_VERSION)) {
80+
String exMsg = "Can't find the version: "
81+
+ OpensslArtifactFetcher.getTestOpensslBundleVersion()
82+
+ " of openssl binary on this machine, please install"
83+
+ " and set openssl path with property 'test.openssl.path'";
84+
throw new SkippedException(exMsg);
85+
} else {
86+
return path;
87+
}
8088
}
8189

8290
private static String getOpensslFromSystemProp(String version) {
@@ -112,23 +120,9 @@ private static boolean verifyOpensslVersion(String path, String version) {
112120
}
113121

114122
private static String fetchOpenssl(Class<?> clazz) {
115-
String path = null;
116-
try {
117-
path = ArtifactResolver.resolve(clazz).entrySet().stream()
118-
.findAny().get().getValue() + File.separator + "openssl"
119-
+ File.separator + "bin" + File.separator + "openssl";
120-
System.out.println("path: " + path);
121-
} catch (ArtifactResolverException e) {
122-
Throwable cause = e.getCause();
123-
if (cause == null) {
124-
System.out.println("Cannot resolve artifact, "
125-
+ "please check if JIB jar is present in classpath.");
126-
} else {
127-
throw new RuntimeException("Fetch artifact failed: " + clazz
128-
+ "\nPlease make sure the artifact is available.", e);
129-
}
130-
}
131-
return path;
123+
return ArtifactResolver.fetchOne(clazz)
124+
.resolve("openssl").resolve("bin").resolve("openssl")
125+
.toString();
132126
}
133127

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

0 commit comments

Comments
 (0)