Skip to content

Commit

Permalink
8328957: Update PKCS11Test.java to not use hardcoded path
Browse files Browse the repository at this point in the history
Backport-of: 16576b87b7267aaa99c41f77993287e3479577aa
  • Loading branch information
GoeLin committed Apr 23, 2024
1 parent bb046b7 commit a36d3de
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions test/jdk/sun/security/pkcs11/PKCS11Test.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -54,9 +54,7 @@
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;
Expand Down Expand Up @@ -261,19 +259,13 @@ private static Path getNSSLibPath() throws Exception {

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() {
Expand Down Expand Up @@ -735,42 +727,42 @@ static byte[] generateData(int length) {
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, "
Expand All @@ -784,6 +776,16 @@ private static String fetchNssLib(Class<?> clazz) {
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) {
Expand Down

1 comment on commit a36d3de

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.