Skip to content

Commit e60304a

Browse files
committed
8350964: Add an ArtifactResolver.fetch(clazz) method
Reviewed-by: rrich Backport-of: e62becc6ce8e1b777c3e5b78a9d9406b6389cd25
1 parent 245d53c commit e60304a

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;
@@ -64,7 +63,6 @@
6463
import jdk.test.lib.Utils;
6564
import jdk.test.lib.artifacts.Artifact;
6665
import jdk.test.lib.artifacts.ArtifactResolver;
67-
import jdk.test.lib.artifacts.ArtifactResolverException;
6866
import jtreg.SkippedException;
6967

7068
public abstract class PKCS11Test {
@@ -245,10 +243,6 @@ public static String getNSSLibDir() throws Exception {
245243

246244
static String getNSSLibDir(String library) throws Exception {
247245
Path libPath = getNSSLibPath(library);
248-
if (libPath == null) {
249-
return null;
250-
}
251-
252246
String libDir = String.valueOf(libPath.getParent()) + File.separatorChar;
253247
System.out.println("nssLibDir: " + libDir);
254248
System.setProperty("pkcs11test.nss.libdir", libDir);
@@ -262,12 +256,7 @@ private static Path getNSSLibPath() throws Exception {
262256
static Path getNSSLibPath(String library) throws Exception {
263257
String osid = getOsId();
264258
Path libraryName = Path.of(System.mapLibraryName(library));
265-
Path nssLibPath = fetchNssLib(osid, libraryName);
266-
if (nssLibPath == null) {
267-
throw new SkippedException("Warning: unsupported OS: " + osid
268-
+ ", please initialize NSS library location, skipping test");
269-
}
270-
return nssLibPath;
259+
return fetchNssLib(osid, libraryName);
271260
}
272261

273262
private static String getOsId() {
@@ -728,7 +717,7 @@ static byte[] generateData(int length) {
728717
return data;
729718
}
730719

731-
private static Path fetchNssLib(String osId, Path libraryName) {
720+
private static Path fetchNssLib(String osId, Path libraryName) throws IOException {
732721
switch (osId) {
733722
case "Windows-amd64-64":
734723
return fetchNssLib(WINDOWS_X64.class, libraryName);
@@ -753,28 +742,13 @@ private static Path fetchNssLib(String osId, Path libraryName) {
753742
return fetchNssLib(LINUX_AARCH64.class, libraryName);
754743
}
755744
default:
756-
return null;
745+
throw new SkippedException("Unsupported OS: " + osId);
757746
}
758747
}
759748

760-
private static Path fetchNssLib(Class<?> clazz, Path libraryName) {
761-
Path path = null;
762-
try {
763-
Path p = ArtifactResolver.resolve(clazz).entrySet().stream()
764-
.findAny().get().getValue();
765-
path = findNSSLibrary(p, libraryName);
766-
} catch (ArtifactResolverException | IOException e) {
767-
Throwable cause = e.getCause();
768-
if (cause == null) {
769-
System.out.println("Cannot resolve artifact, "
770-
+ "please check if JIB jar is present in classpath.");
771-
} else {
772-
throw new RuntimeException("Fetch artifact failed: " + clazz
773-
+ "\nPlease make sure the artifact is available.", e);
774-
}
775-
}
776-
Policy.setPolicy(null); // Clear the policy created by JIB if any
777-
return path;
749+
private static Path fetchNssLib(Class<?> clazz, Path libraryName) throws IOException {
750+
Path p = ArtifactResolver.fetchOne(clazz);
751+
return findNSSLibrary(p, libraryName);
778752
}
779753

780754
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;
@@ -68,6 +70,38 @@ public static Path resolve(String name, Map<String, Object> artifactDescription,
6870
return manager.resolve(name, artifactDescription, unpack);
6971
}
7072

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