8296718: Refactor bootstrap Test Common Functionalities to test/lib/Utils

Reviewed-by: sspitsyn, kevinw
This commit is contained in:
Bill Huang 2022-11-09 19:02:50 +00:00
parent 17e3412363
commit cc8bf95046
4 changed files with 239 additions and 237 deletions
test
jdk/sun/management/jmxremote/bootstrap
lib/jdk/test/lib

@ -21,6 +21,8 @@
* questions.
*/
import jdk.test.lib.Utils;
import sun.management.jmxremote.ConnectorBootstrap;
import java.io.File;

@ -26,6 +26,7 @@
* */
import jdk.test.lib.Platform;
import jdk.test.lib.Utils;
import java.io.File;
import java.io.FilenameFilter;
@ -144,45 +145,6 @@ public class RmiTestBase {
return Collections.unmodifiableList(propertyFiles);
}
/**
* Grant file access.
*
* @param file file to grant access
* @param access user access or full access
* @throws IOException if error occurs
*/
static void grantAccess(Path file, AccessControl access) throws IOException {
Set<String> attr = file.getFileSystem().supportedFileAttributeViews();
if (attr.contains("posix")) {
String perms = access == AccessControl.OWNER ? "rw-------" : "rwxrwxrwx";
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString(perms));
} else if (attr.contains("acl")) {
AclFileAttributeView view = Files.getFileAttributeView(file, AclFileAttributeView.class);
List<AclEntry> acl = new ArrayList<>();
for (AclEntry thisEntry : view.getAcl()) {
if (access == AccessControl.OWNER) {
if (thisEntry.principal().getName().equals(view.getOwner().getName())) {
acl.add(Utils.allowAccess(thisEntry));
} else if (thisEntry.type() == AclEntryType.ALLOW) {
acl.add(Utils.revokeAccess(thisEntry));
} else {
acl.add(thisEntry);
}
} else {
if (!thisEntry.principal().getName().contains("NULL SID")
&& thisEntry.type() != AclEntryType.ALLOW) {
acl.add(Utils.allowAccess(thisEntry));
} else {
acl.add(thisEntry);
}
}
}
view.setAcl(acl);
} else {
throw new RuntimeException("Unsupported file attributes: " + attr);
}
}
/**
* Grant files' access.
*
@ -190,9 +152,10 @@ public class RmiTestBase {
* @param access user access or full access
* @throws IOException if error occurs
*/
static void grantFilesAccess(List<Path> files, AccessControl access) throws IOException {
static void grantFilesAccess(List<Path> files, AccessControl access)
throws IOException {
for (Path thisFile : files) {
grantAccess(thisFile, access);
Utils.grantFileAccess(thisFile, access == AccessControl.OWNER);
}
}
@ -209,7 +172,7 @@ public class RmiTestBase {
Utils.copyFiles(files, sslTarget, StandardCopyOption.REPLACE_EXISTING);
for (Path file : files) {
grantAccess(sslTarget.resolve(file.getFileName()), AccessControl.EVERYONE);
Utils.fullAccess(sslTarget.resolve(file.getFileName()));
}
}

@ -1,195 +0,0 @@
/*
* Copyright (c) 2003, 2004, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.*;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Utility class.
*/
public class Utils {
static private String fileSeparator = System.getProperty("file.separator");
/**
* Converts slashes in a pathname to backslashes
* if slashes is not the file separator.
*/
static String convertPath(String path) {
// No need to do the conversion if file separator is '/'
if (fileSeparator.length() == 1 && fileSeparator.charAt(0) == '/') {
return path;
}
char[] cs = path.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (cs[i] == '/') {
cs[i] = '\\';
}
}
String newPath = new String(cs);
return newPath;
}
/**
* Return file directories that satisfy the specified filter.
*
* @param searchDirectory the base directory to search
* @param filter a filename filter
* @return file directories
*/
public static List<Path> findFiles(Path searchDirectory,
FilenameFilter filter) {
return Arrays.stream(searchDirectory.toFile().listFiles(filter))
.map(f -> f.toPath())
.collect(Collectors.toList());
}
/**
* Copy files to the target path.
*
* @param source the paths to the files to copy
* @param target the path to the target files
* @param filenameMapper mapper function applied to filenames
* @param options options specifying how the copy should be done
* @return the paths to the target files
* @throws IOException if error occurs
*/
public static List<Path> copyFiles(List<Path> source, Path target,
Function<String, String> filenameMapper,
CopyOption... options) throws IOException {
List<Path> result = new ArrayList<>();
if (!target.toFile().exists()) {
Files.createDirectory(target);
}
for (Path file : source) {
if (!file.toFile().exists()) {
continue;
}
String baseName = file.getFileName().toString();
Path targetFile = target.resolve(filenameMapper.apply(baseName));
Files.copy(file, targetFile, options);
result.add(targetFile);
}
return result;
}
/**
* Copy files to the target path.
*
* @param source the paths to the files to copy
* @param target the path to the target files
* @param options options specifying how the copy should be done
* @return the paths to the target files
* @throws IOException if error occurs
*/
public static List<Path> copyFiles(List<Path> source, Path target,
CopyOption... options) throws IOException {
return copyFiles(source, target, (s) -> s, options);
}
/**
* Return an ACL entry that revokes owner access.
*
* @param acl original ACL entry to build from
* @return an ACL entry that revokes all access
*/
public static AclEntry revokeAccess(AclEntry acl) {
return buildAclEntry(acl, AclEntryType.DENY);
}
/**
* Return an ACL entry that allow owner access.
* @param acl original ACL entry to build from
* @return an ACL entry that allows all access
*/
public static AclEntry allowAccess(AclEntry acl) {
return buildAclEntry(acl, AclEntryType.ALLOW);
}
/**
* Build an ACL entry with a given ACL entry type.
*
* @param acl original ACL entry to build from
* @return an ACL entry with a given ACL entry type
*/
public static AclEntry buildAclEntry(AclEntry acl, AclEntryType type) {
return AclEntry.newBuilder()
.setType(type)
.setPrincipal(acl.principal())
.setPermissions(acl.permissions())
.build();
}
/**
* Replace file string by applying the given mapper function.
*
* @param source the file to read
* @param contentMapper the mapper function applied to file's content
* @throws IOException if an I/O error occurs
*/
public static void replaceFileString(Path source,
Function<String, String> contentMapper) throws IOException {
StringBuilder sb = new StringBuilder();
String lineSep = System.getProperty("line.separator");
try (BufferedReader reader =
new BufferedReader(new FileReader(source.toFile()))) {
String line;
// read all and replace all at once??
while ((line = reader.readLine()) != null) {
sb.append(contentMapper.apply(line))
.append(lineSep);
}
}
try (FileWriter writer = new FileWriter(source.toFile())) {
writer.write(sb.toString());
}
}
/**
* Replace files' string by applying the given mapper function.
*
* @param source the file to read
* @param contentMapper the mapper function applied to files' content
* @throws IOException if an I/O error occurs
*/
public static void replaceFilesString(List<Path> source,
Function<String, String> contentMapper) throws IOException {
for (Path file : source) {
replaceFileString(file, contentMapper);
}
}
}

