8328957: Update PKCS11Test.java to not use hardcoded path

Reviewed-by: mbalao, rhalade
This commit is contained in:
Matthew Donovan 2024-04-03 17:17:12 +00:00
parent 375bfac8e7
commit 16576b87b7

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2024, 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
@ -54,9 +54,7 @@ import java.util.Properties;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@ -261,19 +259,13 @@ public abstract class PKCS11Test {
static Path getNSSLibPath(String library) throws Exception {
String osid = getOsId();
String nssLibDir = fetchNssLib(osid);
if (nssLibDir == null) {
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");
}
String libraryName = System.mapLibraryName(library);
Path libPath = Paths.get(nssLibDir).resolve(libraryName);
if (!Files.exists(libPath)) {
throw new SkippedException("NSS library \"" + libraryName + "\" was not found in " + nssLibDir);
}
return libPath;
return nssLibPath;
}
private static String getOsId() {
@ -735,42 +727,42 @@ public abstract class PKCS11Test {
return data;
}
private static String fetchNssLib(String osId) {
private static Path fetchNssLib(String osId, Path libraryName) {
switch (osId) {
case "Windows-amd64-64":
return fetchNssLib(WINDOWS_X64.class);
return fetchNssLib(WINDOWS_X64.class, libraryName);
case "MacOSX-x86_64-64":
return fetchNssLib(MACOSX_X64.class);
return fetchNssLib(MACOSX_X64.class, libraryName);
case "MacOSX-aarch64-64":
return fetchNssLib(MACOSX_AARCH64.class);
return fetchNssLib(MACOSX_AARCH64.class, libraryName);
case "Linux-amd64-64":
if (Platform.isOracleLinux7()) {
throw new SkippedException("Skipping Oracle Linux prior to v8");
} else {
return fetchNssLib(LINUX_X64.class);
return fetchNssLib(LINUX_X64.class, libraryName);
}
case "Linux-aarch64-64":
if (Platform.isOracleLinux7()) {
throw new SkippedException("Skipping Oracle Linux prior to v8");
} else {
return fetchNssLib(LINUX_AARCH64.class);
return fetchNssLib(LINUX_AARCH64.class, libraryName);
}
default:
return null;
}
}
private static String fetchNssLib(Class<?> clazz) {
String path = null;
private static Path fetchNssLib(Class<?> clazz, Path libraryName) {
Path path = null;
try {
path = ArtifactResolver.resolve(clazz).entrySet().stream()
.findAny().get().getValue() + File.separator + "nss"
+ File.separator + "lib" + File.separator;
} catch (ArtifactResolverException e) {
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, "
@ -784,6 +776,16 @@ public abstract class PKCS11Test {
return path;
}
private static Path findNSSLibrary(Path path, Path libraryName) throws IOException {
try(Stream<Path> files = Files.find(path, 10,
(tp, attr) -> tp.getFileName().equals(libraryName))) {
return files.findAny()
.orElseThrow(() -> new SkippedException(
"NSS library \"" + libraryName + "\" was not found in " + path));
}
}
public abstract void main(Provider p) throws Exception;
protected boolean skipTest(Provider p) {