Path: blob/master/test/jdk/com/sun/crypto/provider/KeyProtector/IterationCount.java
41159 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 /test/lib28* @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.test.lib.process.OutputAnalyzer;50import jdk.test.lib.process.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("--add-opens=java.base/com.sun.crypto.provider=ALL-UNNAMED");91cmd.add(IterationCount.class.getName());92cmd.add(clientStr);93cmd.add(expectedCount);94OutputAnalyzer out = ProcessTools.executeCommand(95cmd.toArray(new String[cmd.size()]));96out.shouldHaveExitValue(0);97}9899private static void testSystem(String expectedCount, String setValue)100throws Throwable {101System.out.println("Test setting " +102(setValue != null ? setValue : "nothing") +103" as a System property");104List<String> cmd = getBasicCommand();105if (setValue != null) {106cmd.add("-Djdk.jceks.iterationCount=" + setValue);107}108executeCommand(cmd, expectedCount);109System.out.println(".............................");110}111112private static void testSecurity(String expectedCount, String setValue)113throws Throwable {114testSecurity(expectedCount, setValue, getBasicCommand());115}116117private static void testSecurity(String expectedCount, String setValue,118List<String> cmd) throws Throwable {119System.out.println("Test setting " +120(setValue != null ? setValue : "nothing") +121" as a Security property");122Path tmpDirPath = Files.createTempDirectory("tmpdir");123try {124if (setValue != null) {125String javaSecurityPath = tmpDirPath +126File.separator + "java.security";127writeJavaSecurityProp(javaSecurityPath, setValue);128cmd.add("-Djava.security.properties=" + javaSecurityPath);129}130executeCommand(cmd, expectedCount);131System.out.println(".............................");132} finally {133deleteDir(tmpDirPath);134}135}136137private static void testSystemOverridesSecurity() throws Throwable {138System.out.println("Test that setting a System property overrides" +139" the Security one");140String systemValue = Integer.toString(30000);141System.out.println("System value: " + systemValue);142List<String> cmd = getBasicCommand();143cmd.add("-Djdk.jceks.iterationCount=" + systemValue);144testSecurity(systemValue, Integer.toString(40000), cmd);145}146147private static void writeJavaSecurityProp(String javaSecurityPath,148String setValue) throws IOException {149try (FileOutputStream fos = new FileOutputStream(150new File(javaSecurityPath))) {151fos.write(("jdk.jceks.iterationCount=" + setValue).getBytes());152}153}154155private static int getCurrentIterationCountValue() throws Exception {156Class<?> KeyProtectorClass =157Class.forName("com.sun.crypto.provider.KeyProtector");158Field iterationCountField =159KeyProtectorClass.getDeclaredField("ITERATION_COUNT");160iterationCountField.setAccessible(true);161return iterationCountField.getInt(KeyProtectorClass);162}163164private static void deleteDir(Path directory) throws IOException {165Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {166167@Override168public FileVisitResult visitFile(Path file,169BasicFileAttributes attrs) throws IOException {170Files.delete(file);171return FileVisitResult.CONTINUE;172}173174@Override175public FileVisitResult postVisitDirectory(Path dir, IOException exc)176throws IOException {177Files.delete(dir);178return FileVisitResult.CONTINUE;179}180});181}182}183184185