Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java
41153 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import jdk.test.lib.process.OutputAnalyzer;24import jdk.test.lib.process.ProcessTools;25import jdk.test.lib.Platform;2627import java.io.BufferedWriter;28import java.io.IOException;29import java.nio.charset.Charset;30import java.nio.file.FileSystem;31import java.nio.file.FileSystems;32import java.nio.file.Files;33import java.nio.file.Path;34import java.nio.file.attribute.PosixFilePermission;35import java.util.ArrayList;36import java.util.Arrays;37import java.util.Collections;38import java.util.HashSet;39import java.util.List;40import java.util.Set;4142/**43* Change file permission for out-of-the-box management an do test used by44* PasswordFilePermissionTest and SSLConfigFilePermissionTest tests45*46* @author Taras Ledkov47*/48public abstract class AbstractFilePermissionTest {49private final String TEST_CLASS_PATH = System.getProperty("test.class.path");50protected final String TEST_CLASSES = System.getProperty("test.classes");51protected final FileSystem FS = FileSystems.getDefault();52private int MAX_GET_FREE_PORT_TRIES = 10;5354protected final Path libDir = FS.getPath(TEST_CLASSES, "lib");55protected final Path mgmt = libDir.resolve("management.properties");56private final String mp = "-Dcom.sun.management.config.file=" + mgmt.toFile().getAbsolutePath();57private final String className = "Dummy";58private int failures = 0;5960protected final Path file2PermissionTest;6162protected AbstractFilePermissionTest(String fileName2PermissionTest) {63this.file2PermissionTest = libDir.resolve(fileName2PermissionTest);6465try {66MAX_GET_FREE_PORT_TRIES = Integer.parseInt(System.getProperty("test.getfreeport.max.tries", "10"));67} catch (NumberFormatException ex) {68ex.printStackTrace();69}70}717273public static void createFile(Path path, String... content) throws IOException {74if (Files.exists(path) && Files.isRegularFile(path)) {75try {76Files.delete(path);77} catch (Exception ex) {78System.out.println("WARNING: " + path.toFile().getAbsolutePath() + " already exists - unable to remove old copy");79ex.printStackTrace();80}81}8283try (BufferedWriter bw = Files.newBufferedWriter(path, Charset.defaultCharset())) {84for (String str : content) {85bw.write(str, 0, str.length());86bw.newLine();87}88}89}9091public boolean skipTest() {92if ((TEST_CLASSES == null) || ("".equals(TEST_CLASSES))) {93System.out.println("Test is designed to be run from jtreg only");94return true;95}9697if (!Platform.isLinux()) {98System.out.println("Test not designed to run on this operating system, skipping...");99return true;100}101return false;102}103104protected abstract void testSetup() throws IOException;105106public void runTest(String[] args) throws Exception {107108if (skipTest()) {109return;110}111112Files.deleteIfExists(mgmt);113Files.deleteIfExists(file2PermissionTest);114libDir.toFile().mkdir();115116testSetup();117118try {119test1();120test2();121122if (failures == 0) {123System.out.println("All test(s) passed");124} else {125throw new Error(String.format("%d test(s) failed", failures));126}127} finally {128resetPasswordFilePermission();129}130}131132/**133* Test 1 - SSL config file is secure - VM should start134*/135private void test1() throws Exception {136final Set<PosixFilePermission> perms_0700 = new HashSet<>();137perms_0700.add(PosixFilePermission.OWNER_WRITE);138perms_0700.add(PosixFilePermission.OWNER_READ);139perms_0700.add(PosixFilePermission.OWNER_EXECUTE);140Files.setPosixFilePermissions(file2PermissionTest, perms_0700);141142if (doTest() != 0) {143++failures;144}145}146147/**148* Test 1 - SSL config file is secure - VM should start149*/150private void test2() throws Exception {151final Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file2PermissionTest);152perms.add(PosixFilePermission.OTHERS_READ);153perms.add(PosixFilePermission.OTHERS_EXECUTE);154Files.setPosixFilePermissions(file2PermissionTest, perms);155156if (doTest() == 0) {157++failures;158}159}160161private int doTest() throws Exception {162163for (int i = 0; i < MAX_GET_FREE_PORT_TRIES; ++i) {164final String pp = "-Dcom.sun.management.jmxremote.port=" + jdk.test.lib.Utils.getFreePort();165166List<String> command = new ArrayList<>();167Collections.addAll(command, jdk.test.lib.Utils.getTestJavaOpts());168command.add(mp);169command.add(pp);170command.add("-cp");171command.add(TEST_CLASSES);172command.add(className);173174175ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(176command.toArray(new String[command.size()]));177178System.out.println("test cmdline: " + Arrays.toString(processBuilder.command().toArray()).replace(",", ""));179OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);180181System.out.println("test output:");182System.out.println(output.getOutput());183184if ((output.getExitValue() == 0) ||185!output.getOutput().contains("Exception thrown by the agent : " +186"java.rmi.server.ExportException: Port already in use")) {187return output.getExitValue();188}189}190191return -1;192}193194private void resetPasswordFilePermission() throws Exception {195final Set<PosixFilePermission> perms_0777 = new HashSet<>();196Arrays.asList(PosixFilePermission.values()).stream().forEach(p -> {197perms_0777.add(p);198});199Files.setPosixFilePermissions(file2PermissionTest, perms_0777);200}201}202203204