@ -23,7 +23,11 @@
package jdk.test.lib;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
@ -35,11 +39,16 @@ import java.net.URLClassLoader;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.FileAttribute;
import java.nio.channels.SocketChannel;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@ -53,12 +62,14 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static jdk.test.lib.Asserts.assertTrue;
import jdk.test.lib.process.ProcessTools;
@ -129,6 +140,11 @@ public final class Utils {
*/
public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed";
/**
* Returns the value of 'file.separator' system property
*/
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
/**
* Random generator with predefined seed.
*/
@ -818,4 +834,220 @@ public final class Utils {
Path dir = Paths.get(System.getProperty("user.dir", "."));
return Files.createTempDirectory(dir, prefix, attrs);
}
/**
* Converts slashes in a pathname to backslashes
* if slashes is not the file separator.
*/
public static String convertPath(String path) {
// No need to do the conversion if file separator is '/'
if (FILE_SEPARATOR.length() == 1 && FILE_SEPARATOR.charAt(0) == '/') {
return path;
}
char[] cs = path.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (cs[i] == '/') {
cs[i] = '\\';
}
}
String newPath = new String(cs);
return newPath;
}
/**
* Return file directories that satisfy the specified filter.
*
* @param searchDirectory the base directory to search
* @param filter a filename filter
* @return file directories
*/
public static List<Path> findFiles(Path searchDirectory,
FilenameFilter filter) {
return Arrays.stream(searchDirectory.toFile().listFiles(filter))
.map(f -> f.toPath())
.collect(Collectors.toList());
}
/**
* Copy files to the target path.
*
* @param source the paths to the files to copy
* @param target the path to the target files
* @param filenameMapper mapper function applied to filenames
* @param options options specifying how the copy should be done
* @return the paths to the target files
* @throws IOException if error occurs
*/
public static List<Path> copyFiles(List<Path> source, Path target,
Function<String, String> filenameMapper,
CopyOption... options) throws IOException {
List<Path> result = new ArrayList<>();
if (!target.toFile().exists()) {
Files.createDirectory(target);
}
for (Path file : source) {
if (!file.toFile().exists()) {
continue;
}
String baseName = file.getFileName().toString();
Path targetFile = target.resolve(filenameMapper.apply(baseName));
Files.copy(file, targetFile, options);
result.add(targetFile);
}
return result;
}
/**
* Copy files to the target path.
*
* @param source the paths to the files to copy
* @param target the path to the target files
* @param options options specifying how the copy should be done
* @return the paths to the target files
* @throws IOException if error occurs
*/
public static List<Path> copyFiles(List<Path> source, Path target,
CopyOption... options) throws IOException {
return copyFiles(source, target, (s) -> s, options);
}
/**
* Replace file string by applying the given mapper function.
*
* @param source the file to read
* @param contentMapper the mapper function applied to file's content
* @throws IOException if an I/O error occurs
*/
public static void replaceFileString(Path source,
Function<String, String> contentMapper) throws IOException {
StringBuilder sb = new StringBuilder();
String lineSep = System.getProperty("line.separator");
try (BufferedReader reader =
new BufferedReader(new FileReader(source.toFile()))) {
String line;
// read all and replace all at once??
while ((line = reader.readLine()) != null) {
sb.append(contentMapper.apply(line))
.append(lineSep);
}
}
try (FileWriter writer = new FileWriter(source.toFile())) {
writer.write(sb.toString());
}
}
/**
* Replace files' string by applying the given mapper function.
*
* @param source the file to read
* @param contentMapper the mapper function applied to files' content
* @throws IOException if an I/O error occurs
*/
public static void replaceFilesString(List<Path> source,
Function<String, String> contentMapper) throws IOException {
for (Path file : source) {
replaceFileString(file, contentMapper);
}
}
/**
* Grant file user access or full access to everyone.
*
* @param file file to grant access
* @param userOnly true for user access, otherwise full access to everyone
* @throws IOException if error occurs
*/
public static void grantFileAccess(Path file, boolean userOnly)
throws IOException {
Set<String> attr = file.getFileSystem().supportedFileAttributeViews();
if (attr.contains("posix")) {
String perms = userOnly ? "rwx------" : "rwxrwxrwx";
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString(perms));
} else if (attr.contains("acl")) {
AclFileAttributeView view =
Files.getFileAttributeView(file, AclFileAttributeView.class);
List<AclEntry> acl = new ArrayList<>();
for (AclEntry thisEntry : view.getAcl()) {
if (userOnly) {
if (thisEntry.principal().getName()
.equals(view.getOwner().getName())) {
acl.add(allowAccess(thisEntry));
} else if (thisEntry.type() == AclEntryType.ALLOW) {
acl.add(revokeAccess(thisEntry));
} else {
acl.add(thisEntry);
}
} else {
if (thisEntry.type() != AclEntryType.ALLOW) {
acl.add(allowAccess(thisEntry));
} else {
acl.add(thisEntry);
}
}
}
view.setAcl(acl);
} else {
throw new RuntimeException("Unsupported file attributes: " + attr);
}
}
/**
* Return an ACL entry that revokes owner access.
*
* @param acl original ACL entry to build from
* @return an ACL entry that revokes all access
*/
public static AclEntry revokeAccess(AclEntry acl) {
return buildAclEntry(acl, AclEntryType.DENY);
}
/**
* Return an ACL entry that allow owner access.
* @param acl original ACL entry to build from
* @return an ACL entry that allows all access
*/
public static AclEntry allowAccess(AclEntry acl) {
return buildAclEntry(acl, AclEntryType.ALLOW);
}
/**
* Build an ACL entry with a given ACL entry type.
*
* @param acl original ACL entry to build from
* @return an ACL entry with a given ACL entry type
*/
public static AclEntry buildAclEntry(AclEntry acl, AclEntryType type) {
return AclEntry.newBuilder(acl)
.setType(type)
.build();
}
/**
* Grant file user access.
*
* @param file file to grant access
* @throws IOException if error occurs
*/
public static void userAccess(Path file) throws IOException {
grantFileAccess(file, true);
}
/**
* Grant file full access to everyone.
*
* @param file file to grant access
* @throws IOException if error occurs
*/
public static void fullAccess(Path file) throws IOException {
grantFileAccess(file, false);
}
}