Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyProtector/IterationCount.java
38867 views
/*1* Copyright (c) 2019, Red Hat, Inc.2*3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @bug 823340427* @library /lib/testlibrary28* @run main/othervm/timeout=30 IterationCount HOST 20000029* @run main/othervm/timeout=30 IterationCount HOST 200000 130* @run main/othervm/timeout=30 IterationCount HOST 200000 600000031* @run main/othervm/timeout=30 IterationCount HOST 200000 invalid32* @run main/othervm/timeout=30 IterationCount HOST 30000 3000033* @run main/othervm/timeout=30 IterationCount OVERRIDE34* @author Martin Balao ([email protected])35*/3637import java.io.File;38import java.io.FileOutputStream;39import java.io.IOException;40import java.lang.reflect.Field;41import java.nio.file.FileVisitResult;42import java.nio.file.Files;43import java.nio.file.Path;44import java.nio.file.SimpleFileVisitor;45import java.nio.file.attribute.BasicFileAttributes;46import java.util.ArrayList;47import java.util.List;4849import jdk.testlibrary.OutputAnalyzer;50import jdk.testlibrary.ProcessTools;5152public class IterationCount {53private static final String clientStr = "CLIENT";54private static final String javaBinPath =55System.getProperty("java.home", ".") + File.separator + "bin" +56File.separator + "java";5758public static void main(String[] args) throws Throwable {59if (args[0].equals("HOST")) {60String setValue = null;61if (args.length > 2) {62setValue = args[2];63}64testSystem(args[1], setValue);65testSecurity(args[1], setValue);66} else if (args[0].equals(clientStr)) {67int expectedIterationCount = Integer.parseInt(args[1]);68int currentIterationCount = getCurrentIterationCountValue();69System.out.println("Expected value: " + expectedIterationCount);70System.out.println("Current value: " + currentIterationCount);71if (currentIterationCount != expectedIterationCount) {72throw new Exception("Expected value different than current");73}74} else if (args[0].equals("OVERRIDE")) {75testSystemOverridesSecurity();76}77System.out.println("TEST PASS - OK");78}7980private static List<String> getBasicCommand() {81List<String> cmd = new ArrayList<>();82cmd.add(javaBinPath);83cmd.add("-cp");84cmd.add(System.getProperty("test.classes", "."));85return cmd;86}8788private static void executeCommand(List<String> cmd, String expectedCount)89throws Throwable {90cmd.add(IterationCount.class.getName());91cmd.add(clientStr);92cmd.add(expectedCount);93OutputAnalyzer out = ProcessTools.executeCommand(94cmd.toArray(new String[cmd.size()]));95out.shouldHaveExitValue(0);96}9798private static void testSystem(String expectedCount, String setValue)99throws Throwable {100System.out.println("Test setting " +101(setValue != null ? setValue : "nothing") +102" as a System property");103List<String> cmd = getBasicCommand();104if (setValue != null) {105cmd.add("-Djdk.jceks.iterationCount=" + setValue);106}107executeCommand(cmd, expectedCount);108System.out.println(".............................");109}110111private static void testSecurity(String expectedCount, String setValue)112throws Throwable {113testSecurity(expectedCount, setValue, getBasicCommand());114}115116private static void testSecurity(String expectedCount, String setValue,117List<String> cmd) throws Throwable {118System.out.println("Test setting " +119(setValue != null ? setValue : "nothing") +120" as a Security property");121Path tmpDirPath = Files.createTempDirectory("tmpdir");122try {123if (setValue != null) {124String javaSecurityPath = tmpDirPath +125File.separator + "java.security";126writeJavaSecurityProp(javaSecurityPath, setValue);127cmd.add("-Djava.security.properties=" + javaSecurityPath);128}129executeCommand(cmd, expectedCount);130System.out.println(".............................");131} finally {132deleteDir(tmpDirPath);133}134}135136private static void testSystemOverridesSecurity() throws Throwable {137System.out.println("Test that setting a System property overrides" +138" the Security one");139String systemValue = Integer.toString(30000);140System.out.println("System value: " + systemValue);141List<String> cmd = getBasicCommand();142cmd.add("-Djdk.jceks.iterationCount=" + systemValue);143testSecurity(systemValue, Integer.toString(40000), cmd);144}145146private static void writeJavaSecurityProp(String javaSecurityPath,147String setValue) throws IOException {148try (FileOutputStream fos = new FileOutputStream(149new File(javaSecurityPath))) {150fos.write(("jdk.jceks.iterationCount=" + setValue).getBytes());151}152}153154private static int getCurrentIterationCountValue() throws Exception {155Class<?> KeyProtectorClass =156Class.forName("com.sun.crypto.provider.KeyProtector");157Field iterationCountField =158KeyProtectorClass.getDeclaredField("ITERATION_COUNT");159iterationCountField.setAccessible(true);160return iterationCountField.getInt(KeyProtectorClass);161}162163private static void deleteDir(Path directory) throws IOException {164Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {165166@Override167public FileVisitResult visitFile(Path file,168BasicFileAttributes attrs) throws IOException {169Files.delete(file);170return FileVisitResult.CONTINUE;171}172173@Override174public FileVisitResult postVisitDirectory(Path dir, IOException exc)175throws IOException {176Files.delete(dir);177return FileVisitResult.CONTINUE;178}179});180}181}182183184