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
Reviewed-by: mbalao, rhalade
  • Loading branch information
Matthew Donovan committed Apr 3, 2024
1 parent 375bfac commit 16576b8
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

5 comments on commit 16576b8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 16576b8 Apr 22, 2024

Choose a reason for hiding this comment

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

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 16576b8 Apr 22, 2024

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-16576b87 in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 16576b87 from the openjdk/jdk repository.

The commit being backported was authored by Matthew Donovan on 3 Apr 2024 and was reviewed by Martin Balao and Rajan Halade.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-16576b87:backport-GoeLin-16576b87
$ git checkout backport-GoeLin-16576b87
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-16576b87

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 16576b8 Sep 18, 2024

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 16576b8 Sep 18, 2024

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-16576b87-master in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 16576b87 from the openjdk/jdk repository.

The commit being backported was authored by Matthew Donovan on 3 Apr 2024 and was reviewed by Martin Balao and Rajan Halade.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-16576b87-master:backport-GoeLin-16576b87-master
$ git checkout backport-GoeLin-16576b87-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-16576b87-master

Please sign in to comment.