Skip to content

Commit e62becc

Browse files
author
Matthew Donovan
committed
8350964: Add an ArtifactResolver.fetch(clazz) method
Reviewed-by: weijun
1 parent dbf47d6 commit e62becc

File tree

6 files changed

+61
-86
lines changed

6 files changed

+61
-86
lines changed

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

Lines changed: 6 additions & 31 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;
@@ -63,7 +62,6 @@
6362
import jdk.test.lib.Utils;
6463
import jdk.test.lib.artifacts.Artifact;
6564
import jdk.test.lib.artifacts.ArtifactResolver;
66-
import jdk.test.lib.artifacts.ArtifactResolverException;
6765
import jtreg.SkippedException;
6866

6967
public abstract class PKCS11Test {
@@ -232,10 +230,6 @@ public static String getNSSLibDir() throws Exception {
232230

233231
static String getNSSLibDir(String library) throws Exception {
234232
Path libPath = getNSSLibPath(library);
235-
if (libPath == null) {
236-
return null;
237-
}
238-
239233
String libDir = String.valueOf(libPath.getParent()) + File.separatorChar;
240234
System.out.println("nssLibDir: " + libDir);
241235
System.setProperty("pkcs11test.nss.libdir", libDir);
@@ -249,12 +243,7 @@ private static Path getNSSLibPath() throws Exception {
249243
static Path getNSSLibPath(String library) throws Exception {
250244
String osid = getOsId();
251245
Path libraryName = Path.of(System.mapLibraryName(library));
252-
Path nssLibPath = fetchNssLib(osid, libraryName);
253-
if (nssLibPath == null) {
254-
throw new SkippedException("Warning: unsupported OS: " + osid
255-
+ ", please initialize NSS library location, skipping test");
256-
}
257-
return nssLibPath;
246+
return fetchNssLib(osid, libraryName);
258247
}
259248

260249
private static String getOsId() {
@@ -715,7 +704,7 @@ static byte[] generateData(int length) {
715704
return data;
716705
}
717706

718-
private static Path fetchNssLib(String osId, Path libraryName) {
707+
private static Path fetchNssLib(String osId, Path libraryName) throws IOException {
719708
switch (osId) {
720709
case "Windows-amd64-64":
721710
return fetchNssLib(WINDOWS_X64.class, libraryName);
@@ -740,27 +729,13 @@ private static Path fetchNssLib(String osId, Path libraryName) {
740729
return fetchNssLib(LINUX_AARCH64.class, libraryName);
741730
}
742731
default:
743-
return null;
732+
throw new SkippedException("Unsupported OS: " + osId);
744733
}
745734
}
746735

747-
private static Path fetchNssLib(Class<?> clazz, Path libraryName) {
748-
Path path = null;
749-
try {
750-
Path p = ArtifactResolver.resolve(clazz).entrySet().stream()
751-
.findAny().get().getValue();
752-
path = findNSSLibrary(p, libraryName);
753-
} catch (ArtifactResolverException | IOException e) {
754-
Throwable cause = e.getCause();
755-
if (cause == null) {
756-
System.out.println("Cannot resolve artifact, "
757-
+ "please check if JIB jar is present in classpath.");
758-
} else {
759-
throw new RuntimeException("Fetch artifact failed: " + clazz
760-
+ "\nPlease make sure the artifact is available.", e);
761-
}
762-
}
763-
return path;
736+
private static Path fetchNssLib(Class<?> clazz, Path libraryName) throws IOException {
737+
Path p = ArtifactResolver.fetchOne(clazz);
738+
return findNSSLibrary(p, libraryName);
764739
}
765740

766741
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/jdk/sun/security/provider/acvp/Launcher.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
import jdk.test.lib.artifacts.Artifact;
2525
import jdk.test.lib.artifacts.ArtifactResolver;
26-
import jdk.test.lib.artifacts.ArtifactResolverException;
2726
import jdk.test.lib.json.JSONValue;
28-
import jtreg.SkippedException;
2927

3028
import java.io.InputStream;
3129
import java.nio.file.Path;
@@ -106,7 +104,7 @@ public class Launcher {
106104

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

109-
Path archivePath = fetchACVPServerTests(ACVP_SERVER_TESTS.class);
107+
Path archivePath = ArtifactResolver.fetchOne(ACVP_SERVER_TESTS.class);
110108
System.out.println("Data path: " + archivePath);
111109

112110
if (PROVIDER != null) {
@@ -178,21 +176,6 @@ static void run(InputStream test) {
178176
}
179177
}
180178

181-
private static Path fetchACVPServerTests(Class<?> clazz) {
182-
try {
183-
return ArtifactResolver.resolve(clazz).entrySet().stream()
184-
.findAny().get().getValue();
185-
} catch (ArtifactResolverException e) {
186-
Throwable cause = e.getCause();
187-
if (cause == null) {
188-
throw new SkippedException("Cannot resolve artifact, "
189-
+ "please check if JIB jar is present in classpath.", e);
190-
}
191-
192-
throw new SkippedException("Fetch artifact failed: " + clazz, e);
193-
}
194-
}
195-
196179
@Artifact(
197180
organization = ACVP_BUNDLE_LOC,
198181
name = ACVP_BUNDLE_NAME,

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", "bin", "openssl")
125+
.toString();
132126
}
133127

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

0 commit comments

Comments
 (0)